단계적 변수 선택을 위해 1970년 보스턴 지역의 주거 데이터를 저장한 BostonHousing을 사용해보자. 보스턴 집 가격 medv를 종속 변수로 하고 범죄율, 방의 수 등의 모든 변수를 독립 변수로 하여 회귀 분석을 한 다음 step(모델, direction=“both”)를 사용해 변수의 추가, 삭제를 반복하면서 최적의 모델을 찾아보자.

    > library(mlbench)
    > data(BostonHousing)
    > m <- lm(medv ~ ., data=BostonHousing)
    > m2 <- step(m, direction="both")
    Start: AIC=1589.64
    medv ~ crim + zn + indus + chas + nox + rm + age + dis + rad + 
        tax + ptratio + b + lstat
    
             Df Sum of Sq   RSS    AIC
    - age     1      0.06 11079 1587.7
    - indus   1      2.52 11081 1587.8
    <none>                11079 1589.6
    - chas    1    218.97 11298 1597.5
    - tax     1    242.26 11321 1598.6
    - crim    1    243.22 11322 1598.6
    - zn      1    257.49 11336 1599.3
    - b       1    270.63 11349 1599.8
    - rad     1    479.15 11558 1609.1
    - nox     1    487.16 11566 1609.4
    - ptratio 1   1194.23 12273 1639.4
    - dis     1   1232.41 12311 1641.0
    - rm      1   1871.32 12950 1666.6
    - lstat   1   2410.84 13490 1687.3
    
    Step: AIC=1587.65
    medv ~ crim + zn + indus + chas + nox + rm + dis + rad + tax + 
        ptratio + b + lstat
    
             Df Sum of Sq RSS AIC
    - indus   1      2.52 11081 1585.8
    <none>                11079 1587.7
    + age     1      0.06 11079 1589.6
    - chas    1    219.91 11299 1595.6
    - tax     1    242.24 11321 1596.6
    - crim    1    243.20 11322 1596.6
    - zn      1    260.32 11339 1597.4
    - b       1    272.26 11351 1597.9
    - rad     1    481.09 11560 1607.2
    - nox     1    520.87 11600 1608.9
    - ptratio 1   1200.23 12279 1637.7
    - dis     1   1352.26 12431 1643.9
    - rm      1   1959.55 13038 1668.0
    - lstat   1   2718.88 13798 1696.7
    
    Step: AIC=1585.76
    medv ~ crim + zn + chas + nox + rm + dis + rad + tax + ptratio +
        b + lstat
    
             Df Sum of Sq   RSS    AIC
    <none>                11081 1585.8
    + indus   1      2.52 11079 1587.7
    + age     1      0.06 11081 1587.8
    - chas    1    227.21 11309 1594.0
    - crim    1    245.37 11327 1594.8
    - zn      1    257.82 11339 1595.4
    - b       1    270.82 11352 1596.0
    - tax     1    273.62 11355 1596.1
    - rad     1    500.92 11582 1606.1
    - nox     1    541.91 11623 1607.9
    - ptratio 1   1206.45 12288 1636.0
    - dis     1   1448.94 12530 1645.9
    - rm      1   1963.66 13045 1666.3
    - lstat   1   2723.48 13805 1695.0
    
    Call:
    lm(formula = medv ~ crim + zn + chas + nox + rm + dis + rad +
        tax + ptratio + b + lstat, data = BostonHousing)
    
    Coefficients:
    (Intercept)        crim         zn       chas1          nox
      36.341145   -0.108413   0.045845    2.718716   -17.376023
             rm         dis        rad         tax      ptratio
       3.801579   -1.492711   0.299608   -0.011778    -0.946525
              b       lstat
       0.009291   -0.522553
    
    > formula(m2)  # 구해진 최적 모델의 포뮬러
    medv ~ crim + zn + chas + nox + rm + dis + rad + tax + ptratio +
        b + lstat
    
    신간 소식 구독하기
    뉴스레터에 가입하시고 이메일로 신간 소식을 받아 보세요.