[R] Calculation of r squared from a linear regression
Peter Ehlers
ehlers at ucalgary.ca
Fri Jun 11 11:50:58 CEST 2010
On 2010-06-11 2:16, Sandra Hawthorne wrote:
> Hi,
>
> I'm trying to verify the calculation of coefficient of determination (r squared) for linear regression. I've done the calculation manually with a simple test case and using the definition of r squared outlined in summary(lm) help. There seems to be a discrepancy between the what R produced and the manual calculation. Does anyone know why this is so? What does the multiple r squared reported in summary(lm) represent?
>
> # The test case:
> x<- c(1,2,3,4)
> y<- c(1.6,4.4,5.5,8.3)
> dummy<- data.frame(x, y)
> fm1<- lm(y ~ x-1, data = dummy)
> summary(fm1)
> betax<- fm1$coeff[x] * sd(x) / sd(y)
> # cd is coefficient of determination
> cd<- betax * cor(y, x)
The discrepancy is due to incorrect manual calculation.
You're using (incorrectly, at that) formulas for simple
regression _with an intercept term_ whereas you model
has _no_ intercept term.
What summary.lm reports is clearly described on the
help page. See r.squared in the Value section.
-Peter Ehlers
More information about the R-help
mailing list