<p><div><div><div><div data-width="100" data-show-faces="false" data-send="true" data-layout="button_count" data-height="21" data-href="http://www.r-bloggers.com/power-curves-in-r-using-plotly-ggplot2-library/"></div></div><div><a data-url="http://www.r-bloggers.com/power-curves-in-r-using-plotly-ggplot2-library/" data-counturl="http://www.r-bloggers.com/power-curves-in-r-using-plotly-ggplot2-library/" data-count="horizontal" data-text="Power Curves in R Using Plotly ggplot2 Library" class="twitter-share-button" data-via="rbloggers" href="https://twitter.com/share"></a></div><div></div></div><div></div></div>
<div>
(This article was first published on <strong><a href="http://moderndata.plot.ly/power-curves-r-plotly-ggplot2/"> R – Modern Data</a></strong>, and kindly contributed to <a rel="nofollow" href="http://www.r-bloggers.com/">R-bloggers)</a> </div>
<p>When performing <a target="_blank" href="https://en.wikipedia.org/wiki/Student%27s_t-test">Student’s t-test</a> to compare the difference in means between two groups, it is a useful exercise to determine the effect of unequal sample sizes in the comparison groups on power. Formally, power can be defined as the probability of rejecting the null hypothesis when the alternative hypothesis is true. Informally, <a target="_blank" href="https://en.wikipedia.org/wiki/Statistical_power">power</a> is the ability of a statistical test to detect an effect, if the effect actually exists. Large imbalances generally will not have adequate statistical power to detect even large effect sizes associated with a factor, leading to a high Type II error rate.</p>
<p>To jusity this reasoning I performed a power analysis for different group sizes. I considered the following group sizes, where n1 are the number of subjects in group 1 and n2 are the number of subjects in group 2:</p>
<ol>
<li>n1 = 28, n2 = 1406: n1 represents 2% of the entire sample size of 1434</li>
<li>n1 = 144, n2 = 1290: n1 represents 10% of the entire sample size of 1434</li>
<li>n1 = 287, n2 = 1147: n1 represents 20% of the entire sample size of 1434</li>
<li>n1 = 430, n2 = 1004: n1 represents 30% of the entire sample size of 1434</li>
<li>n1 = 574, n2 = 860: n1 represents 40% of the entire sample size of 1434</li>
<li>n1 = 717, n2 = 717: equal size groups (this is optimal because it leads to the highest power for a given effect size)</li>
</ol>
<p>In the figure below I plotted the power curves for the t-test, as a function of the effect size, assuming a Type I error rate of 5%. Comparing different power curves (based on the sample size of each group) on the same plot, is a useful visual representation of this analysis. We also plot a horizontal dashed line at an acceptable power level of 80%, and a vertical line at the effect size that would have to be present in our data to achieve 80% power. We see that the effect size must be greater than 0.54 to attain an acceptable power level given highly imbalanced group sizes of n1 = 28 and n2 = 1406, compared to all other scenarios that lead to 100% power.</p>
<p><iframe height="800" width="900" src="https://plot.ly/~sahirbhatnagar/48.embed"></iframe></p>

<pre>library(pwr) # for power calcs
library(dplyr) # for data manipulation
library(tidyr) # for data manipulation
library(ggplot2) # for plotting power curves
library(plotly) # for interactive power curves

# Generate power calculations
ptab &lt;- cbind(NULL, NULL)       

for (i in seq(0,1, length.out = 200)){
  pwrt1 &lt;- pwr.t2n.test(n1 = 28, n2 = 1406, 
                        sig.level = 0.05, power = NULL, 
                        d = i, alternative=&quot;two.sided&quot;)
  pwrt2 &lt;- pwr.t2n.test(n1 = 144, n2 = 1290, 
                        sig.level = 0.05, power = NULL, 
                        d = i, alternative=&quot;two.sided&quot;)
  pwrt3 &lt;- pwr.t2n.test(n1 = 287, n2 = 1147, 
                        sig.level = 0.05, power = NULL, 
                        d = i, alternative=&quot;two.sided&quot;)
  pwrt4 &lt;- pwr.t2n.test(n1 = 430, n2 = 1004, 
                        sig.level = 0.05, power = NULL, 
                        d = i, alternative=&quot;two.sided&quot;)
  pwrt5 &lt;- pwr.t2n.test(n1 = 574, n2 = 860, 
                        sig.level = 0.05, power = NULL, 
                        d = i, alternative=&quot;two.sided&quot;)
  pwrt6 &lt;- pwr.t2n.test(n1 = 717, n2 = 717, 
                        sig.level = 0.05, power = NULL, 
                        d = i, alternative=&quot;two.sided&quot;)
  ptab &lt;- rbind(ptab, cbind(pwrt1$d, pwrt1$power,
                            pwrt2$d, pwrt2$power,
                            pwrt3$d, pwrt3$power,
                            pwrt4$d, pwrt4$power,
                            pwrt5$d, pwrt5$power,
                            pwrt6$d, pwrt6$power))
}

ptab &lt;- cbind(seq_len(nrow(ptab)), ptab)

colnames(ptab) &lt;- c(&quot;id&quot;,&quot;n1=28, n2=1406.effect size&quot;,&quot;n1=28, n2=1406.power&quot;,
                    &quot;n1=144, n2=1290.effect size&quot;,&quot;n1=144, n2=1290.power&quot;,
                    &quot;n1=287, n2=1147.effect size&quot;,&quot;n1=287, n2=1147.power&quot;,
                    &quot;n1=430, n2=1004.effect size&quot;,&quot;n1=430, n2=1004.power&quot;,
                    &quot;n1=574, n2=860.effect size&quot;,&quot;n1=574, n2=860.power&quot;,
                    &quot;n1=717, n2=717.effect size&quot;,&quot;n1=717, n2=717.power&quot;)

# get data into right format for ggplot2
temp &lt;- ptab %>%
  as.data.frame() %>%
  gather(key = name, value = val, 2:13) %>%
  separate(col = name, into = c(&quot;group&quot;, &quot;var&quot;), sep = &quot;\.&quot;) %>%
  spread(key = var, value = val)

# factor group
temp$group &lt;- factor(temp$group, 
                levels = c(&quot;n1=28, n2=1406&quot;, &quot;n1=144, n2=1290&quot;, 
                &quot;n1=287, n2=1147&quot;, &quot;n1=430, n2=1004&quot;,
                &quot;n1=574, n2=860&quot;, &quot;n1=717, n2=717&quot;))


# plot
p &lt;- ggplot(temp, aes(x = `effect size`, y = power, color = group))
     geom_line(size=2) + 
     theme_bw() + 
     theme(axis.text=element_text(size=14), 
           axis.title=element_text(size=14), 
           legend.text=element_text(size=14)) +
     geom_vline(xintercept = .54, linetype = 2) +
     geom_hline(yintercept = 0.80, linetype = 2)

# so simple to make interactive plots
plotly::ggplotly(p)</pre>
<p><div><div><div><div data-width="55" data-show-faces="false" data-send="true" data-layout="box_count" data-height="62" data-href="http://www.r-bloggers.com/power-curves-in-r-using-plotly-ggplot2-library/"></div></div><div><a data-url="http://www.r-bloggers.com/power-curves-in-r-using-plotly-ggplot2-library/" data-counturl="http://www.r-bloggers.com/power-curves-in-r-using-plotly-ggplot2-library/" data-count="vertical" data-text="Power Curves in R Using Plotly ggplot2 Library" class="twitter-share-button" data-via="rbloggers" href="https://twitter.com/share"></a></div><div></div></div><div></div></div>
<div>
<div>To <strong>leave a comment</strong> for the author, please follow the link and comment on their blog: <strong><a href="http://moderndata.plot.ly/power-curves-r-plotly-ggplot2/"> R – Modern Data</a></strong>.</div>
<hr>
<a rel="nofollow" href="http://www.r-bloggers.com/">R-bloggers.com</a> offers <strong><a rel="nofollow" href="http://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="http://www.r-project.org/">R</a> news and <a title="R tutorials" rel="nofollow" href="http://www.r-bloggers.com/?s=tutorial">tutorials</a> on topics such as: <a title="Data science" rel="nofollow" href="http://www.r-bloggers.com/?s=data%20science">Data science</a>, <a title="Big Data" rel="nofollow" href="http://www.r-bloggers.com/?s=Big%20Data">Big Data, <a title="R jobs" rel="nofollow" href="http://www.r-users.com/">R jobs</a>, visualization (<a title="ggplot and ggplot2 tutorials" rel="nofollow" href="http://www.r-bloggers.com/?s=ggplot2">ggplot2</a>, <a title="Boxplots using lattice and ggplot2 tutorials" rel="nofollow" href="http://www.r-bloggers.com/?s=boxplot">Boxplots</a>, <a title="Maps and gis" rel="nofollow" href="http://www.r-bloggers.com/?s=map">maps</a>, <a title="Animation in R" rel="nofollow" href="http://www.r-bloggers.com/?s=animation">animation</a>), programming (<a title="RStudio IDE for R" rel="nofollow" href="http://www.r-bloggers.com/?s=RStudio">RStudio</a>, <a title="Sweave and literate programming" rel="nofollow" href="http://www.r-bloggers.com/?s=sweave">Sweave</a>, <a title="LaTeX in R" rel="nofollow" href="http://www.r-bloggers.com/?s=LaTeX">LaTeX</a>, <a title="SQL and databases" rel="nofollow" href="http://www.r-bloggers.com/?s=SQL">SQL</a>, <a title="Eclipse IDE for R" rel="nofollow" href="http://www.r-bloggers.com/?s=eclipse">Eclipse</a>, <a title="git and github, Version Control System" rel="nofollow" href="http://www.r-bloggers.com/?s=git">git</a>, <a title="Large data in R using Hadoop" rel="nofollow" href="http://www.r-bloggers.com/?s=hadoop">hadoop</a>, <a title="Web Scraping of google, facebook, yahoo, twitter and more using R" rel="nofollow" href="http://www.r-bloggers.com/?s=Web+Scraping">Web Scraping</a>) statistics (<a title="Regressions and ANOVA analysis tutorials" rel="nofollow" href="http://www.r-bloggers.com/?s=regression">regression</a>, <a title="principal component analysis tutorial" rel="nofollow" href="http://www.r-bloggers.com/?s=PCA">PCA</a>, <a title="Time series" rel="nofollow" href="http://www.r-bloggers.com/?s=time+series">time series</a>, <a title="finance trading" rel="nofollow" href="http://www.r-bloggers.com/?s=trading">trading</a>) and more...
<img height="1" alt="" width="1" src="http://feeds.feedburner.com/~r/RBloggers/~4/T5ZNZr9BBf4"></a></div>