の続き
lmの決定係数が切片を明示的になしとしている場合で、そうでない場合とで異なる理由について、R FAQが答えてくれていた。
CRANのFAQなのでこれが公式回答でいいんだと思う。
7.41 Why does summary() report strange results for the R^2 estimate when I fit a linear model with no intercept?
As described in ?summary.lm, when the intercept is zero (e.g., from y ~ x - 1 or y ~ x + 0), summary.lm() uses the formula R^2 = 1 - Sum(R[i]^2) / Sum((y[i])^2) which is different from the usual R^2 = 1 - Sum(R[i]^2) / Sum( (y[i] - mean(y) )^2). There are several reasons for this:
- Otherwise the R^2 could be negative (because the model with zero intercept can fit worse than the constant-mean model it is implicitly compared to).
- If you set the slope to zero in the model with a line through the origin you get fitted values y*=0
- The model with constant, non-zero mean is not nested in the model with a line through the origin.
All these come down to saying that if you know a priori that E[Y]=0 when x=0 then the ‘null’ model that you should compare to the fitted line, the model where x doesn’t explain any of the variance, is the model where E[Y]=0 everywhere. (If you don’t know a priori that E[Y]=0 when x=0, then you probably shouldn’t be fitting a line through the origin.)
CARNのFAQにも載ってるし、ちゃんと調べるとstack overflowなどでも話題になっているので割とメジャーな問題だと思うけど、日本語の情報がなかなかなかった。
FAQの雑翻訳。
7.41 切片のない線形モデルに当てはめた場合、summary()でR^2推定値で異常な結果が報告されるのはなぜですか。
?summary.mlで説明されているとおり、切片が0のとき(例:y ~ x - 1またはy ~ x + 0)はsummary.lm()は決定係数の計算に通常使用するR^2 = 1 - Sum(R[i]^2) / Sum( (y[i] - mean(y) )^2)ではなく、R^2 = 1 - Sum(R[i]^2) / Sum((y[i])^2)という式を使用します。
これにはいくつか理由があります。
- そうでない(通常のR^2を求める式)の場合、R^2が負なりえます(切片が0のモデルは、潜在的な比較対象の平均値の定数モデルよりも適合度が悪いためです)。
- 原点を通る直線のモデルで傾きを0にすると、y*=0の値に適合してしまいます。
- 0以外の平均値の一定値のモデルは、原点を通る直線をもつモデルに織り込めません。
これらのことから、x=0のときE[Y]=0であることがあらかじめわかっている場合、当てはめられたセント比較すべきnullモデル(xが分散のいずれも説明しないモデル)は、E[Y]=0のモデルであるということになります(x=0のときE[Y]=0であることが予めわかっていない場合は、原点を通る直線を当てはめるべきではありません)。
とのこと(英語力がなさすぎて苦しい)。
要するに、CARNのFAQとしてはRのlmで切片の有無で式が変わる挙動をしているのは、決定係数が負の値をとり得るから別の式を使ってますよというのが大きな理由のよう。
残りの箇所は、原点を通る回帰分析を行うことが妥当かちゃんと検討しなさいよ、ということで決定係数の式が変わる理由について触れていない様子。
しかし、ソースコードをからみるとRが出力する決定係数は負の値を実数範囲では取り得ない、と思う。
とりあえず、このことはまた別でまとめることとして、今回はCRANのFAQでの決定係数の式が切片の有無で変わる理由に言及しているよ、ということまでで。
(追記)このことについて書いた