Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    text
    copied!<p>It sounds like you might do well to just use the cost function (i.e. the one named <code>cost</code>) defined further down in the "Examples" section of <code>?cv.glm</code>. Quoting from that section:</p> <pre><code> # [...] Since the response is a binary variable an # appropriate cost function is cost &lt;- function(r, pi = 0) mean(abs(r-pi) &gt; 0.5) </code></pre> <p>This does essentially what you were trying to do with your example. Replacing your "no" and "yes" with <code>0</code> and <code>1</code>, lets say you have two vectors, <code>predict</code> and <code>response</code>. Then <code>cost()</code> is nicely designed to take them and return the mean classification rate:</p> <pre><code>## Simulate some reasonable data set.seed(1) predict &lt;- seq(0.1, 0.9, by=0.1) response &lt;- rbinom(n=length(predict), prob=predict, size=1) response # [1] 0 0 0 1 0 0 0 1 1 ## Demonstrate the function 'cost()' in action cost(response, predict) # [1] 0.3333333 ## Which is right, as 3/9 elements (4, 6, &amp; 7) are misclassified ## (assuming you use 0.5 as the cutoff for your predictions). </code></pre> <p>I'm guessing the trickiest bit of this will be just getting your mind fully wrapped around the idea of passing a function in as an argument. (At least that was for me, for the longest time, the hardest part of using the <strong>boot</strong> package, which requires that move in a fair number of places.)</p> <hr> <p><strong>Added on 2016-03-22:</strong></p> <p>The function <code>cost()</code>, given above is in my opinion unnecessarily obfuscated; the following alternative does exactly the same thing but in a more expressive way:</p> <pre><code>cost &lt;- function(r, pi = 0) { mean((pi &lt; 0.5) &amp; r==1 | (pi &gt; 0.5) &amp; r==0) } </code></pre>
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload