Describe the bug
I'm getting an error message when I use the plotResponse() function to plot the marginal effect of a categorical predictor for a SDMmodelCV object (RF-class). I do not receive the same error message for models of class ANN or BRT trained on the same CV folds.
Expected behavior
I expect the function to produce a bar chart with the predicted probability of occurrence for each level of the categorical predictor +/- 1 SD. Instead I receive the error message:
Error in predict.randomForest(object@model, data, type = "prob") :
Type of predictors in new data do not match that of the training data.
Additional context
I gather this is an error message generated by the randomForest package when the factor levels in the new testing data do not match those found in the data used to train the model. I am using v. 4.6-14 of randomForest.
I think the error stems from the definition of the category levels in the plotResponse() function (L95 of the source code):
Converting the unique levels to class numeric results in a mismatch between the SWD_obj@data in which the categorical variable is class factor and the new object 'data' generated by the .get_plot_data() function (L170-182) that will be used to plot the predicted response. Specifically, this will cause the error described above when L191 is executed:
Describe the bug I'm getting an error message when I use the plotResponse() function to plot the marginal effect of a categorical predictor for a SDMmodelCV object (RF-class). I do not receive the same error message for models of class ANN or BRT trained on the same CV folds.
Expected behavior I expect the function to produce a bar chart with the predicted probability of occurrence for each level of the categorical predictor +/- 1 SD. Instead I receive the error message:
Error in predict.randomForest(object@model, data, type = "prob") : Type of predictors in new data do not match that of the training data.
Additional context I gather this is an error message generated by the randomForest package when the factor levels in the new testing data do not match those found in the data used to train the model. I am using v. 4.6-14 of randomForest.
I think the error stems from the definition of the category levels in the plotResponse() function (L95 of the source code):
categ <- unique(as.numeric(levels(df[, var]))[df[, var]])
Converting the unique levels to class numeric results in a mismatch between the SWD_obj@data in which the categorical variable is class factor and the new object 'data' generated by the .get_plot_data() function (L170-182) that will be used to plot the predicted response. Specifically, this will cause the error described above when L191 is executed:
pred <- predict(model, data, type = type)