# Computation of R-squared with lm() in R [duplicate]

Confusion on difference between the $$R^2$$ results from the lm() function in R and from 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  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? ## marked as duplicate by whuber♦ r StackExchange.ready(function() { if (StackExchange.options.isMobile) return;$('.dupe-hammer-message-hover:not(.hover-bound)').each(function() { var $hover =$(this).addClass('hover-bound'), $msg =$hover.siblings('.dupe-hammer-message'); $hover.hover( function() {$hover.showInfoMessage('', { messageElement: \$msg.clone().show(), transient: false, position: { my: 'bottom left', at: 'top center', offsetTop: -7 }, dismissable: false, relativeToBody: true }); }, function() { StackExchange.helpers.removeMessages(); } ); }); }); Dec 16 '18 at 21:32

• Take that -1 from model, try again. fit = lm(y ~ z1 + z2 + z3 + z4 + z5, data = data.frame(y, z)) – user158565 Dec 16 '18 at 4:17