Skip to content

Instantly share code, notes, and snippets.

@jknowles
Created March 6, 2018 20:40
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save jknowles/9f575f7eb97fad090e67e8bbc80185bf to your computer and use it in GitHub Desktop.
Save jknowles/9f575f7eb97fad090e67e8bbc80185bf to your computer and use it in GitHub Desktop.
Robust Prediction Intervals for LM
predict.robust <- function(model, data, robust_vcov = NULL, level = 0.95,
interval = "prediction"){
# adapted from
# https://stackoverflow.com/questions/38109501/how-does-predict-lm-compute-confidence-interval-and-prediction-interval
# model is an lm object from r
# data is the dataset to predict from
# robust_vcov must be a robust vcov matrix created by V <- sandwich::vcovHC(model, ...)
# level = the % of the confidence interval, default is 95%
# interval = either "prediction" or "confidence" - prediction includes uncertainty about the model itself
if(is.null(robust_vcov)){
robust_vcov <- vcov(model)
}
if(is.null(data)){
data <- model$model
}
fit <- as.numeric(model.matrix(model, data=data) %*% model$coefficients)
se2 <- unname(rowSums((model.matrix(model) %*% robust_vcov) * model.matrix(model)))
alpha <- qt((1-level)/2, df = model$df.residual)
if(interval == "confidence"){
upr <- fit + alpha*sqrt(se2)
lwr <- fit - alpha*sqrt(se2)
} else if(interval == "prediction"){
sigma2 <- sum(model$residuals ^ 2) / model$df.residual
upr <- fit + alpha*sqrt(se2+sigma2)
lwr <- fit - alpha*sqrt(se2+sigma2)
}
preds <- cbind(fit, lwr, upr)
return(list(fit = preds, se.fit = sqrt(se2)))
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment