[R] plot.randomForest default mtry values

Wensui Liu liuwensui at gmail.com
Thu Mar 15 18:35:14 CET 2007


Joe,
here is a piece of junk copied from my blog, showing how to use mtry and hth.
library(MASS);
library(randomForest);
data(Boston);

set.seed(2007);

# SEARCH FOR BEST VALUE OF MTRY FOR RANDOM FORESTS
mtry <- tuneRF(Boston[, -14], Boston[, 14], mtryStart = 1,
               stepFactor = 2, ntreeTry = 500, improve = 0.01);
best.m <- mtry[mtry[, 2] == min(mtry[, 2]), 1];

# FIT A RF MODEL
rf <- randomForest(medv~., data = Boston, mtry = best.m, ntree = 1000,
importance = TRUE);

# EXTRACT VARIABLE IMPORTANCE
imp.tmp <- importance(rf, type = 1);
rf.imp <- imp.tmp[order(imp.tmp[, 1], decreasing = TRUE),];
par(mar = c(3, 0, 4, 0));
barplot(rf.imp, col = gray(0:(ncol(Boston) - 1)/(ncol(Boston) - 1)),
        names.arg = names(rf.imp), yaxt = "n", cex.names = 1);
title(main = list("Importance Rank of Predictors", font = 4, cex = 1.5));

# PLOT PARTIAL DEPENDENCE OF EACH PREDICTOR
par(mfrow = c(3, 5), mar = c(2, 2, 2, 2), pty = "s");
for (i in 1:(ncol(Boston) - 1))
  {
    partialPlot(rf, Boston, names(Boston)[i], xlab = names(Boston)[i],
main = NULL);
  }

On 3/15/07, Joseph Retzer <joe_retzer at yahoo.com> wrote:
> When using the plot.randomForest method, 3 error series (by number of trees) are plotted. I suspect they are associated with the 3 default values of mtry that are used, for example, in the tuneRF method but I'm not sure. Could someone confirm?
>
> Also, is it possible to force different values of mtry to be used when creating the plots? I specified them explicitly in the randomForest statement but it did not seem to have an effect.
> Many thanks,
> Joe Retzer
>
>         [[alternative HTML version deleted]]
>
> ______________________________________________
> R-help at stat.math.ethz.ch 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.
>


-- 
WenSui Liu
A lousy statistician who happens to know a little programming
(http://spaces.msn.com/statcompute/blog)



More information about the R-help mailing list