Paired Samples T-Test

Lecture 14

Dave Brocker

Farmingdale State College

Introduction to Paired Samples t-Test

What is a Paired Samples t-Test?

  • A statistical method used to compare the means of two related groups.
  • Commonly used when measuring the same group before and after an intervention or over two different conditions.

Purpose of Paired Samples t-Test

Why Use Paired Samples t-Test?

  • To determine if there is a statistically significant difference between two related group means.
  • Useful in psychology for assessing changes due to an intervention, therapy, or experiment.

Formula for Paired Samples t-Test

Mathematical Representation

  • Formula: \(\large{t = \frac{\bar{D}}{\frac{s_D}{\sqrt{n}}}}\)
    • \(\bar{D}\): Mean of the differences between paired observations.
    • \(s_D\): Standard deviation of the differences.
    • \(n\): Number of pairs.

Theoretical Formula Explanation

Understanding Each Component

  • Difference Mean \(\bar{D}\): Average of the differences between paired scores.
  • Standard Deviation of Differences \(s_D\): Indicates how much individual differences deviate from the mean difference.
  • Sample Size \(n\): Number of pairs, affecting the significance of results.

Application of Paired Samples t-Test

When to Apply This Test

  • Pretest-Posttest Design: Measure changes in the same group over time.
  • Matched Pairs: Compare two related groups, such as twins or matched participants.
  • Repeated Measures: Test the same group under different conditions or at different times.

Paired Samples t-Test Example

Paired Samples t-Test

Application in Psychology Research

A psychologist measures stress levels of a group before and after a mindfulness program.

Steps:

  • Calculate the difference between pre- and post-intervention scores.
  • Find the mean \(\bar{D}\) and standard deviation \(s_D\) of the differences.
  • Apply the formula to calculate the t-value and check for significance.

Paired Samples t-Test

Application in Psychology Research

Code
# Load Packages
library(dplyr)
library(broom)
library(gt)

# Random Seed
set.seed(20)

# Create data
data <- 
  tibble(
  # Make a variable, `group` with 30 T1 and 30 T2
  grp = rep(c("T1","T2"), each = 30),
  # Make a variable, `score` with 60 scores
  score = c(rnorm(30,8,2),rnorm(30,4,1)) |> round(0)
) 

Paired Samples t-Test

Data Preview

Code
set.seed(20)
pt_data <- 
  tibble(
  grp = rep(c("T1","T2"), each = 30),
  score = c(rnorm(30,8,2),rnorm(30,4,1)) |> round(0)
) 

pt_data|> 
  slice_sample(n = 5) |> 
  gt()
grp score
T1 8
T2 5
T2 4
T2 5
T1 6

Paired Samples t-Test

Compare Scores

Code
data_new <- 
  pt_data |> 
  group_by(grp) |> 
  mutate(
    id = row_number()
  ) |> 
  tidyr::pivot_wider(
                     names_from = grp,
                     values_from = score
                     ) |> 
  ungroup() |> 
  mutate(
    D = T2-T1,
    Dfi = ifelse(stringr::str_detect(D,"^-"),paste0("(",D,")"),D)
  )

data_new |> 
  select(-D) |> 
  gt_preview()
id T1 T2 Dfi
1 1 10 4 (-6)
2 2 7 6 (-1)
3 3 12 6 (-6)
4 4 5 5 0
5 5 7 3 (-4)
6..29
30 30 7 4 (-3)

Paired Samples t-Test

Construct the Formula (\(\bar{D}\) | \(\sigma_{\bar{D}}\))

\[\large{t = \frac{D}{\frac{\sigma}{\sqrt{n}}} = \frac{-3.26}{\frac{2.083}{\sqrt{30}}} = (-8.57)}\]

Paired Samples t-Test

Construct the Formula (\(\bar{D}\) | \(\sigma_{\bar{D}}\))

Code
t.test(pt_data$score~pt_data$grp) |> 
  tidy() |> 
  rename(
    D = estimate,
    t = statistic, 
    p = p.value,
    df = parameter) |> 
  mutate(
    `CI[ll,uu]` = paste0("[",conf.low |> round(2),", ",conf.high |> round(2),"]"),
    p = p |> round(10)
  ) |> 
  select(-conf.low:-alternative) |> 
  select(D,t,df,p,`CI[ll,uu]`) |> 
  gt()
D t df p CI[ll,uu]
3.266667 7.58112 41.65179 2.3e-09 [2.4, 4.14]
  • D = mean difference of T2-T1
  • t = t-test statistic
  • df = n-1 (30-1)
  • p = probability results came from the Null Distribution
  • CI[ll,uu] = 95% CI of Mean Difference (D)

Paired T-Tests

Visualizing Results: Option 1

Code
library(ggplot2)
library(viridis)

data |> 
  ggplot(aes(grp, score, fill = grp)) + 
  stat_summary(
    fun = "mean",
    geom = "bar"
  ) + 
  stat_summary(
    fun.data = "mean_se",
    geom = "errorbar",
    width=.2
  ) +
  theme_minimal() +
  labs(
    x = "\nTreatment Group",
    y = "Stress Score\n",
    fill = "Time",
    title = "Mindfulness Intervention decreases stress levels\n"
  ) + 
  theme(
    plot.title.position =  "plot",
    plot.title = element_text(face = "bold")
  ) +
  scale_fill_viridis(discrete = TRUE, option = "E")

Paired T-Tests

Visualizing Results: Option 2

Code
library(ggplot2)
library(viridis)

data |> 
  ggplot(aes(grp, score, color = grp)) + 
  geom_jitter(aes(alpha = .2)) + 
  stat_summary(
    fun.data = "mean_se",
    geom = "line",
    group = 1,
    color = "black"
  ) + 
  stat_summary(
    fun.data = "mean_se",
    geom = "pointrange"
  ) +
  theme_minimal() +
  labs(
    x = "\nTreatment Group",
    y = "Stress Score\n",
    fill = "Time",
    title = "Mindfulness Intervention decreases stress levels\n"
  ) + 
  theme(
    plot.title.position =  "plot",
    plot.title = element_text(face = "bold"),
    legend.position = "none"
  ) +
  scale_color_manual(values = c("darkgreen","darkred"))

Summary of Paired Samples t-Test

Key Takeaways

  • Paired samples t-tests are ideal for comparing two related groups.
  • They help determine if interventions or conditions produce significant changes.
  • Remember to check assumptions: normality of differences and appropriate pairing.