title: cforest varimp does not seem to work with categorical predictors
body: <p>I am trying to run a random forest model using the Party package. I would like to use the <code>varimp</code> function to determine conditional variable importance, however it does not seem to accept categorical variables. Here is a <a href="https://drive.google.com/file/d/0B-JMocik79JzVndKWUdLSy1oNW8/edit?usp=sharing" rel="nofollow">link</a> to my data and below is the code I am using. </p>
  
  <pre><code>&gt; #set up dataframe
  &gt; bll = read.csv("bll_Nov2013.csv", header=TRUE)
  &gt; SB_Pres &lt;- bll$Sandbar_Presence #binary presence/absnece
  &gt; Slope &lt;-bll$Slope
  &gt; Dist2Shr &lt;-bll$Dist2Shr
  &gt; Bathy &lt;-bll$Bathy2
  &gt; Chla &lt;-bll$GSM_Chl_Daily_MF
  &gt; SST &lt;-bll$SST_PF_daily
  &gt; Region &lt;- bll$Region
  &gt; MoonPhase &lt;-bll$MoonPhase
  &gt; DaylightHours &lt;- bll$DaylightHours
  &gt; bll_SB &lt;- na.omit(data.frame(SB_Pres, Slope, Dist2Shr, Bathy, Chla, SST, DaylightHours, MoonPhase, Region))
  
  &gt; #run cforest model
  &gt; SBcf&lt;- cforest(formula = factor(SB_Pres) ~ SST + Chla + Dist2Shr+ DaylightHours + Bathy + Slope + MoonPhase + factor(Region), data = bll_SB,  control = cforest_unbiased())
  &gt; SBcf
  
       Random Forest using Conditional Inference Trees
  
  Number of trees:  500 
  
  Response:  factor(SB_Pres) 
  Inputs:  SST, Chla, Dist2Shr, DaylightHours, Bathy, Slope, MoonPhase, factor(Region) 
  Number of observations:  534 
  
  &gt; #Varimp works if conditional = FALSE
  &gt; varimp(SBcf, conditional = FALSE)
             SST           Chla       Dist2Shr  DaylightHours          Bathy          Slope 
     0.024744898    0.084244898    0.015632653    0.009571429    0.006448980    0.003357143 
       MoonPhase factor(Region) 
     0.002724490    0.095000000 
  
  
  &gt; #Varimp does NOT work if conditional = TRU
  &gt; varimp(SBcf, conditional = TRUE)
  Error in model.frame.default(formula = ~SST + Chla + Dist2Shr + DaylightHours +  : 
    variable lengths differ (found for 'factor(Region)')
  </code></pre>
  
  <p>If I drop the <code>factor(Region)</code> variable then conditional variable importance can be calculated. </p>
  
  <p>Is this a known behavior of the party package <code>varimp</code> function with categorical predictors? From what I've read it should be able to handle categorical predictors (<a href="http://journal.r-project.org/archive/2009-2/RJournal_2009-2_Strobl~et~al.pdf" rel="nofollow">Conditional variable importance for random forests - Strobl et al</a>) - it does not explicitly say that <code>varimp(obj, conditional = TRUE)</code> can be used with categorical predictors. </p>
  
  <p>Any insight would be greatly appreciated!</p>
  
  <p>Thanks, </p>
  
  <p>Liza</p>
  
  <p>EDIT: Illustrating that if you define the variable using as.factor outside of the formula, the as.factor does not actually take effect - results are the same whether region is specified as a factor or not. Compare these results to the other varimp (conditional = false) run above, where the output shows the variable as "factor(Region)", whereas below it just shows up as "Region" in both runs. </p>
  
  <pre><code>&gt; library("party")
  &gt; packageDescription("party")$Version
  [1] "1.0-10"
  &gt; bll = read.csv("bll_SB.csv", header=TRUE)
  &gt; bll_SB &lt;- na.omit(data.frame(bll))
  
  &gt; # region is specified as a factor
  &gt; bll_SB$SB_Pres &lt;- factor(bll_SB$SB_Pres)
  &gt; bll_SB$Region &lt;- factor(bll_SB$Region)
  &gt; set.seed(1)
  &gt; SBcf &lt;- cforest(SB_Pres ~ ., data=bll_SB,  control=cforest_unbiased())
  &gt; SBcf
  
  
       Random Forest using Conditional Inference Trees
  
  Number of trees:  500 
  
  Response:  SB_Pres 
  Inputs:  Slope, Dist2Shr, Bathy, Chla, SST, DaylightHours, MoonPhase, Region 
  Number of observations:  534 
  
  &gt; system.time(res1 &lt;- varimp(SBcf, conditional = FALSE))
     user  system elapsed 
    4.466   0.013   4.480 
  &gt; res1
          Slope      Dist2Shr         Bathy          Chla           SST DaylightHours 
    0.003632653   0.015908163   0.008285714   0.085367347   0.028846939   0.009520408 
      MoonPhase        Region 
    0.002969388   0.093061224 
  
  
  &gt; # Run again, region is not specified as a factor
  &gt; bll_SB$Region &lt;- bll_SB$Region
  &gt; set.seed(1)
  &gt; SBcf &lt;- cforest(SB_Pres ~ ., data=bll_SB,  control=cforest_unbiased())
  &gt; system.time(res2 &lt;- varimp(SBcf, conditional = FALSE))
     user  system elapsed 
    4.562   0.015   4.578 
  &gt; res2
          Slope      Dist2Shr         Bathy          Chla           SST DaylightHours 
    0.003632653   0.015908163   0.008285714   0.085367347   0.028846939   0.009520408 
      MoonPhase        Region 
    0.002969388   0.093061224 
  </code></pre>