13.1: Sampling Error (Section @ref{samplingerror})

Statistical Thinking for the 21st Century · LibreTexts

0%

Contents

    Text

    Size
    100%
    Theme
    Typeface
    Line length
    Spacing

    Read aloud

    How

    Speed: 1×
    Light up
    Highlight

    Symbols: Mulberry Symbols, © 2018–2026 Steve Lee, CC BY-SA 4.0 (licence, the set), the site's own copies.

    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.

    13.1: Sampling Error (Section @ref{samplingerror})

    Here we will repeatedly sample from the NHANES Height variable in order to obtain the sampling distribution of the mean.

    sampSize <- 50 # size of sample
    nsamps <- 5000 # number of samples we will take
    
    # set up variable to store all of the results
    sampMeans <- tibble(meanHeight=rep(NA,nsamps))
    
    # Loop through and repeatedly sample and compute the mean
    for (i in 1:nsamps) {
      sampMeans$meanHeight[i] <- NHANES_adult %>%
        sample_n(sampSize) %>%
        summarize(meanHeight=mean(Height)) %>%
        pull(meanHeight)
    }

    Now let’s plot the sampling distribution. We will also overlay the sampling distribution of the mean predicted on the basis of the population mean and standard deviation, to show that it properly describes the actual sampling distribution.

    # pipe the sampMeans data frame into ggplot
    sampMeans %>% 
      ggplot(aes(meanHeight)) +
      # create histogram using density rather than count
      geom_histogram(
        aes(y = ..density..),
        bins = 50,
        col = "gray", 
        fill = "gray"
      ) +
      # add a vertical line for the population mean
      geom_vline(xintercept = mean(NHANES_adult$Height),
                 size=1.5) +
      # add a label for the line
      annotate(
        "text",
        x = 169.6, 
        y = .4,
        label = "Population mean",
        size=6
      ) +
      # label the x axis
      labs(x = "Height (inches)") +
      # add normal based on population mean/sd
      stat_function(
          fun = dnorm, n = sampSize,
          args = list(
            mean = mean(NHANES_adult$Height),
            sd = sd(NHANES_adult$Height)/sqrt(sampSize)
          ), 
          size = 1.5,
          color = "black",
          linetype='dotted'
        ) 

    file69.png


    This page titled 13.1: Sampling Error (Section @ref{samplingerror}) was last modified on Mon, 11 Jan 2021 05:14:03 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.