4.2 統計モデルのあてはまりの悪さ : 逸脱度
- 逸脱度(deviance)(あてはまりの悪さ)は以下の式で表される
- \(D = -2logL\) (ただし\(logL\)は対数尤度)
#まずは各モデルを作成
setwd("~/Desktop/Statistics_Study/StatisticalModeling/kubobook_2012/03poisson")
library(readr)
d<- read_csv("data3a.csv")
##
## ─ Column specification ────────────────────────────
## cols(
## y = col_double(),
## x = col_double(),
## f = col_character()
## )
#xモデル
fit <- glm(y ~ x, data = d, family = poisson(link = "log"))
#fモデル
fit.f <- glm(y ~ f, data = d, family = poisson)
#x + f モデル
fit.all <- glm(y ~ x + f, data = d, family = poisson)
#一定モデル(nullモデル)
fit.null <-glm(y ~ 1, data = d, family = poisson)
#各モデルの要約
library(stargazer)
##
## Please cite as:
## Hlavac, Marek (2018). stargazer: Well-Formatted Regression and Summary Statistics Tables.
## R package version 5.2.2. https://CRAN.R-project.org/package=stargazer
stargazer(fit, fit.f, fit.all, fit.null, type = "text", style = "all", ci = TRUE,
star.cutoffs = NA, omit.table.layout = 'n',
align = TRUE)
##
## ===========================================================================================
## Dependent variable:
## -------------------------------------------------------------------
## y
## (1) (2) (3) (4)
## -------------------------------------------------------------------------------------------
## x 0.076 0.080
## (0.006, 0.145) (0.007, 0.153)
## t = 2.125 t = 2.162
## p = 0.034 p = 0.031
## fT 0.013 -0.032
## (-0.127, 0.153) (-0.178, 0.114)
## t = 0.179 t = -0.430
## p = 0.859 p = 0.668
## Constant 1.292 2.052 1.263 2.058
## (0.579, 2.005) (1.952, 2.151) (0.539, 1.988) (1.988, 2.128)
## t = 3.552 t = 40.463 t = 3.417 t = 57.586
## p = 0.0004 p = 0.000 p = 0.001 p = 0.000
## -------------------------------------------------------------------------------------------
## Observations 100 100 100 100
## Log Likelihood -235.386 -237.627 -235.294 -237.643
## Akaike Inf. Crit. 474.773 479.255 476.587 477.286
## Residual Deviance 84.993 (df = 98) 89.475 (df = 98) 84.808 (df = 97) 89.507 (df = 99)
## Null Deviance (df = 99) 89.507 89.507 89.507 89.507
## ===========================================================================================
- 結果より, xモデルの最大対数尤度は\(-235.4\)なので, 逸脱度:\(D = 470.8\)となる
- ここで残差逸脱度(Residual Deviance)は以下の式で定義される
- \(D - (ポアソン分布モデルで可能な最小逸脱度)\)
- \(最小逸脱度 = フルモデルの逸脱度\) →今回であればデータ数100個をあてはめたモデル
- 残差逸脱度は以下のように求まる
#最小逸脱度
MD <-sum(log(dpois(d$y, lambda = d$y)))
#xモデルの逸脱度
x_D <-(-2*(-235.386))
#xモデルの残差逸脱度
x_RD <- x_D -(-2 * MD)
#結果のxモデルのResidual Devianceと一致
x_RD
## [1] 84.99249
- つまり残差逸脱度は385.8を基準とする「あてはまりの悪さの相対値」
- 逸脱度が大きいほど「あてはまりの悪いモデル」→ここではnullモデルが該当
- 結果を見るとパラメータが増えるほど残差逸脱度は小さくなっているのがわかる