title: guidelines for testing a statistical function in R?
body: <p><strong>Question:</strong> I am testing functions in a package that I am developing and would like to know if you can suggest some general guidelines for how to do this. The functions include a large range of statistical modeling, transformations, subsetting, and plotting. Is there a 'standard' or some sufficient test?</p>
  
  <p><strong>An Example:</strong> the test that prompted me ask this question, </p>
  
  <p>The function dtheta:</p>
  
  <pre><code>dtheta &lt;- function(x) {
    ## find the quantile of the mean
    q.mean &lt;- mean(mean(x) &gt;= x)
    ## find the quantiles of ucl and lcl (q.mean +/- 0.15)
    q.ucl  &lt;- q.mean + 0.15
    q.lcl  &lt;- q.mean - 0.15
    qs &lt;- c(q.lcl, q.mean, q.ucl)
    ## find the lcl, mean, and ucl of the vector
    c(quantile(x,qs), var(x), sqrt(var(x))/mean(x))
  }
  </code></pre>
  
  <p>Step 1: make test data:</p>
  
  <pre><code>set.seed(100) # per Dirk's recommendation
  test &lt;- rnorm(100000,10,1)
  </code></pre>
  
  <p>Step 2: compare the expected output from the function with the actual output from the function:</p>
  
  <pre><code> expected &lt;- quantile(test, c(0.35, 0.65, 0.5))
   actual   &lt;- dtheta(test)[1:3]
   signif(expected,2) %in% signif(actual,2)
  </code></pre>
  
  <p>Step 3: maybe do another test</p>
  
  <pre><code>test2 &lt;- runif(100000, 0, 100)
  expected &lt;- c(35, 50, 65)
  actual   &lt;- dtheta(test2)
  expected %in% signif(actual,2)
  </code></pre>
  
  <p>Step 4: if true, consider function 'functional'</p>