4
$\begingroup$

I did a toy experiment with linear regression, but getting different results for $R^2$, could any one help me?

library(ggplot2)
fit=lm(price~carat+depth+table+x+y+z-1,data=diamonds)
summary(fit)
sse=crossprod(diamonds$price-fit$fitted.values)
sst=crossprod(diamonds$price-mean(diamonds$price))
1-sse/sst

enter image description here

$\endgroup$
4
$\begingroup$

I found the problem.

I am not adding the intercept in the model and without intercept SST is calculated differently, where it should be

crossprod(diamonds$price)

but not

crossprod(diamonds$price-mean(diamonds$price))

I think most books are confusing that just give us the formula to SST as

$$ \|y-\bar y\|^2 $$

but not mention such formula only holds for model with intercept term.

| cite | improve this answer | |
$\endgroup$
  • 2
    $\begingroup$ You're correct but I believe this effectively duplicates a number of questions already on site. $\endgroup$ – Glen_b -Reinstate Monica Sep 4 '16 at 6:08

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.