如何使用 R 中的包“caret”将因子包含在回归模型中?

Yan*_*ang 4 regression r machine-learning r-caret dummy-variable

我正在尝试使用 R 包构建不同的回归模型caret。对于数据来说,它既包括数值,也包括因子。

问题 1: 在回归模型中同时包含数值和因子的正确方法是什么caret

问题2:回归模型通常需要进行数据预处理(中心和尺度),那么因子的预处理如何进行?

library(caret)
data("mtcars")
mydata = mtcars[, -c(8,9)]
set.seed(100)
mydata$dir = sample(x=c("N", "E", "S", "W"), size = 32, replace = T)
mydata$dir = as.factor(mydata$dir)
class(mydata$dir)    # Factor with four levels

MyControl = trainControl(
  method = "repeatedcv", 
  number = 5, 
  repeats = 2, 
  verboseIter = TRUE, 
  savePredictions = "final"
)

model_glm <- train(
  hp ~ ., 
  data = mydata, 
  method = "glm", 
  metric = "RMSE", 
  preProcess = c('center', 'scale'), 
  trControl = MyControl
)

model_pls <- train(
  hp ~ ., 
  data = mydata, 
  method = "pls", 
  metric = "RMSE", 
  preProcess = c('center', 'scale'), 
  trControl = MyControl
)

model_rf <- train(
  hp ~ ., 
  data = mydata, 
  tuneLength = 5, 
  method = "ranger", 
  metric = "RMSE", 
  preProcess = c('center', 'scale'), 
  trControl = MyControl
)

model_knn <- train(
  hp ~ ., 
  data = mydata, 
  tuneLength = 5, 
  method = "knn", 
  metric = "RMSE", 
  preProcess = c('center', 'scale'), 
  trControl = MyControl
)

model_svmr <- train(
  hp ~ ., 
  data = mydata, 
  tuneLength = 5, 
  method = "svmRadial", 
  metric = "RMSE", 
  preProcess = c('center', 'scale'), 
  trControl = MyControl
)
Run Code Online (Sandbox Code Playgroud)

Qui*_*ten 6

根据函数的文档,train输入 x 可以是:

包含训练数据的数据框,其中样本位于行中,特征位于列中。

y 目标输入:

包含每个样本结果的数字或因子向量。

所以你可以使用数值变量和因子变量。使用该~.符号,您将涵盖 x 中的所有变量。用中心和尺度预处理数值变量确实是一个好方法。在train函数中,您可以使用preProcess参数来缩放和居中。根据文档,这将忽略您的因子变量:

矩阵或数据框。允许使用非数字预测变量,但会被忽略。

在《Applied Predictive Modeling》 (作者 Max Kuhn、Kjell Johnson)一书的第 550 页上,您可以找到建议用于线性回归的预处理方法。它说:

  • 中心和缩放 (CS)
  • 删除近零预测变量 (NZV)
  • 删除高度相关的预测变量 (Corr)

在预处理中处理因子变量的一个选项是使用虚拟变量。在插入符号中,您可以使用该函数dummyVars将因子变量转换为数值变量。

示例代码:

library(caret)
library(tibble)
# Data
data("mtcars")
mydata = mtcars[, -c(8,9)]
set.seed(100)
mydata$dir = sample(x=c("N", "E", "S", "W"), size = 32, replace = T)
mydata$dir = as.factor(mydata$dir)
class(mydata$dir)    # Factor with four levels
#> [1] "factor"

# Create dummy variables
dummy_mydata <- dummyVars(hp~., data = mydata)
dummy_mydata_updated <- as_tibble(predict(dummy_mydata, newdata = mydata))
# remember to include the outcome variable too
dummy_mydata_updated <- cbind(hp = mydata$hp, dummy_mydata_updated)
head(dummy_mydata_updated)
#>    hp  mpg cyl disp drat    wt  qsec gear carb dir.E dir.N dir.S dir.W
#> 1 110 21.0   6  160 3.90 2.620 16.46    4    4     1     0     0     0
#> 2 110 21.0   6  160 3.90 2.875 17.02    4    4     0     0     1     0
#> 3  93 22.8   4  108 3.85 2.320 18.61    4    1     1     0     0     0
#> 4 110 21.4   6  258 3.08 3.215 19.44    3    1     0     0     0     1
#> 5 175 18.7   8  360 3.15 3.440 17.02    3    2     0     0     1     0
#> 6 105 18.1   6  225 2.76 3.460 20.22    3    1     0     1     0     0

MyControl = trainControl(
  method = "repeatedcv", 
  number = 5, 
  repeats = 2, 
  verboseIter = TRUE, 
  savePredictions = "final"
)

model_glm <- train(
  hp ~ ., 
  data = mydata, 
  method = "glm", 
  metric = "RMSE", 
  preProcess = c('center', 'scale'), 
  trControl = MyControl
)

plot(model_glm$pred$pred, model_glm$pred$obs,
     xlab='Predicted Values',
     ylab='Actual Values',
     main='Predicted vs. Actual Values')
abline(a=0, b=1)
Run Code Online (Sandbox Code Playgroud)

model_glm_dummy <- train(
  hp ~ ., 
  data = dummy_mydata_updated, 
  method = "glm", 
  metric = "RMSE", 
  preProcess = c('center', 'scale'), 
  trControl = MyControl
)

plot(model_glm_dummy$pred$pred, model_glm_dummy$pred$obs,
     xlab='Predicted Values',
     ylab='Actual Values',
     main='Predicted vs. Actual Values')
abline(a=0, b=1)
Run Code Online (Sandbox Code Playgroud)

# Results
model_glm$results
#>   parameter     RMSE  Rsquared      MAE   RMSESD RsquaredSD    MAESD
#> 1      none 37.13849 0.7302309 32.09739 11.50226  0.2143993 10.10452
model_glm_dummy$results
#>   parameter     RMSE  Rsquared    MAE   RMSESD RsquaredSD    MAESD
#> 1      none 35.71861 0.8095385 29.678 8.959409  0.1193792 5.616908
Run Code Online (Sandbox Code Playgroud)

创建于 2022 年 10 月 23 日,使用reprex v2.0.2

正如您所看到的,两个模型的结果略有不同。


对于真正有用的来源,请检查此website