Lab 10: t-tests

Learning Objectives

By the end of this lab, you will be able to:

  • use histograms to assess distribution shape and potential outliers
  • choose and perform the appropriate test (t-test or Wilcoxon test) based on the data
  • perform one-sample, two-sample, and paired comparisons in R
  • extract and report p-values and confidence intervals from test output
  • interpret test results in the context of the question

Getting started

Before you begin the lab activities, review the steps for setting up a project for a lab activity:

Then, complete the following steps to make sure you are fully set up.

  1. Get the Lab Worksheet.

    Pick up a physical copy of the lab worksheet, or print one if you are working outside of class.
    Download Lab Worksheet (PDF, if needed)

  2. Open Posit Cloud and create Lab 10 in Your Workspace.

  3. Create a new R script and save it as lab-10-script.R.

  4. Add the following code to your R script and save the script. Upon save, you should see a message in the source prompting you to install the packages. Click “Install”. Then run the line of code.

    # load packages ----------------------------------------------
    
    library(tidyverse)
    library(haven)
    library(lterdatasampler)
    library(PairedData)
NoteCheckpoint

At this point, you should have:

  • These instructions open in a web browser.
  • Your Lab 10 project open in Posit Cloud in another browser window.
  • The required packages installed and loaded without errors
  • The Lab 10 worksheet in front of you.

Do not continue until all of the above steps are working correctly.

Overview

In this lab, you will use statistical tests to compare means between groups and against a reference value. You will work with three common study designs:

  • a one-sample comparison (sleep duration vs a recommended value)
  • a two-sample comparison (body mass between male and female bison)
  • a paired comparison (differences within individuals in shoulder strength)

For each case, you will:

  • visualize the data using histograms
  • assess whether assumptions for a t-test are reasonable
  • choose an appropriate test (e.g. t-test or Wilcoxon test)
  • run the test in R
  • interpret p-values and confidence intervals in context

The goal is to connect data visualization, statistical testing, and interpretation so that you can justify your choice of method and clearly explain your conclusions.

Questions

1. Sleep duration: one-sample test

Public health guidelines often recommend that adults get about 8 hours of sleep per night. We can use sample data to evaluate whether typical sleep duration differs from this recommendation.

The NHANES dataset contains self-reported sleep duration (in hours) for a sample of individuals. We will examine whether the average sleep duration in this sample differs from 8 hours.

Use the code below to read in the data.

Code
# question 1 ----------------------------------------------

slq_data <-
  haven::read_xpt("https://wwwn.cdc.gov/Nchs/Data/Nhanes/Public/2021/DataFiles/SLQ_L.xpt") |> 
  drop_na(SLD012) |>
  slice_head(n = 100) 
  1. Plot a histogram of sleep duration (variable SLD012). Use a binwidth of 1 and ensure the first bin starts at 0. Does the distribution appear approximately normal? Are there any strong deviations (e.g., skew or outliers) that would affect a t-test?

  2. Write the R code to perform a one-sample t-test to test whether mean sleep duration differs from 8 hours.

    Use the vector form of the t.test() function, with slq_data$SLD012 as the data and mu = 8.

  3. What is the 95% confidence interval for the mean sleep duration? Does this interval include 8 hours?

  4. Based on your results, what do you conclude about whether average sleep duration differs from 8 hours in this sample?

2. Bison body mass: two-sample comparison

Body mass often differs between males and females in many animal species. We can use sample data to evaluate whether body mass differs between male and female bison.

In this exercise, you will use the Konza Prairie (KNZ) bison dataset. The dataset includes body mass (animal_weight) and sex (animal_sex) for individual bison.

Use the code in the box below to read in the data.

Code
# question 2 ----------------------------------------------

bison_data <-
  lterdatasampler::knz_bison |> 
  drop_na(animal_sex, animal_weight) |>
  slice_head(n = 100, by = animal_sex)
  1. Plot histograms of body mass (animal_weight) for males and females. Use a binwidth of 50 and display the groups in separate panels. Do the distributions appear approximately normal? Do the variances look similar between groups? Are there any strong deviations that would affect a t-test?

  2. Which test is more appropriate? a two-sample t-test or a Wilcoxon rank-sum test?

  3. Write the R code to perform the test to compare body mass between males and females using either t.test() or wilcox.test(). Use the formula form of the function, with animal_weight as the response and animal_sex as the grouping variable.

    Remember that the formula form requires a data argument and a formula argument, e.g. *.test(formula = grouping_variable ~ response_variable, data = df).

  4. Run the test and report the p-value. Based on this value, do you conclude that body mass differs between males and females?

3. Paired comparison: shoulder strength and swimming

Dominant limbs are typically stronger than non-dominant limbs. However, activities such as swimming may lead to more balanced strength between arms. We can use paired measurements to evaluate whether individuals tend to have stronger dominant arms, and whether this pattern differs between swimmers and non-swimmers.

The dataset contains shoulder strength measurements for the left and right arms of individuals, along with a grouping variable indicating whether each individual is a Swimmer or Control (non-swimmer).

Use the code in the box below to prepare the data, including calculating strong arm and weak arm for each individual.

Code
# question 3 ----------------------------------------------

data("Shoulder", package = "PairedData")

shoulder_data <-
  Shoulder |>
  as_tibble() |> 
  rowwise() |>
  mutate(
    strongarm = max(Left, Right),
    weakarm = min(Left, Right)
  )

swimmers <- 
  shoulder_data |> 
  filter(Group == "Swimmer")

nonswimmers <-
  shoulder_data |> 
  filter(Group == "Control")
  1. Explain why directly comparing left vs right arm strength may not be appropriate for this dataset. Why is it better to compare strong arm vs weak arm?

  2. Write the R code to perform a paired t-test comparing strong arm and weak arm strength for swimmers. Use paired = TRUE.

  3. Run the test for swimmers. Report the p-value. Based on this value, do swimmers tend to have a difference between strong and weak arm strength?

  4. Repeat the paired t-test for non-swimmers. Report the p-value. Do non-swimmers show a difference between strong and weak arm strength?

  5. Based on your results, what do you conclude about differences in arm strength within individuals, and how these patterns differ between swimmers and non-swimmers?

Wrap-up and Submission

  1. Make sure your script is saved in your project on Posit Cloud.
  2. Ask the instructor to explain anything you aren’t sure about.
  3. Show your handout to a Learning Assistant for a completion grade before you leave lab. You may do this as soon as you finish. Keep the handout for yourself.