Reputation: 31
I've been running different algorithms to predict performance of Facebook posts based on several other parameters. The last method I'm trying out is XG-Boost.
I'm continuously getting an error even after re-checking my code and documentation of the package. Both my train and test data have been cleaned and all the factors have been converted into columns with 1s and 0s.
// Treating test and train data
temp.treat <- prepare(treatplan,temp, varRestriction = newvars)
test.treat <- prepare(treatplan,test, varRestriction = newvars)
//Training the model
cv <- xgb.cv(data = as.matrix(temp.treat),
label = temp$Reach,
objective = "reg:linear",
nrounds = 400, nfold = 5, eta = 0.3, depth = 6)
//Getting predictions
test$pred <- predict(cv, as.matrix(test.treat))
The data trains without throwing an error, but the moment I run the predict command, I get the error -
Error in UseMethod("predict") : no applicable method for 'predict' applied to an object of class "xgb.cv.synchronous"
Can anyone tell me what am I doing wrong?
Upvotes: 1
Views: 2527
Reputation: 60
As JMichaelJ suggests, here's what's happening:
You are using the xgb.cv()
function in the xgboost
package. The xgb.cv()
function runs preliminary modeling to help you determine information such as the appropriate number of nrounds
(trees) to specify. This can be extracted after calling xgb.cv()
cv <- xgb.cv(data = as.matrix(temp.treat),
label = temp$Reach,
objective = "reg:linear",
nrounds = 400, nfold = 5, eta = 0.3, depth = 6)
elog <- as.data.frame(cv$evaluation_log)
(nrounds <- which.min(elog$test_rmse_mean)) #this will print the number of trees you need
It's important to keep in mind that this step has so far only helped you determine the number of trees (nrounds) you need to specify for your model. Now you need to actually build it using xgboost()
:
nrounds <- #Whatever number it told you above
model <- xgboost(data = as.matrix(data.treat),
label = data$outcome,
nrounds = nrounds, # this is where the xgb.cv() results matter
objective = "reg:linear", #or whatever type you need
eta = 0.3,
depth = 6)
Assuming you have already built the treatment plan for your test dataset, you now can use predict()
based on the output you stored in model
above:
test$predictions <- predict(model, as.matrix(test.treat))
It looks like maybe you are using example code from the Data Camp course. That's what I'm using. I had the same problem. Hopefully this solution works for you, too.
Upvotes: 1
Reputation: 1
You should use the function
xgboost()
in the packages.
xgb.cv()
can only help you get the $evaluation table to information about
Upvotes: 0