Can anyone recommend a function in R to me with which i can calculate the Out of Sample R-squared of a previously calculated linear model lm()
.
Regards and thanks in advance!
Asked
Active
Viewed 4,688 times
2
1 Answers
6
You could use the following format:
# Fit model
model.lm <- lm(Sepal.Length ~ Petal.Length, data = iris[1:75,])
# Predict data for some new data
pred.dat <- predict(model.lm, newdata = iris[76:150,])
# Calculate correlation between predicted values for new data and actual values, then square
cor(iris[1:75,"Sepal.Length"], pred.dat)^2
You can pack it all in a function if you like. If you need any help, just ask.
Cheers
Felipe

ramirfel
- 61
- 2
-
thanks a lot Felipe! I will try now, looks very good! – Stephane May 30 '17 at 07:05