Rで学ぶ caret を使った Machine Learning
Zach Mayer
Data Scientist at DataRobot and co-author of caret
# mtcars にモデルを当てはめる
data(mtcars)
model <- lm(mpg ~ hp, mtcars[1:20, ])
# サンプル外で予測
predicted <- predict(
model, mtcars[21:32, ], type = "response"
)
# 誤差を評価
actual <- mtcars[21:32, "mpg"]
sqrt(mean((predicted - actual) ^ 2))
5.507236
# 全データでモデルを当てはめる
model2 <- lm(mpg ~ hp, mtcars)
# サンプル内で予測
predicted2 <- predict(
model, mtcars, type = "response"
)
# 誤差を評価
actual2 <- mtcars[, "mpg"]
sqrt(mean((predicted2 - actual2) ^ 2))
3.74
サンプル外 RMSE 5.5 と比較。
Rで学ぶ caret を使った Machine Learning