<?xml version="1.0" encoding="utf-8"?>
<feed xmlns="http://www.w3.org/2005/Atom">
 
 <title>Statistics, plain and sample.</title>
 <link href="http://gjkerns.github.com/atom.xml" rel="self"/>
 <link href="http://gjkerns.github.com/"/>
 <updated>2012-01-22T22:44:06-08:00</updated>
 <id>http://gjkerns.github.com/</id>
 <author>
   <name>G. Jay Kerns</name>
   <email>gkerns@ysu.edu</email>
 </author>

 
 <entry>
   <title>Power and Sample Size for Repeated Measures ANOVA with R</title>
   <link href="http://gjkerns.github.com/R/2012/01/20/power-sample-size.html"/>
   <updated>2012-01-20T00:00:00-08:00</updated>
   <id>http://gjkerns.github.com/R/2012/01/20/power-sample-size</id>
   <content type="html">&lt;div id=&quot;outline-container-1&quot; class=&quot;outline-2&quot;&gt;
&lt;h2 id=&quot;sec-1&quot;&gt;Background&lt;/h2&gt;
&lt;div class=&quot;outline-text-2&quot; id=&quot;text-1&quot;&gt;

&lt;p&gt;One of my colleagues is an academic physical therapist (PT), and he's working on a paper to his colleagues related to power, sample size, and navigating the thicket of trouble that surrounds those two things.  We recently got together to walk through some of the issues, and I thought I would share some of the wildlife we observed along the way.  If you just want the code and don't care about the harangue, see &lt;a href=&quot;https://gist.github.com/1608265&quot;&gt;this gist on GitHub&lt;/a&gt;. 
&lt;/p&gt;
&lt;/div&gt;

&lt;/div&gt;

&lt;div id=&quot;outline-container-2&quot; class=&quot;outline-2&quot;&gt;
&lt;h2 id=&quot;sec-2&quot;&gt;The problem&lt;/h2&gt;
&lt;div class=&quot;outline-text-2&quot; id=&quot;text-2&quot;&gt;

&lt;p&gt;Suppose you are a PT, and you've come up with a brand new exercise method that you think will decrease neck pain, say. How can you demonstrate that your method is effective?  Of course, you collect data and show that people using your method have significantly lower neck pain than those from a control group. 
&lt;/p&gt;
&lt;p&gt;
The standard approach in the PT literature to analyze said data is repeated measures ANOVA. (Yes, those guys should really be using mixed-effects models, but those haven't quite taken off yet.) There are two groups: the &quot;Treatment&quot; group does your new exercise method, and a &quot;Sham&quot; group does nothing (or just the placebo exercise method).  For each Subject, you measure their pain at time 0, 15 minutes, 48 hours, and 96 hours.  Pain is measured by an index (there are several); the one we're using is something called NDI, which stands for &quot;Neck Disability Index&quot;.  The index ranges from 0 to 100 (more on this later).  There is some brief information about the index &lt;a href=&quot;http://www.chiro.org/LINKS/OUTCOME/Painter_1.shtml&quot;&gt;here&lt;/a&gt;.
&lt;/p&gt;
&lt;p&gt;
Now comes the question: how many people should you recruit for your study?  The answer is: it depends.  &quot;On what?&quot;  Well, it depends on how good the statistical test is, and how good your method is, but more to the point, it depends on &lt;i&gt;the effect size&lt;/i&gt;, that is, how far apart the two groups are, given that the method actually works.
&lt;/p&gt;
&lt;p&gt;
I encounter some variant of this question a lot.  I used to go look for research papers where somebody's worked out the &lt;i&gt;F&lt;/i&gt;-test and sample sizes required, and pore over tables and tables.  Then I resorted to online calculators (the proprietary versions were too expensive for my department!), which are fine, but they all use different notation and it takes a lot of time poring through documentation (which is often poor, pardon the pun) to recall how it works.  And I was never really sure whether I'd got it right, or if I had screwed up with a parameter somewhere.
&lt;/p&gt;
&lt;p&gt;
Some of the calculators advertise &lt;i&gt;Cohen's effect sizes&lt;/i&gt;, which are usually stated something like &quot;small&quot;, &quot;medium&quot;, and &quot;large&quot;, with accompanying numerical values.  &lt;a href=&quot;http://www.stat.uiowa.edu/~rlenth/Power/&quot;&gt;Russell Lenth&lt;/a&gt; calls these &quot;T-shirt effect sizes&quot;.  I agree with him.
&lt;/p&gt;
&lt;p&gt;
Nowadays the fashionable people say, &quot;Just run a simulation and estimate the power,&quot;  but the available materials online are scantly detailed.  So my buddy and I worked it all out from start to finish for this simple example, in the hopes that by sharing this information people can get a better idea of how to do it the &lt;b&gt;right&lt;/b&gt; way, the &lt;b&gt;first&lt;/b&gt; time.
&lt;/p&gt;
&lt;/div&gt;

&lt;/div&gt;

&lt;div id=&quot;outline-container-3&quot; class=&quot;outline-2&quot;&gt;
&lt;h2 id=&quot;sec-3&quot;&gt;How to attack it&lt;/h2&gt;
&lt;div class=&quot;outline-text-2&quot; id=&quot;text-3&quot;&gt;


&lt;p&gt;
&lt;b&gt;The avenue of attack is simple:&lt;/b&gt; for a given sample size,
&lt;/p&gt;&lt;ol&gt;
&lt;li&gt;use prior research and practitioner experience to decide what difference would be &quot;meaningful&quot; to detect,
&lt;/li&gt;
&lt;li&gt;simulate data consistent with the above difference and run the desired statistical test to see whether or not it rejected, and
&lt;/li&gt;
&lt;li&gt;repeat step 2 hundreds of times.  An estimate of the power (for that sample size) is the proportion of times that the test rejected.
&lt;/li&gt;
&lt;/ol&gt;


&lt;p&gt;
If the power isn't high enough, then increase the given sample size and start over.  The value we get is just an &lt;i&gt;estimate&lt;/i&gt; of the power, but we can increase the precision of our estimate by increasing the number of repetitions in step 3.
&lt;/p&gt;
&lt;p&gt;
What you find when you start down this path is that there is a &lt;b&gt;lot&lt;/b&gt; of information required to be able to answer the question.  Of course, this information had been hiding behind the scenes all along, even with those old research papers and online calculators, but the other methods make it easy to gloss over the details, or they're so complicated that researchers will give up and fall back to something like Cohen's T-shirt effect sizes.
&lt;/p&gt;
&lt;/div&gt;

&lt;/div&gt;

&lt;div id=&quot;outline-container-4&quot; class=&quot;outline-2&quot;&gt;
&lt;h2 id=&quot;sec-4&quot;&gt;Now for the legwork&lt;/h2&gt;
&lt;div class=&quot;outline-text-2&quot; id=&quot;text-4&quot;&gt;


&lt;p&gt;
The details we need include: A) prior knowledge of how average pain decreases for people in the &lt;code&gt;Sham&lt;/code&gt; group, B) some idea about the variability of scores, C) how scores would be correlated with one another over time, and D) how much better the &lt;code&gt;Treat&lt;/code&gt; group would need to be in order for the new procedure to be considered clinically meaningful.
&lt;/p&gt;
&lt;p&gt;
As a first step, the PT sat down and filled in the following table.
&lt;/p&gt;
&lt;table border=&quot;2&quot; cellspacing=&quot;0&quot; cellpadding=&quot;6&quot; rules=&quot;groups&quot; frame=&quot;hsides&quot;&gt;
&lt;caption&gt;&lt;/caption&gt;
&lt;colgroup&gt;&lt;col class=&quot;left&quot; /&gt;&lt;col class=&quot;right&quot; /&gt;&lt;col class=&quot;right&quot; /&gt;&lt;col class=&quot;right&quot; /&gt;&lt;col class=&quot;right&quot; /&gt;
&lt;/colgroup&gt;
&lt;tbody&gt;
&lt;tr&gt;&lt;td class=&quot;left&quot;&gt;&lt;/td&gt;&lt;td class=&quot;right&quot;&gt;0 hrs&lt;/td&gt;&lt;td class=&quot;right&quot;&gt;15 min&lt;/td&gt;&lt;td class=&quot;right&quot;&gt;48 hrs&lt;/td&gt;&lt;td class=&quot;right&quot;&gt;96 hrs&lt;/td&gt;&lt;/tr&gt;
&lt;tr&gt;&lt;td class=&quot;left&quot;&gt;Treat&lt;/td&gt;&lt;td class=&quot;right&quot;&gt;37&lt;/td&gt;&lt;td class=&quot;right&quot;&gt;32&lt;/td&gt;&lt;td class=&quot;right&quot;&gt;20&lt;/td&gt;&lt;td class=&quot;right&quot;&gt;15&lt;/td&gt;&lt;/tr&gt;
&lt;tr&gt;&lt;td class=&quot;left&quot;&gt;Sham&lt;/td&gt;&lt;td class=&quot;right&quot;&gt;37&lt;/td&gt;&lt;td class=&quot;right&quot;&gt;32&lt;/td&gt;&lt;td class=&quot;right&quot;&gt;25&lt;/td&gt;&lt;td class=&quot;right&quot;&gt;22&lt;/td&gt;&lt;/tr&gt;
&lt;/tbody&gt;
&lt;/table&gt;


&lt;p&gt;
All of the entries in the above table represent population mean NDI scores for people in the respective groups at the respective measurement times, and were filled in based on prior research and educated guesses by the PT.  It was known from other studies that NDI scores have a standard deviation of around 12, and those have been observed to decrease over time.
&lt;/p&gt;
&lt;p&gt;
&lt;b&gt;Note:&lt;/b&gt; we could have assumed a simpler model for the means.  For example, we could have assumed that mean NDI was linear, with possibly different slopes/intercepts for the Treat/Sham groups.  Prior info available to the PT said that such an assumption wasn't reasonable for this example.
&lt;/p&gt;
&lt;p&gt;
Repeated measures designs assume sphericity for the exact &lt;i&gt;F&lt;/i&gt; tests to hold, so we need to specify a variance for the differences, \(\mathrm{Var}(X_{i} - X_{j})\), and sphericity says this variance should be the same for all time points \(i\) and \(j\).  As it turns out, this last choice implicitly determines all of the remaining covariance structure. We set this standard deviation to \(9\).
&lt;/p&gt;
&lt;/div&gt;

&lt;/div&gt;

&lt;div id=&quot;outline-container-5&quot; class=&quot;outline-2&quot;&gt;
&lt;h2 id=&quot;sec-5&quot;&gt;Finally we do some coding&lt;/h2&gt;
&lt;div class=&quot;outline-text-2&quot; id=&quot;text-5&quot;&gt;


&lt;p&gt;
We are now ready to turn on the computer.  We first intialize the parameters we'll need, next we set up the independent variable data, then we do the simulation, and finally we rinse-and-repeat.  Let's go.
&lt;/p&gt;



&lt;pre class=&quot;src src-R&quot;&gt;set.seed(1)
nPerGroup &lt;span style=&quot;color: #008b8b;&quot;&gt;&amp;lt;-&lt;/span&gt; 10
nTime     &lt;span style=&quot;color: #008b8b;&quot;&gt;&amp;lt;-&lt;/span&gt; 4
muTreat   &lt;span style=&quot;color: #008b8b;&quot;&gt;&amp;lt;-&lt;/span&gt; c(37, 32, 20, 15)
muSham    &lt;span style=&quot;color: #008b8b;&quot;&gt;&amp;lt;-&lt;/span&gt; c(37, 32, 25, 22)
stdevs    &lt;span style=&quot;color: #008b8b;&quot;&gt;&amp;lt;-&lt;/span&gt; c(12, 10, 8, 6)
stdiff    &lt;span style=&quot;color: #008b8b;&quot;&gt;&amp;lt;-&lt;/span&gt; 9
nSim      &lt;span style=&quot;color: #008b8b;&quot;&gt;&amp;lt;-&lt;/span&gt; 500
&lt;/pre&gt;


&lt;p&gt;
All of the above should be self-explanatory. Next comes setting up the data - creatively named &lt;code&gt;theData&lt;/code&gt; - for the independent variables.  Just for the sake of argument I used code to generate the data frame, but we wouldn't have had to.  We could have imported an external text file had we wished.
&lt;/p&gt;



&lt;pre class=&quot;src src-R&quot;&gt;Subject &lt;span style=&quot;color: #008b8b;&quot;&gt;&amp;lt;-&lt;/span&gt; factor(1:(nPerGroup*2))
Time &lt;span style=&quot;color: #008b8b;&quot;&gt;&amp;lt;-&lt;/span&gt; factor(1:nTime, labels = c(&lt;span style=&quot;color: #8b2252;&quot;&gt;&quot;0min&quot;&lt;/span&gt;, &lt;span style=&quot;color: #8b2252;&quot;&gt;&quot;15min&quot;&lt;/span&gt;, &lt;span style=&quot;color: #8b2252;&quot;&gt;&quot;48hrs&quot;&lt;/span&gt;, &lt;span style=&quot;color: #8b2252;&quot;&gt;&quot;96hrs&quot;&lt;/span&gt;))

theData &lt;span style=&quot;color: #008b8b;&quot;&gt;&amp;lt;-&lt;/span&gt; expand.grid(Time, Subject)
names(theData) &lt;span style=&quot;color: #008b8b;&quot;&gt;&amp;lt;-&lt;/span&gt; c(&lt;span style=&quot;color: #8b2252;&quot;&gt;&quot;Time&quot;&lt;/span&gt;, &lt;span style=&quot;color: #8b2252;&quot;&gt;&quot;Subject&quot;&lt;/span&gt;)

tmp &lt;span style=&quot;color: #008b8b;&quot;&gt;&amp;lt;-&lt;/span&gt; rep(c(&lt;span style=&quot;color: #8b2252;&quot;&gt;&quot;Treat&quot;&lt;/span&gt;, &lt;span style=&quot;color: #8b2252;&quot;&gt;&quot;Sham&quot;&lt;/span&gt;), each = nPerGroup * nTime)
theData$Method &lt;span style=&quot;color: #008b8b;&quot;&gt;&amp;lt;-&lt;/span&gt; factor(tmp)
&lt;/pre&gt;


&lt;p&gt;
Again, the above should be self-explanatory for the most part.  The data are in &quot;long&quot; form, where each subject appears over multiple rows.  In fact, let's take a look at the data frame to make sure it looks right.
&lt;/p&gt;



&lt;pre class=&quot;src src-R&quot;&gt;head(theData)
&lt;/pre&gt;


&lt;pre class=&quot;example&quot;&gt;
   Time Subject Method
1  0min       1  Treat
2 15min       1  Treat
3 48hrs       1  Treat
4 96hrs       1  Treat
5  0min       2  Treat
6 15min       2  Treat
&lt;/pre&gt;


&lt;p&gt;
Lookin' good.  Now for the fun part. We generate the single remaining column, the NDI scores.  The repeated measures model is multivariate normal.  The population covariance matrix is a little bit tricky, but it's not too bad and to make things easy we'll assume both groups have the same covariance.  See &lt;a href=&quot;http://www.jstor.org/stable/2284340&quot;&gt;the original paper by Huynh and Feldt&lt;/a&gt; for details.
&lt;/p&gt;



&lt;pre class=&quot;src src-R&quot;&gt;&lt;span style=&quot;color: #b22222;&quot;&gt;# &lt;/span&gt;&lt;span style=&quot;color: #b22222;&quot;&gt;to set up variance-covariance matrix&lt;/span&gt;
ones &lt;span style=&quot;color: #008b8b;&quot;&gt;&amp;lt;-&lt;/span&gt; rep(1, nTime)
A &lt;span style=&quot;color: #008b8b;&quot;&gt;&amp;lt;-&lt;/span&gt; stdevs^2 %o% ones
B &lt;span style=&quot;color: #008b8b;&quot;&gt;&amp;lt;-&lt;/span&gt; (A + t(A) + (stdiff^2)*(diag(nTime) - ones %o% ones))/2
&lt;/pre&gt;


&lt;p&gt;
We simulate with the &lt;code&gt;mvrnorm&lt;/code&gt; function from the &lt;code&gt;MASS&lt;/code&gt; package.
&lt;/p&gt;



&lt;pre class=&quot;src src-R&quot;&gt;&lt;span style=&quot;color: #008b8b;&quot;&gt;library&lt;/span&gt;(MASS)
tmp1 &lt;span style=&quot;color: #008b8b;&quot;&gt;&amp;lt;-&lt;/span&gt; mvrnorm(nPerGroup, mu = muTreat, Sigma = B)
tmp2 &lt;span style=&quot;color: #008b8b;&quot;&gt;&amp;lt;-&lt;/span&gt; mvrnorm(nPerGroup, mu = muSham, Sigma = B)
theData$NDI &lt;span style=&quot;color: #008b8b;&quot;&gt;&amp;lt;-&lt;/span&gt; c(as.vector(t(tmp1)), as.vector(t(tmp2)))
&lt;/pre&gt;


&lt;p&gt;
Now that we have our data, we can run the test:
&lt;/p&gt;


&lt;pre class=&quot;src src-R&quot;&gt;aovComp &lt;span style=&quot;color: #008b8b;&quot;&gt;&amp;lt;-&lt;/span&gt; aov(NDI ~ Time*Method + Error(Subject/Time), theData)
summary(aovComp)
&lt;/pre&gt;



&lt;pre class=&quot;example&quot;&gt;
Error: Subject
          Df Sum Sq Mean Sq F value Pr(&amp;gt;F)
Method     1  157.9   157.9   1.499  0.237
Residuals 18 1896.2   105.3               

Error: Subject:Time
            Df Sum Sq Mean Sq F value   Pr(&amp;gt;F)    
Time         3   5082  1693.9  43.066 2.38e-14 ***
Time:Method  3    119    39.6   1.006    0.397    
Residuals   54   2124    39.3                     
---
Signif. codes:  0 ‘***’ 0.001 ‘**’ 0.01 ‘*’ 0.05 ‘.’ 0.1 ‘ ’ 1
&lt;/pre&gt;


&lt;p&gt;
Terrific!  For these data, we observe a highly significant &lt;code&gt;Time&lt;/code&gt; effect (this should be obvious given our table above), an insignificant &lt;code&gt;Method&lt;/code&gt; fixed effect, and an insignificant &lt;code&gt;Time:Method&lt;/code&gt; interaction.  If we think about our model and what we're interested in, it's the interaction which we care about and that which we'd like to detect.  If our significance level had been \(\alpha = 0.05\), we would not have rejected this time, but who knows what would happen next time.
&lt;/p&gt;
&lt;p&gt;
Now it's time to rinse-and-repeat, which we accomplish with the &lt;code&gt;replicate&lt;/code&gt; function.  Before we get there, though, let's look at a plot.  There are several relevant ones, but in the interest of brevity let's satisfy ourselves with an &lt;code&gt;interaction.plot&lt;/code&gt;:
&lt;/p&gt;



&lt;pre class=&quot;src src-R&quot;&gt;with(theData, interaction.plot(Time, Method, NDI))
&lt;/pre&gt;

&lt;div id=&quot;fig-interaction-plot&quot; class=&quot;figure&quot;&gt;
&lt;p&gt;
&lt;img src=&quot;/images/120120.png&quot;  alt=&quot;/images/120120.png&quot; /&gt;
&lt;/p&gt;
&lt;/div&gt;
&lt;p&gt;
Everything is going according to plan.  There is definitely a &lt;code&gt;Time&lt;/code&gt; effect (the lines both slope downward) but there isn't any evidence of an interaction (the lines have similar slopes).
&lt;/p&gt;
&lt;p&gt;
On to rinse-and-repeat, we first set up the function that runs the test once:
&lt;/p&gt;



&lt;pre class=&quot;src src-R&quot;&gt;&lt;span style=&quot;color: #0000ff;&quot;&gt;runTest&lt;/span&gt; &lt;span style=&quot;color: #008b8b;&quot;&gt;&amp;lt;-&lt;/span&gt; &lt;span style=&quot;color: #a020f0;&quot;&gt;function&lt;/span&gt;(){
  tmp1 &lt;span style=&quot;color: #008b8b;&quot;&gt;&amp;lt;-&lt;/span&gt; mvrnorm(nPerGroup, mu = muTreat, Sigma = B)
  tmp2 &lt;span style=&quot;color: #008b8b;&quot;&gt;&amp;lt;-&lt;/span&gt; mvrnorm(nPerGroup, mu = muSham, Sigma = B)
  theData$NDI &lt;span style=&quot;color: #008b8b;&quot;&gt;&amp;lt;-&lt;/span&gt; c(as.vector(t(tmp1)), as.vector(t(tmp2)))
  aovComp &lt;span style=&quot;color: #008b8b;&quot;&gt;&amp;lt;-&lt;/span&gt; aov(NDI ~ Time*Method + Error(Subject/Time), theData)  
  b &lt;span style=&quot;color: #008b8b;&quot;&gt;&amp;lt;-&lt;/span&gt; summary(aovComp)$&lt;span style=&quot;color: #8b2252;&quot;&gt;'Error: Subject:Time'&lt;/span&gt;[[1]][2,5]
  b &amp;lt; 0.05
}
&lt;/pre&gt;


&lt;p&gt;
and finally do the repeating:
&lt;/p&gt;



&lt;pre class=&quot;src src-R&quot;&gt;mean(replicate(nSim, runTest()))
&lt;/pre&gt;


&lt;pre class=&quot;example&quot;&gt;
[1] 0.372
&lt;/pre&gt;


&lt;p&gt;
Whoa!  The power is 0.372?  That's pretty low.  We recall that this is just an &lt;i&gt;estimate&lt;/i&gt; of power - how precise is the estimate?  The standard error of \(\hat{p}\) is approximately \(\sqrt{\hat{p}(1 - \hat{p})/n}\), so in our case, our estimate's standard error is approximately 0.022. That means we are approximately 95% confident that the true power at this particular alternative is covered by the interval \([0.329,0.415]\).
&lt;/p&gt;
&lt;p&gt;
Standard practice is to shoot for a power of around \(\beta = 0.80\), so our power isn't even close to what we'd need.  We can increase power by increasing sample size (the parameter &lt;code&gt;nPerGroup&lt;/code&gt;).  A larger sample size means a longer time needed to run the simulation.  Below are some results of running the above script at assorted sample sizes.
&lt;/p&gt;
&lt;table border=&quot;2&quot; cellspacing=&quot;0&quot; cellpadding=&quot;6&quot; rules=&quot;groups&quot; frame=&quot;hsides&quot;&gt;
&lt;caption&gt;&lt;/caption&gt;
&lt;colgroup&gt;&lt;col class=&quot;right&quot; /&gt;&lt;col class=&quot;right&quot; /&gt;&lt;col class=&quot;right&quot; /&gt;
&lt;/colgroup&gt;
&lt;tbody&gt;
&lt;tr&gt;&lt;td class=&quot;right&quot;&gt;&lt;code&gt;nPerGroup&lt;/code&gt;&lt;/td&gt;&lt;td class=&quot;right&quot;&gt;Power (estimate)&lt;/td&gt;&lt;td class=&quot;right&quot;&gt;SE (approx)&lt;/td&gt;&lt;/tr&gt;
&lt;tr&gt;&lt;td class=&quot;right&quot;&gt;10&lt;/td&gt;&lt;td class=&quot;right&quot;&gt;0.372&lt;/td&gt;&lt;td class=&quot;right&quot;&gt;0.022&lt;/td&gt;&lt;/tr&gt;
&lt;tr&gt;&lt;td class=&quot;right&quot;&gt;20&lt;/td&gt;&lt;td class=&quot;right&quot;&gt;0.686&lt;/td&gt;&lt;td class=&quot;right&quot;&gt;0.021&lt;/td&gt;&lt;/tr&gt;
&lt;tr&gt;&lt;td class=&quot;right&quot;&gt;30&lt;/td&gt;&lt;td class=&quot;right&quot;&gt;0.886&lt;/td&gt;&lt;td class=&quot;right&quot;&gt;0.014&lt;/td&gt;&lt;/tr&gt;
&lt;/tbody&gt;
&lt;/table&gt;


&lt;p&gt;
Now we're talking.  It looks like somewhere between 20 and 30 subjects per group would be enough to detect the clinically meaningful difference proposed above with a power of 0.80.
&lt;/p&gt;
&lt;p&gt;
Unfortunately, the joke is on us.  Because, as it happens, it's no small order for a lone, practicing PT (around here) to snare 60 humans with neck pain for a research study. A person would need to be in (or travel to) a heavily populated area, and even &lt;i&gt;then&lt;/i&gt; there would be dropout, people not showing up for subsequent appointments.  
&lt;/p&gt;
&lt;p&gt;
&lt;b&gt;So what can we do?&lt;/b&gt;
&lt;/p&gt;&lt;ol&gt;
&lt;li&gt;&lt;b&gt;Modify the research details.&lt;/b&gt;  If we take a closer look at the table, there isn't an expected difference in the means until 48 hours, so why not measure differently, say, at 0, 48, 96, and 144 hours?  Is there something else about the measurement process we could change to decrease the variance?
&lt;/li&gt;
&lt;li&gt;&lt;b&gt;Use a different test.&lt;/b&gt;  We are going with boilerplate repeated-measures ANOVA here.  Is that really the best choice?  What would happen if we tried the mixed-effects approach?
&lt;/li&gt;
&lt;li&gt;&lt;b&gt;Take a second look at the model.&lt;/b&gt;  We should not only double-check our parameter choices, but rethink: is the repeated-measures model (multivariate normal) the most appropriate?  Is it reasonable for the variance of differences at all time pairs to be identical?  What about the covariance structure? There are others we could try, such as an autoregressive model (another arrow in the mixed-effects models' quiver).
&lt;/li&gt;
&lt;/ol&gt;


&lt;/div&gt;

&lt;/div&gt;

&lt;div id=&quot;outline-container-6&quot; class=&quot;outline-2&quot;&gt;
&lt;h2 id=&quot;sec-6&quot;&gt;Other things to keep in mind&lt;/h2&gt;
&lt;div class=&quot;outline-text-2&quot; id=&quot;text-6&quot;&gt;

&lt;ul&gt;
&lt;li&gt;This example is simple enough to have done analytically; we didn't have to simulate anything at all.
&lt;/li&gt;
&lt;li&gt;Even if the example hadn't been simple, we could still have searched for an &lt;i&gt;approximate&lt;/i&gt; analytic solution which, if nothing else, might have given some insight into the power function's behavior.
&lt;/li&gt;
&lt;li&gt;We could have adjusted all the means upward by 7 and nothing would have changed.  We based our initial values on literature review and clinical expertise.
&lt;/li&gt;
&lt;li&gt;We didn't bother with contrasts, functional means, or anything else.  We just generated data consistent with our null and salient alternative and went on with our business.
&lt;/li&gt;
&lt;li&gt;We could have used whatever test we liked yet the method of attack would have been the same.  Multiple comparisons, nested tests, nonparametric tests, whatever.  As long as we include the full procedure in &lt;code&gt;runTest&lt;/code&gt;, we will get valid estimates of power for &lt;i&gt;that&lt;/i&gt; procedure at &lt;i&gt;that&lt;/i&gt; alternative.
&lt;/li&gt;
&lt;li&gt;We need to be careful that the test we use (whatever it is) has its significance level controlled.  This is easy to check in our example.  We can set the means equal (&lt;code&gt;muTreat&lt;/code&gt; = &lt;code&gt;muSham&lt;/code&gt;) and run the simulation.  We should get a power equal to 0.05 (within margin of error).  Go ahead, check yourself. In fact, since we only care about the interaction, we could vertically offset the means by any fixed number, not necessarily zero.
&lt;/li&gt;
&lt;li&gt;Had we not been careful with our &lt;code&gt;stdevs&lt;/code&gt;, our simulated NDIs would have gone negative, particularly at the latter time points.  That would not have been reasonable since NDI is nonnegative.
&lt;/li&gt;
&lt;li&gt;&lt;b&gt;Simulation is not a silver bullet.&lt;/b&gt; 
&lt;/li&gt;
&lt;li&gt;Effective simulation requires substantial investment of thought into &lt;b&gt;both&lt;/b&gt; the probability model and the parameter settings.
&lt;/li&gt;
&lt;li&gt;Our model had 13 parameters, and we had 4 more we didn't even touch&lt;sup&gt;&lt;a class=&quot;footref&quot; name=&quot;fnr.1&quot; href=&quot;#fn.1&quot;&gt;1&lt;/a&gt;&lt;/sup&gt;. A person could be forgiven for wondering how in the world all of those parameters can be expressively spun into a T-shirt effect size. (They can't.)
&lt;/li&gt;
&lt;li&gt;The complexity can get out of control quickly. Simulation run times can take forever.  The more complicated the model/test the worse it gets.
&lt;/li&gt;
&lt;li&gt;Informative simulation demands literature review and content expertise as a prerequisite. Some researchers are unable (due to lack of existing/quality studies) or unwilling (for all sorts of reasons, not all of which are good) to help the statistician fill in the details.  For the statistician, this is a problem.  If you don't know anything, then you can't say anything.
&lt;/li&gt;
&lt;li&gt;We can address uncertainty in our parameter guesses with prior distributions on the parameters.  This adds a layer of complexity to the simulation since we must first simulate the parameters before simulating the data.  Sometimes there's no other choice. 
&lt;/li&gt;
&lt;li&gt;Theory tells us that the standard research designs (including our current one) can usually be re-parameterized by a single non-centrality parameter which ultimately determines the power at any particular alternative. Following our nose, it suggests that our problem is simpler than we're making it, that if we would just write down the non-centrality parameter (and the right numerator/denominator degrees of freedom), we'd be all set.  Yep, we would.  Good luck with all&amp;hellip; that.
&lt;/li&gt;
&lt;/ul&gt;


&lt;/div&gt;

&lt;/div&gt;

&lt;div id=&quot;outline-container-7&quot; class=&quot;outline-2&quot;&gt;
&lt;h2 id=&quot;sec-7&quot;&gt;References&lt;/h2&gt;
&lt;div class=&quot;outline-text-2&quot; id=&quot;text-7&quot;&gt;

&lt;ul&gt;
&lt;li&gt;See &lt;a href=&quot;http://stats.stackexchange.com/questions/21237/calculating-statistical-power&quot;&gt;this question&lt;/a&gt; on &lt;a href=&quot;http://stats.stackexchange.com/&quot;&gt;CrossValidated&lt;/a&gt; which came up while I was working on this document (I might not have answered so quickly otherwise).  Thanks to all who contributed to that discussion.

&lt;/li&gt;
&lt;li&gt;&lt;i&gt;Conditions Under Which Mean Square Ratios in Repeated Measurements Designs Have Exact F-Distributions&lt;/i&gt;. Huynh Huynh and Leonard S. Feldt, Journal of the American Statistical Association, Vol. 65, No. 332 (Dec., 1970), pp. 1582-1589, &lt;a href=&quot;http://www.jstor.org/stable/2284340&quot;&gt;stable link&lt;/a&gt;.

&lt;/li&gt;
&lt;li&gt;I found &lt;a href=&quot;http://personality-project.org/r/r.anova.html&quot;&gt;this website&lt;/a&gt; while preparing for the initial meeting and got some inspiration from the discussion near the middle.

&lt;/li&gt;
&lt;li&gt;There are several papers on &lt;a href=&quot;http://www.stat.uiowa.edu/~rlenth/Power/&quot;&gt;Russell Lenth&lt;/a&gt;'s webpage which are good reading. 

&lt;/li&gt;
&lt;li&gt;I also like &lt;a href=&quot;http://onlinelibrary.wiley.com/doi/10.1348/000711001159357/abstract&quot;&gt;this paper&lt;/a&gt;.  Keselman, H. J., Algina, J. and Kowalchuk, R. K. (2001), &lt;i&gt;The analysis of repeated measures designs: A review.&lt;/i&gt; British Journal of Mathematical and Statistical Psychology, 54: 1–20. doi: 10.1348/000711001159357

&lt;/li&gt;
&lt;li&gt;Many of the concepts above are explained more formally in my &lt;a href=&quot;https://github.com/gjkerns/STAT5840&quot;&gt;Statistical Computing&lt;/a&gt; course which you can get on GitHub with
&lt;pre class=&quot;example&quot;&gt;
git clone git://github.com/gjkerns/STAT5840.git
&lt;/pre&gt;


&lt;/li&gt;
&lt;li&gt;To learn more about Monte Carlo methods with R I recommend &lt;a href=&quot;http://www.springer.com/statistics/computanional+statistics/book/978-1-4419-1575-7&quot;&gt;Introducing Monte Carlo Methods with R&lt;/a&gt; by Robert and Casella.  I also like &lt;a href=&quot;http://personal.bgsu.edu/~mrizzo/SCR.htm&quot;&gt;Statistical Computing with R&lt;/a&gt; by Rizzo which has a section about simulating power of statistical tests.

&lt;/li&gt;
&lt;li&gt;For the record, here is my &lt;code&gt;sessionInfo&lt;/code&gt;.



  

&lt;pre class=&quot;example&quot;&gt;  R version 2.14.1 (2011-12-22)
  Platform: x86_64-pc-linux-gnu (64-bit)

  locale:
   [1] LC_CTYPE=en_US.UTF-8       LC_NUMERIC=C              
   [3] LC_TIME=en_US.UTF-8        LC_COLLATE=en_US.UTF-8    
   [5] LC_MONETARY=en_US.UTF-8    LC_MESSAGES=en_US.UTF-8   
   [7] LC_PAPER=C                 LC_NAME=C                 
   [9] LC_ADDRESS=C               LC_TELEPHONE=C            
  [11] LC_MEASUREMENT=en_US.UTF-8 LC_IDENTIFICATION=C       

  attached base packages:
  [1] stats     graphics  grDevices utils     datasets  methods   base     

  other attached packages:
  [1] MASS_7.3-16
&lt;/pre&gt;


&lt;/li&gt;
&lt;/ul&gt;


&lt;div id=&quot;footnotes&quot;&gt;
&lt;h2 class=&quot;footnotes&quot;&gt;Footnotes: &lt;/h2&gt;
&lt;div id=&quot;text-footnotes&quot;&gt;
&lt;p class=&quot;footnote&quot;&gt;&lt;sup&gt;&lt;a class=&quot;footnum&quot; name=&quot;fn.1&quot; href=&quot;#fnr.1&quot;&gt;1&lt;/a&gt;&lt;/sup&gt; John von Neumann once said, &quot;With four parameters I can fit an elephant, and with five I can make him wiggle his trunk.&quot;
&lt;/p&gt;&lt;/div&gt;
&lt;/div&gt;

&lt;/div&gt;
&lt;/div&gt;
</content>
 </entry>
 
 <entry>
   <title>Estimating a normal mean with a cauchy prior</title>
   <link href="http://gjkerns.github.com/R/2011/08/24/estimate-normal-mean.html"/>
   <updated>2011-08-24T00:00:00-07:00</updated>
   <id>http://gjkerns.github.com/R/2011/08/24/estimate-normal-mean</id>
   <content type="html">&lt;div id=&quot;outline-container-1&quot; class=&quot;outline-2&quot;&gt;
&lt;h2 id=&quot;sec-1&quot;&gt;The setup&lt;/h2&gt;
&lt;div class=&quot;outline-text-2&quot; id=&quot;text-1&quot;&gt;

&lt;p&gt;When doing statistics the Bayesian way, we are sometimes bombarded with complicated integrals that do not lend themselves to closed-form solutions.  This used to be a problem.  Nowadays, not so much.  This post illustrates how a person can use the Monte Carlo method (and R) to get a good estimate for an integral that might otherwise look unwieldy at first glance.  Of course, in this example, the integral isn't very complicated.  But the &lt;i&gt;method&lt;/i&gt; works the same, regardless of the mess in which we find ourselves.  The current example is derived from one in &lt;a href=&quot;http://www.springer.com/statistics/statistical+theory+and+methods/book/978-0-387-21239-5&quot;&gt;Monte Carlo Statistical Methods&lt;/a&gt; by Robert/Casella (in Chapter 4).  For that matter, check out their &lt;a href=&quot;http://www.springer.com/statistics/computanional+statistics/book/978-1-4419-1575-7&quot;&gt;Introducing Monte Carlo Methods with R&lt;/a&gt;.
&lt;/p&gt;

&lt;p&gt;
Suppose we have one observation \( X \sim N(\theta,1) \) but we have a (robust) prior distribution on \(\theta\), namely, \( \theta \sim \mathrm{Cauchy}(0,1) \).  We would like to update our beliefs about \(\theta\) based on the information provided by \(x\).  So our likelihood is 
  \[
  f(x|\theta) = \frac{1}{\sqrt{2\pi}}\exp \left[-\frac{1}{2}(x - \theta)^2  \right],
  \]
  and our prior is
  \[
  g(\theta) = \frac{1}{\pi}\frac{1}{(1 + \theta^{2})}.
  \]
  The posterior distribution is proportional to the likelihood times prior, that is, 
  \[
  g(\theta|x) \propto \exp \left[-\frac{1}{2}(x - \theta)^2  \right] \frac{1}{(1 + \theta^{2})},
  \]
  with the proportionality constant being the reciprocal of
  \[
  C = \int \exp \left[-\frac{1}{2}(x - \theta)^2  \right] \frac{1}{(1 + \theta^{2})} \mathrm{d} \theta.
  \]
  Our point estimate (or best guess) for \(\theta\) will be just the &lt;i&gt;posterior mean&lt;/i&gt;, given by
  \[
  E (\theta | \mbox{data}) = \frac{ \int \theta \exp \left[-\frac{1}{2}(x - \theta)^2  \right] \frac{1}{(1 + \theta^{2})} \mathrm{d} \theta }{C}.
  \]

We notice that the integrand for \(C\) looks like &lt;i&gt;something&lt;/i&gt; times a Cauchy PDF, where the &lt;i&gt;something&lt;/i&gt; (let's call it \(h\)) is
\[
h(\theta) = \pi \exp \left[-\frac{1}{2}(x - \theta)^2  \right],
\] 
so one way to use the Monte Carlo method follows. 
&lt;/p&gt;&lt;dl&gt;
&lt;dt&gt;Procedure:&lt;/dt&gt;&lt;dd&gt;Given observed data \(X=x\),
&lt;ol&gt;
&lt;li&gt;Simulate a bunch of Cauchys, \(\theta_{1},\theta_{2},\ldots,\theta_{m}\), say.
&lt;/li&gt;
&lt;li&gt;Estimate the integral in the denominator with
     \[
     \frac{1}{m}\sum_{i=1}^{m} \pi\exp\left[-\frac{1}{2}(x - \theta_{i})^2  \right]. 
     \]
&lt;/li&gt;
&lt;li&gt;Estimate the integral in the numerator with
     \[
     \frac{1}{m}\sum_{i=1}^{m} \pi\theta_{i} \exp \left[-\frac{1}{2}(x - \theta_{i})^2  \right].
     \]
&lt;/li&gt;
&lt;li&gt;Take the ratio, and we're done.
&lt;/li&gt;
&lt;/ol&gt;

&lt;/dd&gt;
&lt;/dl&gt;


&lt;p&gt;
The &lt;a href=&quot;http://en.wikipedia.org/wiki/Strong_Law_of_Small_Numbers&quot;&gt;Strong Law of Large Numbers&lt;/a&gt; says that the averages in 2 and 3 both converge to where they should, so the ratio should converge to the right place as well.  
&lt;/p&gt;
&lt;/div&gt;

&lt;/div&gt;

&lt;div id=&quot;outline-container-2&quot; class=&quot;outline-2&quot;&gt;
&lt;h2 id=&quot;sec-2&quot;&gt;How to do it with R&lt;/h2&gt;
&lt;div class=&quot;outline-text-2&quot; id=&quot;text-2&quot;&gt;

&lt;p&gt;The following is an R script which does the above.  For laughs, let's suppose that we observed \(X=3\).
&lt;/p&gt;



&lt;pre class=&quot;src src-R&quot;&gt;&lt;span style=&quot;color: #b22222;&quot;&gt;# &lt;/span&gt;&lt;span style=&quot;color: #b22222;&quot;&gt;cauchyprior.R&lt;/span&gt;
set.seed(1)           &lt;span style=&quot;color: #b22222;&quot;&gt;# &lt;/span&gt;&lt;span style=&quot;color: #b22222;&quot;&gt;makes the experiment reproducible&lt;/span&gt;
m &lt;span style=&quot;color: #008b8b;&quot;&gt;&amp;lt;-&lt;/span&gt; 2000             &lt;span style=&quot;color: #b22222;&quot;&gt;# &lt;/span&gt;&lt;span style=&quot;color: #b22222;&quot;&gt;number of simulated values&lt;/span&gt;
x &lt;span style=&quot;color: #008b8b;&quot;&gt;&amp;lt;-&lt;/span&gt; 3                &lt;span style=&quot;color: #b22222;&quot;&gt;# &lt;/span&gt;&lt;span style=&quot;color: #b22222;&quot;&gt;observed data&lt;/span&gt;

&lt;span style=&quot;color: #b22222;&quot;&gt;# &lt;/span&gt;&lt;span style=&quot;color: #b22222;&quot;&gt;Now simulate some random variables&lt;/span&gt;
theta &lt;span style=&quot;color: #008b8b;&quot;&gt;&amp;lt;-&lt;/span&gt; rcauchy(m)                  &lt;span style=&quot;color: #b22222;&quot;&gt;# &lt;/span&gt;&lt;span style=&quot;color: #b22222;&quot;&gt;simulate m standard Cauchys&lt;/span&gt;
h &lt;span style=&quot;color: #008b8b;&quot;&gt;&amp;lt;-&lt;/span&gt; pi * exp(-0.5*(x - theta)^2)    &lt;span style=&quot;color: #b22222;&quot;&gt;# &lt;/span&gt;&lt;span style=&quot;color: #b22222;&quot;&gt;who wants to write this over and over&lt;/span&gt;

Constant &lt;span style=&quot;color: #008b8b;&quot;&gt;&amp;lt;-&lt;/span&gt; mean(h)                  &lt;span style=&quot;color: #b22222;&quot;&gt;# &lt;/span&gt;&lt;span style=&quot;color: #b22222;&quot;&gt;estimate normalizing constant&lt;/span&gt;
post.mean &lt;span style=&quot;color: #008b8b;&quot;&gt;&amp;lt;-&lt;/span&gt; mean(theta * h)/mean(h)   &lt;span style=&quot;color: #b22222;&quot;&gt;# &lt;/span&gt;&lt;span style=&quot;color: #b22222;&quot;&gt;estimate posterior mean&lt;/span&gt;
&lt;/pre&gt;



&lt;/div&gt;

&lt;/div&gt;

&lt;div id=&quot;outline-container-3&quot; class=&quot;outline-2&quot;&gt;
&lt;h2 id=&quot;sec-3&quot;&gt;At the command prompt&lt;/h2&gt;
&lt;div class=&quot;outline-text-2&quot; id=&quot;text-3&quot;&gt;

&lt;p&gt;After copy-pasting the above into an R session we can see what the results were with an additional
&lt;/p&gt;


&lt;pre class=&quot;src src-R&quot;&gt;Constant
post.mean
&lt;/pre&gt;



&lt;p&gt;
For this simple example we can actually calculate what the true values are (to machine precision) with the following: for the constant \(C\) we get
&lt;/p&gt;


&lt;pre class=&quot;src src-R&quot;&gt;&lt;span style=&quot;color: #0000ff;&quot;&gt;f&lt;/span&gt; &lt;span style=&quot;color: #008b8b;&quot;&gt;&amp;lt;-&lt;/span&gt; &lt;span style=&quot;color: #a020f0;&quot;&gt;function&lt;/span&gt;(x) exp(-0.5*(x - 3)^2)/(1 + x^2)  
integrate(f, -&lt;span style=&quot;color: #228b22;&quot;&gt;Inf&lt;/span&gt;, &lt;span style=&quot;color: #228b22;&quot;&gt;Inf&lt;/span&gt;)                    
&lt;/pre&gt;



&lt;p&gt;
so our estimate of \(C\) overshot the mark by about 0.03, and in the posterior mean case we get
&lt;/p&gt;


&lt;pre class=&quot;src src-R&quot;&gt;&lt;span style=&quot;color: #0000ff;&quot;&gt;g&lt;/span&gt; &lt;span style=&quot;color: #008b8b;&quot;&gt;&amp;lt;-&lt;/span&gt; &lt;span style=&quot;color: #a020f0;&quot;&gt;function&lt;/span&gt;(x) x * f(x)  
integrate(g, -&lt;span style=&quot;color: #228b22;&quot;&gt;Inf&lt;/span&gt;, &lt;span style=&quot;color: #228b22;&quot;&gt;Inf&lt;/span&gt;)$value / integrate(f, -&lt;span style=&quot;color: #228b22;&quot;&gt;Inf&lt;/span&gt;, &lt;span style=&quot;color: #228b22;&quot;&gt;Inf&lt;/span&gt;)$value
&lt;/pre&gt;



&lt;p&gt;
so our estimate of the posterior mean was around 0.05 too high.  If we would like to get better estimates, we could increase the value of &lt;code&gt;m = 2000&lt;/code&gt; to something higher (assuming these things are actually converging someplace).
&lt;/p&gt;
&lt;/div&gt;

&lt;/div&gt;

&lt;div id=&quot;outline-container-4&quot; class=&quot;outline-2&quot;&gt;
&lt;h2 id=&quot;sec-4&quot;&gt;Are we waiting long enough?&lt;/h2&gt;
&lt;div class=&quot;outline-text-2&quot; id=&quot;text-4&quot;&gt;

&lt;p&gt;Our estimates were a little bit off; we might like to take a look at a plot to see how we're doing &amp;ndash; is this thing really converging like we'd expect?  We can look at a running average plot to assess convergence. If the plot bounces around indeterminably, that's bad, but if it settles down to a finite constant, that's better.  Here's a quick way to check this out with base graphics.
&lt;/p&gt;



&lt;pre class=&quot;src src-R&quot;&gt;rc &lt;span style=&quot;color: #008b8b;&quot;&gt;&amp;lt;-&lt;/span&gt; cumsum(h)/seq_along(h)        &lt;span style=&quot;color: #b22222;&quot;&gt;# &lt;/span&gt;&lt;span style=&quot;color: #b22222;&quot;&gt;running mean of C&lt;/span&gt;
rpm &lt;span style=&quot;color: #008b8b;&quot;&gt;&amp;lt;-&lt;/span&gt; cumsum(h * theta)/cumsum(h)  &lt;span style=&quot;color: #b22222;&quot;&gt;# &lt;/span&gt;&lt;span style=&quot;color: #b22222;&quot;&gt;running posterior mean&lt;/span&gt;
&lt;/pre&gt;


&lt;p&gt;
Now we plot the results.
&lt;/p&gt;



&lt;pre class=&quot;src src-R&quot;&gt;A &lt;span style=&quot;color: #008b8b;&quot;&gt;&amp;lt;-&lt;/span&gt; data.frame(iter = 1:m, rc = rc, rpm=rpm)
&lt;span style=&quot;color: #008b8b;&quot;&gt;library&lt;/span&gt;(reshape)
A.short &lt;span style=&quot;color: #008b8b;&quot;&gt;&amp;lt;-&lt;/span&gt; melt(A[3:200, ], id=&lt;span style=&quot;color: #8b2252;&quot;&gt;&quot;iter&quot;&lt;/span&gt;)
a &lt;span style=&quot;color: #008b8b;&quot;&gt;&amp;lt;-&lt;/span&gt; ggplot(A.short, aes(iter, value, colour=variable)) + geom_line() +
      opts(title = &lt;span style=&quot;color: #8b2252;&quot;&gt;&quot;First 200&quot;&lt;/span&gt;)
A.long &lt;span style=&quot;color: #008b8b;&quot;&gt;&amp;lt;-&lt;/span&gt; melt(A, id=&lt;span style=&quot;color: #8b2252;&quot;&gt;&quot;iter&quot;&lt;/span&gt;)
b &lt;span style=&quot;color: #008b8b;&quot;&gt;&amp;lt;-&lt;/span&gt; ggplot(A.long, aes(iter, value, colour=variable)) + geom_line() +
      opts(title = &lt;span style=&quot;color: #8b2252;&quot;&gt;&quot;All 2000 iterations&quot;&lt;/span&gt;)
grid.newpage()
pushViewport(viewport(layout = grid.layout(1, 2, widths = unit(c(3,5),&lt;span style=&quot;color: #8b2252;&quot;&gt;&quot;null&quot;&lt;/span&gt;))))
&lt;span style=&quot;color: #0000ff;&quot;&gt;vplayout&lt;/span&gt; &lt;span style=&quot;color: #008b8b;&quot;&gt;&amp;lt;-&lt;/span&gt; &lt;span style=&quot;color: #a020f0;&quot;&gt;function&lt;/span&gt;(x, y)
viewport(layout.pos.row = x, layout.pos.col = y)
print(a, vp = vplayout(1, 1))
print(b, vp = vplayout(1, 2))
&lt;/pre&gt;



&lt;div id=&quot;fig-yplot&quot; class=&quot;figure&quot;&gt;
&lt;p&gt;&lt;img src=&quot;/images/110824.png&quot;  alt=&quot;/images/110824.png&quot; /&gt;&lt;/p&gt;
&lt;p&gt;Running averages for assessing convergence of the estimators&lt;/p&gt;
&lt;/div&gt;

&lt;p&gt;
In this example, the estimates look to be still unstable at around &lt;code&gt;m = 200&lt;/code&gt;, but by the time we reach &lt;code&gt;m = 2000&lt;/code&gt; they look to have pretty much settled down.  Here we knew what the true values were, so we could tell immediately how well we were doing.  On the battlefield we are not so lucky.  In general, with Monte Carlo estimates like these it is wise to take a look at some plots to judge the behavior of our estimators.  If our plot looks more like the one on the left, then we should consider increasing the sample size.  If our plot looks more like the one on the right, then maybe we would be satisfied with &quot;close enough&quot;. (We can always wait longer, tight purse-strings notwithstanding.)
&lt;/p&gt;

&lt;/div&gt;

&lt;/div&gt;

&lt;div id=&quot;outline-container-5&quot; class=&quot;outline-2&quot;&gt;
&lt;h2 id=&quot;sec-5&quot;&gt;Other approaches&lt;/h2&gt;
&lt;div class=&quot;outline-text-2&quot; id=&quot;text-5&quot;&gt;

&lt;p&gt;When we were looking to estimate \(C\) we noticed that the integrand was &lt;i&gt;something&lt;/i&gt; times a Cauchy distribution.  If we look again, we can see that the same integrand also looks like a &lt;i&gt;normal&lt;/i&gt; distribution times &lt;i&gt;something&lt;/i&gt;. So, another approach would be to simulate a bunch of normals and average the new &lt;i&gt;somethings&lt;/i&gt;.  Do we get the same answer (in the limit)?
&lt;/p&gt;
&lt;p&gt;
Yes, of course. It turns out, the approach simulating normals does a little bit better than the one simulating Cauchys, but they're really pretty close.  Check out chapter 4 of &lt;a href=&quot;http://www.springer.com/statistics/statistical+theory+and+methods/book/978-0-387-21239-5&quot;&gt;Monte Carlo Statistical Methods&lt;/a&gt; for discussion on this.
&lt;/p&gt;
&lt;/div&gt;

&lt;/div&gt;

&lt;div id=&quot;outline-container-6&quot; class=&quot;outline-2&quot;&gt;
&lt;h2 id=&quot;sec-6&quot;&gt;Where to find more&amp;hellip;&lt;/h2&gt;
&lt;div class=&quot;outline-text-2&quot; id=&quot;text-6&quot;&gt;


&lt;p&gt;
The above is a variant of an example we did in &lt;a href=&quot;https://github.com/gjkerns/STAT5840&quot;&gt;STAT 5840, Statistical Computing&lt;/a&gt;.  The entire course is available online at &lt;a href=&quot;https://github.com/gjkerns/STAT5840&quot;&gt;github&lt;/a&gt;. Go to the Downloads for a &lt;code&gt;.zip&lt;/code&gt; file or &lt;code&gt;.tar.gz&lt;/code&gt;.  Or, if you have &lt;a href=&quot;http://git-scm.com/&quot;&gt;git&lt;/a&gt; installed, you can get (git?) it all with
&lt;/p&gt;&lt;pre class=&quot;example&quot;&gt;
 git clone git://github.com/gjkerns/STAT5840.git
&lt;/pre&gt;

&lt;/div&gt;
&lt;/div&gt;
</content>
 </entry>
 
 <entry>
   <title>Maiden voyage</title>
   <link href="http://gjkerns.github.com/2011/08/23/first-post.html"/>
   <updated>2011-08-23T00:00:00-07:00</updated>
   <id>http://gjkerns.github.com/2011/08/23/first-post</id>
   <content type="html">&lt;div id=&quot;outline-container-1&quot; class=&quot;outline-2&quot;&gt;
&lt;h2 id=&quot;sec-1&quot;&gt;Who&lt;/h2&gt;
&lt;div class=&quot;outline-text-2&quot; id=&quot;text-1&quot;&gt;

&lt;p&gt;&lt;a href=&quot;http://people.ysu.edu/~gkerns/&quot;&gt;Me&lt;/a&gt;.  I'm an associate professor of Statistics at &lt;a href=&quot;http://web.ysu.edu/stem/math&quot;&gt;Youngstown State University&lt;/a&gt; in &lt;a href=&quot;http://en.wikipedia.org/wiki/Youngstown,_Ohio&quot;&gt;Youngstown, Ohio, USA&lt;/a&gt;.  I've been using &lt;a href=&quot;http://www.r-project.org/&quot;&gt;R&lt;/a&gt; for about 7 years,  &lt;a href=&quot;http://www.gnu.org/software/emacs/&quot;&gt;Emacs&lt;/a&gt; about 3 years,  &lt;a href=&quot;http://git-scm.com/&quot;&gt;git&lt;/a&gt; about 1 year, and &lt;a href=&quot;http://orgmode.org/&quot;&gt;Org-Mode&lt;/a&gt; for less than a year.
&lt;/p&gt;
&lt;/div&gt;

&lt;/div&gt;

&lt;div id=&quot;outline-container-2&quot; class=&quot;outline-2&quot;&gt;
&lt;h2 id=&quot;sec-2&quot;&gt;What&lt;/h2&gt;
&lt;div class=&quot;outline-text-2&quot; id=&quot;text-2&quot;&gt;

&lt;p&gt;I want this blog to be about statistics, plain and sample. No frills, no tomfoolery, just bare-boned statistics from beginning to end.  Plus Emacs, ESS, Org-Mode, and R, but that goes without saying. 
&lt;/p&gt;
&lt;/div&gt;

&lt;/div&gt;

&lt;div id=&quot;outline-container-3&quot; class=&quot;outline-2&quot;&gt;
&lt;h2 id=&quot;sec-3&quot;&gt;When&lt;/h2&gt;
&lt;div class=&quot;outline-text-2&quot; id=&quot;text-3&quot;&gt;

&lt;p&gt;I've wanted to do this for a long time, but had as of yet convinced myself that I didn't have time for it.  A sabbatical coupled with the renewed energy of a &lt;a href=&quot;http://www.warwick.ac.uk/statsdept/user-2011/&quot;&gt;use R! conference&lt;/a&gt; can change things considerably. 
&lt;/p&gt;
&lt;/div&gt;

&lt;/div&gt;

&lt;div id=&quot;outline-container-4&quot; class=&quot;outline-2&quot;&gt;
&lt;h2 id=&quot;sec-4&quot;&gt;Emacs + Org mode + Jekyll + Github = Blog + R!&lt;/h2&gt;
&lt;div class=&quot;outline-text-2&quot; id=&quot;text-4&quot;&gt;

&lt;p&gt;After much fiddling and googling I have managed to figure out how to run a blog entirely through Emacs and Git.  If you'd like to do the same I recommend reading &lt;a href=&quot;http://orgmode.org/worg/org-tutorials/org-jekyll.html&quot;&gt;here&lt;/a&gt; and &lt;a href=&quot;http://vitobotta.com/how-to-migrate-from-wordpress-to-jekyll/&quot;&gt;here&lt;/a&gt;, with liberal doses of &lt;a href=&quot;http://blog.envylabs.com/2009/08/publishing-a-blog-with-github-pages-and-jekyll/&quot;&gt;here&lt;/a&gt; and &lt;a href=&quot;https://github.com/mojombo/jekyll&quot;&gt;here&lt;/a&gt;.  Ultimately, if you'd like to know how I do it then you can find the org-mode source code for this blog &lt;a href=&quot;https://github.com/gjkerns/blog&quot;&gt;here&lt;/a&gt; and you can download the final result &lt;a href=&quot;https://github.com/gjkerns/gjkerns.github.com&quot;&gt;here&lt;/a&gt; (which still is source code but is as close to final as possible).
&lt;/p&gt;
&lt;p&gt;
The bottom line: with this setup I can effortlessly do R code like this:
&lt;/p&gt;



&lt;pre class=&quot;src src-R&quot;&gt;rnorm(10)
&lt;/pre&gt;


&lt;pre class=&quot;example&quot;&gt;
 [1] -1.07503636 -0.11587837  0.64801870 -0.78416095 -0.05825559 -0.26152707
 [7]  0.36192812 -0.63710301 -0.35185059  0.30624394
&lt;/pre&gt;


&lt;p&gt;
And can include plots like this:
&lt;/p&gt;



&lt;div id=&quot;fig-yplot&quot; class=&quot;figure&quot;&gt;
&lt;p&gt;&lt;img src=&quot;/images/skiddaw.png&quot;  alt=&quot;/images/skiddaw.png&quot; /&gt;&lt;/p&gt;
&lt;p&gt;A plot to get things started&lt;/p&gt;
&lt;/div&gt;

&lt;p&gt;
all housed inside a simple, dynamic text file that I can edit with &lt;a href=&quot;http://www.gnu.org/software/emacs/&quot;&gt;Emacs&lt;/a&gt; and can version-control with &lt;a href=&quot;http://git-scm.com/&quot;&gt;git&lt;/a&gt;.  On top of all this, I get LaTeX formatting in HTML via &lt;a href=&quot;http://www.mathjax.org/&quot;&gt;MathJax&lt;/a&gt;.  Life is good.
&lt;/p&gt;
&lt;/div&gt;
&lt;/div&gt;
</content>
 </entry>
 
 
</feed>

