4. 변수 변환 (R)
출처: Montgomery, D. C., Peck, E. A., & Vining, G. G. (2012). Introduction to linear regression analysis (Vol. 821). John Wiley & Sons.
데이터: http://bcs.wiley.com/he-bcs/Books?action=resource&bcsId=9068&itemId=0470542810&resourceId=36322
5장
목차
- Transformation on y
- Transformation on x
1. Transformation on y¶
1) Empirical approach: Variance Stabilizing Transformations
- constant variance assumption 만족 못할 때 사용
σ2∝ constant => y′=y No transformation
σ2∝ E(y) => y′=√y square root; Poisson data
σ2∝ E(y)[1−E(y)] => y′=sin−1(√y) arcsin; binomial proportion
σ2∝ [E(y)]2 => y′=ln(y) log transformation
σ2∝ [E(y)]3 => y′=y1/2 Reciprocal square root
σ2∝ [E(y)]4 => y′=y−1 Reciprocal
Non constnt error variance 문제가 생기면 Least square estimator가 unbiased여도 minimum variance가 아니게 됨
따라서 coefficient 추정량의 분산 커짐 => 모델의 추정값이 부정확해지고 통계적 검정의 sensitivity 증가
setwd('C:/Users/bki19/desktop/Linear_Regression/data')
df<-read.csv('./Electric_Utility.csv')
df<-df[,c(2,3)]
colnames(df)<-c('x','y')
plot(df)
The Electric Utility Data
53명의 거주자의 8월 달 전력 사용 데이터
전력 사용량(X)과 피크 전력 시간(y)의 관계
전력 계획을 세우기 위해 전력 사용량 데이터 분석
fit<-lm(y~x,data=df)
summary(fit)
R2=0.7046: 약 70%의 변동성이 선형 fitting으로 설명 되고 있다
Coefficient도 유의하여 모형에 아무런 문제가 없어 보임
anova(fit)
par(mfrow=c(2,2))
plot(fit)
Residual vs fited plot으로 보아 variance가 점차 증가함
y는 전력 사용량이지만 횟수로 보고 poisson transformation y′=√y
fit2<-lm( (y)^0.5~x,data=df)
summary(fit2)
par(mfrow=c(2,2))
plot(fit2)
Residual vs Fitted plot 통해 variance stabilized 된 것 확인 가능
여전한 문제는 26과 50번의 residual 꽤 높음이 높아 outlier의 가능성
2) Analytical approach: Box Cox
y(λ)=yλ−1λ˙yλ−1,λ≠0
y(λ)=˙ylny,λ=0
where ˙y=ln−1[1n∑ni=1lnyi]
λ정하는방법
λ=1이면 transformation 필요 없다는 뜻
Maximum Likelihood: L(λ)=−12nln[SSRes(λ)]<=> minimize SSRes(λ)
Approximate CI for λ: L(ˆλ)−12χ2α,1를 L(λ)에 수평으로 그었을 때 아래쪽 구간
library(MASS)
fit_box<-boxcox(y~x,data=df)
grid search로 SSres를 minimize하는 lambda 찾음
lambdas<-fit_box$x
loglik<-fit_box$y
cbind(lambdas,loglik)
lambdas[which.max(loglik)]
maximized 하는 lambda는 0.5454이고 이것에 대한 CI가 1을 포함 안하기 떄문에 유의함
앞선 root(y) transformation이랑 비슷한 결과 의미 있음
2. Transformation on X¶
- Y와 X의 linear relationship 가정 위반 했을 때
1) Empirical approach
df<-read.csv('./Windmill.csv')
colnames(df)<-c('Velocity','Output')
The Windmill Data
풍속에 따라 전력 발전을 얼마나 하는지
plot(df)
Plot 봤을 때 nonlinear 가능성
fit<-lm(Output~Velocity,data=df)
summary(fit)
plot(fitted(fit),resid(fit))
residual plot 봤을 떄 활 모양으로 nonlinear
quadratic term 추가 고려해야 됨
하지만 풍속이 중가할 수록 전력 생산량이 증가하다가 감소한다는 산업적 지식에서 1x transformation
df2<-df
df2$Velocity<- (1/df$Velocity)
fit2<-lm(Output~ Velocity,data=df2)
summary(fit2)
par(mfrow=c(1,2))
plot(df2)
plot(fitted(fit2),rstudent(fit2))
residual plot을 봤을 때 ienquality of varaince 문제 없어 보임
따라서 transformed model이 적합
2) Analytical approach: Box Tidwell Procedur
처음에 transformation이 ξ=xa라고 가정
예제에서는 α0=1
ˆy=ˆβ0+ˆβ1x 추정
w=xlnx로 설정
ˆy=ˆβ∗0+ˆβ∗1x+ˆγw 추정
α1=ˆγˆβ1+1 계산
alpha=1
df3<-df
df3$w<- df$Velocity*log(df$Velocity)
fit3<-lm(Output~Velocity+w,data=df3)
summary(fit3)
coef(fit3)
alpha1=alpha+coef(fit3)[3]/coef(fit)[2]
alpha1
α가 -1로 가깝게 됨
앞선 1x transformation와 비슷한 결과
댓글 쓰기
0 댓글