R/kernel_prediction.R

Defines functions kernel_prediction

Documented in kernel_prediction

#' Prediction based only on step length terms
#'
#' @export
kernel_prediction <- function(f, data,
                              kernel_vars = c("step_length", "ta"),
                              coefs) {

  # get movement/kernel variables from the formula
  # ignoring the interactions
  all_vars <- attr(terms(f), "term.labels")
  kernel_variables <- all_vars[grepl(paste(kernel_vars, collapse = "|"), all_vars)]
  kernel_variables <- kernel_variables[!grepl(":", kernel_variables)]
  # make sure to remove strata
  kernel_variables <- kernel_variables[grep("strata", kernel_variables, invert = TRUE)]

  # returning prediction of only this variables, based on the fitted coefficients
  f2 <- as.formula(paste0(extract_response_strata(f, covars = F)$response, " ~ -1 + ",
                          paste0(kernel_variables, collapse = "+")))
  pred_vals_kernel <- model.matrix(f2, data) %*% coefs[match(kernel_variables, names(coefs))]
  return(pred_vals_kernel)
}
NINAnor/oneimpact documentation built on June 14, 2025, 12:27 a.m.