prediction: Extract Predictions from a Model Object

Description Usage Arguments Details Value See Also Examples

Description

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

Usage

  1
  2
  3
  4
  5
  6
  7
  8
  9
 10
 11
 12
 13
 14
 15
 16
 17
 18
 19
 20
 21
 22
 23
 24
 25
 26
 27
 28
 29
 30
 31
 32
 33
 34
 35
 36
 37
 38
 39
 40
 41
 42
 43
 44
 45
 46
 47
 48
 49
 50
 51
 52
 53
 54
 55
 56
 57
 58
 59
 60
 61
 62
 63
 64
 65
 66
 67
 68
 69
 70
 71
 72
 73
 74
 75
 76
 77
 78
 79
 80
 81
 82
 83
 84
 85
 86
 87
 88
 89
 90
 91
 92
 93
 94
 95
 96
 97
 98
 99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
prediction(model, ...)

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

## S3 method for class '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 class 'glmnet'
prediction(model, data, lambda = model[["lambda"]][1L],
  at = NULL, type = c("response", "link"), calculate_se = FALSE, ...)

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

## S3 method for class '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.

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.

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.

See Also

find_data, build_datalist, mean_or_mode, seq_range

Examples

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
require("datasets")
x <- lm(Petal.Width ~ Sepal.Length * Sepal.Width * Species, data = iris)
# prediction for every case
prediction(x)

# prediction for first case
prediction(x, iris[1,])

# basic use of 'at' argument
summary(prediction(x, at = list(Species = c("setosa", "virginica"))))

# basic use of 'at' argument
prediction(x, at = list(Sepal.Length = seq_range(iris$Sepal.Length, 5)))

# prediction at means/modes of input variables
prediction(x, at = lapply(iris, mean_or_mode))

# 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)

## End(Not run)

prediction documentation built on June 18, 2019, 1:03 a.m.