Klausos Klausos
Klausos Klausos

Reputation: 16050

Error in eval(expr, envir, enclos) : object 'Param1' not found

I read train_data from csv and then train GBM model as follows:

train_rows <- sample(nrow(train_data), round(nrow(train_data) * 0.5))
traindf <- data[train_rows, ]
testdf <- data[-train_rows, ]

gbm_formula <- as.formula("traindf$myTarget ~ Param1 + Param2 + Param3")
gbm_model <- gbm(gbm_formula, 
                 traindf, 
                 distribution = "bernoulli", 
                 n.trees = 200, 
                 bag.fraction = 0.75, 
                 cv.folds = 5, 
                 interaction.depth = 3)

Then I get the following error appears:

Error in eval(expr, envir, enclos) : object 'Param1' not found

The only solution that I know is to specify gbm_formula as follows:

gbm_formula <- as.formula("traindf$myTarget ~ traindf$Param1 + traindf$Param2 + traindf$Param3")

Is there another solution to fix this?

Upvotes: 1

Views: 7197

Answers (2)

Metrics
Metrics

Reputation: 15458

This may work:

gbm_formula <- as.formula(paste0("myTarget~",paste0("Param",1:3,collapse="+")))
>gbm_formula
myTarget ~ Param1 + Param2 + Param3

Upvotes: 1

eipi10
eipi10

Reputation: 93811

Change traindf to data=traindf in your call to gbm. You need to name the argument, since the second-position argument in the function is actually distribution.

You can also change the formula to as.formula("myTarget ~ Param1 + Param2 + Param3") (though that wasn't the cause of the error).

Upvotes: 1

Related Questions