title: R: Determine the threshold that maximally separates two groups based on a continuous variable?
body: <p>Say I have 200 subjects, 100 in group A and 100 in group B, and for each I measure some continuous parameter.</p>
  
  <pre><code>require(ggplot2)
  set.seed(100)
  
  value &lt;- c(rnorm(100, mean = 5, sd = 3), rnorm(100, mean = 10, sd = 3))
  group &lt;- c(rep('A', 100), rep('B', 100))
  
  data &lt;- data.frame(value, group)
  
  ggplot(data = data, aes(x = value)) +
    geom_bar(aes(color = group))
  </code></pre>
  
  <p>I would like to determine the value (Threshold?  Breakpoint?) that maximizes separation and minimizes misclassification between the groups.  Does such a function exist in R?</p>
  
  <p>I've tried searching along the lines of "r breakpoint maximal separation between groups," and "r threshold minimize misclassification," but my google-foo seems to be off today.</p>
  
  <p><strong>EDIT:</strong></p>
  
  <p>Responding to @Thomas's comment, I have tried to fit the data using logistic regression and then solve for the threshold, but I haven't gotten very far.</p>
  
  <pre><code>lr &lt;- glm(group~value)
  coef(lr)
  # (Intercept)       value 
  # 1.1857435  -0.0911762
  </code></pre>
  
  <p>So Bo = 1.1857435 and B1 = -0.0911762</p>
  
  <p>From <a href="http://en.wikipedia.org/wiki/Logistic_regression" rel="nofollow">Wikipedia</a>, I see that F(x) = 1/(1+e^-(Bo + B1x)), and solving for x:</p>
  
  <p>x = (ln(F(x) / (1 - F(x))) - Bo)/B1</p>
  
  <p>But trying this in R, I get an obviously incorrect answer:</p>
  
  <pre><code>(log(0.5/(1 - 0.5)) - 1.1857435)/-0.0911762 # 13.00497
  </code></pre>