R/predict.R

Defines functions predict.plm

Documented in predict.plm

#' @title Predict method for plm models
#' @description Obtains predictions from a plm object.
#' @param object a fitted object of class inheriting from "plm".
#' @param newdata optionally, a data frame in which to look for variables with which to predict. If omitted, the fitted linear predictors are used.
#' @param ... other arguments.
#' @export
predict.plm <- function(object, newdata, ...){
  nn <- names(newdata)
  lapply(1:nrow(newdata), FUN = function(i){
    r <- newdata[i, ]
    r <- as.data.frame(r, stringsAsFactors = FALSE) %>%
      purrr::set_names(nn)
    r_ <- as.list(r)
    which_model <- do.call(object[["condition"]], r_)
    mod <- object[["models"]][which_model]
    sapply(mod, predict, newdata = r)
  })
}
alvarofranq/piecewise documentation built on May 27, 2019, 7:43 a.m.