R에서 대치(Imputation)로 결측치 다루기
Michal Oleszak
Machine Learning Engineer
머신러닝 모델로 결측값을 예측합니다!
본 강의: randomForest 기반 missForest 패키지 사용


nhanes %>% is.na() %>% colSums()
Age Gender Weight Height Diabetes TotChol Pulse PhysActive
0 0 9 8 1 85 32 26
library(missForest)
imp_res <- missForest(nhanes)
nhanes_imp <- imp_res$ximp
nhanes_imp %>% is.na() %>% colSums()
Age Gender Weight Height Diabetes TotChol Pulse PhysActive
0 0 0 0 0 0 0 0
missForest()는 가방 밖(OOB) 대치 오차 추정을 제공합니다:
둘 다 값이 0에 가까울수록 좋고, 1에 가까우면 성능이 낮습니다.
imp_res <- missForest(nhanes)
imp_res$OOBerror
NRMSE PFC
0.147687025 0.003676471
missForest()는 가방 밖(OOB) 대치 오차 추정을 제공합니다:
두 경우 모두 0에 가까울수록 좋고, 1 부근은 나쁩니다.
imp_res <- missForest(nhanes, variablewise = TRUE)
imp_res$OOBerror
MSE PFC MSE MSE PFC MSE MSE MSE
0.00000 0.00000 285.79563 40.42142 0.00735 0.53444 129.03609 0.17576
여러 랜덤 포레스트를 학습하는 데는 시간이 많이 듭니다.
아이디어: 약간의 정확도를 포기하고 포레스트 크기를 줄여 계산 시간을 단축합니다.
ntree 인자).mtry 인자).계산 시간 영향은 다릅니다:
ntree 감소는 선형적으로 줄어듭니다.mtry 감소가 속도를 더 크게 높입니다.기본 설정:
start_time <- Sys.time()
imp_res <- missForest(nhanes)
end_time <- Sys.time()
print(imp_res$OOBerror)
print(end_time - start_time)
NRMSE PFC
0.147687025 0.003676471
Time difference of 5.496582 secs
축소된 포레스트:
start_time <- Sys.time()
imp_res <- missForest(nhanes,
ntree = 10,
mtry = 2)
end_time <- Sys.time()
print(imp_res$OOBerror)
print(end_time - start_time)
NRMSE PFC
0.162420139 0.007425743
Time difference of 0.516367 secs
R에서 대치(Imputation)로 결측치 다루기