Skip to main content
Statistics LibreTexts

19.3: Simulating Statistical Power

  • Page ID
    8816
  • \( \newcommand{\vecs}[1]{\overset { \scriptstyle \rightharpoonup} {\mathbf{#1}} } \) \( \newcommand{\vecd}[1]{\overset{-\!-\!\rightharpoonup}{\vphantom{a}\smash {#1}}} \)\(\newcommand{\id}{\mathrm{id}}\) \( \newcommand{\Span}{\mathrm{span}}\) \( \newcommand{\kernel}{\mathrm{null}\,}\) \( \newcommand{\range}{\mathrm{range}\,}\) \( \newcommand{\RealPart}{\mathrm{Re}}\) \( \newcommand{\ImaginaryPart}{\mathrm{Im}}\) \( \newcommand{\Argument}{\mathrm{Arg}}\) \( \newcommand{\norm}[1]{\| #1 \|}\) \( \newcommand{\inner}[2]{\langle #1, #2 \rangle}\) \( \newcommand{\Span}{\mathrm{span}}\) \(\newcommand{\id}{\mathrm{id}}\) \( \newcommand{\Span}{\mathrm{span}}\) \( \newcommand{\kernel}{\mathrm{null}\,}\) \( \newcommand{\range}{\mathrm{range}\,}\) \( \newcommand{\RealPart}{\mathrm{Re}}\) \( \newcommand{\ImaginaryPart}{\mathrm{Im}}\) \( \newcommand{\Argument}{\mathrm{Arg}}\) \( \newcommand{\norm}[1]{\| #1 \|}\) \( \newcommand{\inner}[2]{\langle #1, #2 \rangle}\) \( \newcommand{\Span}{\mathrm{span}}\)\(\newcommand{\AA}{\unicode[.8,0]{x212B}}\)

    Let’s simulate this to see whether the power analysis actually gives the right answer. We will sample data for two groups, with a difference of 0.5 standard deviations between their underlying distributions, and we will look at how often we reject the null hypothesis.

    nRuns <- 5000
    effectSize <- 0.5
    # perform power analysis to get sample size
    pwr.result <- pwr.t.test(d=effectSize, power=.8)
    # round up from estimated sample size
    sampleSize <- ceiling(pwr.result$n)
    
    # create a function that will generate samples and test for
    # a difference between groups using a two-sample t-test
    
    get_t_result <- function(sampleSize, effectSize){
      # take sample for the first group from N(0, 1)
      group1 <- rnorm(sampleSize)
      group2 <- rnorm(sampleSize, mean=effectSize)
      ttest.result <- t.test(group1, group2)
      return(tibble(pvalue=ttest.result$p.value))
    }
    
    index_df <- tibble(id=seq(nRuns)) %>%
      group_by(id)
    
    power_sim_results <- index_df %>%
      do(get_t_result(sampleSize, effectSize))
    
    p_reject <-
      power_sim_results %>%
      ungroup() %>%
      summarize(pvalue = mean(pvalue<.05)) %>%
      pull()
    
    p_reject
    ## [1] 0.8

    This should return a number very close to 0.8.


    This page titled 19.3: Simulating Statistical Power is shared under a not declared license and was authored, remixed, and/or curated by Russell A. Poldrack via source content that was edited to the style and standards of the LibreTexts platform; a detailed edit history is available upon request.

    • Was this article helpful?