1
$\begingroup$

I am using leave-one-out cross-validation to evaluate a linear regression model. In subsequent analysis, I need three specific values for each observation: observed value, predicted value, prediction standard error. Prediction standard error values can be retrieved from function predict.lm setting argument se.fit = TRUE. The following code (adapted from here) can be used to do what I currently need:

library(faraway)
gala[1:3, ]
c1 <- c(1:30)
gala2 <- cbind(gala, c1)
gala2[1:3, ]
obs  <- numeric(30)
pred <- numeric(30)
se   <- numeric(30)
for (i in 1:30) {
     model1  <- lm(Species ~ Endemics + Area + Elevation,
                   subset = (c1 != i), data = gala2)
     specpr  <- predict(model1, gala2[i, ], se.fit = TRUE)
     obs[i]  <- gala2[i, 1]
     pred[i] <- specpr$fit
     se[i]   <- specpr$se.fit
}
res <- data.frame(obs, pred, se)
head(res)
  obs       pred       se
1  58  70.185063 5.524249
2  31  72.942732 6.509655
3   3  -8.303608 7.055163
4  25  20.948932 6.998093
5   2 -15.953141 7.403062
6  18  27.274440 6.220029

I searched through the documentation of some of the packages that offer functions for cross-validation, but did not find any that saves prediction standard errors. Is there any package that already offers such functionality?

$\endgroup$

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Browse other questions tagged or ask your own question.