I am trying to run a partialPlot with Random Forest (as I have done many times 
before).

First I run my forest... Cell is a 6 level factor that is the dependent 
variable - all other variables are predictors, most of these are factors as 
well.

predCell<-randomForest(x=tempdata[-match("Cell",names(tempdata))],y=tempdata$Cell,importance=T)

Then I try my partial plot to look at the effect of a specific predictor.

partialPlot(x=predCell,pred.data=tempdata[-match("Cell",names(tempdata))],x.var="P7_6")

I get this error:

Error in predict.randomForest(x, x.data, type = "prob") :
  Type of predictors in new data do not match that of the training data.

In examining randomForest:::predict.randomForest I see the following code that 
produces this error message.

        cat.new <- sapply(x, function(x) if (is.factor(x) &&
            !is.ordered(x))
            length(levels(x))
        else 1)
        if (!all(object$forest$ncat == cat.new))
            stop("Type of predictors in new data do not match that of the 
training data.")
    }


The odd thing is that if I run this code outside of the function:

> all(predCell$forest$ncat==
+ sapply(tempdata[-match("Cell",names(tempdata))], function(x) if (is.factor(x) 
&&
+             !is.ordered(x))
+             length(levels(x))
+         else 1))
[1] TRUE

Which should avoid the "stop" function.

Here is the session info.

R version 2.8.1 (2008-12-22)
i386-pc-mingw32

locale:
LC_COLLATE=English_United States.1252;LC_CTYPE=English_United 
States.1252;LC_MONETARY=English_United 
States.1252;LC_NUMERIC=C;LC_TIME=English_United States.1252

attached base packages:
[1] stats     graphics  grDevices utils     datasets  methods   base

other attached packages:
[1] randomForest_4.5-30
>

Any ideas would be greatly appreciated.

W. Michael Conklin
Chief Methodologist

MarketTools, Inc. | www.markettools.com<http://www.markettools.com>
6465 Wayzata Blvd | Suite 170 |  St. Louis Park, MN 55426.  PHONE: 952.417.4719 
| CELL: 612.201.8978
This email and attachment(s) may contain confidential and/or proprietary 
information and is intended only for the intended addressee(s) or its 
authorized agent(s). Any disclosure, printing, copying or use of such 
information is strictly prohibited. If this email and/or attachment(s) were 
received in error, please immediately notify the sender and delete all copies


        [[alternative HTML version deleted]]

______________________________________________
R-help@r-project.org mailing list
https://stat.ethz.ch/mailman/listinfo/r-help
PLEASE do read the posting guide http://www.R-project.org/posting-guide.html
and provide commented, minimal, self-contained, reproducible code.

Reply via email to