title: How can I apply class representation requirements to the nodes of an rpart model in R?
body: <p>Consider the following data:</p>
  <pre><code>dat&lt;- structure(list(Success = c(1L, 0L, 0L, 1L, 0L, 0L, 1L, 0L, 1L, 
  0L, 1L, 1L, 0L, 1L, 1L, 0L, 1L, 1L, 1L, 0L, 1L, 1L, 1L, 1L, 0L, 
  1L, 1L, 1L, 1L), Var1 = c(&quot;A&quot;, &quot;B&quot;, &quot;A&quot;, &quot;A&quot;, &quot;A&quot;, &quot;B&quot;, &quot;A&quot;, 
  &quot;A&quot;, &quot;A&quot;, &quot;B&quot;, &quot;A&quot;, &quot;B&quot;, &quot;B&quot;, &quot;B&quot;, &quot;A&quot;, &quot;B&quot;, &quot;A&quot;, &quot;B&quot;, &quot;B&quot;, &quot;B&quot;, 
  &quot;B&quot;, &quot;B&quot;, &quot;A&quot;, &quot;A&quot;, &quot;B&quot;, &quot;B&quot;, &quot;B&quot;, &quot;B&quot;, &quot;B&quot;), Score = 1:29), class = &quot;data.frame&quot;, row.names = 
  c(NA, 
  -29L))
  </code></pre>
  <p>In this data, &quot;Success&quot; is a binary flag, with 1 representing success and 0 representing failure.  Score is a numeric score on some measure we want to use to predict Success.  Var1 is a class variable, used to identify subjects belonging to a particular categorical grouping.</p>
  <p>I am interested in making a model with <code>rpart</code>, using the &quot;Score&quot; variable to predict success, <em>but with minimum and/or maximum class representations required.</em></p>
  <p>A tree in rpart is constructed simply enough with:</p>
  <pre><code>mod&lt;-rpart(Success~Score,data=dat)
  </code></pre>
  <p>In looking at the output of:</p>
  <pre><code>dat_out&lt;-cbind(dat,predict(mod))
  
  dat_out_sum&lt;-dat_out%&gt;%
    group_by(`predict(mod)`)%&gt;%
    count(Var1)
  </code></pre>
  <p>We see that 7 of the ten samples with the lower predicted value belong to the group with a Var1 value of &quot;A&quot;.</p>
  <p>Say, for our purposes, this proportion is unacceptable.  We would like the best tree possible, but with the additional requirement that a maximum of 60% of the records belonging to this lower node have a Var1 value of A.</p>
  <p>Is there an argument or way to modify <code>rpart()</code> code to find the best-fitting model that meets this parameter?</p>
  <p>I've scoured the documentation here: <a href="https://cran.r-project.org/web/packages/rpart/rpart.pdf" rel="nofollow noreferrer">https://cran.r-project.org/web/packages/rpart/rpart.pdf</a> and so far think I've got a few ideas how this might be able to happen, but haven't managed to get the coding correct.</p>
  <p><strong>Approach 1 - Manual identification of acceptable split points.</strong></p>
  <p>With a little manipulation, I can arrange my Score variable from smallest to largest, add columns that display the proportions of the different classes within Var1, and flag all places where a split could occur that meets the requirements on the proportions for Var1. Probably not the best code, but the code below adds a column with all acceptable split points flagged.</p>
  <pre><code>dat2&lt;-dat%&gt;%
    mutate(one=1)%&gt;%
    group_by(Var1)%&gt;%
    mutate(Members=cumsum(one))%&gt;%
    ungroup()%&gt;%
    mutate(Total_People=cumsum(one))%&gt;%
    mutate(Proportion=Members/Total_People)%&gt;%
    pivot_wider(names_from = c(Var1),values_from=c(Proportion))%&gt;%
    rowwise()%&gt;%
    mutate(A=ifelse(is.na(A),1-sum(A,B,na.rm=TRUE),A))%&gt;%
    mutate(B=ifelse(is.na(B),1-sum(A,B,na.rm=TRUE),B))%&gt;%
    mutate(PotentialSplit=ifelse(A&lt;.6,1,0))
  </code></pre>
  <p>What I'd need to do from here, and have yet to figure out, is to determine how to tell <code>rpart()</code> that only split points where PotentialSplit==1 are acceptable.</p>
  <p><strong>Approach 2 - Rework the minsplit argument in rpart.control</strong></p>
  <p>In building models with <code>rpart()</code>, we can specify a raw minimum number of observations with something like:</p>
  <pre><code>modelalt&lt;-rpart(Success~Score, data=dat, control=rpart.control(minsplit=5))
  </code></pre>
  <p>I hoped that I could add some qualifiers to minsplit.  <em>Note that the following code crashes my R session when I try running it</em>.  Here's my breaks-my-r-session attempt (without the #):</p>
  <pre><code>#modalt&lt;-rpart(Success~Score,data=dat,control=rpart.control(minsplit=5[dat$Var1==&quot;B&quot;]))
  </code></pre>
  <p>So far, I've had no success in exploring either way.  Ideally, I'm hoping for a solution that will allow for flexibility in the number of classes that appear in the constraining categorical variable, and also in the relationship of the predictor(s) and the target variable.  Any help would be greatly appreciated.</p>