
Foundations of inference

Practice Exercise 1: Exploring the Grayling Dataset
Let’s explore the Arctic grayling data from lakes I3 and I8. Use the grayling_df data frame to create basic summary statistics.
# Write your code here to explore the basic structure of the data
# also note plotting a box plot is really useful
# Calculate summary statistics
grayling_summary <- grayling_df %>%
group_by(lake) %>%
summarize(
mean_length = mean(length_mm, na.rm = TRUE),
sd_length = sd(length_mm, na.rm = TRUE),
se_length = sd_length/sqrt(sum(!is.na(length_mm))),
count = sum(!is.na(length_mm)),
.groups = "drop")
grayling_summary# A tibble: 2 × 5
lake mean_length sd_length se_length count
<chr> <dbl> <dbl> <dbl> <int>
1 I3 266. 28.3 3.48 66
2 I8 363. 52.3 5.18 102
Note
📖 Reference
Gotelli & Ellison, A Primer of Ecological Statistics, Ch. 2 — Random Variables and Probability Distributions.

The standard normal distribution is crucial for understanding statistical inference:
Z-scores allow us to convert any normal distribution to the standard normal distribution.
Note
📖 Reference
Whitlock & Schluter, Analysis of Biological Data, Ch. 10 — The Normal Distribution.

Practice Exercise 2: Calculating Z-scores
Let’s practice converting raw values to Z-scores using the Arctic grayling data.
Z Score = (length - mean) / standard deviation
# Calculate the mean and standard deviation of fish lengths
mean_length <- mean(i3_df$length_mm, na.rm = TRUE)
sd_length <- sd(i3_df$length_mm, na.rm = TRUE)
# Calculate Z-scores for fish lengths
i3_df <- i3_df %>%
mutate(z_score = (length_mm - mean_length) / sd_length)
# View the first few rows with Z-scores
head(i3_df)# A tibble: 6 × 6
site lake species length_mm mass_g z_score
<dbl> <chr> <chr> <dbl> <dbl> <dbl>
1 113 I3 arctic grayling 266 135 0.0139
2 113 I3 arctic grayling 290 185 0.862
3 113 I3 arctic grayling 262 145 -0.127
4 113 I3 arctic grayling 275 160 0.332
5 113 I3 arctic grayling 240 105 -0.905
6 113 I3 arctic grayling 265 145 -0.0214
So if we plot this data what does it look like in a standard normal distribution?

Proportion within 1 standard deviation = sum of absolute values of Z Scores that are less than or equal to 1 divided by the number in the sample…
Remember in a true normal distribution it is 68% within 1 std dev.
should be approximately (varies if distribution is not normal):
You want to know things about this population like
# A tibble: 1 × 1
mean_length
<dbl>
1 266.

Standard Normal Distribution
~68% of the curve area within +/- 1 σ of the mean,
~95% within +/- 2 σ of the mean,
~99.7% within +/- 3 σ of the mean
*remember σ = standard deviation

Areas under curve of Standard Normal Distribution
transformed into the standard normal distribution
a value can be looked up in a table

Done by converting original data points to z-scores
So lets do this for a fish that is 300mm long and guess the probability of catching something larger
z = (300 - 265.61)/28.3 = 1.215194

Done by converting original data points to z-scores
So lets do this for a fish that is 300mm long and guess the probability of catching something larger
At what point do you think its not likely to catch a larger fish - what percentage?
do this the other way using that percent and why?

We can use R to get these values easier…
# For standard normal distribution (mean=0, sd=1):
[1] 0.8887676
[1] 0.1112324
[1] 0.9544997
[1] 0.9500042
[1] 1.959964
[1] 1.644854
Lets say we are interested in knowing at what point from I3 it is not likely to catch a larger fish?
Maybe we expect 95% of the time to catch a fish that is “common” but the 5% is the unlikely portion….
Only 5% of fish are longer than: 312.2 mm
This corresponds to z-score: 1.645
Every probability we just calculated assumed a normal distribution. Let’s check that assumption.
So how wrong were we?
| normal theory | actual data | |
|---|---|---|
| 95th percentile | 312.2 mm | 310.0 mm |
| P(fish > 300 mm) | 11.2% | 7.6% |
The percentile is fine. The tail probability is off by a third — and the tails are exactly where we do hypothesis testing.
This is why we check assumptions before trusting a p-value.
Shapiro-Wilk normality test
data: i3_df$length_mm
W = 0.91051, p-value = 0.0001623
normal theory P(>300): 11.2 %
actual P(>300): 7.6 %
We have now used the normal curve for two different things. Keep them straight:
| Question | Spread to use |
|---|---|
| How long is a single fish? | SD (s) |
| Where is the population mean? | SE (s/√n) |
That is why:
Using SE when you meant SD makes your interval about √n times too narrow.
SD (spread of fish): 28.3 mm
SE (spread of means): 3.5 mm
n = 66 -> SE is 8.1 x smaller
We can look at Standard normal distributions and know probability of a value being in a range under the standard normal curve…
Previously we had calculated Standard Error and Confidence Intervals -
Instead, we use Student’s t distribution

Small sample sizes
When population standard deviation is unknown
Calculating confidence intervals
Conducting t-tests

Where:




Two-tailed questions refer to area between certain values


Let’s calculate CIs again:
Use two-sided test

Practice Exercise 4: Using the t-distribution
Let’s compare confidence intervals using the normal approximation (z) versus the t-distribution for our fish data — a random sample of 10 fish from I3.
Look at how much wider the t interval is: with n = 10 the critical value is t = 2.262 rather than z = 1.96, about 15% wider. That extra width is the price of having estimated σ from only 10 fish.
## \(\text{CI} = \bar{y} \pm t \cdot \frac{s}{\sqrt{n}}\)
# Display results
cat("Mean:", round(sample_mean, 1), "mm\n",
"Standard deviation:", round(sample_sd, 2), "mm\n",
"Standard error:", round(sample_se, 2), "mm\n",
"95% CI using z:", round(z_ci_lower, 1), "to", round(z_ci_upper, 1), "mm\n",
"95% CI using t:", round(t_ci_lower, 1), "to", round(t_ci_upper, 1), "mm\n",
"t critical value:", round(t_crit, 3), "vs z critical value: 1.96\n")Mean: 258.9 mm
Standard deviation: 34.73 mm
Standard error: 10.98 mm
95% CI using z: 237.4 to 280.4 mm
95% CI using t: 234.1 to 283.7 mm
t critical value: 2.262 vs z critical value: 1.96
Hypothesis testing is a systematic way to evaluate research questions using data.
Key components:
Null hypothesis (H₀): Typically assumes “no effect” or “no difference”
Alternative hypothesis (Hₐ): The claim we’re trying to support
Statistical test: Method for evaluating evidence against H₀
P-value: Probability of observing our results (or more extreme) if H₀ is true
Significance level (α): Threshold for rejecting H₀, typically 0.05
Decision rule: Reject H₀ if p-value < α
lets test if our sample mean of 320 is larger than 285 or not? Essentially we are looking at the confidence intervals!!! But we are only interested if it is larger
Note
📖 Reference
Gotelli & Ellison, Ch. 4 — Framing and Testing Hypotheses.
Summary of One-Tailed Hypothesis Test:
Sample mean: 320
Hypothesized mean: 285
Sample size: 12
Standard deviation: 42.15
Standard error: 12.168
t-statistic: 2.876
Critical t-value (one-tailed): 1.796
Critical value: 306.85
Decision: Reject Ho (sample mean falls in upper rejection region)
Hypothesis testing is a systematic way to evaluate research questions using data.
Key components:
Null hypothesis (H₀): Typically assumes “no effect” or “no difference”
Alternative hypothesis (Hₐ): The claim we’re trying to support
Statistical test: Method for evaluating evidence against H₀
P-value: Probability of observing our results (or more extreme) if H₀ is true
Significance level (α): Threshold for rejecting H₀, typically 0.05
Decision rule: Reject H₀ if p-value < α
lets test if our sample mean of 320 is larger than 285 or not? Essentially we are looking at the confidence intervals!!! But we are only interested if it is larger

Hypothesis testing is a systematic way to evaluate research questions using data.
Key components:
Decision rule: Reject H₀ if p-value < α
lets test if our sample mean of 320 is equal to 270 or not? Essentially we are looking at the confidence intervals!!!
Summary of Hypothesis Test:
Sample mean: 320
Hypothesized mean: 270
Standard error: 12.603
t-statistic: 3.967
Critical t-value (±): 2.306
Critical values: 240.94 to 299.06
Decision: Reject Ho (sample mean falls in rejection region)
Hypothesis testing is a systematic way to evaluate research questions using data.
Key components:
Decision rule: Reject H₀ if p-value < α
lets test if our sample mean of 320 is equal to 270 or not? Essentially we are looking at the confidence intervals!!!

Practice Exercise 5: Lets practice a One-Sample t-Test
Let’s perform a one-sample t-test to determine if the mean fish length in Lake I3 differs from 260 mm:
Mean: 265.6 mm
One Sample t-test
data: i3_df$length_mm
t = 1.6091, df = 65, p-value = 0.1124
alternative hypothesis: true mean is not equal to 260
95 percent confidence interval:
258.6481 272.5640
sample estimates:
mean of x
265.6061
Interpret this test result by answering these questions:
Practice Exercise 6: Formulating Hypotheses
For the following research questions about Arctic grayling, write the null and alternative hypotheses:
Welch Two Sample t-test
data: length_mm by lake
t = -15.532, df = 161.63, p-value < 2.2e-16
alternative hypothesis: true difference in means between group I3 and group I8 is less than 0
95 percent confidence interval:
-Inf -86.66138
sample estimates:
mean in group I3 mean in group I8
265.6061 362.5980
Based on this t-test, what can we conclude about the difference in fish length between the two lakes?
A p-value is the probability of observing the sample result (or something more extreme) if the null hypothesis is true.
Common interpretations:
Common misinterpretations:
p < 0.001 rather than p = 1.2e-16, and always report the effect size alongside it
When making decisions based on hypothesis tests, two types of errors can occur:
Type I Error (False Positive)
Type II Error (False Negative)
Statistical Power = 1 - β
- Larger sample size
- Larger effect size
- Lower variability
- Higher α level

Black curve (Null Distribution): distribution of test statistics when Ho is true
Green curve (Alternative Distribution): distribution when Ha is true (is an effect)
The Key Insight fundamental trade-off in hypothesis testing:

Practice Exercise 7: Interpreting P-values and Errors
Given the following scenarios, identify whether a Type I or Type II error might have occurred:
A researcher concludes that a new fishing regulation increased grayling size, when in fact it had no effect.
A study fails to detect a real decline in grayling population due to warming water, concluding there was no effect.
Let’s calculate the power of our t-test to detect a 30 mm difference in length between lakes:
# Power to detect a 30 mm difference in mean length between lakes
lake_I3 <- grayling_df %>% filter(lake == "I3")
lake_I8 <- grayling_df %>% filter(lake == "I8")
# count only the fish we actually measured (the length() trap from week 2)
n1 <- sum(!is.na(lake_I3$length_mm))
n2 <- sum(!is.na(lake_I8$length_mm))
sd_pooled <- sqrt((var(lake_I3$length_mm, na.rm = TRUE) * (n1 - 1) +
var(lake_I8$length_mm, na.rm = TRUE) * (n2 - 1)) /
(n1 + n2 - 2))
# power.t.test assumes EQUAL group sizes. Ours are 66 and 102, so use the
# harmonic mean - the effective n per group when the design is unbalanced.
n_effective <- 2 / (1/n1 + 1/n2)
power_result <- power.t.test(n = n_effective,
delta = 30, # difference we care about, in mm
sd = sd_pooled, # same scale as delta
sig.level = 0.05,
type = "two.sample",
alternative = "two.sided")
cat("n per group (harmonic mean):", round(n_effective, 1), "\n")n per group (harmonic mean): 80.1
pooled SD: 44.5 mm
Cohen's d for a 30 mm difference: 0.67
Two-sample t test power calculation
n = 80.14286
delta = 30
sd = 44.50181
sig.level = 0.05
power = 0.9887372
alternative = two.sided
NOTE: n is number in *each* group
Warning
⚠️ An assumption we just quietly made
sd_pooled averages the two lakes’ SDs, which assumes they are equal. They are not: sd(I3) = 28.3 mm but sd(I8) = 52.3 mm — nearly double. That is also why the t-test two slides back was a Welch test (R’s default), which does not assume equal variances.
So treat this power number as a rough guide, not a precise answer.
Key concepts covered:
mean ± t(0.975, df) × SE