17.2: Simulating p-values

Statistical Thinking for the 21st Century · LibreTexts

0%

Contents

    Text

    Size
    100%
    Theme
    Typeface
    Line length
    Spacing

    From LibreTexts, under CC BY-NC 4.0, as the source states it; its equations are written in TeX, which the PDF shows typeset. This page at LibreTexts.

    17.2: Simulating p-values

    In this exercise we will perform hypothesis testing many times in order to test whether the p-values provided by our statistical test are valid. We will sample data from a normal distribution with a mean of zero, and for each sample perform a t-test to determine whether the mean is different from zero. We will then count how often we reject the null hypothesis; since we know that the true mean is zero, these are by definition Type I errors.

    nRuns <- 5000
    
    # create input data frame for do()
    input_df <- tibble(id=seq(nRuns)) %>%
      group_by(id)
    
    # create a function that will take a sample
    # and perform a one-sample t-test
    
    sample_ttest <- function(sampSize=32){
      tt.result <- t.test(rnorm(sampSize))
      return(tibble(pvalue=tt.result$p.value))
    }
    
    # perform simulations
    
    sample_ttest_result <- input_df %>%
      do(sample_ttest())
    
    p_error <-
      sample_ttest_result %>%
      ungroup() %>%
      summarize(p_error = mean(pvalue<.05)) %>%
      pull()
    
    p_error
    ## [1] 0.048

    We should see that the proportion of samples with is about 5%.


    This page titled 17.2: Simulating p-values was last modified on Mon, 11 Jan 2021 05:13:58 GMT and is shared under a CC BY-NC 4.0 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.