Reputation: 187
Is it possible to extract the variable importance using the randomForest
package from a cforest
using the partykit
package?
Here is some code, where I try to extract the variable importance of both a "normal" randomforest
and a cforest
:
# Load the required libraries
library(randomForest)
library(partykit)
# Set a random seed for reproducibility
set.seed(123)
# Generate synthetic data
n <- 100 # Number of data points
x1 <- rnorm(n) # Predictor 1
x2 <- rnorm(n) # Predictor 2
y <- ifelse(x1^2 + x2^2 > 1, 1, 0) # Response variable
# Create a data frame
data <- data.frame(x1 = x1, x2 = x2, y = y)
# Fit a Random Forest model
rf_model <- randomForest(y ~ x1 + x2, data = data , ntree = 100)
# Fit a Conditional Inference Random Forest (cforest) model
cforest_model <- partykit::cforest(y ~ x1 + x2, data = data, ntree = 500)
# Varimp for randomforest
myvarimp1 <- randomForest::importance(rf_model)
# Varimp for conditional randomforest
myvarimp2 <- randomForest::importance(cforest_model)
However when trying to use it on the cforest_model. I get this error:
Error in UseMethod("importance") : no applicable method for 'importance' applied to an object of class "c('cforest', 'constparties', 'parties')"
Is it even possible to do that?
Thanks
Upvotes: 2
Views: 267
Reputation: 8166
You can use the following code to calculate the variable importance for the conditional random forest model.
myvarimp2 = varimp(cforest_model, conditional = T)
dotchart(sort(myvarimp2), main = "Conditional Importance of Variables")
The varimp
function is available in partykit
package for the variable importance calculation of conditional random forest model following the permutation principle of the ‘mean decrease in accuracy’ importance in ‘randomForest’.
Upvotes: 2