공선성(collinearity): 하나의 독립변수가 다른 하나의 독립변수로 잘 예측되는 경우, 또는 서로 상관이 높은 경우
다중공선성(multicollinearity): 하나의 독립변수가 다른 여러 개의 독립변수들로 잘 예측되는 경우
(다중)공선성이 있으면:
crab.csv를 다운로드 받아 연다. 이 데이터는 게의 크기와 무게 등을 측정한 데이터이다.
df = read.csv('crab.csv')
head(df)
crab sat y weight width color spine 1 1 8 1 3.05 28.3 2 3 2 2 0 0 1.55 22.5 3 3 3 3 9 1 2.30 26.0 1 1 4 4 0 0 2.10 24.8 3 3 5 5 4 1 2.60 26.0 3 3 6 6 0 0 2.10 23.8 2 3
먼저 회귀분석을 해본다.
model = lm(y ~ sat + weight + width, df)
summary(model)
Call: lm(formula = y ~ sat + weight + width, data = df) Residuals: Min 1Q Median 3Q Max -0.89580 -0.29752 0.01245 0.28001 0.70921 Coefficients: Estimate Std. Error t value Pr(>|t|) (Intercept) -0.936650 0.500403 -1.872 0.0630 . sat 0.097113 0.008814 11.018 <2e-16 *** weight -0.046514 0.097890 -0.475 0.6353 width 0.053544 0.026465 2.023 0.0446 * --- Signif. codes: 0 ‘***’ 0.001 ‘**’ 0.01 ‘*’ 0.05 ‘.’ 0.1 ‘ ’ 1 Residual standard error: 0.3381 on 169 degrees of freedom Multiple R-squared: 0.5145, Adjusted R-squared: 0.5058 F-statistic: 59.69 on 3 and 169 DF, p-value: < 2.2e-16
결과를 보면 유의수준 5%에서 sat
와 width
는 통계적으로 유의미하고, weight
는 유의미하지 않게 나왔다.
car
라이브러리를 불러들인다. 없으면 install.packages('car')
로 설치한다.
library(car)
Loading required package: carData
VIF를 계산하는 함수는 vif
이다.
vif(model)
sat weight width 1.158837 4.801679 4.688660
weight
와 width
의 VIF가 각각 4.8과 4.6이다. 게의 무게(weight)와 너비(width)는 서로 상관이 높기 때문에 VIF가 약간 높게 나타나는 것이다.
weight
와 width
가 VIF 기준을 넘는 것은 아니지만 실험삼아 width
를 제거해보자.
summary(lm(y ~ sat + weight, df))
Call: lm(formula = y ~ sat + weight, data = df) Residuals: Min 1Q Median 3Q Max -0.89198 -0.30621 0.03393 0.27644 0.65124 Coefficients: Estimate Std. Error t value Pr(>|t|) (Intercept) 0.04951 0.11426 0.433 0.6653 sat 0.09763 0.00889 10.982 <2e-16 *** weight 0.12601 0.04850 2.598 0.0102 * --- Signif. codes: 0 ‘***’ 0.001 ‘**’ 0.01 ‘*’ 0.05 ‘.’ 0.1 ‘ ’ 1 Residual standard error: 0.3411 on 170 degrees of freedom Multiple R-squared: 0.5027, Adjusted R-squared: 0.4969 F-statistic: 85.93 on 2 and 170 DF, p-value: < 2.2e-16
이전의 분석에서는 weight
가 유의미하지 않게 나왔지만, width
를 제거한 후에는 유의미하게 나왔다. weight
와 width
가 공선성이 있기 때문에 width
를 제거하자 weight
가 유의미해진 것으로 볼 수 있다.