newdata: a data frame in which to look for variables with which to predict
se.fit: logical. Whether standard errors are required, default = FALSE
interval: type of interval calculation. Can be abbreviated, default = none
alpha: numeric denoting the test size for confidence intervals
na.action: function determining what should be done with missing values in newdata. The default is to predict NA.
pred.var: the variance(s) for future observations to be assumed for prediction intervals.
weights: variance weights for prediction. This can be a numeric vector or a bare (unquoted) name of the weights variable in the supplied newdata.
...: other arguments, unused
Details
Produces predicted values, obtained by evaluating the regression function in the frame newdata for fits from lm_robust and lm_lin. If the logical se.fit is TRUE, standard errors of the predictions are calculated. Setting intervals specifies computation of confidence or prediction (tolerance) intervals at the specified level, sometimes referred to as narrow vs. wide intervals.
The equation used for the standard error of a prediction given a row of data x is:
(xΣx′),
where Σ is the estimated variance-covariance matrix from lm_robust.
The prediction intervals are for a single observation at each case in newdata with error variance(s) pred.var. The the default is to assume that future observations have the same error variance as those used for fitting, which is gotten from the fit lm_robust object. If weights is supplied, the inverse of this is used as a scale factor. If the fit was weighted, the default is to assume constant prediction variance, with a warning.
Examples
# Set seedset.seed(42)# Simulate datan <-10dat <- data.frame(y = rnorm(n), x = rnorm(n))# Fit lmlm_out <- lm_robust(y ~ x, data = dat)# Get predicted fitsfits <- predict(lm_out, newdata = dat)# With standard errors and confidence intervalsfits <- predict(lm_out, newdata = dat, se.fit =TRUE, interval ="confidence")# Use new data as wellnew_dat <- data.frame(x = runif(n,5,8))predict(lm_out, newdata = new_dat)# You can also supply custom variance weights for prediction intervalsnew_dat$w <- runif(n)predict(lm_out, newdata = new_dat, weights = w, interval ="prediction")# Works for 'lm_lin' models as welldat$z <- sample(1:3, size = nrow(dat), replace =TRUE)lmlin_out1 <- lm_lin(y ~ z, covariates =~ x, data = dat)predict(lmlin_out1, newdata = dat, interval ="prediction")# Predictions from Lin models are equivalent with and without an intercept# and for multi-level treatments entered as numeric or factor variableslmlin_out2 <- lm_lin(y ~ z -1, covariates =~ x, data = dat)lmlin_out3 <- lm_lin(y ~ factor(z), covariates =~ x, data = dat)lmlin_out4 <- lm_lin(y ~ factor(z)-1, covariates =~ x, data = dat)predict(lmlin_out2, newdata = dat, interval ="prediction")predict(lmlin_out3, newdata = dat, interval ="prediction")predict(lmlin_out4, newdata = dat, interval ="prediction")# In Lin models, predict will stop with an error message if new# treatment levels are supplied in the new datanew_dat$z <- sample(0:3, size = nrow(new_dat), replace =TRUE)# predict(lmlin_out, newdata = new_dat)