| predict_mi | R Documentation |
Predict method for linear models with multiply imputed data
predict_mi(
object,
newdata,
pool = TRUE,
se.fit = FALSE,
interval = c("none", "confidence", "prediction"),
level = 0.95,
iterations = 20,
probs = NULL,
nsamples = NULL,
...
)
object |
A prediction model, either a single lm object or a list of lm objects obtained from multiply imputed data (object can also be of class mira). |
newdata |
An optional data frame in which to look for variables with which to predict. Can be a data.frame, list, or mids object. If omitted, the fitted values are used. |
pool |
Logical indicating whether to pool the predictions (and potentially obtain pooled prediction intervals). |
se.fit |
A switch indicating if standard errors are required. |
interval |
Type of interval calculation. Can be abbreviated. |
level |
Tolerance/confidence level. |
iterations |
Number of iterations for the bisection algorithm that finds the mixture quantiles |
probs |
Quantiles from the posterior predictive distribution corresponding
to the given probabilities will be returned (overwrites |
nsamples |
Number of samples from the posterior predictive distribution for
each imputed dataset. Defaults to |
... |
Arguments passed on to
|
If pool = TRUE, predict_mi produces a vector of predictions or a
matrix of predictions and confidence/prediction bounds. The bounds depend on
the probabilities specified by the user. The default returns a prediction matrix
with column names fit, lwr, upr if interval is set. For custom probabilities,
names are set to the respective probabilities used to calculate the prediction quantiles.
For type = "terms", a matrix is returned with a column per term and potentially an
attribute "constant". If se.fit = TRUE, a list is returned with the following
components:
fit: vector or matrix as above
se.fit: standard error of pooled predicted means
residual scale: average residual standard deviations
df: degrees of freedom for residual (per observation) according to Barnard-
Rubin (1999).
If nsamples is set, this many samples are drawn from the posterior predictive
distribution per imputed dataset. The output will be a list with variable
samples, which is an array with dimensions n \times m \times nsamples.
With interval = "confidence", the samples are returned for the
conditional mean of y; otherwise they are for new observations.
If pool = FALSE, the function produces a list with predictions with the same
structure as predict.lm, with one list-element per imputed dataset.
stats::lm(), stats::predict.lm(), mice(), with.mids()
# Dataframe with missings in X
# Create imputations and linear model object
dat <- mice::nhanes
# add indicator training and test
# first 20 for training, last 5 for testing
dat$set <- c(rep("train", 20), rep("test", 5))
dat$age[dat$set == "test"] <- NA # set test cases outcome to missing
# Make prediction matrix and ensure that set is not used as a predictor
predmat <- mice::make.predictorMatrix(dat)
predmat[,"set"] <- 0
# Impute missing values based on the train set
imp <- mice(dat, m = 20, maxit = 5 , seed = 1, predictorMatrix = predmat,
ignore = ifelse(dat$set == "test", TRUE, FALSE), print = FALSE)
impdats <- complete(imp, "all")
# extract the training and test data sets
traindats <- lapply(impdats, function(dat) subset(dat, set == "train", select = -set))
testdats <- lapply(impdats, function(dat) subset(dat, set == "test", select = -c(set)))
# Fit the prediction models, based on the imputed training data
fits <- lapply(traindats, function(dat) lm(age ~ bmi + hyp + chl, data = dat))
# pool the predictions with function
pool_preds <- mice::predict_mi(object = fits, newdata = testdats,
pool = TRUE, interval = "prediction", level = 0.95)
Add the following code to your website.
For more information on customizing the embed code, read Embedding Snippets.