회귀모형에서 예측변수가 ordinal factor인 경우 어떻게 해석해야 하나요? 예를 들어 mtcars 데이터의 carb를 ordinal factor로 바꾸면 다음과 같은 결과가 나옵니다. 먼저 그냥 범주형변수로 바꾸었을 때입니다.
mtcars$carb1=factor(mtcars$carb)
fit1=lm(mpg~carb1,data=mtcars)
summary(fit1)
Call: lm(formula = mpg ~ carb1, data = mtcars)
Residuals:
Min 1Q Median 3Q Max
-7.243 -3.325 0.000 2.360 8.557
Coefficients:
Estimate Std. Error t value Pr(>|t|)
(Intercept) 25.343 1.854 13.670 2.21e-13 ***
carb12 -2.943 2.417 -1.218 0.23435
carb13 -9.043 3.385 -2.672 0.01285 *
carb14 -9.553 2.417 -3.952 0.00053 ***
carb16 -5.643 5.243 -1.076 0.29174
carb18 -10.343 5.243 -1.973 0.05927 .
---
Signif. codes: 0 '***' 0.001 '**' 0.01 '*' 0.05 '.' 0.1 ' ' 1
Residual standard error: 4.905 on 26 degrees of freedom
Multiple R-squared: 0.4445, Adjusted R-squared: 0.3377
F-statistic: 4.161 on 5 and 26 DF, p-value: 0.006546
이 때의 회귀계수는 reference가 1일 경우에 비해 carb1이 2,3,4,6,8일 때의 회귀계수가 나오는 것으로 알고 있습니다. 하지만 ordinal factor인 경우는 결과를 어떻게 해석해야 하는지요?
mtcars$carb2=factor(mtcars$carb,ordered=TRUE)
fit2=lm(mpg~carb2,data=mtcars)
summary(fit2)
Call:
lm(formula = mpg ~ carb2, data = mtcars)
Residuals:
Min 1Q Median 3Q Max
-7.243 -3.325 0.000 2.360 8.557
Coefficients:
Estimate Std. Error t value Pr(>|t|)
(Intercept) 19.0888 1.3373 14.274 8.18e-14 ***
carb2.L -7.2101 3.6566 -1.972 0.0594 .
carb2.Q 3.4101 3.2378 1.053 0.3019
carb2.C -2.2938 3.4567 -0.664 0.5128
carb2^4 -4.1155 3.3132 -1.242 0.2253
carb2^5 -0.1224 2.6213 -0.047 0.9631
---
Signif. codes: 0 '***' 0.001 '**' 0.01 '*' 0.05 '.' 0.1 ' ' 1
Residual standard error: 4.905 on 26 degrees of freedom
Multiple R-squared: 0.4445, Adjusted R-squared: 0.3377
F-statistic: 4.161 on 5 and 26 DF, p-value: 0.006546
첫댓글 헉... 이런 건 처음 봅니다.
L: linear, Q: quadratic, C: cubicle 각각 1승, 2승, 3승을 뜻합니다. 나머지도 둘도 ^4, ^5 이죠.
summary(with(mtcars,lm(mpg ~ I(carb)+I(carb^2)+I(carb^3)+I(carb^4)+I(carb^5)))) 같이 polynomial regression을 한 것으로 추정됩니다.
그런데 t-value가 아래로 갈수록 점점 주는 경향이 있는 걸 보면 orthogonal polynomial regression같은 변형을 사용한 것으로 보입니다.
Linear일때 p-value가 가장 작으니 linear 관계가 유력하다는 걸까요?
저도 궁금해져서 구글링을 해봤는데 아래와 같은 답변을 찾았습니다. 결론적으로는 안재형 박사님이 말씀하신게 맞는 것 같네요! ordered factor를 linear model에 사용한다는 것은 level이 하나 증가할 때 마다의 효과가 linear함을 가정한다는 것인데, 그 부분을 알 수 없기에 spline등을 사용한다는 것 같습니다. 원문은 아래와 같습니다.
A modern way of handling it could be via an additive model, representing the ordinal independent variable via a spline. In R, if you make the ordinal predictor an "ordered factor" (with for instance the code ord <- factor(sample(1:5,20,replace=TRUE),ordered=TRUE) ) then in a linear model it will be represented via orthogonal polynomials.
https://stats.stackexchange.com/questions/195246/how-to-handle-ordinal-categorical-variable-as-independent-variable
@JHK21 수수께끼를 푸셨네요. polynomial contrasts... 1,2,3,4,5,6 대신 다음을 회귀계수로 이용했겠네요.
L, Q, C ^4, ^5가 나오는걸 보니 맞나봅니다^^
맞나 확인해 보려면 1,2,3,4,5,6 자리에 아랫걸 합쳐봐야겠는데요.
> contr.poly(6)
.L .Q .C ^4 ^5
[1,] -0.5976143 0.5455447 -0.3726780 0.1889822 -0.06299408
[2,] -0.3585686 -0.1091089 0.5217492 -0.5669467 0.31497039
[3,] -0.1195229 -0.4364358 0.2981424 0.3779645 -0.62994079
[4,] 0.1195229 -0.4364358 -0.2981424 0.3779645 0.62994079
[5,] 0.3585686 -0.1091089 -0.5217492 -0.5669467 -0.31497039
[6,] 0.5976143 0.5455447 0.3726780 0.1889822 0.06299408
이런... carb에 1~6외에 8도 있네요.
pp=data.frame(id=c(1:6,8), contr.poly(7)) # polynomial contrasts를 만들어서
mc=sqldf("select mtcars.*, pp.* from mtcars left join pp on mtcars.carb=pp.id") # mtcars에 merge해서
summary(lm(mpg~.L+.Q+.C+X.4+X.5,data=mc)) # 했는데...
비슷한데 좀 다르게 나옵니다.
이런 식으로 했을텐데 8을 어떻게 처리한 건지 모르겠네요^^
contr.poly(7) 대신 아래와 같이 score에 8을 넣어도 여전히 같지는 않은데요.
contr.poly(n=7,scores=c(1:6,8))
contr.poly(n=8) 이것도 다르고요.
@안재형 Level 5는 없는 것 같은데 c(1:4, 6,8)로 해보면 동일하지 않을까요? 컴퓨터를 킬 수 없는 환경이라 따로 돌려보지 못했네요
저도 이렇게 저렇게 해보는데 잘 모르겠네요... 비밀을 풀면 다시 글 올리겠습니다.