Skip to content
Prev 82348 / 398513 Next

summary[["r.squared"]] gives strange results

Felix Flory wrote:
Hi, Felix,

The first model fits your data without an intercept and thus has a 
different formula of R^2. The justification should be in any intro 
regression text. Here is the relevant snippet from summary.lm:

         mss <- if (attr(z$terms, "intercept"))
             sum((f - mean(f))^2)
         else sum(f^2)
         rss <- sum(r^2)
         <snip>
         ans$r.squared <- mss/(mss + rss)

Try the following to see a direct comparison of the two methods:

lm.v1 <- lm(y ~ X - 1)
lm.v2 <- lm(y ~ zf * xf)
lm.v3 <- lm(y ~ X[, -1])

summary(lm.v1)$r.squared
summary(lm.v2)$r.squared
summary(lm.v3)$r.squared

HTH,

--sundar