Median imputation

Machine Learning ด้วย caret ใน R

Max Kuhn

Software Engineer at RStudio and creator of caret

การจัดการค่าที่หายไป

  • โมเดลส่วนใหญ่ต้องการตัวเลข และรับค่าที่หายไปไม่ได้
  • วิธีทั่วไป: ลบแถวที่มีค่าหายไป
    • อาจทำให้ข้อมูลเกิดความลำเอียง
    • โมเดลอาจมั่นใจเกินจริง
  • วิธีที่ดีกว่า: median imputation!
    • แทนค่าที่หายไปด้วยค่ามัธยฐาน
    • ใช้ได้ดีเมื่อข้อมูลหายไปแบบสุ่ม (MAR)
Machine Learning ด้วย caret ใน R

ตัวอย่าง: 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 
Machine Learning ด้วย caret ใน R

วิธีแก้ปัญหาที่เรียบง่าย

# 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. 
Machine Learning ด้วย caret ใน R

มาฝึกกันเถอะ!

Machine Learning ด้วย caret ใน R

Preparing Video For Download...