Skip to main content
Statistics LibreTexts

19: Statistical power in R

  • Page ID
    7658
  • 19 Statistical power in R

    In this chapter we focus specifically on statistical power.

    19.1 Power analysis

    We can compute a power analysis using functions from the pwr package. Let’s focus on the power for a t-test in order to determine a difference in the mean between two groups. Let’s say that we think than an effect size of Cohen’s d=0.5 is realistic for the study in question (based on previous research) and would be of scientific interest. We wish to have 80% power to find the effect if it exists. We can compute the sample size needed for adequate power using the pwr.t.test() function:

    ## 
    ##      Two-sample t test power calculation 
    ## 
    ##               n = 64
    ##               d = 0.5
    ##       sig.level = 0.05
    ##           power = 0.8
    ##     alternative = two.sided
    ## 
    ## NOTE: n is number in *each* group

    Thus, about 64 participants would be needed in each group in order to test the hypothesis with adequate power.

    19.2 Power curves

    We can also create plots that can show us how the power to find an effect varies as a function of effect size and sample size. We willl use the crossing() function from the tidyr package to help with this. This function takes in two vectors, and returns a tibble that contains all possible combinations of those values.

    ## Observations: 150
    ## Variables: 2
    ## $ effect_sizes <dbl> 0.2, 0.2, 0.2, 0.2, 0.2, 0.2, 0.2, 0…
    ## $ sample_sizes <dbl> 10, 20, 30, 40, 50, 60, 70, 80, 90, …

    Using this, we can then perform a power analysis for each combination of effect size and sample size to create our power curves. In this case, let’s say that we wish to perform a two-sample t-test.

    Now we can plot the power curves, using a separate line for each effect size.

    file90.png