title: Stratified sampling with Random Forests in R
body: <p>I read the following in the documentation of <code>randomForest</code>:</p>
  
  <blockquote>
    <p>strata: A (factor) variable that is used for stratified sampling.</p>
    
    <p>sampsize: Size(s) of sample to draw.  For classification, if sampsize
              is a vector of the length the number of strata, then sampling
              is stratified by strata, and the elements of sampsize
              indicate the numbers to be drawn from the strata.</p>
  </blockquote>
  
  <p>For reference, the interface to the function is given by:</p>
  
  <pre><code> randomForest(x, y=NULL,  xtest=NULL, ytest=NULL, ntree=500,
                mtry=if (!is.null(y) &amp;&amp; !is.factor(y))
                max(floor(ncol(x)/3), 1) else floor(sqrt(ncol(x))),
                replace=TRUE, classwt=NULL, cutoff, strata,
                sampsize = if (replace) nrow(x) else ceiling(.632*nrow(x)),
                nodesize = if (!is.null(y) &amp;&amp; !is.factor(y)) 5 else 1,
                maxnodes = NULL,
                importance=FALSE, localImp=FALSE, nPerm=1,
                proximity, oob.prox=proximity,
                norm.votes=TRUE, do.trace=FALSE,
                keep.forest=!is.null(y) &amp;&amp; is.null(xtest), corr.bias=FALSE,
                keep.inbag=FALSE, ...)
  </code></pre>
  
  <p>My question is: How exactly would one use <code>strata</code>  and <code>sampsize</code>? Here is a minimal working example where I would like to test these parameters:</p>
  
  <pre><code>library(randomForest)
  iris = read.table("http://archive.ics.uci.edu/ml/machine-learning-databases/iris/iris.data", sep = ",", header = FALSE)
  names(iris) = c("sepal.length", "sepal.width", "petal.length", "petal.width", "iris.type")
  
  model = randomForest(iris.type ~ sepal.length + sepal.width, data = iris)
  
  &gt; model
  500 samples
    6 predictors
    2 classes: 'Y0', 'Y1' 
  
  No pre-processing
  Resampling: Bootstrap (7 reps) 
  
  Summary of sample sizes: 477, 477, 477, 477, 477, 477, ... 
  
  Resampling results across tuning parameters:
  
    mtry  ROC    Sens  Spec  ROC SD  Sens SD  Spec SD
    2     0.763  1     0     0.156   0        0      
    4     0.782  1     0     0.231   0        0      
    6     0.847  1     0     0.173   0        0      
  
  ROC was used to select the optimal model using  the largest value.
  The final value used for the model was mtry = 6.
  </code></pre>
  
  <p>I come to these parameters since I would like RF to use bootstrap samples that respect the proportion of positives to negatives in my data.</p>
  
  <p><a href="https://stackoverflow.com/questions/8704681/random-forest-with-classes-that-are-very-unbalanced">This other thread</a>, started a discussion on the topic, but it was settled without clarifying how one would use these parameters.</p>