1
$\begingroup$

Confusion on difference between the $R^2$ results from the lm() function in R and from the Equation

$1-rss/sst$ (1)

Ref: https://onlinecourses.science.psu.edu/stat501/node/255/

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

0.9305206

summary(fit)$r.squared

0.9998195

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

$\endgroup$

marked as duplicate by whuber r Dec 16 '18 at 21:32

This question has been asked before and already has an answer. If those answers do not fully address your question, please ask a new question.

  • 1
    $\begingroup$ Take that -1 from model, try again. fit = lm(y ~ z1 + z2 + z3 + z4 + z5, data = data.frame(y, z)) $\endgroup$ – user158565 Dec 16 '18 at 4:17
  • $\begingroup$ @user158565 Thanks for the quick reply. I tried to take the -1 off the formula, now the two methods are consistent. Does that mean the Equation (1) holds only when the intercept is considered in the linear regression model? $\endgroup$ – vtshen Dec 16 '18 at 4:21
  • $\begingroup$ See also here: stats.stackexchange.com/questions/26176/… $\endgroup$ – Stefan Dec 16 '18 at 4:25
  • $\begingroup$ @Stefan Thanks so much, this link is useful and solves my problem. $\endgroup$ – vtshen Dec 16 '18 at 4:32
  • $\begingroup$ Yes it is a very good answer indeed! $\endgroup$ – Stefan Dec 16 '18 at 4:44

Browse other questions tagged or ask your own question.