[R] R squared for lm prediction

Joshua Wiley jwiley.psych at gmail.com
Tue Oct 5 05:50:25 CEST 2010


Hi Brima,


# 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

Cheers,

Josh


On Mon, Oct 4, 2010 at 7:06 PM, Brima <adamsteve2000 at yahoo.com> wrote:
>
> Hi all,
>
> I have used a hold out sample to predict a model but now I want to compute
> an R squared value for the prediction. Any help is appreciated.
>
> Best regards
> --
> View this message in context: http://r.789695.n4.nabble.com/R-squared-for-lm-prediction-tp2955328p2955328.html
> Sent from the R help mailing list archive at Nabble.com.
>
> ______________________________________________
> R-help at r-project.org mailing list
> https://stat.ethz.ch/mailman/listinfo/r-help
> PLEASE do read the posting guide http://www.R-project.org/posting-guide.html
> and provide commented, minimal, self-contained, reproducible code.
>



-- 
Joshua Wiley
Ph.D. Student, Health Psychology
University of California, Los Angeles
http://www.joshuawiley.com/



More information about the R-help mailing list