0

I'm trying to bag conditional inference trees following the advice of Kuhn et al in 'Applied Predictive Modeling', Ch.8:

Conditional inference trees can also be bagged using the cforest function > in the party package if the argument mtry is equal to the number of predictors:

library(party)

The mtry parameter should be the number of predictors (the number of columns minus 1 for the outcome).

bagCtrl <- cforest_control(mtry = ncol(trainData) - 1)

baggedTree <- cforest(y ~ ., data = trainData, controls = bagCtrl)

Note there may be a typo in the above code (and also in the package's help file), as discussed here: R package 'partykit' unused argument in ctree_control

However when I try to replicate this code using a dataframe (and trainData in above code is also a dataframe) such that there is more than one independent/predictor variable, I'm getting an error though it works for just one independent variable:

Some dummy code for simulations:

library(party)
df = data.frame(y = runif(5000), x = runif(5000), z = runif(5000))
bagCtrl <- cforest_control(mtry = ncol(df) - 1)
baggedTree_cforest <- cforest(y ~ ., data = df, control = bagCtrl)

The error message is:

Error: $ operator not defined for this S4 class

Thanks for any help.

shanlodh
  • 1,015
  • 2
  • 11
  • 30
  • I would avoid using `data ` as a variable name, try doing the same example with `df` instead of `data`. – missuse Feb 26 '18 at 09:30
  • edited 'data' to 'df' – shanlodh Feb 26 '18 at 10:00
  • are you still having a problem after renaming `data` to `df`? – missuse Feb 26 '18 at 10:38
  • 1
    changing 'data' to 'df' retained the problem but what solved it was adding the party namespace explicitly to the function call, so party::cforest() instead of just cforest(). I've also got library(partykit) loaded in my actual program which too has a cforest() function and the error could be stemming from there though both functions are essentially the same – shanlodh Feb 26 '18 at 10:56
  • Since you have found the solution to your problem you have two options: add it as an answer if you trust it will help other users, or delete the question if you think this is a common namespace problem, – missuse Feb 26 '18 at 11:04

1 Answers1

0

As suggested, posting my comment from above as an answer as a general R 'trick' if something expected doesn't work and the program has several libraries loaded:

but what solved it was adding the party namespace explicitly to the function > call, so party::cforest() instead of just cforest(). I've also got library(partykit) loaded in my actual program which too has a cforest() function and the error could be stemming from there though both functions are > essentially the same

caret::train() is another example where this often pops up

shanlodh
  • 1,015
  • 2
  • 11
  • 30