The xgboost model is trained on a dataset with 73 features, but the “candidates_predict_sparse” matrix has only 10 features because it’s not in dummy form. To make this work, you need to add dummy variables to the “candidates_predict” matrix.
Here is how you can do it:
# arbitrary value to ensure model.matrix has a formula
candidates_predict$job_change <- 0
# create dummy matrix for job_change column
candidates_predict_dummied <- model.matrix(job_change ~ 0 + ., data = candidates_predict)
In the code above, model.matrix
is used to create a dummy matrix for the “job_change” column. The ~ 0 + .
part means that we want to include all interactions between features and the intercept (0). This will result in one more column than the number of unique values in the “job_change” column, which is necessary because xgboost expects the same number of features as the model used for training.
After you create this dummy matrix, you can use it to predict with your trained model:
# Now you have the same structure and you can use it to predict:
predict(xgb_model, candidates_predict_dummied)
This will give you a prediction array of the same length as the input data.
Last modified on 2024-06-14