投稿日:2022.3.9
irisを使用した単回帰分析
まずは最小二乗法
dat = iris fit1 <- lm(Sepal.Length ~ Sepal.Width, data = dat) summary(fit1) Coefficients: Estimate Std. Error t value Pr(>|t|) (Intercept) 6.5262 0.4789 13.63 <2e-16 *** Sepal.Width -0.2234 0.1551 -1.44 0.152 --- Residual standard error: 0.8251 on 148 degrees of freedom Multiple R-squared: 0.01382, Adjusted R-squared: 0.007159 F-statistic: 2.074 on 1 and 148 DF, p-value: 0.1519
残差の標準誤差や決定係数などが算出されます。
またF値やp値は次に示すような分散分析の結果になります。
anova(fit1) Df Sum Sq Mean Sq F value Pr(>F) Sepal.Width 1 1.412 1.41224 2.0744 0.1519 Residuals 148 100.756 0.68078
参考
次に最尤推定法による単回帰分析(lmがglmに変わるだけ・・・です)。もちろん答えは同じです。
fit2 <- glm(Sepal.Length ~ Sepal.Width, data = dat) summary(fit2) Coefficients: Estimate Std. Error t value Pr(>|t|) (Intercept) 6.5262 0.4789 13.63 <2e-16 *** Sepal.Width -0.2234 0.1551 -1.44 0.152 --- Signif. codes: 0 ‘***’ 0.001 ‘**’ 0.01 ‘*’ 0.05 ‘.’ 0.1 ‘ ’ 1 (Dispersion parameter for gaussian family taken to be 0.6807844) Null deviance: 102.17 on 149 degrees of freedom Residual deviance: 100.76 on 148 degrees of freedom AIC: 371.99
ただし対数尤度を利用した解析結果ですのでdeviance、AICが算出されます。
確率分布は正規分布で、リンク関数は恒等リンクがデフォルトなので、本当は以下のように書かれています(算出結果は全く同じ)。
fit3 <- glm(Sepal.Length ~ Sepal.Width, family = gaussian (link = identity), data = dat) summary(fit3)
family = gaussianのリンク関数は、 identity, log, inverseが使用できるようです。
The gaussian family accepts the links (as names) identity, log and inverse
ちなみにリンク関数を変えたら、(当たり前ですが)異なる結果となります。
fit4 <- glm(Sepal.Length ~ Sepal.Width, family = gaussian(link=log), data = dat) summary(fit4) Coefficients: Estimate Std. Error t value Pr(>|t|) (Intercept) 1.87900 0.08200 22.914 <2e-16 *** Sepal.Width -0.03723 0.02668 -1.396 0.165 (Dispersion parameter for gaussian family taken to be 0.6810537) Null deviance: 102.17 on 149 degrees of freedom Residual deviance: 100.80 on 148 degrees of freedom AIC: 372.05