User Tools

Site Tools


multiple_regression_examples

Differences

This shows you the differences between two versions of the page.

Link to this comparison view

Both sides previous revisionPrevious revision
Next revision
Previous revision
Next revisionBoth sides next revision
multiple_regression_examples [2017/05/31 09:58] – [Regression Diagnostics] hkimscilmultiple_regression_examples [2019/11/01 13:20] – [Backward elimination] hkimscil
Line 110: Line 110:
 x3  work ethic x3  work ethic
 </code> </code>
 +from the above output:
 +| x  | zero-order cor  | part cor  | squared zero-order cor  | squared part cor  | shared square cor |
 +| x1 | .764  | .588  | 0.583696  | 0.345744  | 0.237952  |
 +| x2 | .769  | .594  | 0.591361  | 0.352836  | 0.238525  |
 +note that the values of two raws at the last column are similar. The portion is the shared effects from both x1 and x2.  
 +
 $$ \hat{Y_{i}} = 101.222 + 1.000X1_{i} + 1.071X2_{i} $$ $$ \hat{Y_{i}} = 101.222 + 1.000X1_{i} + 1.071X2_{i} $$
 $$ \hat{Y_{i}} = 101.222 + 1.000 \ \text{intell. ability}_{i} + 1.071 \ \text{work ethic}_{i} $$ $$ \hat{Y_{i}} = 101.222 + 1.000 \ \text{intell. ability}_{i} + 1.071 \ \text{work ethic}_{i} $$
Line 537: Line 543:
  
 {{:state.x77.reg.diagnostic.720.png}} {{:state.x77.reg.diagnostic.720.png}}
 +
 +===== Model objects =====
 +
 +<code>> names(model5)
 + [1] "coefficients"  "residuals"     "effects"       "rank"          "fitted.values"
 + [6] "assign"        "qr"            "df.residual"   "xlevels"       "call"         
 +[11] "terms"         "model"
 +</code>
 +<code>> coef(model5)                         # an extractor function
 +  (Intercept)    Population        Murder       HS.Grad         Frost 
 + 7.102713e+01  5.013998e-05 -3.001488e-01  4.658225e-02 -5.943290e-03 
 +> model5$coefficients                  # list indexing
 +  (Intercept)    Population        Murder       HS.Grad         Frost 
 + 7.102713e+01  5.013998e-05 -3.001488e-01  4.658225e-02 -5.943290e-03
 +> model5[[1]]                          # recall by position in the list (double brackets for lists)
 +  (Intercept)    Population        Murder       HS.Grad         Frost 
 + 7.102713e+01  5.013998e-05 -3.001488e-01  4.658225e-02 -5.943290e-03
 +</code>
 +
 +<code>> model5$resid
 +       Alabama         Alaska        Arizona       Arkansas     California 
 +    0.56888134    -0.54740399    -0.86415671     1.08626119    -0.08564599 
 +      Colorado    Connecticut       Delaware        Florida        Georgia 
 +    0.95645816     0.44541028    -1.06646884     0.04460505    -0.09694227 
 +        Hawaii          Idaho       Illinois        Indiana           Iowa 
 +    1.50683146     0.37010714    -0.05244160    -0.02158526     0.16347124 
 +        Kansas       Kentucky      Louisiana          Maine       Maryland 
 +    0.67648037     0.85582067    -0.39044846    -1.47095411    -0.29851996 
 + Massachusetts       Michigan      Minnesota    Mississippi       Missouri 
 +   -0.61105391     0.76106640     0.69440380    -0.91535384     0.58389969 
 +       Montana       Nebraska         Nevada  New Hampshire     New Jersey 
 +   -0.84024805     0.42967691    -0.49482393    -0.49635615    -0.66612086 
 +    New Mexico       New York North Carolina   North Dakota           Ohio 
 +    0.28880945    -0.07937149    -0.07624179     0.90350550    -0.26548767 
 +      Oklahoma         Oregon   Pennsylvania   Rhode Island South Carolina 
 +    0.26139958    -0.28445333    -0.95045527     0.13992982    -1.10109172 
 +  South Dakota      Tennessee          Texas           Utah        Vermont 
 +    0.06839119     0.64416651     0.92114057     0.84246817     0.57865019 
 +      Virginia     Washington  West Virginia      Wisconsin        Wyoming 
 +   -0.06691392    -0.96272426    -0.96982588     0.47004324    -0.58678863
 +</code>
 +
 +<code>
 +> sort(model5$resid)                   # extract residuals and sort them
 +         Maine South Carolina       Delaware  West Virginia     Washington 
 +   -1.47095411    -1.10109172    -1.06646884    -0.96982588    -0.96272426 
 +  Pennsylvania    Mississippi        Arizona        Montana     New Jersey 
 +   -0.95045527    -0.91535384    -0.86415671    -0.84024805    -0.66612086 
 + Massachusetts        Wyoming         Alaska  New Hampshire         Nevada 
 +   -0.61105391    -0.58678863    -0.54740399    -0.49635615    -0.49482393 
 +     Louisiana       Maryland         Oregon           Ohio        Georgia 
 +   -0.39044846    -0.29851996    -0.28445333    -0.26548767    -0.09694227 
 +    California       New York North Carolina       Virginia       Illinois 
 +   -0.08564599    -0.07937149    -0.07624179    -0.06691392    -0.05244160 
 +       Indiana        Florida   South Dakota   Rhode Island           Iowa 
 +   -0.02158526     0.04460505     0.06839119     0.13992982     0.16347124 
 +      Oklahoma     New Mexico          Idaho       Nebraska    Connecticut 
 +    0.26139958     0.28880945     0.37010714     0.42967691     0.44541028 
 +     Wisconsin        Alabama        Vermont       Missouri      Tennessee 
 +    0.47004324     0.56888134     0.57865019     0.58389969     0.64416651 
 +        Kansas      Minnesota       Michigan           Utah       Kentucky 
 +    0.67648037     0.69440380     0.76106640     0.84246817     0.85582067 
 +  North Dakota          Texas       Colorado       Arkansas         Hawaii 
 +    0.90350550     0.92114057     0.95645816     1.08626119     1.50683146
 +    
 +</code>
 +
 +====== e.g., ======
 +<code>
 +library(ISLR)
 +head(Carseats)
 +str(Carseats)
 +
 +lm.full <- lm(Sales ~ . , data = Carseats)
 +lm.null <- lm(Sales ~ 1 , data = Carseats)
 +
 +</code>
 +
 +<WRAP group>
 +<WRAP half column>
 +<code>
 +> stepAIC(lm.full)
 +Start:  AIC=26.82
 +Sales ~ CompPrice + Income + Advertising + Population + Price + 
 +    ShelveLoc + Age + Education + Urban + US
 +
 +              Df Sum of Sq     RSS    AIC
 +- Population        0.33  403.16  25.15
 +- Education    1      1.19  404.02  26.00
 +- Urban        1      1.23  404.06  26.04
 +- US                1.57  404.40  26.38
 +<none>                      402.83  26.82
 +- Income           76.16  478.99  94.09
 +- Advertising  1    127.14  529.97 134.54
 +- Age          1    217.44  620.27 197.48
 +- CompPrice    1    519.91  922.74 356.35
 +- ShelveLoc    2   1053.20 1456.03 536.80
 +- Price        1   1323.23 1726.06 606.85
 +
 +Step:  AIC=25.15
 +Sales ~ CompPrice + Income + Advertising + Price + ShelveLoc + 
 +    Age + Education + Urban + US
 +
 +              Df Sum of Sq     RSS    AIC
 +- Urban        1      1.15  404.31  24.29
 +- Education    1      1.36  404.52  24.49
 +- US                1.89  405.05  25.02
 +<none>                      403.16  25.15
 +- Income           75.94  479.10  92.18
 +- Advertising  1    145.38  548.54 146.32
 +- Age          1    218.52  621.68 196.38
 +- CompPrice    1    521.69  924.85 355.27
 +- ShelveLoc    2   1053.18 1456.34 534.89
 +- Price        1   1323.51 1726.67 605.00
 +
 +Step:  AIC=24.29
 +Sales ~ CompPrice + Income + Advertising + Price + ShelveLoc + 
 +    Age + Education + US
 +
 +              Df Sum of Sq     RSS    AIC
 +- Education    1      1.44  405.76  23.72
 +- US                1.85  406.16  24.12
 +<none>                      404.31  24.29
 +- Income           76.64  480.96  91.73
 +- Advertising  1    146.03  550.34 145.63
 +- Age          1    217.59  621.91 194.53
 +- CompPrice    1    526.17  930.48 355.69
 +- ShelveLoc    2   1053.93 1458.25 533.41
 +- Price        1   1322.80 1727.11 603.10
 +
 +Step:  AIC=23.72
 +Sales ~ CompPrice + Income + Advertising + Price + ShelveLoc + 
 +    Age + US
 +
 +              Df Sum of Sq     RSS    AIC
 +- US                1.63  407.39  23.32
 +<none>                      405.76  23.72
 +- Income           77.87  483.62  91.94
 +- Advertising  1    145.30  551.06 144.15
 +- Age          1    217.97  623.73 193.70
 +- CompPrice    1    525.25  931.00 353.92
 +- ShelveLoc    2   1056.88 1462.64 532.61
 +- Price        1   1322.83 1728.58 601.44
 +
 +Step:  AIC=23.32
 +Sales ~ CompPrice + Income + Advertising + Price + ShelveLoc + 
 +    Age
 +
 +              Df Sum of Sq     RSS    AIC
 +<none>                      407.39  23.32
 +- Income           76.68  484.07  90.30
 +- Age          1    219.12  626.51 193.48
 +- Advertising  1    234.03  641.42 202.89
 +- CompPrice    1    523.83  931.22 352.01
 +- ShelveLoc    2   1055.51 1462.90 530.68
 +- Price        1   1324.42 1731.81 600.18
 +
 +Call:
 +lm(formula = Sales ~ CompPrice + Income + Advertising + Price + 
 +    ShelveLoc + Age, data = Carseats)
 +
 +Coefficients:
 +    (Intercept)        CompPrice           Income      Advertising  
 +        5.47523          0.09257          0.01578          0.11590  
 +          Price    ShelveLocGood  ShelveLocMedium              Age  
 +       -0.09532          4.83567          1.95199         -0.04613  
 +
 +
 +</code>
 +</WRAP>
 +
 +<WRAP half column>
 +<code>
 +> step(lm.full, direction="both")
 +Start:  AIC=26.82
 +Sales ~ CompPrice + Income + Advertising + Population + Price + 
 +    ShelveLoc + Age + Education + Urban + US
 +
 +              Df Sum of Sq     RSS    AIC
 +- Population        0.33  403.16  25.15
 +- Education    1      1.19  404.02  26.00
 +- Urban        1      1.23  404.06  26.04
 +- US                1.57  404.40  26.38
 +<none>                      402.83  26.82
 +- Income           76.16  478.99  94.09
 +- Advertising  1    127.14  529.97 134.54
 +- Age          1    217.44  620.27 197.48
 +- CompPrice    1    519.91  922.74 356.35
 +- ShelveLoc    2   1053.20 1456.03 536.80
 +- Price        1   1323.23 1726.06 606.85
 +
 +Step:  AIC=25.15
 +Sales ~ CompPrice + Income + Advertising + Price + ShelveLoc + 
 +    Age + Education + Urban + US
 +
 +              Df Sum of Sq     RSS    AIC
 +- Urban        1      1.15  404.31  24.29
 +- Education    1      1.36  404.52  24.49
 +- US                1.89  405.05  25.02
 +<none>                      403.16  25.15
 ++ Population        0.33  402.83  26.82
 +- Income           75.94  479.10  92.18
 +- Advertising  1    145.38  548.54 146.32
 +- Age          1    218.52  621.68 196.38
 +- CompPrice    1    521.69  924.85 355.27
 +- ShelveLoc    2   1053.18 1456.34 534.89
 +- Price        1   1323.51 1726.67 605.00
 +
 +Step:  AIC=24.29
 +Sales ~ CompPrice + Income + Advertising + Price + ShelveLoc + 
 +    Age + Education + US
 +
 +              Df Sum of Sq     RSS    AIC
 +- Education    1      1.44  405.76  23.72
 +- US                1.85  406.16  24.12
 +<none>                      404.31  24.29
 ++ Urban        1      1.15  403.16  25.15
 ++ Population        0.25  404.06  26.04
 +- Income           76.64  480.96  91.73
 +- Advertising  1    146.03  550.34 145.63
 +- Age          1    217.59  621.91 194.53
 +- CompPrice    1    526.17  930.48 355.69
 +- ShelveLoc    2   1053.93 1458.25 533.41
 +- Price        1   1322.80 1727.11 603.10
 +
 +Step:  AIC=23.72
 +Sales ~ CompPrice + Income + Advertising + Price + ShelveLoc + 
 +    Age + US
 +
 +              Df Sum of Sq     RSS    AIC
 +- US                1.63  407.39  23.32
 +<none>                      405.76  23.72
 ++ Education    1      1.44  404.31  24.29
 ++ Urban        1      1.24  404.52  24.49
 ++ Population        0.41  405.35  25.32
 +- Income           77.87  483.62  91.94
 +- Advertising  1    145.30  551.06 144.15
 +- Age          1    217.97  623.73 193.70
 +- CompPrice    1    525.25  931.00 353.92
 +- ShelveLoc    2   1056.88 1462.64 532.61
 +- Price        1   1322.83 1728.58 601.44
 +
 +Step:  AIC=23.32
 +Sales ~ CompPrice + Income + Advertising + Price + ShelveLoc + 
 +    Age
 +
 +              Df Sum of Sq     RSS    AIC
 +<none>                      407.39  23.32
 ++ US                1.63  405.76  23.72
 ++ Education    1      1.22  406.16  24.12
 ++ Urban        1      1.19  406.20  24.15
 ++ Population        0.72  406.67  24.62
 +- Income           76.68  484.07  90.30
 +- Age          1    219.12  626.51 193.48
 +- Advertising  1    234.03  641.42 202.89
 +- CompPrice    1    523.83  931.22 352.01
 +- ShelveLoc    2   1055.51 1462.90 530.68
 +- Price        1   1324.42 1731.81 600.18
 +
 +Call:
 +lm(formula = Sales ~ CompPrice + Income + Advertising + Price + 
 +    ShelveLoc + Age, data = Carseats)
 +
 +Coefficients:
 +    (Intercept)        CompPrice           Income      Advertising  
 +        5.47523          0.09257          0.01578          0.11590  
 +          Price    ShelveLocGood  ShelveLocMedium              Age  
 +       -0.09532          4.83567          1.95199         -0.04613  
 +
 +
 +</code>
 +</WRAP>
 +</WRAP>
 +
 +<WRAP group>
 +<WRAP half column>
 +<code>> stepAIC(lm.full, direction="backward")
 +Start:  AIC=26.82
 +Sales ~ CompPrice + Income + Advertising + Population + Price + 
 +    ShelveLoc + Age + Education + Urban + US
 +
 +              Df Sum of Sq     RSS    AIC
 +- Population        0.33  403.16  25.15
 +- Education    1      1.19  404.02  26.00
 +- Urban        1      1.23  404.06  26.04
 +- US                1.57  404.40  26.38
 +<none>                      402.83  26.82
 +- Income           76.16  478.99  94.09
 +- Advertising  1    127.14  529.97 134.54
 +- Age          1    217.44  620.27 197.48
 +- CompPrice    1    519.91  922.74 356.35
 +- ShelveLoc    2   1053.20 1456.03 536.80
 +- Price        1   1323.23 1726.06 606.85
 +
 +Step:  AIC=25.15
 +Sales ~ CompPrice + Income + Advertising + Price + ShelveLoc + 
 +    Age + Education + Urban + US
 +
 +              Df Sum of Sq     RSS    AIC
 +- Urban        1      1.15  404.31  24.29
 +- Education    1      1.36  404.52  24.49
 +- US                1.89  405.05  25.02
 +<none>                      403.16  25.15
 +- Income           75.94  479.10  92.18
 +- Advertising  1    145.38  548.54 146.32
 +- Age          1    218.52  621.68 196.38
 +- CompPrice    1    521.69  924.85 355.27
 +- ShelveLoc    2   1053.18 1456.34 534.89
 +- Price        1   1323.51 1726.67 605.00
 +
 +Step:  AIC=24.29
 +Sales ~ CompPrice + Income + Advertising + Price + ShelveLoc + 
 +    Age + Education + US
 +
 +              Df Sum of Sq     RSS    AIC
 +- Education    1      1.44  405.76  23.72
 +- US                1.85  406.16  24.12
 +<none>                      404.31  24.29
 +- Income           76.64  480.96  91.73
 +- Advertising  1    146.03  550.34 145.63
 +- Age          1    217.59  621.91 194.53
 +- CompPrice    1    526.17  930.48 355.69
 +- ShelveLoc    2   1053.93 1458.25 533.41
 +- Price        1   1322.80 1727.11 603.10
 +
 +Step:  AIC=23.72
 +Sales ~ CompPrice + Income + Advertising + Price + ShelveLoc + 
 +    Age + US
 +
 +              Df Sum of Sq     RSS    AIC
 +- US                1.63  407.39  23.32
 +<none>                      405.76  23.72
 +- Income           77.87  483.62  91.94
 +- Advertising  1    145.30  551.06 144.15
 +- Age          1    217.97  623.73 193.70
 +- CompPrice    1    525.25  931.00 353.92
 +- ShelveLoc    2   1056.88 1462.64 532.61
 +- Price        1   1322.83 1728.58 601.44
 +
 +Step:  AIC=23.32
 +Sales ~ CompPrice + Income + Advertising + Price + ShelveLoc + 
 +    Age
 +
 +              Df Sum of Sq     RSS    AIC
 +<none>                      407.39  23.32
 +- Income           76.68  484.07  90.30
 +- Age          1    219.12  626.51 193.48
 +- Advertising  1    234.03  641.42 202.89
 +- CompPrice    1    523.83  931.22 352.01
 +- ShelveLoc    2   1055.51 1462.90 530.68
 +- Price        1   1324.42 1731.81 600.18
 +
 +Call:
 +lm(formula = Sales ~ CompPrice + Income + Advertising + Price + 
 +    ShelveLoc + Age, data = Carseats)
 +
 +Coefficients:
 +    (Intercept)        CompPrice           Income      Advertising            Price  
 +        5.47523          0.09257          0.01578          0.11590         -0.09532  
 +  ShelveLocGood  ShelveLocMedium              Age  
 +        4.83567          1.95199         -0.04613  
 +
 +> </code>
 +</WRAP>
 +
 +<WRAP half column>
 +<code>> step(lm.full, direction="backward")
 +Start:  AIC=26.82
 +Sales ~ CompPrice + Income + Advertising + Population + Price + 
 +    ShelveLoc + Age + Education + Urban + US
 +
 +              Df Sum of Sq     RSS    AIC
 +- Population        0.33  403.16  25.15
 +- Education    1      1.19  404.02  26.00
 +- Urban        1      1.23  404.06  26.04
 +- US                1.57  404.40  26.38
 +<none>                      402.83  26.82
 +- Income           76.16  478.99  94.09
 +- Advertising  1    127.14  529.97 134.54
 +- Age          1    217.44  620.27 197.48
 +- CompPrice    1    519.91  922.74 356.35
 +- ShelveLoc    2   1053.20 1456.03 536.80
 +- Price        1   1323.23 1726.06 606.85
 +
 +Step:  AIC=25.15
 +Sales ~ CompPrice + Income + Advertising + Price + ShelveLoc + 
 +    Age + Education + Urban + US
 +
 +              Df Sum of Sq     RSS    AIC
 +- Urban        1      1.15  404.31  24.29
 +- Education    1      1.36  404.52  24.49
 +- US                1.89  405.05  25.02
 +<none>                      403.16  25.15
 +- Income           75.94  479.10  92.18
 +- Advertising  1    145.38  548.54 146.32
 +- Age          1    218.52  621.68 196.38
 +- CompPrice    1    521.69  924.85 355.27
 +- ShelveLoc    2   1053.18 1456.34 534.89
 +- Price        1   1323.51 1726.67 605.00
 +
 +Step:  AIC=24.29
 +Sales ~ CompPrice + Income + Advertising + Price + ShelveLoc + 
 +    Age + Education + US
 +
 +              Df Sum of Sq     RSS    AIC
 +- Education    1      1.44  405.76  23.72
 +- US                1.85  406.16  24.12
 +<none>                      404.31  24.29
 +- Income           76.64  480.96  91.73
 +- Advertising  1    146.03  550.34 145.63
 +- Age          1    217.59  621.91 194.53
 +- CompPrice    1    526.17  930.48 355.69
 +- ShelveLoc    2   1053.93 1458.25 533.41
 +- Price        1   1322.80 1727.11 603.10
 +
 +Step:  AIC=23.72
 +Sales ~ CompPrice + Income + Advertising + Price + ShelveLoc + 
 +    Age + US
 +
 +              Df Sum of Sq     RSS    AIC
 +- US                1.63  407.39  23.32
 +<none>                      405.76  23.72
 +- Income           77.87  483.62  91.94
 +- Advertising  1    145.30  551.06 144.15
 +- Age          1    217.97  623.73 193.70
 +- CompPrice    1    525.25  931.00 353.92
 +- ShelveLoc    2   1056.88 1462.64 532.61
 +- Price        1   1322.83 1728.58 601.44
 +
 +Step:  AIC=23.32
 +Sales ~ CompPrice + Income + Advertising + Price + ShelveLoc + 
 +    Age
 +
 +              Df Sum of Sq     RSS    AIC
 +<none>                      407.39  23.32
 +- Income           76.68  484.07  90.30
 +- Age          1    219.12  626.51 193.48
 +- Advertising  1    234.03  641.42 202.89
 +- CompPrice    1    523.83  931.22 352.01
 +- ShelveLoc    2   1055.51 1462.90 530.68
 +- Price        1   1324.42 1731.81 600.18
 +
 +Call:
 +lm(formula = Sales ~ CompPrice + Income + Advertising + Price + 
 +    ShelveLoc + Age, data = Carseats)
 +
 +Coefficients:
 +    (Intercept)        CompPrice           Income      Advertising            Price  
 +        5.47523          0.09257          0.01578          0.11590         -0.09532  
 +  ShelveLocGood  ShelveLocMedium              Age  
 +        4.83567          1.95199         -0.04613  
 +</code>
 +</WRAP>
 +</WRAP>
 +===== Pick the model from stepAIC =====
 +<code>
 +lm.fit.01 <- lm(formula = Sales ~ 
 +                 CompPrice + Income + 
 +                 Advertising + Price + 
 +                 ShelveLoc + Age, 
 +                 data = Carseats)
 +summary(lm.fit.01)
 +</code>
 +
 +<code>
 +> lm.fit.01 <- lm(formula = Sales ~ CompPrice + 
 +         Income + Advertising + Price + 
 +         ShelveLoc + Age, data = Carseats)
 +> summary(lm.fit.01)
 +
 +Call:
 +lm(formula = Sales ~ CompPrice + Income + Advertising + Price + 
 +    ShelveLoc + Age, data = Carseats)
 +
 +Residuals:
 +    Min      1Q  Median      3Q     Max 
 +-2.7728 -0.6954  0.0282  0.6732  3.3292 
 +
 +Coefficients:
 +                 Estimate Std. Error t value Pr(>|t|)    
 +(Intercept)      5.475226   0.505005   10.84   <2e-16 ***
 +CompPrice        0.092571   0.004123   22.45   <2e-16 ***
 +Income           0.015785   0.001838    8.59   <2e-16 ***
 +Advertising      0.115903   0.007724   15.01   <2e-16 ***
 +Price           -0.095319   0.002670  -35.70   <2e-16 ***
 +ShelveLocGood    4.835675   0.152499   31.71   <2e-16 ***
 +ShelveLocMedium  1.951993   0.125375   15.57   <2e-16 ***
 +Age             -0.046128   0.003177  -14.52   <2e-16 ***
 +---
 +Signif. codes:  0 ‘***’ 0.001 ‘**’ 0.01 ‘*’ 0.05 ‘.’ 0.1 ‘ ’ 1
 +
 +Residual standard error: 1.019 on 392 degrees of freedom
 +Multiple R-squared:  0.872, Adjusted R-squared:  0.8697 
 +F-statistic: 381.4 on 7 and 392 DF,  p-value: < 2.2e-16
 +</code>
 +
 +Compare the fitted model to full model
 +
 +<code>
 +anova(lm.full, lm.fit.01)
 +</code>
 +
 +<code>> anova(lm.full, lm.fit.01)
 +Analysis of Variance Table
 +
 +Model 1: Sales ~ CompPrice + Income + Advertising + Population + Price + 
 +    ShelveLoc + Age + Education + Urban + US
 +Model 2: Sales ~ CompPrice + Income + Advertising + Price + ShelveLoc + 
 +    Age
 +  Res.Df    RSS Df Sum of Sq      F Pr(>F)
 +1    388 402.83                           
 +2    392 407.39 -4   -4.5533 1.0964  0.358
 +> </code>
 +===== Backward elimination =====
 +<code>
 +drop1(lm.full, test = "F")
 +</code>
 +
 +<code>
 +> drop1(lm.full, test = "F")
 +Single term deletions
 +
 +Model:
 +Sales ~ CompPrice + Income + Advertising + Population + Price + 
 +    ShelveLoc + Age + Education + Urban + US
 +            Df Sum of Sq     RSS    AIC   F value    Pr(>F)    
 +<none>                    402.83  26.82                        
 +CompPrice    1    519.91  922.74 356.35  500.7659 < 2.2e-16 ***
 +Income           76.16  478.99  94.09   73.3537  2.58e-16 ***
 +Advertising  1    127.14  529.97 134.54  122.4571 < 2.2e-16 ***
 +Population        0.33  403.16  25.15    0.3149    0.5750    
 +Price        1   1323.23 1726.06 606.85 1274.5022 < 2.2e-16 ***
 +ShelveLoc    2   1053.20 1456.03 536.80  507.2079 < 2.2e-16 ***
 +Age          1    217.44  620.27 197.48  209.4333 < 2.2e-16 ***
 +Education    1      1.19  404.02  26.00    1.1450    0.2853    
 +Urban        1      1.23  404.06  26.04    1.1831    0.2774    
 +US                1.57  404.40  26.38    1.5094    0.2200    
 +---
 +Signif. codes:  0 ‘***’ 0.001 ‘**’ 0.01 ‘*’ 0.05 ‘.’ 0.1 ‘ ’ 1
 +
 +> </code>
 +
 +<code>
 +drop1(update(lm.full, ~ . -Population), test = "F")
 +</code>
 +
 +<code>
 +> drop1(update(lm.full, ~ . -Population), test = "F")
 +Single term deletions
 +
 +Model:
 +Sales ~ CompPrice + Income + Advertising + Price + ShelveLoc + 
 +    Age + Education + Urban + US
 +            Df Sum of Sq     RSS    AIC   F value    Pr(>F)    
 +<none>                    403.16  25.15                        
 +CompPrice    1    521.69  924.85 355.27  503.3686 < 2.2e-16 ***
 +Income           75.94  479.10  92.18   73.2717 2.652e-16 ***
 +Advertising  1    145.38  548.54 146.32  140.2694 < 2.2e-16 ***
 +Price        1   1323.51 1726.67 605.00 1277.0276 < 2.2e-16 ***
 +ShelveLoc    2   1053.18 1456.34 534.89  508.0927 < 2.2e-16 ***
 +Age          1    218.52  621.68 196.38  210.8411 < 2.2e-16 ***
 +Education    1      1.36  404.52  24.49    1.3122    0.2527    
 +Urban        1      1.15  404.31  24.29    1.1132    0.2920    
 +US                1.89  405.05  25.02    1.8262    0.1774    
 +---
 +Signif. codes:  0 ‘***’ 0.001 ‘**’ 0.01 ‘*’ 0.05 ‘.’ 0.1 ‘ ’ 1
 +
 +> </code>
 +
 +<code>
 +drop1(update(lm.full, ~. - Population -Urban), test="F")
 +</code>
 +
 +<code>
 +> drop1(update(lm.full, ~. - Population -Urban), test="F")
 +Single term deletions
 +
 +Model:
 +Sales ~ CompPrice + Income + Advertising + Price + ShelveLoc + 
 +    Age + Education + US
 +            Df Sum of Sq     RSS    AIC   F value Pr(>F)    
 +<none>                    404.31  24.29                     
 +CompPrice    1    526.17  930.48 355.69  507.5374 <2e-16 ***
 +Income           76.64  480.96  91.73   73.9307 <2e-16 ***
 +Advertising  1    146.03  550.34 145.63  140.8593 <2e-16 ***
 +Price        1   1322.80 1727.11 603.10 1275.9661 <2e-16 ***
 +ShelveLoc    2   1053.93 1458.25 533.41  508.3098 <2e-16 ***
 +Age          1    217.59  621.91 194.53  209.8897 <2e-16 ***
 +Education    1      1.44  405.76  23.72    1.3930 0.2386    
 +US                1.85  406.16  24.12    1.7848 0.1823    
 +---
 +Signif. codes:  0 ‘***’ 0.001 ‘**’ 0.01 ‘*’ 0.05 ‘.’ 0.1 ‘ ’ 1
 +
 +
 +</code>
 +
 +<code>
 +drop1(update(lm.full, ~. - Population -Urban -Education), test="F")
 +</code>
 +
 +<code>
 +> drop1(update(lm.full, ~. - Population -Urban -Education), test="F")
 +Single term deletions
 +
 +Model:
 +Sales ~ CompPrice + Income + Advertising + Price + ShelveLoc + 
 +    Age + US
 +            Df Sum of Sq     RSS    AIC   F value Pr(>F)    
 +<none>                    405.76  23.72                     
 +CompPrice    1    525.25  931.00 353.92  506.1421 <2e-16 ***
 +Income           77.87  483.62  91.94   75.0336 <2e-16 ***
 +Advertising  1    145.30  551.06 144.15  140.0181 <2e-16 ***
 +Price        1   1322.83 1728.58 601.44 1274.7123 <2e-16 ***
 +ShelveLoc    2   1056.88 1462.64 532.61  509.2202 <2e-16 ***
 +Age          1    217.97  623.73 193.70  210.0409 <2e-16 ***
 +US                1.63  407.39  23.32    1.5693 0.2111    
 +---
 +Signif. codes:  0 ‘***’ 0.001 ‘**’ 0.01 ‘*’ 0.05 ‘.’ 0.1 ‘ ’ 1
 +
 +</code>
 +
 +<code>
 +drop1(update(lm.full, ~. - Population -Urban -Education -US), test="F")
 +</code>
 +
 +<code>
 +> drop1(update(lm.full, ~. - Population -Urban -Education -US), test="F")
 +Single term deletions
 +
 +Model:
 +Sales ~ CompPrice + Income + Advertising + Price + ShelveLoc + 
 +    Age
 +            Df Sum of Sq     RSS    AIC  F value    Pr(>F)    
 +<none>                    407.39  23.32                       
 +CompPrice    1    523.83  931.22 352.01  504.047 < 2.2e-16 ***
 +Income           76.68  484.07  90.30   73.784 < 2.2e-16 ***
 +Advertising  1    234.03  641.42 202.89  225.192 < 2.2e-16 ***
 +Price        1   1324.42 1731.81 600.18 1274.400 < 2.2e-16 ***
 +ShelveLoc    2   1055.51 1462.90 530.68  507.822 < 2.2e-16 ***
 +Age          1    219.12  626.51 193.48  210.848 < 2.2e-16 ***
 +---
 +Signif. codes:  0 ‘***’ 0.001 ‘**’ 0.01 ‘*’ 0.05 ‘.’ 0.1 ‘ ’ 1
 +> </code>
 +
 +
 +<WRAP group>
 +<WRAP half column>
 +<code>lm.fit.be <- lm(Sales ~ CompPrice + 
 +            Income + Advertising + Price + 
 +            ShelveLoc + Age, data = Carseats)
 +summary(lm.fit.be)
 +</code>
 +
 +<code>
 +> lm.fit.be <- lm(Sales ~ CompPrice + Income + Advertising + Price + ShelveLoc + Age, data = Carseats)
 +> summary(lm.fit.be)
 +
 +Call:
 +lm(formula = Sales ~ CompPrice + Income + Advertising + Price + 
 +    ShelveLoc + Age, data = Carseats)
 +
 +Residuals:
 +    Min      1Q  Median      3Q     Max 
 +-2.7728 -0.6954  0.0282  0.6732  3.3292 
 +
 +Coefficients:
 +                 Estimate Std. Error t value Pr(>|t|)    
 +(Intercept)      5.475226   0.505005   10.84   <2e-16 ***
 +CompPrice        0.092571   0.004123   22.45   <2e-16 ***
 +Income           0.015785   0.001838    8.59   <2e-16 ***
 +Advertising      0.115903   0.007724   15.01   <2e-16 ***
 +Price           -0.095319   0.002670  -35.70   <2e-16 ***
 +ShelveLocGood    4.835675   0.152499   31.71   <2e-16 ***
 +ShelveLocMedium  1.951993   0.125375   15.57   <2e-16 ***
 +Age             -0.046128   0.003177  -14.52   <2e-16 ***
 +---
 +Signif. codes:  0 ‘***’ 0.001 ‘**’ 0.01 ‘*’ 0.05 ‘.’ 0.1 ‘ ’ 1
 +
 +Residual standard error: 1.019 on 392 degrees of freedom
 +Multiple R-squared:  0.872, Adjusted R-squared:  0.8697 
 +F-statistic: 381.4 on 7 and 392 DF,  p-value: < 2.2e-16
 +
 +
 +</code>
 +</WRAP>
 +
 +<WRAP half column>
 +<code>
 +lm.fit.01 <- lm(formula = Sales ~ 
 +                 CompPrice + Income + 
 +                 Advertising + Price + 
 +                 ShelveLoc + Age, 
 +                 data = Carseats)
 +summary(lm.fit.01)
 +</code>
 +
 +<code>
 +> lm.fit.01 <- lm(formula = Sales ~ CompPrice + 
 +         Income + Advertising + Price + 
 +         ShelveLoc + Age, data = Carseats)
 +> summary(lm.fit.01)
 +
 +Call:
 +lm(formula = Sales ~ CompPrice + Income + Advertising + Price + 
 +    ShelveLoc + Age, data = Carseats)
 +
 +Residuals:
 +    Min      1Q  Median      3Q     Max 
 +-2.7728 -0.6954  0.0282  0.6732  3.3292 
 +
 +Coefficients:
 +                 Estimate Std. Error t value Pr(>|t|)    
 +(Intercept)      5.475226   0.505005   10.84   <2e-16 ***
 +CompPrice        0.092571   0.004123   22.45   <2e-16 ***
 +Income           0.015785   0.001838    8.59   <2e-16 ***
 +Advertising      0.115903   0.007724   15.01   <2e-16 ***
 +Price           -0.095319   0.002670  -35.70   <2e-16 ***
 +ShelveLocGood    4.835675   0.152499   31.71   <2e-16 ***
 +ShelveLocMedium  1.951993   0.125375   15.57   <2e-16 ***
 +Age             -0.046128   0.003177  -14.52   <2e-16 ***
 +---
 +Signif. codes:  0 ‘***’ 0.001 ‘**’ 0.01 ‘*’ 0.05 ‘.’ 0.1 ‘ ’ 1
 +
 +Residual standard error: 1.019 on 392 degrees of freedom
 +Multiple R-squared:  0.872, Adjusted R-squared:  0.8697 
 +F-statistic: 381.4 on 7 and 392 DF,  p-value: < 2.2e-16
 +
 +</code>
 +</WRAP>
 +</WRAP>
 +
  
  
multiple_regression_examples.txt · Last modified: 2023/10/21 13:26 by hkimscil

Donate Powered by PHP Valid HTML5 Valid CSS Driven by DokuWiki