BB.squared
BB.squared

Reputation: 113

R Caret Train function with variable X-variable

I am trying to run the caret::train function in a code with variable x values. see below for details

I am using the train function as is here below ↓

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

what I want to do is have the x variable (above would be GenCSTempMax ) be inputted from a character variable

example

out.x <- "Test"

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

I have tried to use

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

Also have tried to use the infuser package

this is the normal error I get:

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')

Upvotes: 2

Views: 564

Answers (1)

StupidWolf
StupidWolf

Reputation: 46898

You just need to recreate the formula, for example:

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"))

Upvotes: 1

Related Questions