Varimp() from party package produces different results with each run

Viewed 74

I am using the party package to create a random forest model using the cforest() function. However, when I try to look at the variable importance values of my model using the varimp function, the varimp function produces different values for my features. Is there any way of fixing this issue?

1 Answers

For any process using random values - such as creating a random forest model, or finding the variable importance using random permutation - slight variations from each run is to be expected.

If you want to "lock in" one of these values, e.g. to make your analysis reproducable, you can use set.seed(<any number>). Under is an example:

library(party)

# Create model
mod <- cforest(hp ~ ., mtcars)

# Without seed we get different values each time

varimp(mod)[1]

#>      mpg 
#> 498.8208


varimp(mod)[1]

#>      mpg 
#> 513.8174


# However, if we set the seed, we get the same result each time

set.seed(1)

varimp(mod)[1]

#>     mpg 
#> 464.639

set.seed(1)

varimp(mod)[1]

#>     mpg 
#> 464.639

Update

Note that set.seed() still gives different values with repeated calls to random functions, but in a predictable way. For example, if I call rnorm(1) twice, I get two different values. But with the same seed, I get the same two values each time I reset the seed.

set.seed(1)
rnorm(1)
#> [1] -0.6264538
rnorm(1)
#> [1] 0.1836433

set.seed(1)
rnorm(1)
#> [1] -0.6264538
rnorm(1)
#> [1] 0.1836433

You can reset the seed by calling set.seed() multiple times in your script, or by restarting your R session before you run your script (shortcut keys are CTRL + SHIFT + F10 for Windows).

Related