次のような線形回帰モデルに誤差項を含める方法があるかどうか疑問に思っていました。
r = lm(y ~ x1+x2)
このコードr = lm(y ~ x1+x2)
は、y を x1 と x2 の線形関数としてモデル化することを意味します。モデルは完全ではないため、残差項 (つまり、モデルが適合しなかった残り) が発生します。
数学では、Rob Hyndman がコメントで指摘したように、y = a + b1*x1 + b2*x2 + e
、ここでa
、 、b1
およびb2
は定数でe
あり、残差 (正規分布であると想定されます) です。
具体的な例を見るために、R に付属する虹彩データを考えてみましょう。
model1 <- lm(Sepal.Length ~ Sepal.Width + Petal.Length + Petal.Width, data=iris)
これで、モデルから定数を抽出できます ( a
、b1
、b2
およびこの場合b3
も同様です)。
> coefficients(model1)
(Intercept) Sepal.Width Petal.Length Petal.Width
1.8559975 0.6508372 0.7091320 -0.5564827
残差は、モデルで使用されたデータの行ごとに計算されています。
> residuals(model1)
1 2 3 4 5
0.0845842387 0.2100028184 -0.0492514176 -0.2259940935 -0.0804994772
# etc. There are 150 residuals and 150 rows in the iris dataset.
(編集: 関係のないものとして概要情報をカットします。)
編集:
Error
コメントで言及した値は、aov のヘルプ ページで説明されています。
If the formula contains a single ‘Error’ term, this is used to
specify error strata, and appropriate models are fitted within
each error stratum.
以下を比較してください(?aov
ページから適応)。
> utils::data(npk, package="MASS")
> aov(yield ~ N*P*K, npk)
Call:
aov(formula = yield ~ N * P * K, data = npk)
Terms:
N P K N:P N:K P:K N:P:K Residuals
Sum of Squares 189.2817 8.4017 95.2017 21.2817 33.1350 0.4817 37.0017 491.5800
Deg. of Freedom 1 1 1 1 1 1 1 16
Residual standard error: 5.542901
Estimated effects may be unbalanced
> aov(yield ~ N*P*K + Error(block), npk)
Call:
aov(formula = yield ~ N * P * K + Error(block), data = npk)
Grand Mean: 54.875
Stratum 1: block
Terms:
N:P:K Residuals
Sum of Squares 37.00167 306.29333
Deg. of Freedom 1 4
Residual standard error: 8.750619
Estimated effects are balanced
Stratum 2: Within
Terms:
N P K N:P N:K P:K Residuals
Sum of Squares 189.28167 8.40167 95.20167 21.28167 33.13500 0.48167 185.28667
Deg. of Freedom 1 1 1 1 1 1 12
Residual standard error: 3.929447
1 out of 7 effects not estimable
Estimated effects may be unbalanced