Extract predicted values via predict from a model object, conditional on data, and return a data frame.

prediction(model, ...)

# S3 method for default
prediction(model, data = find_data(model,
  parent.frame()), at = NULL, type = "response",
  vcov = stats::vcov(model), calculate_se = TRUE, ...)

# S3 method for Arima
prediction(model, calculate_se = TRUE, ...)

# S3 method for ar
prediction(model, data, at = NULL, calculate_se = TRUE,
  ...)

# S3 method for arima0
prediction(model, data, at = NULL,
  calculate_se = TRUE, ...)

# S3 method for betareg
prediction(model, data = find_data(model,
  parent.frame()), at = NULL, type = c("response", "link", "precision",
  "variance", "quantile"), calculate_se = FALSE, ...)

# S3 method for biglm
prediction(model, data = find_data(model,
  parent.frame()), at = NULL, type = "response", calculate_se = TRUE,
  ...)

# S3 method for bruto
prediction(model, data = NULL, at = NULL,
  type = "fitted", calculate_se = FALSE, ...)

# S3 method for clm
prediction(model, data = find_data(model, parent.frame()),
  at = NULL, type = NULL, calculate_se = TRUE, category, ...)

# S3 method for coxph
prediction(model, data = find_data(model,
  parent.frame()), at = NULL, type = c("risk", "expected", "lp"),
  calculate_se = TRUE, ...)

# S3 method for crch
prediction(model, data = find_data(model), at = NULL,
  type = c("response", "location", "scale", "quantile"),
  calculate_se = FALSE, ...)

# S3 method for earth
prediction(model, data = find_data(model,
  parent.frame()), at = NULL, type = c("response", "link"),
  calculate_se = TRUE, category, ...)

# S3 method for fda
prediction(model, data = find_data(model, parent.frame()),
  at = NULL, type = NULL, calculate_se = FALSE, category, ...)

# S3 method for Gam
prediction(model, data = find_data(model, parent.frame()),
  at = NULL, type = c("response", "link", "terms"),
  calculate_se = TRUE, ...)

# S3 method for gausspr
prediction(model, data, at = NULL, type = NULL,
  calculate_se = TRUE, category, ...)

# S3 method for gee
prediction(model, calculate_se = FALSE, ...)

# S3 method for glimML
prediction(model, data = find_data(model,
  parent.frame()), at = NULL, type = c("response", "link"),
  calculate_se = TRUE, ...)

# S3 method for glimQL
prediction(model, data = find_data(model,
  parent.frame()), at = NULL, type = c("response", "link"),
  calculate_se = TRUE, ...)

# S3 method for glm
prediction(model, data = find_data(model, parent.frame()),
  at = NULL, type = c("response", "link"), vcov = stats::vcov(model),
  calculate_se = TRUE, ...)

# S3 method for glmnet
prediction(model, data, lambda = model[["lambda"]][1L],
  at = NULL, type = c("response", "link"), calculate_se = FALSE, ...)

# S3 method for glmx
prediction(model, data = find_data(model, parent.frame()),
  at = NULL, type = c("response", "link"), calculate_se = FALSE, ...)

# S3 method for gls
prediction(model, data = find_data(model), at = NULL,
  calculate_se = FALSE, ...)

# S3 method for hetglm
prediction(model, data = find_data(model,
  parent.frame()), at = NULL, type = c("response", "link", "scale"),
  calculate_se = FALSE, ...)

# S3 method for hurdle
prediction(model, data = find_data(model,
  parent.frame()), at = NULL, type = c("response", "count", "prob",
  "zero"), calculate_se = FALSE, ...)

# S3 method for hxlr
prediction(model, data = find_data(model), at = NULL,
  type = c("class", "probability", "cumprob", "location", "scale"),
  calculate_se = FALSE, ...)

# S3 method for ivreg
prediction(model, data = find_data(model,
  parent.frame()), at = NULL, calculate_se = FALSE, ...)

# S3 method for knnreg
prediction(model, data, at = NULL,
  calculate_se = FALSE, ...)

# S3 method for kqr
prediction(model, data, at = NULL, calculate_se = FALSE,
  ...)

# S3 method for ksvm
prediction(model, data, at = NULL, type = NULL,
  calculate_se = TRUE, category, ...)

# S3 method for lm
prediction(model, data = find_data(model, parent.frame()),
  at = NULL, type = "response", vcov = stats::vcov(model),
  calculate_se = TRUE, ...)

# S3 method for lme
prediction(model, data = find_data(model), at = NULL,
  calculate_se = FALSE, ...)

# S3 method for loess
prediction(model, data = find_data(model,
  parent.frame()), at = NULL, type = "response", calculate_se = TRUE,
  ...)

# S3 method for lqs
prediction(model, data = find_data(model), at = NULL,
  calculate_se = FALSE, ...)

# S3 method for mars
prediction(model, data = NULL, at = NULL,
  type = "fitted", calculate_se = FALSE, ...)

# S3 method for mca
prediction(model, data = find_data(model), at = NULL,
  calculate_se = FALSE, ...)

# S3 method for mclogit
prediction(model, data = find_data(model,
  parent.frame()), at = NULL, type = "response",
  vcov = stats::vcov(model), calculate_se = TRUE, ...)

# S3 method for merMod
prediction(model, data = find_data(model), at = NULL,
  type = c("response", "link"), re.form = NULL, calculate_se = FALSE,
  ...)

# S3 method for mnp
prediction(model, data = find_data(model, parent.frame()),
  at = NULL, type = NULL, calculate_se = FALSE, category, ...)

# S3 method for multinom
prediction(model, data = find_data(model,
  parent.frame()), at = NULL, type = NULL, calculate_se = FALSE,
  category, ...)

# S3 method for nls
prediction(model, data = find_data(model, parent.frame()),
  at = NULL, calculate_se = FALSE, ...)

# S3 method for nnet
prediction(model, data = find_data(model, parent.frame()),
  at = NULL, type = NULL, calculate_se = FALSE, category, ...)

# S3 method for plm
prediction(model, data = find_data(model, parent.frame()),
  at = NULL, calculate_se = FALSE, ...)

# S3 method for polr
prediction(model, data = find_data(model, parent.frame()),
  at = NULL, type = NULL, calculate_se = FALSE, category, ...)

# S3 method for polyreg
prediction(model, data = NULL, at = NULL,
  type = "fitted", calculate_se = FALSE, ...)

# S3 method for ppr
prediction(model, data = find_data(model, parent.frame()),
  at = NULL, calculate_se = FALSE, ...)

# S3 method for princomp
prediction(model, data = find_data(model,
  parent.frame()), at = NULL, calculate_se = FALSE, ...)

# S3 method for rlm
prediction(model, data = find_data(model, parent.frame()),
  at = NULL, type = "response", vcov = stats::vcov(model),
  calculate_se = TRUE, ...)

# S3 method for rpart
prediction(model, data = find_data(model,
  parent.frame()), at = NULL, type = NULL, calculate_se = FALSE,
  category, ...)

# S3 method for rq
prediction(model, data = find_data(model, parent.frame()),
  at = NULL, calculate_se = TRUE, ...)

# S3 method for selection
prediction(model, data = find_data(model,
  parent.frame()), at = NULL, type = "response",
  calculate_se = FALSE, ...)

# S3 method for speedglm
prediction(model, data = find_data(model,
  parent.frame()), at = NULL, type = c("response", "link"),
  calculate_se = FALSE, ...)

# S3 method for speedlm
prediction(model, data = find_data(model,
  parent.frame()), at = NULL, calculate_se = FALSE, ...)

# S3 method for survreg
prediction(model, data = find_data(model,
  parent.frame()), at = NULL, type = c("response", "lp", "quantile",
  "uquantile"), calculate_se = TRUE, ...)

# S3 method for svm
prediction(model, data = NULL, at = NULL,
  calculate_se = TRUE, category, ...)

# S3 method for svyglm
prediction(model, data = find_data(model,
  parent.frame()), at = NULL, type = c("response", "link"),
  calculate_se = TRUE, ...)

# S3 method for train
prediction(model, data = find_data(model), at = NULL,
  type = c("raw", "prob"), ...)

# S3 method for truncreg
prediction(model, data, at = NULL,
  calculate_se = FALSE, ...)

# S3 method for zeroinfl
prediction(model, data = find_data(model,
  parent.frame()), at = NULL, type = c("response", "count", "prob",
  "zero"), calculate_se = FALSE, ...)

prediction_summary(model, ..., level = 0.95)

Arguments

model

A model object, perhaps returned by lm or glm.

Additional arguments passed to predict methods.

data

A data.frame over which to calculate marginal effects. If missing, find_data is used to specify the data frame.

at

A list of one or more named vectors, specifically values at which to calculate the predictions. These are used to modify the value of data (see build_datalist for details on use).

type

A character string indicating the type of marginal effects to estimate. Mostly relevant for non-linear models, where the reasonable options are “response” (the default) or “link” (i.e., on the scale of the linear predictor in a GLM). For models of class “polr” (from polr), possible values are “class” or “probs”; both are returned.

vcov

A matrix containing the variance-covariance matrix for estimated model coefficients, or a function to perform the estimation with model as its only argument.

calculate_se

A logical indicating whether to calculate standard errors for observation-specific predictions and average predictions (if possible). The output will always contain a “calculate_se” column regardless of this value; this only controls the calculation of standard errors. Setting it to FALSE may improve speed.

category

For multi-level or multi-category outcome models (e.g., ordered probit, multinomial logit, etc.), a value specifying which of the outcome levels should be used for the "fitted" column. If missing, some default is chosen automatically.

lambda

For models of class “glmnet”, a value of the penalty parameter at which predictions are required.

re.form

An argument passed forward to predict.merMod.

level

A numeric value specifying the confidence level for calculating p-values and confidence intervals.

Value

A data frame with class “prediction” that has a number of rows equal to number of rows in data, or a multiple thereof, if !is.null(at). The return value contains data (possibly modified by at using build_datalist), plus a column containing fitted/predicted values ("fitted") and a column containing the standard errors thereof ("calculate_se"). Additional columns may be reported depending on the object class. The data frame also carries attributes used by print and summary, which will be lost during subsetting.

Details

This function is simply a wrapper around predict that returns a data frame containing the value of data and the predicted values with respect to all variables specified in data.

Methods are currently implemented for the following object classes:

Where implemented, prediction also returns average predictions (and the variances thereof). Variances are implemented using the delta method, as described in http://indiana.edu/~jslsoc/stata/ci_computations/spost_deltaci.pdf.

See also

Examples

require("datasets") x <- lm(Petal.Width ~ Sepal.Length * Sepal.Width * Species, data = iris) # prediction for every case prediction(x)
#> Data frame with 150 predictions from #> lm(formula = Petal.Width ~ Sepal.Length * Sepal.Width * Species, #> data = iris) #> with average prediction: 1.1993
# prediction for first case prediction(x, iris[1,])
#> Data frame with 1 predictions from #> lm(formula = Petal.Width ~ Sepal.Length * Sepal.Width * Species, #> data = iris) #> with average prediction: 0.2533
# basic use of 'at' argument summary(prediction(x, at = list(Species = c("setosa", "virginica"))))
#> at(Species) Prediction SE z p lower upper #> setosa 0.2965 0.13601 2.18 0.02925 0.02993 0.5631 #> virginica 2.1262 0.04935 43.08 0.00000 2.02945 2.2229
# basic use of 'at' argument prediction(x, at = list(Sepal.Length = seq_range(iris$Sepal.Length, 5)))
#> Data frame with 750 predictions from #> lm(formula = Petal.Width ~ Sepal.Length * Sepal.Width * Species, #> data = iris) #> with average predictions:
#> Sepal.Length x #> 4.3 1.143 #> 5.2 1.194 #> 6.1 1.245 #> 7.0 1.296 #> 7.9 1.347
# prediction at means/modes of input variables prediction(x, at = lapply(iris, mean_or_mode))
#> Data frame with 450 predictions from #> lm(formula = Petal.Width ~ Sepal.Length * Sepal.Width * Species, #> data = iris) #> with average prediction:
#> Sepal.Length Sepal.Width Petal.Length Petal.Width Species x #> 5.843 3.057 3.758 1.199 setosa 0.2967
# prediction with multi-category outcome
# NOT RUN { library("mlogit") data("Fishing", package = "mlogit") Fish <- mlogit.data(Fishing, varying = c(2:9), shape = "wide", choice = "mode") mod <- mlogit(mode ~ price + catch, data = Fish) prediction(mod) prediction(mod, category = 3) # }