国产精品天干天干,亚洲毛片在线,日韩gay小鲜肉啪啪18禁,女同Gay自慰喷水

歡迎光臨散文網(wǎng) 會員登陸 & 注冊

拓端tecdat|R語言多元逐步回歸模型分析房價和葡萄酒價格:選擇最合適的預測變量

2021-07-08 21:05 作者:拓端tecdat  | 我要投稿

原文鏈接: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ū)別。


  1. # 數(shù)據(jù):n個觀測值,p = n-1個預測變量


  2. n <- 5

  3. p <- n - 1

  4. df <- data.frame(y = rnorm(n), x = matrix(rnorm(n * p), nrow = n, ncol = p))


  5. # 情況p = n-1 = 2:可以估計beta,但不能估計sigma ^ 2(因此,不能執(zhí)行推斷,因為它需要估計的sigma ^ 2)



  6. summary(lm(y ~ ., data = df))


  7. # 情況p = n-2 = 1:可以估計beta和sigma ^ 2(因此可以進行推斷)


  8. 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)學生與老師的比例

他們將作為模型輸入。

  1. # 具有不同預測變量的兩個模型


  2. mod1 <- lm(medv ~ age + crim, data = Boston)

  3. mod2 <- lm(medv ~ age + crim + lstat, data = Boston)

  4. # BICs

  5. BIC(mod1)

  6. ## [1] 3581.893

  7. BIC(mod2) # 較小->較好

  8. ## [1] 3300.841

  9. # AICs

  10. AIC(mod1)

  11. ## [1] 3564.987

  12. AIC(mod2) # 較小->較好

  13. ## [1] 3279.708

  14. # 檢查摘要


  15. ##

  16. ## Residuals:

  17. ## ? ? Min ? ? ?1Q ?Median ? ? ?3Q ? ? Max

  18. ## -13.940 ?-4.991 ?-2.420 ? 2.110 ?32.033

  19. ##

  20. ## Coefficients:

  21. ## ? ? ? ? ? ? Estimate Std. Error t value Pr(>|t|)

  22. ## (Intercept) 29.80067 ? ?0.97078 ?30.698 ?< 2e-16 ***

  23. ## age ? ? ? ? -0.08955 ? ?0.01378 ?-6.499 1.95e-10 ***

  24. ## crim ? ? ? ?-0.31182 ? ?0.04510 ?-6.914 1.43e-11 ***

  25. ## ---

  26. ## Signif. codes: ?0 '***' 0.001 '**' 0.01 '*' 0.05 '.' 0.1 ' ' 1

  27. ##

  28. ## Residual standard error: 8.157 on 503 degrees of freedom

  29. ## Multiple R-squared: ?0.2166, Adjusted R-squared: ?0.2134

  30. ## F-statistic: 69.52 on 2 and 503 DF, ?p-value: < 2.2e-16

  31. summary(mod2)

  32. ##

  33. ## Call:

  34. ## lm(formula = medv ~ age + crim + lstat, data = Boston)

  35. ##

  36. ## Residuals:

  37. ## ? ? Min ? ? ?1Q ?Median ? ? ?3Q ? ? Max

  38. ## -16.133 ?-3.848 ?-1.380 ? 1.970 ?23.644

  39. ##

  40. ## Coefficients:

  41. ## ? ? ? ? ? ? Estimate Std. Error t value Pr(>|t|)

  42. ## (Intercept) 32.82804 ? ?0.74774 ?43.903 ?< 2e-16 ***

  43. ## age ? ? ? ? ?0.03765 ? ?0.01225 ? 3.074 ?0.00223 **

  44. ## crim ? ? ? ?-0.08262 ? ?0.03594 ?-2.299 ?0.02193 *

  45. ## lstat ? ? ? -0.99409 ? ?0.05075 -19.587 ?< 2e-16 ***

  46. ## ---

  47. ## Signif. codes: ?0 '***' 0.001 '**' 0.01 '*' 0.05 '.' 0.1 ' ' 1

  48. ##

  49. ## Residual standard error: 6.147 on 502 degrees of freedom

  50. ## Multiple R-squared: ?0.5559, Adjusted R-squared: ?0.5533

  51. ## 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準則。

  1. # 完整模型


  2. # 用 BIC

  3. ## Start: ?AIC=-53.29

  4. ## Price ~ Year + WinterRain + AGST + HarvestRain + Age + FrancePop

  5. ##

  6. ##

  7. ## Step: ?AIC=-53.29

  8. ## Price ~ Year + WinterRain + AGST + HarvestRain + FrancePop

  9. ##

  10. ## ? ? ? ? ? ? ? Df Sum of Sq ? ?RSS ? ? AIC

  11. ## - FrancePop ? ?1 ? ?0.0026 1.8058 -56.551

  12. ## - Year ? ? ? ? 1 ? ?0.0048 1.8080 -56.519

  13. ## <none> ? ? ? ? ? ? ? ? ? ? 1.8032 -53.295

  14. ## - WinterRain ? 1 ? ?0.4585 2.2617 -50.473

  15. ## - HarvestRain ?1 ? ?1.8063 3.6095 -37.852

  16. ## - AGST ? ? ? ? 1 ? ?3.3756 5.1788 -28.105

  17. ##

  18. ## Step: ?AIC=-56.55

  19. ## Price ~ Year + WinterRain + AGST + HarvestRain

  20. ##

  21. ## ? ? ? ? ? ? ? Df Sum of Sq ? ?RSS ? ? AIC

  22. ## <none> ? ? ? ? ? ? ? ? ? ? 1.8058 -56.551

  23. ## - WinterRain ? 1 ? ?0.4809 2.2867 -53.473

  24. ## - Year ? ? ? ? 1 ? ?0.9089 2.7147 -48.840

  25. ## - HarvestRain ?1 ? ?1.8760 3.6818 -40.612

  26. ## - AGST ? ? ? ? 1 ? ?3.4428 5.2486 -31.039


  27. summary(modBIC)

  28. ##

  29. ## Call:

  30. ## lm(formula = Price ~ Year + WinterRain + AGST + HarvestRain,

  31. ## ? ? data = wine)

  32. ##

  33. ## Residuals:

  34. ## ? ? ?Min ? ? ? 1Q ? Median ? ? ? 3Q ? ? ?Max

  35. ## -0.46024 -0.23862 ?0.01347 ?0.18601 ?0.53443

  36. ##

  37. ## Coefficients:

  38. ## ? ? ? ? ? ? ? Estimate Std. Error t value Pr(>|t|)

  39. ## (Intercept) 43.6390418 14.6939240 ? 2.970 ?0.00707 **

  40. ## Year ? ? ? ?-0.0238480 ?0.0071667 ?-3.328 ?0.00305 **

  41. ## WinterRain ? 0.0011667 ?0.0004820 ? 2.420 ?0.02421 *

  42. ## AGST ? ? ? ? 0.6163916 ?0.0951747 ? 6.476 1.63e-06 ***

  43. ## HarvestRain -0.0038606 ?0.0008075 ?-4.781 8.97e-05 ***

  44. ## ---

  45. ## Signif. codes: ?0 '***' 0.001 '**' 0.01 '*' 0.05 '.' 0.1 ' ' 1

  46. ##

  47. ## Residual standard error: 0.2865 on 22 degrees of freedom

  48. ## Multiple R-squared: ?0.8275, Adjusted R-squared: ?0.7962

  49. ## F-statistic: 26.39 on 4 and 22 DF, ?p-value: 4.057e-08

  50. # 用 AIC

  51. ## Start: ?AIC=-61.07

  52. ## Price ~ Year + WinterRain + AGST + HarvestRain + Age + FrancePop

  53. ##

  54. ##

  55. ## Step: ?AIC=-61.07

  56. ## Price ~ Year + WinterRain + AGST + HarvestRain + FrancePop

  57. ##

  58. ## ? ? ? ? ? ? ? Df Sum of Sq ? ?RSS ? ? AIC

  59. ## - FrancePop ? ?1 ? ?0.0026 1.8058 -63.031

  60. ## - Year ? ? ? ? 1 ? ?0.0048 1.8080 -62.998

  61. ## <none> ? ? ? ? ? ? ? ? ? ? 1.8032 -61.070

  62. ## - WinterRain ? 1 ? ?0.4585 2.2617 -56.952

  63. ## - HarvestRain ?1 ? ?1.8063 3.6095 -44.331

  64. ## - AGST ? ? ? ? 1 ? ?3.3756 5.1788 -34.584

  65. ##

  66. ## Step: ?AIC=-63.03

  67. ## Price ~ Year + WinterRain + AGST + HarvestRain

  68. ##

  69. ## ? ? ? ? ? ? ? Df Sum of Sq ? ?RSS ? ? AIC

  70. ## <none> ? ? ? ? ? ? ? ? ? ? 1.8058 -63.031

  71. ## - WinterRain ? 1 ? ?0.4809 2.2867 -58.656

  72. ## - Year ? ? ? ? 1 ? ?0.9089 2.7147 -54.023

  73. ## - HarvestRain ?1 ? ?1.8760 3.6818 -45.796

  74. ## - AGST ? ? ? ? 1 ? ?3.4428 5.2486 -36.222

  75. summary(modAIC)

  76. ##

  77. ## Call:

  78. ## lm(formula = Price ~ Year + WinterRain + AGST + HarvestRain,

  79. ## ? ? data = wine)

  80. ##

  81. ## Residuals:

  82. ## ? ? ?Min ? ? ? 1Q ? Median ? ? ? 3Q ? ? ?Max

  83. ## -0.46024 -0.23862 ?0.01347 ?0.18601 ?0.53443

  84. ##

  85. ## Coefficients:

  86. ## ? ? ? ? ? ? ? Estimate Std. Error t value Pr(>|t|)

  87. ## (Intercept) 43.6390418 14.6939240 ? 2.970 ?0.00707 **

  88. ## Year ? ? ? ?-0.0238480 ?0.0071667 ?-3.328 ?0.00305 **

  89. ## WinterRain ? 0.0011667 ?0.0004820 ? 2.420 ?0.02421 *

  90. ## AGST ? ? ? ? 0.6163916 ?0.0951747 ? 6.476 1.63e-06 ***

  91. ## HarvestRain -0.0038606 ?0.0008075 ?-4.781 8.97e-05 ***

  92. ## ---

  93. ## Signif. codes: ?0 '***' 0.001 '**' 0.01 '*' 0.05 '.' 0.1 ' ' 1

  94. ##

  95. ## Residual standard error: 0.2865 on 22 degrees of freedom

  96. ## Multiple R-squared: ?0.8275, Adjusted R-squared: ?0.7962

  97. ## 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。

  1. # 相同的BIC,標準不同



  2. AIC(modBIC, k = log(n))

  3. ## [1] -56.55135

  4. BIC(modBIC)

  5. ## [1] 23.36717

  6. # 觀察到MASS :: stepAIC(mod,k = log(nrow(wine)))返回的最終BIC是由extractAIC()而不是BIC()給出的!但是兩者是等效的

  7. # 相同的AIC,標準不同



  8. AIC(modBIC, k = 2)

  9. ## [1] -63.03053

  10. BIC(modBIC)

  11. ## [1] 23.36717



  12. BIC(modBIC) - AIC(modBIC

  13. ## [1] 79.91852

  14. n * (log(2 * pi+ 1) + log(n)

  15. ## [1] 79.91852

  16. #與AIC相同



  17. AIC(modAIC) - AIC(modAIC

  18. ## [1] 78.62268

  19. n * (log(2 * pi + 1 + 2

  20. ## [1] 78.62268

請注意,所選模型?modBIC?和?modAIC?等效于?modWine2?,我們選擇的最佳模型。這說明,選擇的模型?stepAIC?通常是進一步添加或刪除預測變量的良好起點。

使用?stepAIC?BIC / AIC時,可能會選擇不同的最終模型?direction。這是解釋:

  • “backward”:??從給定模型中順序刪除預測變量。

  • “forward”:?將?預測變量順序添加到給定模型中。

  • “both”?(默認):以上的組合。

該?建議?是嘗試幾種這些方法并保留一個最小的BIC / AIC。設置?trace = 0?為省略冗長的搜索過程信息輸出。下面的代碼塊清楚地說明了如何使用?數(shù)據(jù)集的修改版本?來利用?direction?參數(shù)和的其他選項?。stepAICwinedirection = "forward"direction = "both"scope

  1. # 將無關的預測變量添加到葡萄酒數(shù)據(jù)集中

  2. # 向后選擇:從給定模型中順序刪除預測變量


  3. # 從具有所有預測變量的模型開始




  4. modAll, direction = "backward", k = log(n)

  5. ## Start: ?AIC=-50.13

  6. ## Price ~ Year + WinterRain + AGST + HarvestRain + Age + FrancePop +

  7. ## ? ? noisePredictor

  8. ##

  9. ##

  10. ## Step: ?AIC=-50.13

  11. ## Price ~ Year + WinterRain + AGST + HarvestRain + FrancePop +

  12. ## ? ? noisePredictor

  13. ##

  14. ## ? ? ? ? ? ? ? ? ?Df Sum of Sq ? ?RSS ? ? AIC

  15. ## - FrancePop ? ? ? 1 ? ?0.0036 1.7977 -53.376

  16. ## - Year ? ? ? ? ? ?1 ? ?0.0038 1.7979 -53.374

  17. ## - noisePredictor ?1 ? ?0.0090 1.8032 -53.295

  18. ## <none> ? ? ? ? ? ? ? ? ? ? ? ?1.7941 -50.135

  19. ## - WinterRain ? ? ?1 ? ?0.4598 2.2539 -47.271

  20. ## - HarvestRain ? ? 1 ? ?1.7666 3.5607 -34.923

  21. ## - AGST ? ? ? ? ? ?1 ? ?3.3658 5.1599 -24.908

  22. ##

  23. ## Step: ?AIC=-53.38

  24. ## Price ~ Year + WinterRain + AGST + HarvestRain + noisePredictor

  25. ##

  26. ## ? ? ? ? ? ? ? ? ?Df Sum of Sq ? ?RSS ? ? AIC

  27. ## - noisePredictor ?1 ? ?0.0081 1.8058 -56.551

  28. ## <none> ? ? ? ? ? ? ? ? ? ? ? ?1.7977 -53.376

  29. ## - WinterRain ? ? ?1 ? ?0.4771 2.2748 -50.317

  30. ## - Year ? ? ? ? ? ?1 ? ?0.9162 2.7139 -45.552

  31. ## - HarvestRain ? ? 1 ? ?1.8449 3.6426 -37.606

  32. ## - AGST ? ? ? ? ? ?1 ? ?3.4234 5.2212 -27.885

  33. ##

  34. ## Step: ?AIC=-56.55

  35. ## Price ~ Year + WinterRain + AGST + HarvestRain

  36. ##

  37. ## ? ? ? ? ? ? ? Df Sum of Sq ? ?RSS ? ? AIC

  38. ## <none> ? ? ? ? ? ? ? ? ? ? 1.8058 -56.551

  39. ## - WinterRain ? 1 ? ?0.4809 2.2867 -53.473

  40. ## - Year ? ? ? ? 1 ? ?0.9089 2.7147 -48.840

  41. ## - HarvestRain ?1 ? ?1.8760 3.6818 -40.612

  42. ## - AGST ? ? ? ? 1 ? ?3.4428 5.2486 -31.039

  43. ##

  44. ## Call:

  45. ## lm(formula = Price ~ Year + WinterRain + AGST + HarvestRain,

  46. ## ? ? data = wineNoise)

  47. ##

  48. ## Coefficients:

  49. ## (Intercept) ? ? ? ? Year ? WinterRain ? ? ? ? AGST ?HarvestRain

  50. ## ? 43.639042 ? ?-0.023848 ? ? 0.001167 ? ? 0.616392 ? ?-0.003861

  51. # 從中間模型開始



  52. AIC(modInter, direction = "backward", k = log(n)

  53. ## Start: ?AIC=-32.38

  54. ## Price ~ noisePredictor + Year + AGST

  55. ##

  56. ## ? ? ? ? ? ? ? ? ?Df Sum of Sq ? ?RSS ? ? AIC

  57. ## - noisePredictor ?1 ? ?0.0146 5.0082 -35.601

  58. ## <none> ? ? ? ? ? ? ? ? ? ? ? ?4.9936 -32.384

  59. ## - Year ? ? ? ? ? ?1 ? ?0.7522 5.7459 -31.891

  60. ## - AGST ? ? ? ? ? ?1 ? ?3.2211 8.2147 -22.240

  61. ##

  62. ## Step: ?AIC=-35.6

  63. ## Price ~ Year + AGST

  64. ##

  65. ## ? ? ? ?Df Sum of Sq ? ?RSS ? ? AIC

  66. ## <none> ? ? ? ? ? ? ?5.0082 -35.601

  67. ## - Year ?1 ? ?0.7966 5.8049 -34.911

  68. ## - AGST ?1 ? ?3.2426 8.2509 -25.417

  69. ##

  70. ## Call:

  71. ## lm(formula = Price ~ Year + AGST, data = wineNoise)

  72. ##

  73. ## Coefficients:

  74. ## (Intercept) ? ? ? ? Year ? ? ? ? AGST

  75. ## ? ?41.49441 ? ? -0.02221 ? ? ?0.56067

  76. # 回想一下,在搜索過程中未探索未包含在modInter中的其他預測變量(因此未添加相關的預測變量HarvestRain)



  77. # 正向選擇:從給定模型順序添加預測變量


  78. # 從沒有預測變量的模型開始,僅截距模型(表示為?1)



  79. AIC(modZero, direction = "forward"

  80. ## Start: ?AIC=-22.28

  81. ## Price ~ 1

  82. ##

  83. ## ? ? ? ? ? ? ? ? ?Df Sum of Sq ? ? RSS ? ? AIC

  84. ## + AGST ? ? ? ? ? ?1 ? ?4.6655 ?5.8049 -34.911

  85. ## + HarvestRain ? ? 1 ? ?2.6933 ?7.7770 -27.014

  86. ## + FrancePop ? ? ? 1 ? ?2.4231 ?8.0472 -26.092

  87. ## + Year ? ? ? ? ? ?1 ? ?2.2195 ?8.2509 -25.417

  88. ## + Age ? ? ? ? ? ? 1 ? ?2.2195 ?8.2509 -25.417

  89. ## <none> ? ? ? ? ? ? ? ? ? ? ? ?10.4703 -22.281

  90. ## + WinterRain ? ? ?1 ? ?0.1905 10.2798 -19.481

  91. ## + noisePredictor ?1 ? ?0.1761 10.2942 -19.443

  92. ##

  93. ## Step: ?AIC=-34.91

  94. ## Price ~ AGST

  95. ##

  96. ## ? ? ? ? ? ? ? ? ?Df Sum of Sq ? ?RSS ? ? AIC

  97. ## + HarvestRain ? ? 1 ? 2.50659 3.2983 -46.878

  98. ## + WinterRain ? ? ?1 ? 1.42392 4.3809 -39.214

  99. ## + FrancePop ? ? ? 1 ? 0.90263 4.9022 -36.178

  100. ## + Year ? ? ? ? ? ?1 ? 0.79662 5.0082 -35.601

  101. ## + Age ? ? ? ? ? ? 1 ? 0.79662 5.0082 -35.601

  102. ## <none> ? ? ? ? ? ? ? ? ? ? ? ?5.8049 -34.911

  103. ## + noisePredictor ?1 ? 0.05900 5.7459 -31.891

  104. ##

  105. ## Step: ?AIC=-46.88

  106. ## Price ~ AGST + HarvestRain

  107. ##

  108. ## ? ? ? ? ? ? ? ? ?Df Sum of Sq ? ?RSS ? ? AIC

  109. ## + FrancePop ? ? ? 1 ? 1.03572 2.2625 -53.759

  110. ## + Year ? ? ? ? ? ?1 ? 1.01159 2.2867 -53.473

  111. ## + Age ? ? ? ? ? ? 1 ? 1.01159 2.2867 -53.473

  112. ## + WinterRain ? ? ?1 ? 0.58356 2.7147 -48.840

  113. ## <none> ? ? ? ? ? ? ? ? ? ? ? ?3.2983 -46.878

  114. ## + noisePredictor ?1 ? 0.06084 3.2374 -44.085

  115. ##

  116. ## Step: ?AIC=-53.76

  117. ## Price ~ AGST + HarvestRain + FrancePop

  118. ##

  119. ## ? ? ? ? ? ? ? ? ?Df Sum of Sq ? ?RSS ? ? AIC

  120. ## + WinterRain ? ? ?1 ? 0.45456 1.8080 -56.519

  121. ## <none> ? ? ? ? ? ? ? ? ? ? ? ?2.2625 -53.759

  122. ## + noisePredictor ?1 ? 0.00829 2.2542 -50.562

  123. ## + Age ? ? ? ? ? ? 1 ? 0.00085 2.2617 -50.473

  124. ## + Year ? ? ? ? ? ?1 ? 0.00085 2.2617 -50.473

  125. ##

  126. ## Step: ?AIC=-56.52

  127. ## Price ~ AGST + HarvestRain + FrancePop + WinterRain

  128. ##

  129. ## ? ? ? ? ? ? ? ? ?Df Sum of Sq ? ?RSS ? ? AIC

  130. ## <none> ? ? ? ? ? ? ? ? ? ? ? ?1.8080 -56.519

  131. ## + noisePredictor ?1 0.0100635 1.7979 -53.374

  132. ## + Year ? ? ? ? ? ?1 0.0048039 1.8032 -53.295

  133. ## + Age ? ? ? ? ? ? 1 0.0048039 1.8032 -53.295

  134. ##

  135. ## Call:

  136. ## lm(formula = Price ~ AGST + HarvestRain + FrancePop + WinterRain,

  137. ## ? ? data = wineNoise)

  138. ##

  139. ## Coefficients:

  140. ## (Intercept) ? ? ? ? AGST ?HarvestRain ? ?FrancePop ? WinterRain

  141. ## ?-5.945e-01 ? ?6.127e-01 ? -3.804e-03 ? -5.189e-05 ? ?1.136e-03

  142. # 在進行正向搜索時,充分設置范圍參數(shù)非常重要!在范圍中,您必須定義包含可探索模型集的“最小”(下部)和“最大”(上部)模型。如果未提供,則將最大模型用作傳遞的起始模型(在這種情況下為modZero),而stepAIC將不執(zhí)行任何搜索



  143. #從中間模型開始



  144. ## Start: ?AIC=-32.38

  145. ## Price ~ noisePredictor + Year + AGST

  146. ##

  147. ## ? ? ? ? ? ? ? Df Sum of Sq ? ?RSS ? ? AIC

  148. ## + HarvestRain ?1 ? 2.71878 2.2748 -50.317

  149. ## + WinterRain ? 1 ? 1.35102 3.6426 -37.606

  150. ## <none> ? ? ? ? ? ? ? ? ? ? 4.9936 -32.384

  151. ## + FrancePop ? ?1 ? 0.24004 4.7536 -30.418

  152. ##

  153. ## Step: ?AIC=-50.32

  154. ## Price ~ noisePredictor + Year + AGST + HarvestRain

  155. ##

  156. ## ? ? ? ? ? ? ?Df Sum of Sq ? ?RSS ? ? AIC

  157. ## + WinterRain ?1 ? 0.47710 1.7977 -53.376

  158. ## <none> ? ? ? ? ? ? ? ? ? ?2.2748 -50.317

  159. ## + FrancePop ? 1 ? 0.02094 2.2539 -47.271

  160. ##

  161. ## Step: ?AIC=-53.38

  162. ## Price ~ noisePredictor + Year + AGST + HarvestRain + WinterRain

  163. ##

  164. ## ? ? ? ? ? ? Df Sum of Sq ? ?RSS ? ? AIC

  165. ## <none> ? ? ? ? ? ? ? ? ? 1.7977 -53.376

  166. ## + FrancePop ?1 0.0036037 1.7941 -50.135

  167. ##

  168. ## Call:

  169. ## lm(formula = Price ~ noisePredictor + Year + AGST + HarvestRain +

  170. ## ? ? WinterRain, data = wineNoise)

  171. ##

  172. ## Coefficients:

  173. ## ? ?(Intercept) ?noisePredictor ? ? ? ? ? ?Year ? ? ? ? ? ?AGST ? ? HarvestRain ? ? ?WinterRain

  174. ## ? ? ?44.096639 ? ? ? -0.019617 ? ? ? -0.024126 ? ? ? ?0.620522 ? ? ? -0.003840 ? ? ? ??;叵胍幌?,在搜索期間不會刪除modInter中包含的預測變量(因此會保留無關的noisePredictor)


  175. #兩種選擇:如果從中間模型開始,則很有用


  176. #消除了與從中間模型完成的“向后”和“向前”搜索相關的問題


  177. ## Start: ?AIC=-32.38

  178. ## Price ~ noisePredictor + Year + AGST

  179. ##

  180. ## ? ? ? ? ? ? ? ? ?Df Sum of Sq ? ?RSS ? ? AIC

  181. ## + HarvestRain ? ? 1 ? ?2.7188 2.2748 -50.317

  182. ## + WinterRain ? ? ?1 ? ?1.3510 3.6426 -37.606

  183. ## - noisePredictor ?1 ? ?0.0146 5.0082 -35.601

  184. ## <none> ? ? ? ? ? ? ? ? ? ? ? ?4.9936 -32.384

  185. ## - Year ? ? ? ? ? ?1 ? ?0.7522 5.7459 -31.891

  186. ## + FrancePop ? ? ? 1 ? ?0.2400 4.7536 -30.418

  187. ## - AGST ? ? ? ? ? ?1 ? ?3.2211 8.2147 -22.240

  188. ##

  189. ## Step: ?AIC=-50.32

  190. ## Price ~ noisePredictor + Year + AGST + HarvestRain

  191. ##

  192. ## ? ? ? ? ? ? ? ? ?Df Sum of Sq ? ?RSS ? ? AIC

  193. ## - noisePredictor ?1 ? 0.01182 2.2867 -53.473

  194. ## + WinterRain ? ? ?1 ? 0.47710 1.7977 -53.376

  195. ## <none> ? ? ? ? ? ? ? ? ? ? ? ?2.2748 -50.317

  196. ## + FrancePop ? ? ? 1 ? 0.02094 2.2539 -47.271

  197. ## - Year ? ? ? ? ? ?1 ? 0.96258 3.2374 -44.085

  198. ## - HarvestRain ? ? 1 ? 2.71878 4.9936 -32.384

  199. ## - AGST ? ? ? ? ? ?1 ? 2.94647 5.2213 -31.180

  200. ##

  201. ## Step: ?AIC=-53.47

  202. ## Price ~ Year + AGST + HarvestRain

  203. ##

  204. ## ? ? ? ? ? ? ? ? ?Df Sum of Sq ? ?RSS ? ? AIC

  205. ## + WinterRain ? ? ?1 ? 0.48087 1.8058 -56.551

  206. ## <none> ? ? ? ? ? ? ? ? ? ? ? ?2.2867 -53.473

  207. ## + FrancePop ? ? ? 1 ? 0.02497 2.2617 -50.473

  208. ## + noisePredictor ?1 ? 0.01182 2.2748 -50.317

  209. ## - Year ? ? ? ? ? ?1 ? 1.01159 3.2983 -46.878

  210. ## - HarvestRain ? ? 1 ? 2.72157 5.0082 -35.601

  211. ## - AGST ? ? ? ? ? ?1 ? 2.96500 5.2517 -34.319

  212. ##

  213. ## Step: ?AIC=-56.55

  214. ## Price ~ Year + AGST + HarvestRain + WinterRain

  215. ##

  216. ## ? ? ? ? ? ? ? ? ?Df Sum of Sq ? ?RSS ? ? AIC

  217. ## <none> ? ? ? ? ? ? ? ? ? ? ? ?1.8058 -56.551

  218. ## - WinterRain ? ? ?1 ? ?0.4809 2.2867 -53.473

  219. ## + noisePredictor ?1 ? ?0.0081 1.7977 -53.376

  220. ## + FrancePop ? ? ? 1 ? ?0.0026 1.8032 -53.295

  221. ## - Year ? ? ? ? ? ?1 ? ?0.9089 2.7147 -48.840

  222. ## - HarvestRain ? ? 1 ? ?1.8760 3.6818 -40.612

  223. ## - AGST ? ? ? ? ? ?1 ? ?3.4428 5.2486 -31.039

  224. ##

  225. ## Call:

  226. ## lm(formula = Price ~ Year + AGST + HarvestRain + WinterRain,

  227. ## ? ? data = wineNoise)

  228. ##

  229. ## Coefficients:

  230. ## (Intercept) ? ? ? ? Year ? ? ? ? AGST ?HarvestRain ? WinterRain

  231. ## ? 43.639042 ? ?-0.023848 ? ? 0.616392 ? ?-0.003861 ? ? 0.001167

  232. # 正確定義范圍也很重要,因為“兩個”都求助于“前進”(以及“后退”)


  233. #使用完整模型中的默認值實質(zhì)上會進行向后選擇,但允許已刪除的預測變量在以后的步驟中再次輸入



  234. AIC(modAll direction = "both", k = log(n)

  235. ## Start: ?AIC=-50.13

  236. ## Price ~ Year + WinterRain + AGST + HarvestRain + Age + FrancePop +

  237. ## ? ? noisePredictor

  238. ##

  239. ##

  240. ## Step: ?AIC=-50.13

  241. ## Price ~ Year + WinterRain + AGST + HarvestRain + FrancePop +

  242. ## ? ? noisePredictor

  243. ##

  244. ## ? ? ? ? ? ? ? ? ?Df Sum of Sq ? ?RSS ? ? AIC

  245. ## - FrancePop ? ? ? 1 ? ?0.0036 1.7977 -53.376

  246. ## - Year ? ? ? ? ? ?1 ? ?0.0038 1.7979 -53.374

  247. ## - noisePredictor ?1 ? ?0.0090 1.8032 -53.295

  248. ## <none> ? ? ? ? ? ? ? ? ? ? ? ?1.7941 -50.135

  249. ## - WinterRain ? ? ?1 ? ?0.4598 2.2539 -47.271

  250. ## - HarvestRain ? ? 1 ? ?1.7666 3.5607 -34.923

  251. ## - AGST ? ? ? ? ? ?1 ? ?3.3658 5.1599 -24.908

  252. ##

  253. ## Step: ?AIC=-53.38

  254. ## Price ~ Year + WinterRain + AGST + HarvestRain + noisePredictor

  255. ##

  256. ## ? ? ? ? ? ? ? ? ?Df Sum of Sq ? ?RSS ? ? AIC

  257. ## - noisePredictor ?1 ? ?0.0081 1.8058 -56.551

  258. ## <none> ? ? ? ? ? ? ? ? ? ? ? ?1.7977 -53.376

  259. ## - WinterRain ? ? ?1 ? ?0.4771 2.2748 -50.317

  260. ## + FrancePop ? ? ? 1 ? ?0.0036 1.7941 -50.135

  261. ## - Year ? ? ? ? ? ?1 ? ?0.9162 2.7139 -45.552

  262. ## - HarvestRain ? ? 1 ? ?1.8449 3.6426 -37.606

  263. ## - AGST ? ? ? ? ? ?1 ? ?3.4234 5.2212 -27.885

  264. ##

  265. ## Step: ?AIC=-56.55

  266. ## Price ~ Year + WinterRain + AGST + HarvestRain

  267. ##

  268. ## ? ? ? ? ? ? ? ? ?Df Sum of Sq ? ?RSS ? ? AIC

  269. ## <none> ? ? ? ? ? ? ? ? ? ? ? ?1.8058 -56.551

  270. ## - WinterRain ? ? ?1 ? ?0.4809 2.2867 -53.473

  271. ## + noisePredictor ?1 ? ?0.0081 1.7977 -53.376

  272. ## + FrancePop ? ? ? 1 ? ?0.0026 1.8032 -53.295

  273. ## - Year ? ? ? ? ? ?1 ? ?0.9089 2.7147 -48.840

  274. ## - HarvestRain ? ? 1 ? ?1.8760 3.6818 -40.612

  275. ## - AGST ? ? ? ? ? ?1 ? ?3.4428 5.2486 -31.039

  276. ##

  277. ## Call:

  278. ## lm(formula = Price ~ Year + WinterRain + AGST + HarvestRain,

  279. ## ? ? data = wineNoise)

  280. ##

  281. ## Coefficients:

  282. ## (Intercept) ? ? ? ? Year ? WinterRain ? ? ? ? AGST ?HarvestRain

  283. ## ? 43.639042 ? ?-0.023848 ? ? 0.001167 ? ? 0.616392 ? ?-0.003861

  284. # 省略冗長的輸出



  285. AIC(modAll, direction = "both", trace = 0

  286. ##

  287. ## Call:

  288. ## lm(formula = Price ~ Year + WinterRain + AGST + HarvestRain,

  289. ## ? ? data = wineNoise)

  290. ##

  291. ## Coefficients:

  292. ## (Intercept) ? ? ? ? Year ? WinterRain ? ? ? ? AGST ?HarvestRain

  293. ## ? 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?的信息。因此,我們可以對所有?預測變量進行線性模型處理?:


  1. summary(modHouse)

  2. ##

  3. ##

  4. ## Residuals:

  5. ## ? ? Min ? ? ?1Q ?Median ? ? ?3Q ? ? Max

  6. ## -15.595 ?-2.730 ?-0.518 ? 1.777 ?26.199

  7. ##

  8. ## Coefficients:

  9. ## ? ? ? ? ? ? ? Estimate Std. Error t value Pr(>|t|)

  10. ## (Intercept) ?3.646e+01 ?5.103e+00 ? 7.144 3.28e-12 ***

  11. ## crim ? ? ? ?-1.080e-01 ?3.286e-02 ?-3.287 0.001087 **

  12. ## zn ? ? ? ? ? 4.642e-02 ?1.373e-02 ? 3.382 0.000778 ***

  13. ## indus ? ? ? ?2.056e-02 ?6.150e-02 ? 0.334 0.738288

  14. ## chas ? ? ? ? 2.687e+00 ?8.616e-01 ? 3.118 0.001925 **

  15. ## nox ? ? ? ? -1.777e+01 ?3.820e+00 ?-4.651 4.25e-06 ***

  16. ## rm ? ? ? ? ? 3.810e+00 ?4.179e-01 ? 9.116 ?< 2e-16 ***

  17. ## age ? ? ? ? ?6.922e-04 ?1.321e-02 ? 0.052 0.958229

  18. ## dis ? ? ? ? -1.476e+00 ?1.995e-01 ?-7.398 6.01e-13 ***

  19. ## rad ? ? ? ? ?3.060e-01 ?6.635e-02 ? 4.613 5.07e-06 ***

  20. ## tax ? ? ? ? -1.233e-02 ?3.760e-03 ?-3.280 0.001112 **

  21. ## ptratio ? ? -9.527e-01 ?1.308e-01 ?-7.283 1.31e-12 ***

  22. ## black ? ? ? ?9.312e-03 ?2.686e-03 ? 3.467 0.000573 ***

  23. ## lstat ? ? ? -5.248e-01 ?5.072e-02 -10.347 ?< 2e-16 ***

  24. ## ---

  25. ## Signif. codes: ?0 '***' 0.001 '**' 0.01 '*' 0.05 '.' 0.1 ' ' 1

  26. ##

  27. ## Residual standard error: 4.745 on 492 degrees of freedom

  28. ## Multiple R-squared: ?0.7406, Adjusted R-squared: ?0.7338

  29. ## 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ù)估計的總體準確性。讓我們稍微完善一下以前的模型。

  1. # 最佳模型


  2. AIC(modHouse, k = log(nrow(Boston)

  3. ## Start: ?AIC=1648.81

  4. ## medv ~ crim + zn + indus + chas + nox + rm + age + dis + rad +

  5. ## ? ? tax + ptratio + black + lstat

  6. ##

  7. ## ? ? ? ? ? Df Sum of Sq ? RSS ? ?AIC

  8. ## - age ? ? ?1 ? ? ?0.06 11079 1642.6

  9. ## - indus ? ?1 ? ? ?2.52 11081 1642.7

  10. ## <none> ? ? ? ? ? ? ? ? 11079 1648.8

  11. ## - chas ? ? 1 ? ?218.97 11298 1652.5

  12. ## - tax ? ? ?1 ? ?242.26 11321 1653.5

  13. ## - crim ? ? 1 ? ?243.22 11322 1653.6

  14. ## - zn ? ? ? 1 ? ?257.49 11336 1654.2

  15. ## - black ? ?1 ? ?270.63 11349 1654.8

  16. ## - rad ? ? ?1 ? ?479.15 11558 1664.0

  17. ## - nox ? ? ?1 ? ?487.16 11566 1664.4

  18. ## - ptratio ?1 ? 1194.23 12273 1694.4

  19. ## - dis ? ? ?1 ? 1232.41 12311 1696.0

  20. ## - rm ? ? ? 1 ? 1871.32 12950 1721.6

  21. ## - lstat ? ?1 ? 2410.84 13490 1742.2

  22. ##

  23. ## Step: ?AIC=1642.59

  24. ## medv ~ crim + zn + indus + chas + nox + rm + dis + rad + tax +

  25. ## ? ? ptratio + black + lstat

  26. ##

  27. ## ? ? ? ? ? Df Sum of Sq ? RSS ? ?AIC

  28. ## - indus ? ?1 ? ? ?2.52 11081 1636.5

  29. ## <none> ? ? ? ? ? ? ? ? 11079 1642.6

  30. ## - chas ? ? 1 ? ?219.91 11299 1646.3

  31. ## - tax ? ? ?1 ? ?242.24 11321 1647.3

  32. ## - crim ? ? 1 ? ?243.20 11322 1647.3

  33. ## - zn ? ? ? 1 ? ?260.32 11339 1648.1

  34. ## - black ? ?1 ? ?272.26 11351 1648.7

  35. ## - rad ? ? ?1 ? ?481.09 11560 1657.9

  36. ## - nox ? ? ?1 ? ?520.87 11600 1659.6

  37. ## - ptratio ?1 ? 1200.23 12279 1688.4

  38. ## - dis ? ? ?1 ? 1352.26 12431 1694.6

  39. ## - rm ? ? ? 1 ? 1959.55 13038 1718.8

  40. ## - lstat ? ?1 ? 2718.88 13798 1747.4

  41. ##

  42. ## Step: ?AIC=1636.48

  43. ## medv ~ crim + zn + chas + nox + rm + dis + rad + tax + ptratio +

  44. ## ? ? black + lstat

  45. ##

  46. ## ? ? ? ? ? Df Sum of Sq ? RSS ? ?AIC

  47. ## <none> ? ? ? ? ? ? ? ? 11081 1636.5

  48. ## - chas ? ? 1 ? ?227.21 11309 1640.5

  49. ## - crim ? ? 1 ? ?245.37 11327 1641.3

  50. ## - zn ? ? ? 1 ? ?257.82 11339 1641.9

  51. ## - black ? ?1 ? ?270.82 11352 1642.5

  52. ## - tax ? ? ?1 ? ?273.62 11355 1642.6

  53. ## - rad ? ? ?1 ? ?500.92 11582 1652.6

  54. ## - nox ? ? ?1 ? ?541.91 11623 1654.4

  55. ## - ptratio ?1 ? 1206.45 12288 1682.5

  56. ## - dis ? ? ?1 ? 1448.94 12530 1692.4

  57. ## - rm ? ? ? 1 ? 1963.66 13045 1712.8

  58. ## - lstat ? ?1 ? 2723.48 13805 1741.5

  59. # 模型比較

  60. compare(modBIC, modAIC)

  61. ## Calls:

  62. ## 1: lm(formula = medv ~ crim + zn + chas + nox + rm + dis + rad + tax + ptratio + black + lstat, data = Boston)

  63. ## 2: lm(formula = medv ~ crim + zn + chas + nox + rm + dis + rad + tax + ptratio + black + lstat, data = Boston)

  64. ##

  65. ## ? ? ? ? ? ? ?Model 1 ?Model 2

  66. ## (Intercept) ? ?36.34 ? ?36.34

  67. ## SE ? ? ? ? ? ? ?5.07 ? ? 5.07

  68. ##

  69. ## crim ? ? ? ? -0.1084 ?-0.1084

  70. ## SE ? ? ? ? ? ?0.0328 ? 0.0328

  71. ##

  72. ## zn ? ? ? ? ? ?0.0458 ? 0.0458

  73. ## SE ? ? ? ? ? ?0.0135 ? 0.0135

  74. ##

  75. ## chas ? ? ? ? ? 2.719 ? ?2.719

  76. ## SE ? ? ? ? ? ? 0.854 ? ?0.854

  77. ##

  78. ## nox ? ? ? ? ? -17.38 ? -17.38

  79. ## SE ? ? ? ? ? ? ?3.54 ? ? 3.54

  80. ##

  81. ## rm ? ? ? ? ? ? 3.802 ? ?3.802

  82. ## SE ? ? ? ? ? ? 0.406 ? ?0.406

  83. ##

  84. ## dis ? ? ? ? ? -1.493 ? -1.493

  85. ## SE ? ? ? ? ? ? 0.186 ? ?0.186

  86. ##

  87. ## rad ? ? ? ? ? 0.2996 ? 0.2996

  88. ## SE ? ? ? ? ? ?0.0634 ? 0.0634

  89. ##

  90. ## tax ? ? ? ? -0.01178 -0.01178

  91. ## SE ? ? ? ? ? 0.00337 ?0.00337

  92. ##

  93. ## ptratio ? ? ? -0.947 ? -0.947

  94. ## SE ? ? ? ? ? ? 0.129 ? ?0.129

  95. ##

  96. ## black ? ? ? ?0.00929 ?0.00929

  97. ## SE ? ? ? ? ? 0.00267 ?0.00267

  98. ##

  99. ## lstat ? ? ? ?-0.5226 ?-0.5226

  100. ## SE ? ? ? ? ? ?0.0474 ? 0.0474

  101. ##

  102. summary(modBIC)

  103. ##

  104. ## Call:

  105. ## lm(formula = medv ~ crim + zn + chas + nox + rm + dis + rad +

  106. ## ? ? tax + ptratio + black + lstat, data = Boston)

  107. ##

  108. ## Residuals:

  109. ## ? ? ?Min ? ? ? 1Q ? Median ? ? ? 3Q ? ? ?Max

  110. ## -15.5984 ?-2.7386 ?-0.5046 ? 1.7273 ?26.2373

  111. ##

  112. ## Coefficients:

  113. ## ? ? ? ? ? ? ? Estimate Std. Error t value Pr(>|t|)

  114. ## (Intercept) ?36.341145 ? 5.067492 ? 7.171 2.73e-12 ***

  115. ## crim ? ? ? ? -0.108413 ? 0.032779 ?-3.307 0.001010 **

  116. ## zn ? ? ? ? ? ?0.045845 ? 0.013523 ? 3.390 0.000754 ***

  117. ## chas ? ? ? ? ?2.718716 ? 0.854240 ? 3.183 0.001551 **

  118. ## nox ? ? ? ? -17.376023 ? 3.535243 ?-4.915 1.21e-06 ***

  119. ## rm ? ? ? ? ? ?3.801579 ? 0.406316 ? 9.356 ?< 2e-16 ***

  120. ## dis ? ? ? ? ?-1.492711 ? 0.185731 ?-8.037 6.84e-15 ***

  121. ## rad ? ? ? ? ? 0.299608 ? 0.063402 ? 4.726 3.00e-06 ***

  122. ## tax ? ? ? ? ?-0.011778 ? 0.003372 ?-3.493 0.000521 ***

  123. ## ptratio ? ? ?-0.946525 ? 0.129066 ?-7.334 9.24e-13 ***

  124. ## black ? ? ? ? 0.009291 ? 0.002674 ? 3.475 0.000557 ***

  125. ## lstat ? ? ? ?-0.522553 ? 0.047424 -11.019 ?< 2e-16 ***

  126. ## ---

  127. ## Signif. codes: ?0 '***' 0.001 '**' 0.01 '*' 0.05 '.' 0.1 ' ' 1

  128. ##

  129. ## Residual standard error: 4.736 on 494 degrees of freedom

  130. ## Multiple R-squared: ?0.7406, Adjusted R-squared: ?0.7348

  131. ## F-statistic: 128.2 on 11 and 494 DF, ?p-value: < 2.2e-16

  132. # 置信區(qū)間


  133. conf(modBIC)

  134. ## ? ? ? ? ? ? ? ? ? ? 2.5 % ? ? ? 97.5 %

  135. ## (Intercept) ?26.384649126 ?46.29764088

  136. ## crim ? ? ? ? -0.172817670 ?-0.04400902

  137. ## zn ? ? ? ? ? ?0.019275889 ? 0.07241397

  138. ## chas ? ? ? ? ?1.040324913 ? 4.39710769

  139. ## nox ? ? ? ? -24.321990312 -10.43005655

  140. ## rm ? ? ? ? ? ?3.003258393 ? 4.59989929

  141. ## dis ? ? ? ? ?-1.857631161 ?-1.12779176

  142. ## rad ? ? ? ? ? 0.175037411 ? 0.42417950

  143. ## tax ? ? ? ? ?-0.018403857 ?-0.00515209

  144. ## ptratio ? ? ?-1.200109823 ?-0.69293932

  145. ## black ? ? ? ? 0.004037216 ? 0.01454447

  146. ## 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)))?。?

  1. 應用其公式,我們將獲得

  1. ,因此

  1. 將不會定義。

  2. 具有相同的因變量。

  3. 如果是

  1. ,則

  1. 。

  2. 同樣,由于BIC?在選擇真實的分布/回歸模型時是?一致的:如果提供了足夠的數(shù)據(jù)

  1. ,則可以保證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指標


拓端tecdat|R語言多元逐步回歸模型分析房價和葡萄酒價格:選擇最合適的預測變量的評論 (共 條)

分享到微博請遵守國家法律
保靖县| 千阳县| 商南县| 天津市| 和顺县| 临桂县| 西宁市| 吴堡县| 沾化县| 莱州市| 武穴市| 长寿区| 周口市| 中宁县| 饶阳县| 莱芜市| 武陟县| 北辰区| 滦平县| 广平县| 乳源| 治多县| 英吉沙县| 屏山县| 襄城县| 新密市| 正宁县| 和田县| 陇川县| 淮南市| 木里| 镇巴县| 南木林县| 兴义市| 曲阜市| 临夏县| 建宁县| 遵化市| 曲阜市| 宝丰县| 满洲里市|