具有变量 X 变量的 R Caret Train 函数

R Caret Train function with variable X-variable

我正在尝试在具有变量 x 值的代码中 运行 caret::train 函数。详情见下文

我正在使用如下所示的火车功能↓

model <- train(GenCSTempMax ~., #************************ ENTER THE out.x VALUE
           data=genbrg.data, 
           method="glm", 
           preProcess="scale", 
           trControl=control)

我想要做的是从字符变量输入 x 变量(上面是 GenCSTempMax )

示例

out.x <- "Test"

model <- train("insert out.x here" ~., #************************ ENTER THE out.x VALUE
           data=genbrg.data, 
           method="glm", 
           preProcess="scale", 
           trControl=control)

我试过

paste(out.x) & paste0(out.x) 

也尝试过使用 infuser 包

这是我得到的正常错误:

Error in model.frame.default(form = names(genbrg.data[, 1]) ~ ., data = genbrg.data,  : 
variable lengths differ (found for 'GenCSTempMax')

Error in model.frame.default(form = paste(out.x) ~ ., data = genbrg.data,  : 
variable lengths differ (found for 'GenCSTempMax')

Error in model.frame.default(form = paste0(out.x) ~ ., data = genbrg.data,  : 
variable lengths differ (found for 'GenCSTempMax')

您只需重新创建公式,例如:

genbrg.data = data.frame(GenCSTempMax = rnorm(100),
                         Test = rnorm(100),
                         matrix(rnorm(1000),ncol=10)
                        )

Formula  = reformulate(".",response="Test")

Formula
Test ~ .

model <- train(Formula,
           data=genbrg.data, 
           method="glm", 
           preProcess="scale", 
           trControl=trainControl(method="cv"))