# A tibble: 2 × 5
side mean_length sd_length se_length count
<chr> <dbl> <dbl> <dbl> <int>
1 shady 17.6 2.51 0.886 8
2 sunny 16.2 2.64 0.934 8
one sample
two sample
# A tibble: 2 × 5
side mean_length sd_length se_length count
<chr> <dbl> <dbl> <dbl> <int>
1 shady 17.6 2.51 0.886 8
2 sunny 16.2 2.64 0.934 8
The goals for today
One-tailed questions: area of distribution left or (right) of a certain value for a one sample test
xxxx
Two-tailed questions refer to area between certain values
Let’s calculate CIs again:
Use two-sided test
So:
We want to test if the mean needle length on one side differs from 15mm.
H₀: μ = 15 (The mean needle length on shade side is 15mm)
H₁: μ ≠ 15 (The mean needle length on shade side is not 240mm)
# YOUR TASK: Test normality of all pine needle lengths
# QQ Plot
qqPlot(ps_df$length_mm,
main = "QQ Plot for length of pine needles",
ylab = "Sample Quantiles")
[1] 8 11
Practice Exercise 1: One-Sample t-Test
Let’s perform a one-sample t-test to determine if the mean needle length on the shady side differs from 15 mm:
# what is the mean
ps_shade_mean <- mean(ps_shady_df$length_mm, na.rm = TRUE)
cat("Mean:", round(ps_shade_mean, 1), "mm\n")
Mean: 17.6 mm
One Sample t-test
data: ps_shady_df$length_mm
t = 2.9414, df = 7, p-value = 0.02167
alternative hypothesis: true mean is not equal to 15
95 percent confidence interval:
15.51092 19.70030
sample estimates:
mean of x
17.60561
Interpret this test result by answering these questions:
Hypothesis testing is a systematic way to evaluate research questions using data.
Key components:
Decision rule: Reject Ho if p-value less than α or shorthand p < 0.05
Hypothesis testing is a systematic way to evaluate research questions using data.
Key components:
Decision rule: Reject H₀ if p-value < α
Activity: Interpret the t-test results
How to report this result in a scientific paper:
“A one-sample t-test at α=0.05 showed that the mean needle length (… mm, SD = …) [was/was not] significantly different from the expected 15 mm, t(…) = …, p = …”
For example
Practice Exercise 2: Formulating Hypotheses
For the following research questions about needle lengths write the null and alternative hypotheses:
What are the hypotheses?
Ho =
Ha =
Now, let’s compare needles lengths from the two sides
Question: Is there a significant difference in needle length between the sides?
This requires a two-sample t-test.
Two-sample t-test compares means from two independent groups.
Practice Exercise 3: Calculate summary statistics grouped by lake
Before conducting the test, we need to understand the data for each group.
You need this and the graph to see what is going on ….
group_summary <- ps_df %>%
group_by(side) %>%
summarize(
mean_length = mean(length_mm),
sd_length = sd(length_mm),
n = n(),
se_length = sd_length / sqrt(n)
)
group_summary
# A tibble: 2 × 5
side mean_length sd_length n se_length
<chr> <dbl> <dbl> <int> <dbl>
1 shady 17.6 2.51 8 0.886
2 sunny 16.2 2.64 8 0.934
Practice Exercise 5: Using GGPLOT to get summary plot
GGplot also has code to make the mean and standard error plots we are interested in along with a lot of others
# Assuming your dataframe is called df
needle_mean_se_plot <- ggplot(ps_df, aes(x = side, y = length_mm, color = side)) +
stat_summary(fun = mean, geom = "point") +
stat_summary(fun.data = mean_se, geom = "errorbar", width = 0.2) +
labs(
x = "side",
y = "Mean Length (mm)") +
theme_classic()
needle_mean_se_plot
For a two-sample t-test, we need to check:
If assumptions are violated:
Practice Exercise 7: Test normality of sunny pine needle lengths
Note you need to test each groups separately…
# A tibble: 6 × 5
# Groups: group, tree_no, tree_char [6]
group tree_no tree_char side length_mm
<chr> <dbl> <chr> <chr> <dbl>
1 big_fat_fecund_female_fish 2 tree_2 shady 15.4
2 bill 3 tree_3 shady 16.7
3 ciabatta 5 tree_5 shady 19.1
4 fake_data 8 tree_8 shady 17.4
5 five 1 tree_1 shady 20.3
6 moose_walkin 7 tree_7 shady 20.7
# A tibble: 6 × 5
# Groups: group, tree_no, tree_char [6]
group tree_no tree_char side length_mm
<chr> <dbl> <chr> <chr> <dbl>
1 big_fat_fecund_female_fish 2 tree_2 sunny 13.2
2 bill 3 tree_3 sunny 16.0
3 ciabatta 5 tree_5 sunny 17.7
4 fake_data 8 tree_8 sunny 13.0
5 five 1 tree_1 sunny 19.9
6 moose_walkin 7 tree_7 sunny 18.4
Practice Exercise 8: Test Normality at one time
There are always a lot of ways to do this in R
# there are always two ways
# Test for normality using Shapiro-Wilk test for each wind group
# All in one pipeline using tidyverse approach
normality_results <- ps_df %>%
group_by(side) %>%
summarize(
shapiro_stat = shapiro.test(length_mm)$statistic,
shapiro_p_value = shapiro.test(length_mm)$p.value,
normal_distribution = if_else(shapiro_p_value > 0.05, "Normal", "Non-normal"))
normality_results
# A tibble: 2 × 4
side shapiro_stat shapiro_p_value normal_distribution
<chr> <dbl> <dbl> <chr>
1 shady 0.966 0.868 Normal
2 sunny 0.900 0.289 Normal
Practice Exercise 13: Test equal variances
Levenes test can be done on the original dataframe
Note: the Levenes Test should be NOT SIGNIFICANT - What is the null hypothesis
# Method 1: Using car package's leveneTest
# This is often preferred as it's more robust to departures from normality
levene_result <- leveneTest(length_mm ~ side, data = ps_df)
print("Levene's Test for Homogeneity of Variance:")
[1] "Levene's Test for Homogeneity of Variance:"
Levene's Test for Homogeneity of Variance (center = median)
Df F value Pr(>F)
group 1 0.2062 0.6567
14
Now we can compare the mean needle lengths between shady and sunny sides.
Ho: μ₁ = μ₂ (The needle lengths do not differ)
Ha: μ₁ ≠ μ₂ (The mean needle lengths differ - direction is not specified)
Calculate t-statistic manually (optional) - YOUR CODE HERE:
t = (mean1 - mean2) / sqrt((s1^2/n1) + (s2^2/n2))
Deciding between:
# YOUR TASK: Conduct a two-sample t-test
# Use var.equal=TRUE for standard t-test or var.equal=FALSE for Welch's t-test
# Standard t-test (if variances are equal)
t_test_result <- t.test(length_mm ~ side, data = ps_df, var.equal = TRUE)
print("Standard two-sample t-test:")
[1] "Standard two-sample t-test:"
Two Sample t-test
data: length_mm by side
t = 1.1279, df = 14, p-value = 0.2783
alternative hypothesis: true difference in means between group shady and group sunny is not equal to 0
95 percent confidence interval:
-1.309330 4.214005
sample estimates:
mean in group shady mean in group sunny
17.60561 16.15328
Now we can compare the mean needle lengths between shady and sunny sides.
Ho: μ₁ = μ₂ (The needle lengths do not differ)
Ha: μ₁ ≠ μ₂ (The mean needle lengths differ - direction is not specified)
Calculate t-statistic manually (optional) - YOUR CODE HERE:
t = (mean1 - mean2) / sqrt((s1^2/n1) + (s2^2/n2))
Deciding between:
# YOUR TASK: Conduct a two-sample t-test
# Use var.equal=TRUE for standard t-test or var.equal=FALSE for Welch's t-test
# Standard t-test (if variances are equal)
t_test_result <- t.test(length_mm ~ side, data = ps_df, var.equal = FALSE)
print("Welches two-sample t-test:")
[1] "Welches two-sample t-test:"
Welch Two Sample t-test
data: length_mm by side
t = 1.1279, df = 13.96, p-value = 0.2784
alternative hypothesis: true difference in means between group shady and group sunny is not equal to 0
95 percent confidence interval:
-1.310069 4.214743
sample estimates:
mean in group shady mean in group sunny
17.60561 16.15328
Interpret the results of the two-sample t-test
What can we conclude about the needle lengths on sunny vs shady sides?
How to report this result in a scientific paper:
“A two-tailed, two-sample t-test at α=0.05 showed [a significant/no significant] difference in needle length between sunny (M = …, SD = …) and shady (M = …, SD = …) sides of pine trees, t(…) = …, p = ….”
Paired t-test:
Compares two measurements from the same subjects or matched pairs Tests whether the mean difference between paired observations equals zero Examples: before/after measurements on the same people, left vs right measurements, matched case-control studies Uses the differences between pairs as the data points Generally more powerful because it controls for individual variation
# YOUR TASK: Conduct a two-sample t-test
# Use var.equal=TRUE for standard t-test or var.equal=FALSE for Welch's t-test
ps_wide_df <- ps_df %>%
pivot_wider(
names_from = "side",
values_from = length_mm
)
# Standard t-test (if variances are equal)
paired_t_test_result <- t.test(ps_wide_df$sunny, ps_wide_df$shady, paired = TRUE)
print("Standard two-sample t-test:")
[1] "Standard two-sample t-test:"
Paired t-test
data: ps_wide_df$sunny and ps_wide_df$shady
t = -2.7818, df = 7, p-value = 0.02723
alternative hypothesis: true mean difference is not equal to 0
95 percent confidence interval:
-2.6868652 -0.2178092
sample estimates:
mean difference
-1.452337
Note that thee is a lot of variation within trees but the trend is the same
Common assumptions for t-tests:
What can we do if our data violates these assumptions?
Alternatives when assumptions are violated:
In this activity, we’ve:
Key takeaways: