2 of 5
added 75 characters in body
vtshen
  • 449
  • 4
  • 14

Computation of R-squared with lm in R

Confusion on difference between the R2 results from the lm function in R and the Equation

1-rss/sst (1)

z1 = c(0.603,0.643,0.603,0.643,0.643,0.603,0.601,0.641,0.601,0.603,0.601,0.641,0.643,0.641,0.641,0.601,0.622)
z2 = c(0.38,0.34,0.38,0.34,0.34,0.38,0.38,0.34,0.38,0.38,0.38,0.34,0.34,0.34,0.34,0.38,0.36)
z3= c(0.017,0.017,0.017,0.017,0.017,0.017,0.019,0.019,0.019,0.017,0.019,0.019,0.017,0.019,0.019,0.019,0.018)
z4= c(0.503505,0.536905,0.503505,0.536905,0.581915,0.545715,0.501835,0.535235,0.501835,0.545715,0.543905 ,0.535235,0.581915,0.580105,0.580105,0.543905,0.541140)
z5 = c(0.3420,0.3060,0.3724,0.3332,0.3060,0.3420,0.3420,0.3060,0.3724,0.3724,0.3420,0.3332,0.3332,0.3060,0.3332,0.3724,0.3384)
z = data.frame(z1,z2,z3,z4,z5)
y = c(35.040, 32.100, 37.800, 33.300, 31.320, 34.026, 34.140, 31.968, 36.990, 35.970, 33.870, 33.438, 33.144, 32.106, 33.660, 35.520, 33.438)

fit = lm(y~z1+z2+z3+z4+z5-1, data = data.frame(y, z))

e = fit$residuals
rss = sum(e^2)
sst = sum((y-mean(y))^2)
1-rss/sst

[1] 0.9305206

summary(fit)$r.squared

0.9998195

I also checked anova(fit). I think the R2 results from the summary(fit)$r.squared is correct. But why is the computation from the Equation (1) wrong?

vtshen
  • 449
  • 4
  • 14