7
$\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$

1 Answer 1

8
$\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.

$\endgroup$
1
  • 2
    $\begingroup$ You're correct but I believe this effectively duplicates a number of questions already on site. $\endgroup$
    – Glen_b
    Commented Sep 4, 2016 at 6:08

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

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