caret เป็น package ยอดนิยมในภาษา R ในทำ machine learning (ML)
caret ย่อมาจาก Classification And REgression Training และเป็น package ที่ถูกออกแบบมาช่วยให้การทำ ML เป็นเรื่องง่ายโดยมี functions สำหรับทำงานกับ ML workflow เบ็ดเสร็จใน package เดียว
p = สัดส่วนข้อมูลที่เราต้องการแบ่งให้กับ training set
list = ต้องการผลลัพธ์เป็น list (TRUE) หรือ matrix (FALSE)
สำหรับ BostonHousing เราจะแบ่ง 70% เป็น training set และ 30% เป็น test set แบบนี้:
# Set seed for reproducibility
set.seed(888)
# Get train index
train_index <- createDataPartition(BostonHousing$medv, # Specify the outcome
p = 0.7, # Set aside 70% for training set
list = FALSE) # Return as matrix
# Create training set
bt_train <- BostonHousing[train_index, ]
# Create test set
bt_test <- BostonHousing[-train_index, ]
เราสามารถดูจำนวนข้อมลใน training และ test sets ได้ด้วย nrow():
# Check the results
cat("Rows in training set:", nrow(bt_train), "\\n")
cat("Rows in test set:", nrow(bt_test))
ผลลัพธ์:
Rows in training set: 356
Rows in test set: 150
.
🍳 Step 2. Preprocess the Data
ในขั้นที่ 2 เราจะเตรียมข้อมูลเพื่อใช้ในการสร้าง model
# Learn preprocessing on training set
ppc <- preProcess(bt_train[, -14], # Select all predictors
method = c("center", "scale")) # Centre and scale
ตอนนี้ เราจะได้วิธีการเตรียมข้อมูลมาแล้ว ซึ่งเราจะต้องนำไปปรับใช้กับ training และ test sets ด้วย predict() และ cbind() แบบนี้:
Training set:
# Apply preprocessing to training set
bt_train_processed <- predict(ppc,
bt_train[, -14])
# Combine the preprocessed training set with outcome
bt_train_processed <- cbind(bt_train_processed,
medv = bt_train$medv)
Test set:
# Apply preprocessing to test set
bt_test_processed <- predict(ppc,
bt_test[, -14])
# Combine the preprocessed test set with outcome
bt_test_processed <- cbind(bt_test_processed,
medv = bt_test$medv)
ตอนนี้ training และ test sets ก็พร้อมที่จะใช้ในการสร้าง model แล้ว
.
👟 Step 3. Train the Model
ในขั้นที่ 3 เราจะสร้าง model กัน
โดยในตัวอย่าง เราจะลองสร้าง k-nearest neighbor (KNN) model ซึ่งทำนายข้อมูลด้วยการดูข้อมูลที่อยู่ใกล้เคียง
trControl = ค่าที่กำหนดการสร้าง model (ต้องใช้ function ชื่อ trainControl() ในการกำหนด)
tuneGrid = data frame ที่กำหนดค่า hyperparametre เพื่อทำ model tuning และหา model ที่ดีที่สุด
เราสามารถใช้ train() เพื่อสร้าง KNN model ในการทำนายราคาบ้านได้แบบนี้:
# Define training control:
# use k-fold cross-validation where k = 5
trc <- trainControl(method = "cv",
number = 5)
# Define grid:
# set k as odd numbers between 3 and 13
grid <- data.frame(k = seq(from = 3,
to = 13,
by = 2))
# Train the model
knn_model <- train(medv ~ ., # Specify the formula
data = bt_train_processed, # Use training set
method = "kknn", # Use knn engine
trControl = trc, # Specify training control
tuneGrid = grid) # Use grid to tune the model
เราสามารถดูรายละเอียดของ model ได้ดังนี้:
# Print the model
knn_model
ผลลัพธ์:
k-Nearest Neighbors
356 samples
13 predictor
No pre-processing
Resampling: Cross-Validated (5 fold)
Summary of sample sizes: 284, 286, 286, 284, 284
Resampling results across tuning parameters:
k RMSE Rsquared MAE
3 4.357333 0.7770080 2.840630
5 4.438162 0.7760085 2.849984
7 4.607954 0.7610468 2.941034
9 4.683062 0.7577702 2.972661
11 4.771317 0.7508908 3.043617
13 4.815444 0.7524266 3.053415
RMSE was used to select the optimal model using the smallest value.
The final value used for the model was k = 3.
.
📈 Step 4. Evaluate the Model
ในขั้นสุดท้าย เราจะประเมินความสามารถของ model ในการทำนายราคาบ้านกัน
💪 ผมขอแนะนำ R Book for Psychologists หนังสือสอนใช้ภาษา R เพื่อการวิเคราะห์ข้อมูลทางจิตวิทยา ที่เขียนมาเพื่อนักจิตวิทยาที่ไม่เคยมีประสบการณ์เขียน code มาก่อน
ในหนังสือ เราจะปูพื้นฐานภาษา R และพาไปดูวิธีวิเคราะห์สถิติที่ใช้บ่อยกัน เช่น:
Correlation
t-tests
ANOVA
Reliability
Factor analysis
🚀 เมื่ออ่านและทำตามตัวอย่างใน R Book for Psychologists ทุกคนจะไม่ต้องพึง SPSS และ Excel ในการทำงานอีกต่อไป และสามารถวิเคราะห์ข้อมูลด้วยตัวเองได้ด้วยความมั่นใจ