拓端tecdat|R語言多元逐步回歸模型分析房價和葡萄酒價格:選擇最合適的預測變量
原文鏈接:http://tecdat.cn/?p=19405?
原文出處:拓端數(shù)據(jù)部落公眾號
包含更多的預測變量不是免費的:在系數(shù)估算的更多可變性,更難的解釋以及可能包含高度依賴的預測變量方面要付出代價。確實,??對于樣本大小
,在線性模型中可以考慮?的預測變量最大數(shù)量為 p??;虻刃У?,使用預測變量p 擬合模型需要最小樣本量
。
如果我們考慮p = 1 和 p = 2 的幾何,這一事實的解釋很簡單:
如果p = 1,則至少需要n = 2個點才能唯一地擬合一條線。但是,這條線沒有給出關于其周圍變化的信息,因此無法估計
。因此,我們至少需要
個點,換句話說就是
。
如果p = 2 ,則至少需要n = 3個點才能唯一地擬合平面。但是同樣,該平面沒有提供有關其周圍數(shù)據(jù)變化的信息,因此無法估計
。因此,我們需要
。
下一部分代碼的輸出闡明了
和
之間的區(qū)別。
# 數(shù)據(jù):n個觀測值,p = n-1個預測變量
n <- 5
p <- n - 1
df <- data.frame(y = rnorm(n), x = matrix(rnorm(n * p), nrow = n, ncol = p))
# 情況p = n-1 = 2:可以估計beta,但不能估計sigma ^ 2(因此,不能執(zhí)行推斷,因為它需要估計的sigma ^ 2)
summary(lm(y ~ ., data = df))
# 情況p = n-2 = 1:可以估計beta和sigma ^ 2(因此可以進行推斷)
summary(lm(y ~ . - x.1, data = df))
當
減小時,自由度
量化
的變異性的增加。
?
既然我們已經(jīng)更多地了解了預測變量過多的問題,我們將重點放在??為多元回歸模型選擇最合適的預測變量上。如果沒有獨特的解決方案,這將是一項艱巨的任務。但是,有一個行之有效的程序通常會產(chǎn)生良好的結(jié)果:?逐步模型選擇。其原理是?依次比較具有不同預測變量的多個線性回歸模型。
在介紹該方法之前,我們需要了解什么是?信息準則。信息標準在模型的適用性與采用的預測變量數(shù)量之間取得平衡。兩個常見標準是?貝葉斯信息標準?(BIC)和?赤池信息標準?(AIC)。兩者都基于?模型適用性和復雜性之間的平衡:
其中
是模型的對?數(shù)似然度?(模型擬合數(shù)據(jù)的程度),而
是考慮的參數(shù)數(shù)量在模型中,對于具有p個預測變量的多元線性回歸模型,則為p + 2。AIC在用
替換了
,??因此,與BIC相比,它對?較復雜的模型的處罰較少。這就是為什么一些從業(yè)者更喜歡BIC進行模型比較的原因之一。BIC和AIC可以通過BIC
?和?計算?AIC
。
我們使用地區(qū)房價數(shù)據(jù),變量介紹:
(1)town:每一個人口普查區(qū)所在的城鎮(zhèn)
(2)LON: 人口普查區(qū)中心的經(jīng)度
(3)LAT:?人口普查區(qū)中心的緯度
(4)MEDV: 每一個人口普查區(qū)所對應的房子價值的中位數(shù) (單位為$1000)
(5)CRIM: 人均犯罪率
(6)ZN: 土地中有多少是地區(qū)是大量住宅物業(yè)
(7)INDUS: 區(qū)域中用作工業(yè)用途的土地占比
(8)CHAS: 1:該人口普查區(qū)緊鄰查爾斯河;0: 該人口普查區(qū)沒有緊鄰查爾斯河
(9)NOX: 空氣中氮氧化物的集中度 (衡量空氣污染的指標)
(10)RM: 每個房子的平均房間數(shù)目
(11)AGE: 建于1940年以前的房子的比例
(12)DIS: 該人口普查區(qū)距離波士頓市中心的距離
(13)RAD: 距離重要高速路的遠近程度 (1代表最近;24代表最遠)
(14)TAX: 房子每$10,000價值所對應的稅收金額
(15)PTRATIO: 該城鎮(zhèn)學生與老師的比例
他們將作為模型輸入。
# 具有不同預測變量的兩個模型
mod1 <- lm(medv ~ age + crim, data = Boston)
mod2 <- lm(medv ~ age + crim + lstat, data = Boston)
# BICs
BIC(mod1)
## [1] 3581.893
BIC(mod2) # 較小->較好
## [1] 3300.841
# AICs
AIC(mod1)
## [1] 3564.987
AIC(mod2) # 較小->較好
## [1] 3279.708
# 檢查摘要
##
## Residuals:
## ? ? Min ? ? ?1Q ?Median ? ? ?3Q ? ? Max
## -13.940 ?-4.991 ?-2.420 ? 2.110 ?32.033
##
## Coefficients:
## ? ? ? ? ? ? Estimate Std. Error t value Pr(>|t|)
## (Intercept) 29.80067 ? ?0.97078 ?30.698 ?< 2e-16 ***
## age ? ? ? ? -0.08955 ? ?0.01378 ?-6.499 1.95e-10 ***
## crim ? ? ? ?-0.31182 ? ?0.04510 ?-6.914 1.43e-11 ***
## ---
## Signif. codes: ?0 '***' 0.001 '**' 0.01 '*' 0.05 '.' 0.1 ' ' 1
##
## Residual standard error: 8.157 on 503 degrees of freedom
## Multiple R-squared: ?0.2166, Adjusted R-squared: ?0.2134
## F-statistic: 69.52 on 2 and 503 DF, ?p-value: < 2.2e-16
summary(mod2)
##
## Call:
## lm(formula = medv ~ age + crim + lstat, data = Boston)
##
## Residuals:
## ? ? Min ? ? ?1Q ?Median ? ? ?3Q ? ? Max
## -16.133 ?-3.848 ?-1.380 ? 1.970 ?23.644
##
## Coefficients:
## ? ? ? ? ? ? Estimate Std. Error t value Pr(>|t|)
## (Intercept) 32.82804 ? ?0.74774 ?43.903 ?< 2e-16 ***
## age ? ? ? ? ?0.03765 ? ?0.01225 ? 3.074 ?0.00223 **
## crim ? ? ? ?-0.08262 ? ?0.03594 ?-2.299 ?0.02193 *
## lstat ? ? ? -0.99409 ? ?0.05075 -19.587 ?< 2e-16 ***
## ---
## Signif. codes: ?0 '***' 0.001 '**' 0.01 '*' 0.05 '.' 0.1 ' ' 1
##
## Residual standard error: 6.147 on 502 degrees of freedom
## Multiple R-squared: ?0.5559, Adjusted R-squared: ?0.5533
## F-statistic: 209.5 on 3 and 502 DF, ?p-value: < 2.2e-16
讓我們回到預測變量的選擇。如果我們有p個預測變量,那么一個簡單的過程就是檢查?所有?可用它們構建的可能模型,然后根據(jù)BIC / AIC選擇最佳模型。這就是所謂的?最佳子集選擇。問題在于存在
個可能的模型!
讓我們看看如何研究?wine
?數(shù)據(jù)集,將使用所有可用預測變量的數(shù)據(jù)作為初始模型。
波爾多是法國的葡萄酒產(chǎn)區(qū)。盡管這種酒的生產(chǎn)方式幾乎相同,但已有數(shù)百年歷史,但每年的價格和質(zhì)量差異有時非常顯著。人們普遍認為波爾多葡萄酒陳年越老越好,因此有動力去儲存葡萄酒直至成熟。主要問題在于,僅通過品嘗就很難確定葡萄酒的質(zhì)量,因為在實際飲用時,味道會發(fā)生很大變化。這就是為什么葡萄酒品嘗師和專家會有所幫助的原因。他們品嘗葡萄酒,然后預測以后將是最好的葡萄酒。
1990年3月4日,《紐約時報》宣布普林斯頓大學經(jīng)濟學教授奧利·阿森費爾特(Orley Ashenfelter)可以預測波爾多葡萄酒的質(zhì)量而無需品嘗一滴。 Ashenfelter使用了一種稱為線性回歸的方法。該方法預測結(jié)果變量或因變量。作為自變量,他使用了酒的年份(因此,較老的酒會更昂貴)和與天氣有關的信息,特別是平均生長季節(jié)溫度,收成雨和冬雨。
stepAIC
?將參數(shù)?k
?設為2 (默認值)或
,其中n是樣本大小。k = 2
?它采用AIC準則,?k = log(n)
?它采用BIC準則。
# 完整模型
# 用 BIC
## Start: ?AIC=-53.29
## Price ~ Year + WinterRain + AGST + HarvestRain + Age + FrancePop
##
##
## Step: ?AIC=-53.29
## Price ~ Year + WinterRain + AGST + HarvestRain + FrancePop
##
## ? ? ? ? ? ? ? Df Sum of Sq ? ?RSS ? ? AIC
## - FrancePop ? ?1 ? ?0.0026 1.8058 -56.551
## - Year ? ? ? ? 1 ? ?0.0048 1.8080 -56.519
## <none> ? ? ? ? ? ? ? ? ? ? 1.8032 -53.295
## - WinterRain ? 1 ? ?0.4585 2.2617 -50.473
## - HarvestRain ?1 ? ?1.8063 3.6095 -37.852
## - AGST ? ? ? ? 1 ? ?3.3756 5.1788 -28.105
##
## Step: ?AIC=-56.55
## Price ~ Year + WinterRain + AGST + HarvestRain
##
## ? ? ? ? ? ? ? Df Sum of Sq ? ?RSS ? ? AIC
## <none> ? ? ? ? ? ? ? ? ? ? 1.8058 -56.551
## - WinterRain ? 1 ? ?0.4809 2.2867 -53.473
## - Year ? ? ? ? 1 ? ?0.9089 2.7147 -48.840
## - HarvestRain ?1 ? ?1.8760 3.6818 -40.612
## - AGST ? ? ? ? 1 ? ?3.4428 5.2486 -31.039
summary(modBIC)
##
## Call:
## lm(formula = Price ~ Year + WinterRain + AGST + HarvestRain,
## ? ? data = wine)
##
## Residuals:
## ? ? ?Min ? ? ? 1Q ? Median ? ? ? 3Q ? ? ?Max
## -0.46024 -0.23862 ?0.01347 ?0.18601 ?0.53443
##
## Coefficients:
## ? ? ? ? ? ? ? Estimate Std. Error t value Pr(>|t|)
## (Intercept) 43.6390418 14.6939240 ? 2.970 ?0.00707 **
## Year ? ? ? ?-0.0238480 ?0.0071667 ?-3.328 ?0.00305 **
## WinterRain ? 0.0011667 ?0.0004820 ? 2.420 ?0.02421 *
## AGST ? ? ? ? 0.6163916 ?0.0951747 ? 6.476 1.63e-06 ***
## HarvestRain -0.0038606 ?0.0008075 ?-4.781 8.97e-05 ***
## ---
## Signif. codes: ?0 '***' 0.001 '**' 0.01 '*' 0.05 '.' 0.1 ' ' 1
##
## Residual standard error: 0.2865 on 22 degrees of freedom
## Multiple R-squared: ?0.8275, Adjusted R-squared: ?0.7962
## F-statistic: 26.39 on 4 and 22 DF, ?p-value: 4.057e-08
# 用 AIC
## Start: ?AIC=-61.07
## Price ~ Year + WinterRain + AGST + HarvestRain + Age + FrancePop
##
##
## Step: ?AIC=-61.07
## Price ~ Year + WinterRain + AGST + HarvestRain + FrancePop
##
## ? ? ? ? ? ? ? Df Sum of Sq ? ?RSS ? ? AIC
## - FrancePop ? ?1 ? ?0.0026 1.8058 -63.031
## - Year ? ? ? ? 1 ? ?0.0048 1.8080 -62.998
## <none> ? ? ? ? ? ? ? ? ? ? 1.8032 -61.070
## - WinterRain ? 1 ? ?0.4585 2.2617 -56.952
## - HarvestRain ?1 ? ?1.8063 3.6095 -44.331
## - AGST ? ? ? ? 1 ? ?3.3756 5.1788 -34.584
##
## Step: ?AIC=-63.03
## Price ~ Year + WinterRain + AGST + HarvestRain
##
## ? ? ? ? ? ? ? Df Sum of Sq ? ?RSS ? ? AIC
## <none> ? ? ? ? ? ? ? ? ? ? 1.8058 -63.031
## - WinterRain ? 1 ? ?0.4809 2.2867 -58.656
## - Year ? ? ? ? 1 ? ?0.9089 2.7147 -54.023
## - HarvestRain ?1 ? ?1.8760 3.6818 -45.796
## - AGST ? ? ? ? 1 ? ?3.4428 5.2486 -36.222
summary(modAIC)
##
## Call:
## lm(formula = Price ~ Year + WinterRain + AGST + HarvestRain,
## ? ? data = wine)
##
## Residuals:
## ? ? ?Min ? ? ? 1Q ? Median ? ? ? 3Q ? ? ?Max
## -0.46024 -0.23862 ?0.01347 ?0.18601 ?0.53443
##
## Coefficients:
## ? ? ? ? ? ? ? Estimate Std. Error t value Pr(>|t|)
## (Intercept) 43.6390418 14.6939240 ? 2.970 ?0.00707 **
## Year ? ? ? ?-0.0238480 ?0.0071667 ?-3.328 ?0.00305 **
## WinterRain ? 0.0011667 ?0.0004820 ? 2.420 ?0.02421 *
## AGST ? ? ? ? 0.6163916 ?0.0951747 ? 6.476 1.63e-06 ***
## HarvestRain -0.0038606 ?0.0008075 ?-4.781 8.97e-05 ***
## ---
## Signif. codes: ?0 '***' 0.001 '**' 0.01 '*' 0.05 '.' 0.1 ' ' 1
##
## Residual standard error: 0.2865 on 22 degrees of freedom
## Multiple R-squared: ?0.8275, Adjusted R-squared: ?0.7962
## F-statistic: 26.39 on 4 and 22 DF, ?p-value: 4.057e-08
接下來是stepAIC
?對執(zhí)行情況的解釋?。在每個步驟中,?stepAIC
?顯示有關信息標準當前值的信息。例如,對于?modBIC
,第一步的BIC是Step: AIC=-53.29
?,然后在第二步進行?了改進?Step: AIC=-56.55
?(即使使用“ BIC”,該功能也會始終輸出“ AIC”)。下一個繼續(xù)前進的模型是stepAIC
?通過研究添加或刪除預測變量后得出的不同模型的信息標準來決定的?(取決于?direction
?參數(shù),在下文中進行解釋)。例如modBIC
在第一步中,刪除導致的模型?FrancePop
?的BIC等于?-56.551
,如果?Year
?刪除,則BIC將為?-56.519
。逐步回歸,然后刪除?FrancePop
?(因為它給出了最低的BIC),然后重復此過程,最終導致刪除?<none>
?預測變量是可能的最佳操作。下面的代碼塊說明了stepsAIC
的輸出?extractAIC
,和BIC / AIC的輸出BIC
/?AIC
。
# 相同的BIC,標準不同
AIC(modBIC, k = log(n))
## [1] -56.55135
BIC(modBIC)
## [1] 23.36717
# 觀察到MASS :: stepAIC(mod,k = log(nrow(wine)))返回的最終BIC是由extractAIC()而不是BIC()給出的!但是兩者是等效的
# 相同的AIC,標準不同
AIC(modBIC, k = 2)
## [1] -63.03053
BIC(modBIC)
## [1] 23.36717
BIC(modBIC) - AIC(modBIC
## [1] 79.91852
n * (log(2 * pi+ 1) + log(n)
## [1] 79.91852
#與AIC相同
AIC(modAIC) - AIC(modAIC
## [1] 78.62268
n * (log(2 * pi + 1 + 2
## [1] 78.62268
請注意,所選模型?modBIC
?和?modAIC
?等效于?modWine2
?,我們選擇的最佳模型。這說明,選擇的模型?stepAIC
?通常是進一步添加或刪除預測變量的良好起點。
使用?stepAIC
?BIC / AIC時,可能會選擇不同的最終模型?direction
。這是解釋:
“backward”
:??從給定模型中順序刪除預測變量。“forward”
:?將?預測變量順序添加到給定模型中。“both”
?(默認):以上的組合。
該?建議?是嘗試幾種這些方法并保留一個最小的BIC / AIC。設置?trace = 0
?為省略冗長的搜索過程信息輸出。下面的代碼塊清楚地說明了如何使用?數(shù)據(jù)集的修改版本?來利用?direction
?參數(shù)和的其他選項?。stepAIC
wine
direction = "forward"
direction = "both"
scope
# 將無關的預測變量添加到葡萄酒數(shù)據(jù)集中
# 向后選擇:從給定模型中順序刪除預測變量
# 從具有所有預測變量的模型開始
modAll, direction = "backward", k = log(n)
## Start: ?AIC=-50.13
## Price ~ Year + WinterRain + AGST + HarvestRain + Age + FrancePop +
## ? ? noisePredictor
##
##
## Step: ?AIC=-50.13
## Price ~ Year + WinterRain + AGST + HarvestRain + FrancePop +
## ? ? noisePredictor
##
## ? ? ? ? ? ? ? ? ?Df Sum of Sq ? ?RSS ? ? AIC
## - FrancePop ? ? ? 1 ? ?0.0036 1.7977 -53.376
## - Year ? ? ? ? ? ?1 ? ?0.0038 1.7979 -53.374
## - noisePredictor ?1 ? ?0.0090 1.8032 -53.295
## <none> ? ? ? ? ? ? ? ? ? ? ? ?1.7941 -50.135
## - WinterRain ? ? ?1 ? ?0.4598 2.2539 -47.271
## - HarvestRain ? ? 1 ? ?1.7666 3.5607 -34.923
## - AGST ? ? ? ? ? ?1 ? ?3.3658 5.1599 -24.908
##
## Step: ?AIC=-53.38
## Price ~ Year + WinterRain + AGST + HarvestRain + noisePredictor
##
## ? ? ? ? ? ? ? ? ?Df Sum of Sq ? ?RSS ? ? AIC
## - noisePredictor ?1 ? ?0.0081 1.8058 -56.551
## <none> ? ? ? ? ? ? ? ? ? ? ? ?1.7977 -53.376
## - WinterRain ? ? ?1 ? ?0.4771 2.2748 -50.317
## - Year ? ? ? ? ? ?1 ? ?0.9162 2.7139 -45.552
## - HarvestRain ? ? 1 ? ?1.8449 3.6426 -37.606
## - AGST ? ? ? ? ? ?1 ? ?3.4234 5.2212 -27.885
##
## Step: ?AIC=-56.55
## Price ~ Year + WinterRain + AGST + HarvestRain
##
## ? ? ? ? ? ? ? Df Sum of Sq ? ?RSS ? ? AIC
## <none> ? ? ? ? ? ? ? ? ? ? 1.8058 -56.551
## - WinterRain ? 1 ? ?0.4809 2.2867 -53.473
## - Year ? ? ? ? 1 ? ?0.9089 2.7147 -48.840
## - HarvestRain ?1 ? ?1.8760 3.6818 -40.612
## - AGST ? ? ? ? 1 ? ?3.4428 5.2486 -31.039
##
## Call:
## lm(formula = Price ~ Year + WinterRain + AGST + HarvestRain,
## ? ? data = wineNoise)
##
## Coefficients:
## (Intercept) ? ? ? ? Year ? WinterRain ? ? ? ? AGST ?HarvestRain
## ? 43.639042 ? ?-0.023848 ? ? 0.001167 ? ? 0.616392 ? ?-0.003861
# 從中間模型開始
AIC(modInter, direction = "backward", k = log(n)
## Start: ?AIC=-32.38
## Price ~ noisePredictor + Year + AGST
##
## ? ? ? ? ? ? ? ? ?Df Sum of Sq ? ?RSS ? ? AIC
## - noisePredictor ?1 ? ?0.0146 5.0082 -35.601
## <none> ? ? ? ? ? ? ? ? ? ? ? ?4.9936 -32.384
## - Year ? ? ? ? ? ?1 ? ?0.7522 5.7459 -31.891
## - AGST ? ? ? ? ? ?1 ? ?3.2211 8.2147 -22.240
##
## Step: ?AIC=-35.6
## Price ~ Year + AGST
##
## ? ? ? ?Df Sum of Sq ? ?RSS ? ? AIC
## <none> ? ? ? ? ? ? ?5.0082 -35.601
## - Year ?1 ? ?0.7966 5.8049 -34.911
## - AGST ?1 ? ?3.2426 8.2509 -25.417
##
## Call:
## lm(formula = Price ~ Year + AGST, data = wineNoise)
##
## Coefficients:
## (Intercept) ? ? ? ? Year ? ? ? ? AGST
## ? ?41.49441 ? ? -0.02221 ? ? ?0.56067
# 回想一下,在搜索過程中未探索未包含在modInter中的其他預測變量(因此未添加相關的預測變量HarvestRain)
# 正向選擇:從給定模型順序添加預測變量
# 從沒有預測變量的模型開始,僅截距模型(表示為?1)
AIC(modZero, direction = "forward"
## Start: ?AIC=-22.28
## Price ~ 1
##
## ? ? ? ? ? ? ? ? ?Df Sum of Sq ? ? RSS ? ? AIC
## + AGST ? ? ? ? ? ?1 ? ?4.6655 ?5.8049 -34.911
## + HarvestRain ? ? 1 ? ?2.6933 ?7.7770 -27.014
## + FrancePop ? ? ? 1 ? ?2.4231 ?8.0472 -26.092
## + Year ? ? ? ? ? ?1 ? ?2.2195 ?8.2509 -25.417
## + Age ? ? ? ? ? ? 1 ? ?2.2195 ?8.2509 -25.417
## <none> ? ? ? ? ? ? ? ? ? ? ? ?10.4703 -22.281
## + WinterRain ? ? ?1 ? ?0.1905 10.2798 -19.481
## + noisePredictor ?1 ? ?0.1761 10.2942 -19.443
##
## Step: ?AIC=-34.91
## Price ~ AGST
##
## ? ? ? ? ? ? ? ? ?Df Sum of Sq ? ?RSS ? ? AIC
## + HarvestRain ? ? 1 ? 2.50659 3.2983 -46.878
## + WinterRain ? ? ?1 ? 1.42392 4.3809 -39.214
## + FrancePop ? ? ? 1 ? 0.90263 4.9022 -36.178
## + Year ? ? ? ? ? ?1 ? 0.79662 5.0082 -35.601
## + Age ? ? ? ? ? ? 1 ? 0.79662 5.0082 -35.601
## <none> ? ? ? ? ? ? ? ? ? ? ? ?5.8049 -34.911
## + noisePredictor ?1 ? 0.05900 5.7459 -31.891
##
## Step: ?AIC=-46.88
## Price ~ AGST + HarvestRain
##
## ? ? ? ? ? ? ? ? ?Df Sum of Sq ? ?RSS ? ? AIC
## + FrancePop ? ? ? 1 ? 1.03572 2.2625 -53.759
## + Year ? ? ? ? ? ?1 ? 1.01159 2.2867 -53.473
## + Age ? ? ? ? ? ? 1 ? 1.01159 2.2867 -53.473
## + WinterRain ? ? ?1 ? 0.58356 2.7147 -48.840
## <none> ? ? ? ? ? ? ? ? ? ? ? ?3.2983 -46.878
## + noisePredictor ?1 ? 0.06084 3.2374 -44.085
##
## Step: ?AIC=-53.76
## Price ~ AGST + HarvestRain + FrancePop
##
## ? ? ? ? ? ? ? ? ?Df Sum of Sq ? ?RSS ? ? AIC
## + WinterRain ? ? ?1 ? 0.45456 1.8080 -56.519
## <none> ? ? ? ? ? ? ? ? ? ? ? ?2.2625 -53.759
## + noisePredictor ?1 ? 0.00829 2.2542 -50.562
## + Age ? ? ? ? ? ? 1 ? 0.00085 2.2617 -50.473
## + Year ? ? ? ? ? ?1 ? 0.00085 2.2617 -50.473
##
## Step: ?AIC=-56.52
## Price ~ AGST + HarvestRain + FrancePop + WinterRain
##
## ? ? ? ? ? ? ? ? ?Df Sum of Sq ? ?RSS ? ? AIC
## <none> ? ? ? ? ? ? ? ? ? ? ? ?1.8080 -56.519
## + noisePredictor ?1 0.0100635 1.7979 -53.374
## + Year ? ? ? ? ? ?1 0.0048039 1.8032 -53.295
## + Age ? ? ? ? ? ? 1 0.0048039 1.8032 -53.295
##
## Call:
## lm(formula = Price ~ AGST + HarvestRain + FrancePop + WinterRain,
## ? ? data = wineNoise)
##
## Coefficients:
## (Intercept) ? ? ? ? AGST ?HarvestRain ? ?FrancePop ? WinterRain
## ?-5.945e-01 ? ?6.127e-01 ? -3.804e-03 ? -5.189e-05 ? ?1.136e-03
# 在進行正向搜索時,充分設置范圍參數(shù)非常重要!在范圍中,您必須定義包含可探索模型集的“最小”(下部)和“最大”(上部)模型。如果未提供,則將最大模型用作傳遞的起始模型(在這種情況下為modZero),而stepAIC將不執(zhí)行任何搜索
#從中間模型開始
## Start: ?AIC=-32.38
## Price ~ noisePredictor + Year + AGST
##
## ? ? ? ? ? ? ? Df Sum of Sq ? ?RSS ? ? AIC
## + HarvestRain ?1 ? 2.71878 2.2748 -50.317
## + WinterRain ? 1 ? 1.35102 3.6426 -37.606
## <none> ? ? ? ? ? ? ? ? ? ? 4.9936 -32.384
## + FrancePop ? ?1 ? 0.24004 4.7536 -30.418
##
## Step: ?AIC=-50.32
## Price ~ noisePredictor + Year + AGST + HarvestRain
##
## ? ? ? ? ? ? ?Df Sum of Sq ? ?RSS ? ? AIC
## + WinterRain ?1 ? 0.47710 1.7977 -53.376
## <none> ? ? ? ? ? ? ? ? ? ?2.2748 -50.317
## + FrancePop ? 1 ? 0.02094 2.2539 -47.271
##
## Step: ?AIC=-53.38
## Price ~ noisePredictor + Year + AGST + HarvestRain + WinterRain
##
## ? ? ? ? ? ? Df Sum of Sq ? ?RSS ? ? AIC
## <none> ? ? ? ? ? ? ? ? ? 1.7977 -53.376
## + FrancePop ?1 0.0036037 1.7941 -50.135
##
## Call:
## lm(formula = Price ~ noisePredictor + Year + AGST + HarvestRain +
## ? ? WinterRain, data = wineNoise)
##
## Coefficients:
## ? ?(Intercept) ?noisePredictor ? ? ? ? ? ?Year ? ? ? ? ? ?AGST ? ? HarvestRain ? ? ?WinterRain
## ? ? ?44.096639 ? ? ? -0.019617 ? ? ? -0.024126 ? ? ? ?0.620522 ? ? ? -0.003840 ? ? ? ??;叵胍幌?,在搜索期間不會刪除modInter中包含的預測變量(因此會保留無關的noisePredictor)
#兩種選擇:如果從中間模型開始,則很有用
#消除了與從中間模型完成的“向后”和“向前”搜索相關的問題
## Start: ?AIC=-32.38
## Price ~ noisePredictor + Year + AGST
##
## ? ? ? ? ? ? ? ? ?Df Sum of Sq ? ?RSS ? ? AIC
## + HarvestRain ? ? 1 ? ?2.7188 2.2748 -50.317
## + WinterRain ? ? ?1 ? ?1.3510 3.6426 -37.606
## - noisePredictor ?1 ? ?0.0146 5.0082 -35.601
## <none> ? ? ? ? ? ? ? ? ? ? ? ?4.9936 -32.384
## - Year ? ? ? ? ? ?1 ? ?0.7522 5.7459 -31.891
## + FrancePop ? ? ? 1 ? ?0.2400 4.7536 -30.418
## - AGST ? ? ? ? ? ?1 ? ?3.2211 8.2147 -22.240
##
## Step: ?AIC=-50.32
## Price ~ noisePredictor + Year + AGST + HarvestRain
##
## ? ? ? ? ? ? ? ? ?Df Sum of Sq ? ?RSS ? ? AIC
## - noisePredictor ?1 ? 0.01182 2.2867 -53.473
## + WinterRain ? ? ?1 ? 0.47710 1.7977 -53.376
## <none> ? ? ? ? ? ? ? ? ? ? ? ?2.2748 -50.317
## + FrancePop ? ? ? 1 ? 0.02094 2.2539 -47.271
## - Year ? ? ? ? ? ?1 ? 0.96258 3.2374 -44.085
## - HarvestRain ? ? 1 ? 2.71878 4.9936 -32.384
## - AGST ? ? ? ? ? ?1 ? 2.94647 5.2213 -31.180
##
## Step: ?AIC=-53.47
## Price ~ Year + AGST + HarvestRain
##
## ? ? ? ? ? ? ? ? ?Df Sum of Sq ? ?RSS ? ? AIC
## + WinterRain ? ? ?1 ? 0.48087 1.8058 -56.551
## <none> ? ? ? ? ? ? ? ? ? ? ? ?2.2867 -53.473
## + FrancePop ? ? ? 1 ? 0.02497 2.2617 -50.473
## + noisePredictor ?1 ? 0.01182 2.2748 -50.317
## - Year ? ? ? ? ? ?1 ? 1.01159 3.2983 -46.878
## - HarvestRain ? ? 1 ? 2.72157 5.0082 -35.601
## - AGST ? ? ? ? ? ?1 ? 2.96500 5.2517 -34.319
##
## Step: ?AIC=-56.55
## Price ~ Year + AGST + HarvestRain + WinterRain
##
## ? ? ? ? ? ? ? ? ?Df Sum of Sq ? ?RSS ? ? AIC
## <none> ? ? ? ? ? ? ? ? ? ? ? ?1.8058 -56.551
## - WinterRain ? ? ?1 ? ?0.4809 2.2867 -53.473
## + noisePredictor ?1 ? ?0.0081 1.7977 -53.376
## + FrancePop ? ? ? 1 ? ?0.0026 1.8032 -53.295
## - Year ? ? ? ? ? ?1 ? ?0.9089 2.7147 -48.840
## - HarvestRain ? ? 1 ? ?1.8760 3.6818 -40.612
## - AGST ? ? ? ? ? ?1 ? ?3.4428 5.2486 -31.039
##
## Call:
## lm(formula = Price ~ Year + AGST + HarvestRain + WinterRain,
## ? ? data = wineNoise)
##
## Coefficients:
## (Intercept) ? ? ? ? Year ? ? ? ? AGST ?HarvestRain ? WinterRain
## ? 43.639042 ? ?-0.023848 ? ? 0.616392 ? ?-0.003861 ? ? 0.001167
# 正確定義范圍也很重要,因為“兩個”都求助于“前進”(以及“后退”)
#使用完整模型中的默認值實質(zhì)上會進行向后選擇,但允許已刪除的預測變量在以后的步驟中再次輸入
AIC(modAll direction = "both", k = log(n)
## Start: ?AIC=-50.13
## Price ~ Year + WinterRain + AGST + HarvestRain + Age + FrancePop +
## ? ? noisePredictor
##
##
## Step: ?AIC=-50.13
## Price ~ Year + WinterRain + AGST + HarvestRain + FrancePop +
## ? ? noisePredictor
##
## ? ? ? ? ? ? ? ? ?Df Sum of Sq ? ?RSS ? ? AIC
## - FrancePop ? ? ? 1 ? ?0.0036 1.7977 -53.376
## - Year ? ? ? ? ? ?1 ? ?0.0038 1.7979 -53.374
## - noisePredictor ?1 ? ?0.0090 1.8032 -53.295
## <none> ? ? ? ? ? ? ? ? ? ? ? ?1.7941 -50.135
## - WinterRain ? ? ?1 ? ?0.4598 2.2539 -47.271
## - HarvestRain ? ? 1 ? ?1.7666 3.5607 -34.923
## - AGST ? ? ? ? ? ?1 ? ?3.3658 5.1599 -24.908
##
## Step: ?AIC=-53.38
## Price ~ Year + WinterRain + AGST + HarvestRain + noisePredictor
##
## ? ? ? ? ? ? ? ? ?Df Sum of Sq ? ?RSS ? ? AIC
## - noisePredictor ?1 ? ?0.0081 1.8058 -56.551
## <none> ? ? ? ? ? ? ? ? ? ? ? ?1.7977 -53.376
## - WinterRain ? ? ?1 ? ?0.4771 2.2748 -50.317
## + FrancePop ? ? ? 1 ? ?0.0036 1.7941 -50.135
## - Year ? ? ? ? ? ?1 ? ?0.9162 2.7139 -45.552
## - HarvestRain ? ? 1 ? ?1.8449 3.6426 -37.606
## - AGST ? ? ? ? ? ?1 ? ?3.4234 5.2212 -27.885
##
## Step: ?AIC=-56.55
## Price ~ Year + WinterRain + AGST + HarvestRain
##
## ? ? ? ? ? ? ? ? ?Df Sum of Sq ? ?RSS ? ? AIC
## <none> ? ? ? ? ? ? ? ? ? ? ? ?1.8058 -56.551
## - WinterRain ? ? ?1 ? ?0.4809 2.2867 -53.473
## + noisePredictor ?1 ? ?0.0081 1.7977 -53.376
## + FrancePop ? ? ? 1 ? ?0.0026 1.8032 -53.295
## - Year ? ? ? ? ? ?1 ? ?0.9089 2.7147 -48.840
## - HarvestRain ? ? 1 ? ?1.8760 3.6818 -40.612
## - AGST ? ? ? ? ? ?1 ? ?3.4428 5.2486 -31.039
##
## Call:
## lm(formula = Price ~ Year + WinterRain + AGST + HarvestRain,
## ? ? data = wineNoise)
##
## Coefficients:
## (Intercept) ? ? ? ? Year ? WinterRain ? ? ? ? AGST ?HarvestRain
## ? 43.639042 ? ?-0.023848 ? ? 0.001167 ? ? 0.616392 ? ?-0.003861
# 省略冗長的輸出
AIC(modAll, direction = "both", trace = 0
##
## Call:
## lm(formula = Price ~ Year + WinterRain + AGST + HarvestRain,
## ? ? data = wineNoise)
##
## Coefficients:
## (Intercept) ? ? ? ? Year ? WinterRain ? ? ? ? AGST ?HarvestRain
## ? 43.639042 ? ?-0.023848 ? ? 0.001167 ? ? 0.616392 ? ?-0.003861
對Boston
?數(shù)據(jù)集運行逐步選擇?,目的是清楚地了解不同的搜索方向。特別:
"forward"
?從?逐步擬合?medv ~ 1
開始做。"forward"
?從?逐步擬合?medv ~ crim + lstat + age
開始做。"both"
?從?逐步擬合?medv ~ crim + lstat + age
開始做。"both"
?從逐步擬合?medv ~ .
開始做。"backward"
?從逐步擬合?medv ~ .
開始做。
stepAIC
?假定數(shù)據(jù)中不存在?NA
(缺失值)。建議先刪除數(shù)據(jù)中的缺失值。它們的存在可能會導致錯誤。為此,請使用?data = na.omit(dataset)
?調(diào)用?lm
?(如果您的數(shù)據(jù)集為?dataset
)。
我們通過強調(diào)使用BIC和AIC得出結(jié)論:它們的構造是假設樣本大小n 遠大于模型中參數(shù)的數(shù)量p + 2。因此,如果n >> p + 2 ,它們將工作得相當好,但是如果不是這樣,則它們可能會支持不切實際的復雜模型。下圖對此現(xiàn)象進行了說明。BIC和AIC曲線傾向于使局部最小值接近p = 2,然后增加。但是當p + 2 接近n 時,它們會迅速下降。
圖:n = 200和p從1 到198 的BIC和AIC的比較。M = 100數(shù)據(jù)集僅在前兩個?預測變量有效的情況下進行了模擬?。較粗的曲線是每種顏色曲線的平均值。
房價案例研究應用
我們要建立一個線性模型進行預測和解釋?medv
。有大量的預測模型,其中一些可能對預測medv
沒什么用?。但是,目前尚不清楚哪個預測變量可以更好地解釋?medv
?的信息。因此,我們可以對所有?預測變量進行線性模型處理?:
summary(modHouse)
##
##
## Residuals:
## ? ? Min ? ? ?1Q ?Median ? ? ?3Q ? ? Max
## -15.595 ?-2.730 ?-0.518 ? 1.777 ?26.199
##
## Coefficients:
## ? ? ? ? ? ? ? Estimate Std. Error t value Pr(>|t|)
## (Intercept) ?3.646e+01 ?5.103e+00 ? 7.144 3.28e-12 ***
## crim ? ? ? ?-1.080e-01 ?3.286e-02 ?-3.287 0.001087 **
## zn ? ? ? ? ? 4.642e-02 ?1.373e-02 ? 3.382 0.000778 ***
## indus ? ? ? ?2.056e-02 ?6.150e-02 ? 0.334 0.738288
## chas ? ? ? ? 2.687e+00 ?8.616e-01 ? 3.118 0.001925 **
## nox ? ? ? ? -1.777e+01 ?3.820e+00 ?-4.651 4.25e-06 ***
## rm ? ? ? ? ? 3.810e+00 ?4.179e-01 ? 9.116 ?< 2e-16 ***
## age ? ? ? ? ?6.922e-04 ?1.321e-02 ? 0.052 0.958229
## dis ? ? ? ? -1.476e+00 ?1.995e-01 ?-7.398 6.01e-13 ***
## rad ? ? ? ? ?3.060e-01 ?6.635e-02 ? 4.613 5.07e-06 ***
## tax ? ? ? ? -1.233e-02 ?3.760e-03 ?-3.280 0.001112 **
## ptratio ? ? -9.527e-01 ?1.308e-01 ?-7.283 1.31e-12 ***
## black ? ? ? ?9.312e-03 ?2.686e-03 ? 3.467 0.000573 ***
## lstat ? ? ? -5.248e-01 ?5.072e-02 -10.347 ?< 2e-16 ***
## ---
## Signif. codes: ?0 '***' 0.001 '**' 0.01 '*' 0.05 '.' 0.1 ' ' 1
##
## Residual standard error: 4.745 on 492 degrees of freedom
## Multiple R-squared: ?0.7406, Adjusted R-squared: ?0.7338
## F-statistic: 108.1 on 13 and 492 DF, ?p-value: < 2.2e-16
有幾個不重要的變量,但是到目前為止,該模型具有R ^ 2 = 0.74,并且擬合系數(shù)對預期的結(jié)果很敏感。例如?crim
,??tax
,??ptratio
,和?nox
?對medv
有負面影響?,同時?rm
,?rad
和?chas
?有正面的影響。但是,不重要的系數(shù)不會顯著影響模型,而只會增加噪聲并降低系數(shù)估計的總體準確性。讓我們稍微完善一下以前的模型。
# 最佳模型
AIC(modHouse, k = log(nrow(Boston)
## Start: ?AIC=1648.81
## medv ~ crim + zn + indus + chas + nox + rm + age + dis + rad +
## ? ? tax + ptratio + black + lstat
##
## ? ? ? ? ? Df Sum of Sq ? RSS ? ?AIC
## - age ? ? ?1 ? ? ?0.06 11079 1642.6
## - indus ? ?1 ? ? ?2.52 11081 1642.7
## <none> ? ? ? ? ? ? ? ? 11079 1648.8
## - chas ? ? 1 ? ?218.97 11298 1652.5
## - tax ? ? ?1 ? ?242.26 11321 1653.5
## - crim ? ? 1 ? ?243.22 11322 1653.6
## - zn ? ? ? 1 ? ?257.49 11336 1654.2
## - black ? ?1 ? ?270.63 11349 1654.8
## - rad ? ? ?1 ? ?479.15 11558 1664.0
## - nox ? ? ?1 ? ?487.16 11566 1664.4
## - ptratio ?1 ? 1194.23 12273 1694.4
## - dis ? ? ?1 ? 1232.41 12311 1696.0
## - rm ? ? ? 1 ? 1871.32 12950 1721.6
## - lstat ? ?1 ? 2410.84 13490 1742.2
##
## Step: ?AIC=1642.59
## medv ~ crim + zn + indus + chas + nox + rm + dis + rad + tax +
## ? ? ptratio + black + lstat
##
## ? ? ? ? ? Df Sum of Sq ? RSS ? ?AIC
## - indus ? ?1 ? ? ?2.52 11081 1636.5
## <none> ? ? ? ? ? ? ? ? 11079 1642.6
## - chas ? ? 1 ? ?219.91 11299 1646.3
## - tax ? ? ?1 ? ?242.24 11321 1647.3
## - crim ? ? 1 ? ?243.20 11322 1647.3
## - zn ? ? ? 1 ? ?260.32 11339 1648.1
## - black ? ?1 ? ?272.26 11351 1648.7
## - rad ? ? ?1 ? ?481.09 11560 1657.9
## - nox ? ? ?1 ? ?520.87 11600 1659.6
## - ptratio ?1 ? 1200.23 12279 1688.4
## - dis ? ? ?1 ? 1352.26 12431 1694.6
## - rm ? ? ? 1 ? 1959.55 13038 1718.8
## - lstat ? ?1 ? 2718.88 13798 1747.4
##
## Step: ?AIC=1636.48
## medv ~ crim + zn + chas + nox + rm + dis + rad + tax + ptratio +
## ? ? black + lstat
##
## ? ? ? ? ? Df Sum of Sq ? RSS ? ?AIC
## <none> ? ? ? ? ? ? ? ? 11081 1636.5
## - chas ? ? 1 ? ?227.21 11309 1640.5
## - crim ? ? 1 ? ?245.37 11327 1641.3
## - zn ? ? ? 1 ? ?257.82 11339 1641.9
## - black ? ?1 ? ?270.82 11352 1642.5
## - tax ? ? ?1 ? ?273.62 11355 1642.6
## - rad ? ? ?1 ? ?500.92 11582 1652.6
## - nox ? ? ?1 ? ?541.91 11623 1654.4
## - ptratio ?1 ? 1206.45 12288 1682.5
## - dis ? ? ?1 ? 1448.94 12530 1692.4
## - rm ? ? ? 1 ? 1963.66 13045 1712.8
## - lstat ? ?1 ? 2723.48 13805 1741.5
# 模型比較
compare(modBIC, modAIC)
## Calls:
## 1: lm(formula = medv ~ crim + zn + chas + nox + rm + dis + rad + tax + ptratio + black + lstat, data = Boston)
## 2: lm(formula = medv ~ crim + zn + chas + nox + rm + dis + rad + tax + ptratio + black + lstat, data = Boston)
##
## ? ? ? ? ? ? ?Model 1 ?Model 2
## (Intercept) ? ?36.34 ? ?36.34
## SE ? ? ? ? ? ? ?5.07 ? ? 5.07
##
## crim ? ? ? ? -0.1084 ?-0.1084
## SE ? ? ? ? ? ?0.0328 ? 0.0328
##
## zn ? ? ? ? ? ?0.0458 ? 0.0458
## SE ? ? ? ? ? ?0.0135 ? 0.0135
##
## chas ? ? ? ? ? 2.719 ? ?2.719
## SE ? ? ? ? ? ? 0.854 ? ?0.854
##
## nox ? ? ? ? ? -17.38 ? -17.38
## SE ? ? ? ? ? ? ?3.54 ? ? 3.54
##
## rm ? ? ? ? ? ? 3.802 ? ?3.802
## SE ? ? ? ? ? ? 0.406 ? ?0.406
##
## dis ? ? ? ? ? -1.493 ? -1.493
## SE ? ? ? ? ? ? 0.186 ? ?0.186
##
## rad ? ? ? ? ? 0.2996 ? 0.2996
## SE ? ? ? ? ? ?0.0634 ? 0.0634
##
## tax ? ? ? ? -0.01178 -0.01178
## SE ? ? ? ? ? 0.00337 ?0.00337
##
## ptratio ? ? ? -0.947 ? -0.947
## SE ? ? ? ? ? ? 0.129 ? ?0.129
##
## black ? ? ? ?0.00929 ?0.00929
## SE ? ? ? ? ? 0.00267 ?0.00267
##
## lstat ? ? ? ?-0.5226 ?-0.5226
## SE ? ? ? ? ? ?0.0474 ? 0.0474
##
summary(modBIC)
##
## Call:
## lm(formula = medv ~ crim + zn + chas + nox + rm + dis + rad +
## ? ? tax + ptratio + black + lstat, data = Boston)
##
## Residuals:
## ? ? ?Min ? ? ? 1Q ? Median ? ? ? 3Q ? ? ?Max
## -15.5984 ?-2.7386 ?-0.5046 ? 1.7273 ?26.2373
##
## Coefficients:
## ? ? ? ? ? ? ? Estimate Std. Error t value Pr(>|t|)
## (Intercept) ?36.341145 ? 5.067492 ? 7.171 2.73e-12 ***
## crim ? ? ? ? -0.108413 ? 0.032779 ?-3.307 0.001010 **
## zn ? ? ? ? ? ?0.045845 ? 0.013523 ? 3.390 0.000754 ***
## chas ? ? ? ? ?2.718716 ? 0.854240 ? 3.183 0.001551 **
## nox ? ? ? ? -17.376023 ? 3.535243 ?-4.915 1.21e-06 ***
## rm ? ? ? ? ? ?3.801579 ? 0.406316 ? 9.356 ?< 2e-16 ***
## dis ? ? ? ? ?-1.492711 ? 0.185731 ?-8.037 6.84e-15 ***
## rad ? ? ? ? ? 0.299608 ? 0.063402 ? 4.726 3.00e-06 ***
## tax ? ? ? ? ?-0.011778 ? 0.003372 ?-3.493 0.000521 ***
## ptratio ? ? ?-0.946525 ? 0.129066 ?-7.334 9.24e-13 ***
## black ? ? ? ? 0.009291 ? 0.002674 ? 3.475 0.000557 ***
## lstat ? ? ? ?-0.522553 ? 0.047424 -11.019 ?< 2e-16 ***
## ---
## Signif. codes: ?0 '***' 0.001 '**' 0.01 '*' 0.05 '.' 0.1 ' ' 1
##
## Residual standard error: 4.736 on 494 degrees of freedom
## Multiple R-squared: ?0.7406, Adjusted R-squared: ?0.7348
## F-statistic: 128.2 on 11 and 494 DF, ?p-value: < 2.2e-16
# 置信區(qū)間
conf(modBIC)
## ? ? ? ? ? ? ? ? ? ? 2.5 % ? ? ? 97.5 %
## (Intercept) ?26.384649126 ?46.29764088
## crim ? ? ? ? -0.172817670 ?-0.04400902
## zn ? ? ? ? ? ?0.019275889 ? 0.07241397
## chas ? ? ? ? ?1.040324913 ? 4.39710769
## nox ? ? ? ? -24.321990312 -10.43005655
## rm ? ? ? ? ? ?3.003258393 ? 4.59989929
## dis ? ? ? ? ?-1.857631161 ?-1.12779176
## rad ? ? ? ? ? 0.175037411 ? 0.42417950
## tax ? ? ? ? ?-0.018403857 ?-0.00515209
## ptratio ? ? ?-1.200109823 ?-0.69293932
## black ? ? ? ? 0.004037216 ? 0.01454447
## lstat ? ? ? ?-0.615731781 ?-0.42937513
請注意,相對于完整模型,
略有增加,以及所有預測變量顯著。
我們已經(jīng)量化了預測變量對房價(Q1)的影響,可以得出結(jié)論,在最終模型(Q2)中,顯著性水平為?
:
chas
,??age
,??rad
,?black
?對medv
有?顯著正面?的影響?;nox
,??dis
,??tax
,??ptratio
,?lstat
?對medv
有?顯著負面?的影響。
檢查:
modBIC
?不能通過消除預測指標來改善BIC。modBIC
?無法通過添加預測變量來改進BIC。使用?addterm(modBIC, scope = lm(medv ~ ., data = Boston), k = log(nobs(modBIC)))
?。?
應用其公式,我們將獲得
,因此
將不會定義。
具有相同的因變量。
如果是
,則
。
同樣,由于BIC?在選擇真實的分布/回歸模型時是?一致的:如果提供了足夠的數(shù)據(jù)
,則可以保證BIC在候選列表中選擇真實的數(shù)據(jù)生成模型。如果真實模型包含在該列表中,則模型為線性模型。但是,由于實際模型可能是非線性的,因此在實踐中這可能是不現(xiàn)實的。
最受歡迎的見解
1.R語言多元Logistic邏輯回歸 應用案例
2.面板平滑轉(zhuǎn)移回歸(PSTR)分析案例實現(xiàn)
3.matlab中的偏最小二乘回歸(PLSR)和主成分回歸(PCR)
4.R語言泊松Poisson回歸模型分析案例
5.R語言回歸中的Hosmer-Lemeshow擬合優(yōu)度檢驗
6.r語言中對LASSO回歸,Ridge嶺回歸和Elastic Net模型實現(xiàn)
7.在R語言中實現(xiàn)Logistic邏輯回歸
8.python用線性回歸預測股票價格
9.R語言如何在生存分析與Cox回歸中計算IDI,NRI指標