중앙값 대치

R의 caret로 배우는 Machine Learning

Max Kuhn

Software Engineer at RStudio and creator of caret

결측값 처리하기

  • 대부분의 모델은 수치만 처리, 결측값 처리 불가
  • 일반적 방법: 결측 행 제거
    • 데이터 편향 초래 가능
    • 과신하는 모델 생성
  • 더 나은 전략: 중앙값 대치
    • 결측값을 중앙값으로 대체
    • 무작위 결측(MAR)에 잘 작동
R의 caret로 배우는 Machine Learning

예시: mtcars

# Generate some data with missing values
data(mtcars)
set.seed(42)
mtcars[sample(1:nrow(mtcars), 10), "hp"] <- NA
# Split target from predictors
Y <- mtcars$mpg
X <- mtcars[, 2:4]
# Try to fit a caret model
library(caret)
model <- train(X, Y)
Error in train.default(X, Y) : Stopping 
R의 caret로 배우는 Machine Learning

간단한 해결책

# Now fit with median imputation
model <- train(X, Y, preProcess = "medianImpute")
print(model)
Random Forest 

32 samples
 3 predictor

Pre-processing: median imputation (3) 
Resampling: Bootstrapped (25 reps) 
Summary of sample sizes: 32, 32, 32, 32, 32, 32, ... 
Resampling results across tuning parameters:

  mtry  RMSE      Rsquared 
  2     2.617096  0.8234652
  3     2.670550  0.8164535

RMSE was used to select the optimal model using the smallest value.
The final value used for the model was mtry = 2. 
R의 caret로 배우는 Machine Learning

실습해 봅시다!

R의 caret로 배우는 Machine Learning

Preparing Video For Download...