Conducting a t-test in R (exercise)

You are checking whether the average stress level differs statistically significantly between male and female participants. Using a t-test (and, if necessary, a Mann–Whitney U test), you will learn how to turn a research question into a testable hypothesis—and how to test it in R or SPSS.

Task

Dataset: Einfach_Stressvergleich

Research question:
Does the average stress level of male and female participants in your sample differ statistically significantly?

Hypotheses:
$H_0$: $\mu_{\text{male}} = \mu_{\text{female}}$
$H_1$: $\mu_{\text{male}} e \mu_{\text{female}}$

Solution in R

# t-test
# Import data
df <- read.csv("Data/Einfach_Stressvergleich.csv")

# Data management
group1 <- df[df$gruppe == "männlich", "stress"]
group2 <- df[df$gruppe == "weiblich", "stress"]

# Assumptions
## Homogeneity of variances
library(car)
car::leveneTest(df$stress, group = as.factor(df$gruppe))

## Normal distribution
shapiro.test(group1) #not normally distributed
shapiro.test(group2) #normally distributed

hist(group1)
hist(group2)

# t-test
group1 <- df[df$gruppe == "männlich", "stress"]
group2 <- df[df$gruppe == "weiblich", "stress"]

t.test(x = group1, y = group2) #Welch t-test (as an alternative)
t.test(x = group1, y = group2, var.equal = TRUE) #classic t-test

# U test
wilcox.test(x = group1, y = group2)

Solution in SPSS or PSPP

1. Import data

  • Open SPSS
  • File → Open → select Einfach_Stressvergleich.csv

2. Check the prerequisites

a) Check normality (for each group)

  • Menu: Analyze → Descriptive Statistics → Explore
  • Dependent variable: stress
  • Grouping variable: gruppe
  • In the Plots window, activate “Normality plots with tests”
  • Click OK
    → You will receive Shapiro-Wilk tests for each group

b) Check the equality of variances

This test is automatically included with the t-test (Levene’s test).


3. Independent-samples t-test

  • Menu: Analyze → Compare Means → Independent-Samples T Test
  • Test variable: stress
  • Grouping variable: gruppe → click “Define Groups” (e.g., männlich, weiblich)
  • Click OK
    → You will receive the t-value, p-value, mean difference, confidence interval, and Levene’s test

4. Nonparametric test (Mann-Whitney U)

If the normality assumption is not met:

  • Menu: Analyze → Nonparametric Tests → Two Independent Samples
  • Test variable: stress
  • Grouping variable: gruppe
  • Test: Mann-Whitney U
  • Click OK

Alles klar?

Ich hoffe, der Beitrag war für dich soweit verständlich. Wenn du weitere Fragen hast, nutze bitte hier die Möglichkeit, eine Frage an mich zu stellen!

Stelle Dominik eine Frage