[R]å帰åæ - ç·å½¢éå帰åæ
ä»åã¯ãç·å½¢éå帰åæãå 容ã¯ãRによるデータサイエンス - データ解析の基礎から最新手法までã®7.3ç¯ã«æ²¿ã£ã¦ããã¾ãã
éå帰åæã¨ã¯
説æå¤æ°ãè¤æ°ããå帰åæããéå帰åæã¨å¼ã³ã¾ãã
説æå¤æ°ã®ãã¼ã¿ãXãç®çå¤æ°ãYã誤差ãEãä¿æ°ãAã¨è¡¨ãã¨ãå帰ã¢ãã«ã¯ã
ã¨ãªããä¿æ°ã¯ãæå°ï¼ä¹æ³ãªã©ã§æ±ãããã¨ãåºæ¥ã¾ãã
ç¨ãããã¼ã¿
Rã«ç¨æããã¦ãããairqualityã¨ãããã¼ã¿ã»ããã使ãã¾ãã1973å¹´5æãã9æã¾ã§ã®ãã¥ã¼ã¨ã¼ã¯ã®å¤§æ°ç¶æ ãã6ã¤ã®å¤æ°ã§è¦³æ¸¬ããè¨é²ãã154ã®è¦³æ¸¬å¤ã§ãã
pairs(airquality[,1:4])
解æãã®1
ã¨ãããããOzoneãç®çå¤æ°ã¨ãã¦ãSolar.RãWindãTempã説æå¤æ°ã¨ããéå帰åæãè¡ã£ã¦ã¿ã¾ãã
air.lm <- lm(Ozone~Solar.R+Wind+Temp, data=airquality) summary(air.lm)
è¦ç´
Call: lm(formula = Ozone ~ Solar.R + Wind + Temp, data = airquality) Residuals: Min 1Q Median 3Q Max -40.485 -14.219 -3.551 10.097 95.619 Coefficients: Estimate Std. Error t value Pr(>|t|) (Intercept) -64.34208 23.05472 -2.791 0.00623 ** Solar.R 0.05982 0.02319 2.580 0.01124 * Wind -3.33359 0.65441 -5.094 1.52e-06 *** Temp 1.65209 0.25353 6.516 2.42e-09 *** --- Signif. codes: 0 '***' 0.001 '**' 0.01 '*' 0.05 '.' 0.1 ' ' 1 Residual standard error: 21.18 on 107 degrees of freedom (42 observations deleted due to missingness) Multiple R-squared: 0.6059, Adjusted R-squared: 0.5948 F-statistic: 54.83 on 3 and 107 DF, p-value: < 2.2e-16
AIC
ã¢ãã«ã®è©ä¾¡ã¨ãã¦ãAICã¨ãããã®ããããããã¯å°ããã»ã©è¯ãã¨ããã¦ãã¾ãã
Rã«ã¯ãAICãæ±ããé¢æ°ã¨ãã¦extractAICé¢æ°ãç¨æããã¦ãããã¢ãã«ã®ãã©ã¡ã¼ã¿ã®æ°ã¨AICå¤ãè¿ãã¾ãã
extractAIC(air.lm)
[1] 4.0000 681.7127
ãã©ã¡ã¼ã¿ã®æ°ã4ã¤ã§ãAICã681.7127ã¨åºã¾ããã
解æãã®2
æ£å¸å³ãè¦ãã¨ã説æå¤æ°éã«ããã®å³ã§è¨ãã°ãWindã¨Tempã«ç¸é¢ãããäºãåããã¾ãã
ãã®ãããªå ´åã説æå¤æ°ä¸åã«å¯¾ããå½±é¿ããç®çå¤æ°ã ãã§ãªããããä¸æ¹ã®èª¬æå¤æ°ãWindã®å¤åã®å½±é¿ãTempã«ãåºã¦ãã¾ããé©åãªã¢ãã«ã¨ã¯è¨ãã¾ããã
ããã§ããã®èª¬æå¤æ°éã®ç¸é¢é¢ä¿ï¼ç¸äºä½ç¨ã¨è¨ãã¾ãï¼ãèæ
®ããã¢ãã«ãæ§ç¯ãã¦ã¿ã¾ãã
Rã§ã¯ãã^2ããã¤ããã ãã§ããããåºæ¥ã¾ãã
air.lm2 <- lm(Ozone~(Solar.R+Wind+Temp)^2, data=airquality) summary(air.lm2)
è¦ç´
Call: lm(formula = Ozone ~ (Solar.R + Wind + Temp)^2, data = airquality) Residuals: Min 1Q Median 3Q Max -38.685 -11.727 -2.169 7.360 91.244 Coefficients: Estimate Std. Error t value Pr(>|t|) (Intercept) -1.408e+02 6.419e+01 -2.193 0.03056 * Solar.R -2.260e-01 2.107e-01 -1.073 0.28591 Wind 1.055e+01 4.290e+00 2.460 0.01555 * Temp 2.322e+00 8.330e-01 2.788 0.00631 ** Solar.R:Wind -7.231e-03 6.688e-03 -1.081 0.28212 Solar.R:Temp 5.061e-03 2.445e-03 2.070 0.04089 * Wind:Temp -1.613e-01 5.896e-02 -2.735 0.00733 ** --- Signif. codes: 0 '***' 0.001 '**' 0.01 '*' 0.05 '.' 0.1 ' ' 1 Residual standard error: 19.17 on 104 degrees of freedom (42 observations deleted due to missingness) Multiple R-squared: 0.6863, Adjusted R-squared: 0.6682 F-statistic: 37.93 on 6 and 104 DF, p-value: < 2.2e-16
調æ´æ¸ã¿æ±ºå®ä¿æ°ãã0.5948ãã0.6682ã«ãããã¾ããï¼
解æãã®3
解æãã®2ã®çµæãè¦ãã¨ãã¢ãã«ã«ç¨ãã説æå¤æ°ã®på¤ãããã大ãããã®ãããã¾ãã
ããè¯ãã¢ãã«ãä½æããããã«ã¯ãå¤æ°ãä½ããã®åºæºã§é¸æãã¦ç¨ããå¿
è¦ãããã¾ãã
Rã§ã¯ãAICãç¨ãã¦ã¢ãã«•å¤æ°ãé¸æããé¢æ°stepãç¨æããã¦ãã¾ãã
air.lm3 <- step(air.lm2)
Start: AIC=662.37 Ozone ~ (Solar.R + Wind + Temp)^2 Df Sum of Sq RSS AIC - Solar.R:Wind 1 429.42 38635 661.61 <none> 38205 662.37 - Solar.R:Temp 1 1574.75 39780 664.86 - Wind:Temp 1 2748.20 40954 668.08 Step: AIC=661.61 Ozone ~ Solar.R + Wind + Temp + Solar.R:Temp + Wind:Temp Df Sum of Sq RSS AIC <none> 38635 661.61 - Solar.R:Temp 1 2141.1 40776 665.60 - Wind:Temp 1 4339.8 42975 671.43
çµæãAICã¯661.61ã«ãªãã¾ããã
ã¾ããã¢ãã«ã¯ãSolar.R:Windã®ç¸äºä½ç¨ãåé¤ãã次ã®ã¢ãã«ã«ãªãã¾ãã
- ä¿æ°ãåºå
round(coefficients(air.lm3),2)
(Intercept) Solar.R Wind Temp Solar.R:Temp Wind:Temp -136.81 -0.35 11.15 2.45 0.01 -0.19
- å帰ã¢ãã«
Ozone = -136.81 -0.35*Solar.R + 11.15*Wind + 2.45*Temp + 0.01*Solar.R*Temp -0.19*Wind*Temp
å帰診æå³
æå¾ã«å帰診æå³ã
æ£è¦Q-Qãããããè¦ãã¨ã両端ãç´ç·ããå¤ãã¦ãããæ®å·®ãæ£è¦åå¸ã«å¾ãã¨ããä»®å®ãæãç«ã£ã¦ãã¨ã¯è¨ãé£ããã¨ãåããã¾ãã
ããã«å½ã¦ã¯ã¾ããè¯ãããããã«ã¯ãæ®å·®ã®åå¸ãæ£è¦åå¸ä»¥å¤ã§æ±ããä»ã®ã¢ãã«ãå¿
è¦ã«ãªã£ã¦ãã¾ããããã辺ã«ã¤ãã¦ã¯ãã¾ãå¥ã¨ã³ããªã¼ã§æ¸ãã¾ãã
ã¾ã¨ã
- Rã§ç·å½¢éå帰åæãè¡ãã¾ããã
- 説æå¤æ°éã®ç¸é¢é¢ä¿ãç¸äºä½ç¨ã¨ãããlmé¢æ°ã®formulaã§ã^2ããä»ããäºã§ãããèæ ®ããã¢ãã«ãä½ãã¾ãã
- ã¾ããstepé¢æ°ã§ãAICããã¨ã«ã¢ãã«•å¤æ°é¸æãã§ãã便å©ã ãªã¨æãã¾ããã