<p><div><div><div><div data-width="100" data-show-faces="false" data-send="true" data-layout="button_count" data-height="21" data-href="https://www.r-bloggers.com/calculating-required-sample-size-in-r-and-sas/"></div></div><div></div></div><div></div></div>
<div>
(This article was first published on <strong><a href="http://geraldbelton.com/calculating-required-sample-size-in-r-and-sas/"> R – Gerald Belton</a></strong>, and kindly contributed to <a rel="nofollow" href="https://www.r-bloggers.com/">R-bloggers)</a> </div>
<p>Today we are going to digress from our ongoing “Intro to R” series, and talk about a subject that’s been on my mind lately: sample sizes.</p>
<p>An important question when designing an experiment is “How big a sample do I need?” A larger sample will give more accurate results, but at a cost. Use too small a sample, and you may get inconclusive results; too large a sample, and you’re wasting resources.</p>
<p>To calculate the required sample size, you’ll need to know four things:</p>
<ol>
<li>The size of the response you want to detect</li>
<li>The variance of the response</li>
<li>The desired significance level</li>
<li>The desired power</li>
</ol>
<h2>Delta</h2>
<p>Suppose you are comparing a treatment group to a placebo group, and you will be measuring some continuous response variable which, you hope, will be affected by the treatment. We can consider the mean response in the treatment group, μ<sub>1</sub>, and the mean response in the placebo group, μ<sub>2</sub>. We can then define Δ = μ<sub>1</sub> – μ<sub>2</sub>. The smaller the difference you want to detect, the larger the required sample size.</p>
<h2>Variance</h2>
<p>Of the four variables that go into the sample size calculation, the variance of the responses can be the most difficult to determine. Usually, before you do your experiment, you don’t know what variance to expect. Investigators often conduct a pilot study to determine the expected variance, or information from a previous published study can be used.</p>
<p>The <strong>effect size</strong> combines the minimal relevant difference and the variability into one measurement, Δ/σ.</p>
<h2>Significance</h2>
<p>Significance is equal to 1 – α, where α is the probability of making a Type 1 Error. That is, alpha represents the chance of a falsely rejecting H0 and picking up a false-positive effect. Alpha is usually set at 0.05, for a 95% significance.</p>
<h2>Power</h2>
<p>The power of a test is 1-β, where beta is the probability of a Type 2 error (failing to reject the null hypothesis when the alternative hypothesis is true). In other words, if you have a 20% chance of failing to detect a real difference, then the power of your test is .8.</p>
<h2>Sample Size Calculation</h2>
<p>The calculation for the total sample size is:</p>
<p><img title=" n=\frac{4(Z_{\alpha}+Z_{\beta})^2\sigma^2}{\Delta^2} " alt=" n=\frac{4(Z_{\alpha}+Z_{\beta})^2\sigma^2}{\Delta^2} " class="latex" src="http://s0.wp.com/latex.php?latex=%C2%A0n%3D%5Cfrac%7B4%28Z_%7B%5Calpha%7D%2BZ_%7B%5Cbeta%7D%29%5E2%5Csigma%5E2%7D%7B%5CDelta%5E2%7D+&bg=ffffff&%23038;fg=000&%23038;s=0"></p>
<p>For a two-sided test, we use Z<sub>α/2</sub> instead of Z<sub>α</sub>.</p>
<p>For example, suppose we want to be able to detect a difference of 20 units, with 90% power using a two-sided t-test, and a .05 significance level. We are expecting, based on previous research, that the standard deviation of the responses will be about 60 units.</p>
<p> </p>
<p>In this example, α=.05, β=.10, Δ=20, and σ=60. Z<sub>α/2</sub>=1.96, and Z<sub>β</sub>=1.28. So we have:</p>
<p><img title="n=\frac{4(1.96+1.28)^2\sigma^2}{20^2}\approx 378 " alt="n=\frac{4(1.96+1.28)^2\sigma^2}{20^2}\approx 378 " class="latex" src="http://s0.wp.com/latex.php?latex=n%3D%5Cfrac%7B4%281.96%2B1.28%29%5E2%5Csigma%5E2%7D%7B20%5E2%7D%5Capprox+378+&bg=ffffff&%23038;fg=000&%23038;s=0"></p>
<p>or, about 189 for each treatment group.</p>
<h2>Sample Size in R</h2>
<p>You could write a function in R to do the above calculation, but fortunately, you don’t need to. The <code>pwr</code> library has done it for you. In this case, we will use the <code>pwr.t.test()</code> function.</p>
<p><strong>pwr.t.test(n = , d = , sig.level = , power = , type = c(“two.sample”, “one.sample”, “paired”))</strong></p>
<p>In this case, we will leave out the “n=” parameter, and it will be calculated by R. If we fill in a sample size, and use “power = NULL”, then it will calculate the power of our test.</p>
<p>In this equation, d is the effect size, so we will calculate that from our delta and sigma values. In R, it looks like this:</p>
<pre>> delta &lt;- 20
> sigma &lt;- 60
> d &lt;- delta/sigma
> pwr.t.test(d=d, sig.level=.05, power = .90, type = 'two.sample')

    Two-sample t test power calculation

                      n = 190.0991
                      d = 0.3333333
           sig.level = 0.05
               power = 0.9
        alternative = two.sided

NOTE: n is number in *each* group</pre>
<h3>Sample Size in SAS</h3>
<p>In SAS, we can use <strong>PROC power</strong> to do the same calculations. One difference is that PROC power requires us to enter a value for the mean of each group. Since what we are really interested in is the difference, we can enter ‘0’ for group 1, and ’20’ for group 2, so that the difference in means will be 20. We also need to enter the standard deviation, unlike R where we calculated the effect size separately. The significance level defaults to .05, so we don’t need to enter it.</p>
<pre>proc power;
   twosamplemeans test=diff
   groupmeans = 0 | 20
   stddev = 60
   npergroup = .
   power = 0.9;
 run;</pre>
<p>And here is the output:</p>
<p> </p>
<p><img sizes="(max-width: 581px) 100vw, 581px" alt="" class="wp-image-165" src="https://i0.wp.com/geraldbelton.com/wp-content/uploads/2017/02/Clipboard01.png?resize=581%2C451" data-recalc-dims="1"></p>
<p>The alert reader has, by now, noticed a discrepancy: when we manually calculated the desired sample size, we got 189 per group. R gave us a result of 190.091, and SAS says it’s 191. Why? The simple answer is that neither program is using the above formula. pwr.t.test in R uses the <code>uniroot()</code> function to calculate n, and SAS uses a different formula. Furthermore, SAS and R are actually giving the same result, but SAS rounds up to 191. You can’t have .091 test subjects, and you don’t want to underpower the test, so it’s proper to round up. If you really want the details, the source code for pwr.t.test is on <a target="_blank" rel="nofollow" href="https://github.com/heliosdrm/pwr/blob/master/R/pwr.t.test.R">GitHub</a>, and the method SAS uses to calculate n is on page 4964 of the <a target="_blank" rel="nofollow" href="https://support.sas.com/documentation/cdl/en/statugpower/61819/PDF/default/statugpower.pdf">SAS/STAT User Guide</a>.</p>
<p> </p>
<p> </p>
<p> </p>
<p>
<div>
<div>To <strong>leave a comment</strong> for the author, please follow the link and comment on their blog: <strong><a href="http://geraldbelton.com/calculating-required-sample-size-in-r-and-sas/"> R – Gerald Belton</a></strong>.</div>
<hr>
<a rel="nofollow" href="https://www.r-bloggers.com/">R-bloggers.com</a> offers <strong><a rel="nofollow" href="https://feedburner.google.com/fb/a/mailverify?uri=RBloggers">daily e-mail updates</a></strong> about <a title="The R Project for Statistical Computing" rel="nofollow" href="https://www.r-project.org/">R</a> news and <a title="R tutorials" rel="nofollow" href="https://www.r-bloggers.com/search/tutorial">tutorials</a> on topics such as: <a title="Data science" rel="nofollow" href="https://www.r-bloggers.com/search/data%20science">Data science</a>, <a title="Big Data" rel="nofollow" href="https://www.r-bloggers.com/search/Big%20Data">Big Data, <a title="R jobs" rel="nofollow" href="https://www.r-users.com/">R jobs</a>, visualization (<a title="ggplot and ggplot2 tutorials" rel="nofollow" href="https://www.r-bloggers.com/search/ggplot2">ggplot2</a>, <a title="Boxplots using lattice and ggplot2 tutorials" rel="nofollow" href="https://www.r-bloggers.com/search/boxplot">Boxplots</a>, <a title="Maps and gis" rel="nofollow" href="https://www.r-bloggers.com/search/map">maps</a>, <a title="Animation in R" rel="nofollow" href="https://www.r-bloggers.com/search/animation">animation</a>), programming (<a title="RStudio IDE for R" rel="nofollow" href="https://www.r-bloggers.com/search/RStudio">RStudio</a>, <a title="Sweave and literate programming" rel="nofollow" href="https://www.r-bloggers.com/search/sweave">Sweave</a>, <a title="LaTeX in R" rel="nofollow" href="https://www.r-bloggers.com/search/LaTeX">LaTeX</a>, <a title="SQL and databases" rel="nofollow" href="https://www.r-bloggers.com/search/SQL">SQL</a>, <a title="Eclipse IDE for R" rel="nofollow" href="https://www.r-bloggers.com/search/eclipse">Eclipse</a>, <a title="git and github, Version Control System" rel="nofollow" href="https://www.r-bloggers.com/search/git">git</a>, <a title="Large data in R using Hadoop" rel="nofollow" href="https://www.r-bloggers.com/search/hadoop">hadoop</a>, <a title="Web Scraping of google, facebook, yahoo, twitter and more using R" rel="nofollow" href="https://www.r-bloggers.com/search/Web+Scraping">Web Scraping</a>) statistics (<a title="Regressions and ANOVA analysis tutorials" rel="nofollow" href="https://www.r-bloggers.com/search/regression">regression</a>, <a title="principal component analysis tutorial" rel="nofollow" href="https://www.r-bloggers.com/search/PCA">PCA</a>, <a title="Time series" rel="nofollow" href="https://www.r-bloggers.com/search/time+series">time series</a>, <a title="finance trading" rel="nofollow" href="https://www.r-bloggers.com/search/trading">trading</a>) and more...
<img height="1" alt="" width="1" src="http://feeds.feedburner.com/~r/RBloggers/~4/fixnzPUru60"></a></div>