2

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!

Jaap
  • 81,064
  • 34
  • 182
  • 193
Stephane
  • 29
  • 1
  • 3

1 Answers1

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