```{r, echo = FALSE}
library(knitr);
opts_chunk$set(echo = FALSE);
set.seed(20260226);
```


## Confidence in a sampled mean value

\Huge 

**The goal of CIs is to contain uncertainty:**

- How frequently will the CIs that we draw *contain* the population mean ($\mu_{x}$)?
- Logic is again based on repeated resampling


## Consider the distribution of a sample of N = 40


```{r, echo = FALSE, fig.alt = "40 bird silhouettes in a grey box called 'sample'.", fig.cap = "", out.width="100%"}
knitr::include_graphics("img/just_sample.png")
```


## Sample means given true mean of 7.2 when N = 40

```{r, echo = FALSE, fig.alt = "Grey boxes each with 4 birds representing samples with different average heights.", fig.cap = "", out.width="95%"}
knitr::include_graphics("img/samples_wing_40.png")
```



## Confidence in a sampled mean value (80%)

```{r, echo = FALSE}
samps_mn <- rep(x = NA, times = 20);
samp_mn  <- rep(x = NA, times = 20);
samp_se  <- rep(x = NA, times = 20);
for(i in 1:20){
  the_samp    <- rnorm(n = 40, mean = 1, sd = 1);
  std_err     <- sd(the_samp) / sqrt(40);
  samp_mn[i]  <- mean(the_samp);
  samp_se[i]  <- std_err;
}
samp_mn <- samp_mn + 6.2
offset  <- c(rep(x = 0, times = 4), rep(x = -1000, times = 16))
par(mar = c(6, 6, 2, 2));
plot(x = 1:20, y = samp_mn + offset, ylim = c(6.3, 7.9), pch = 20, cex = 3, 
     xaxt = "n",
     xlab = "Sample from the population (N = 40)", ylab = "Mean of the sample",
     cex.lab = 2, cex.axis = 2);
axis(side = 1, at = 1:20, labels = 1:20, cex.lab = 1.75);
abline(h = 7.2, lwd = 3, lty = "dashed", col = "blue");
for(i in 1:4){
  arrows(x0 = i, x1 = i, y0 = samp_mn[i], y1 = samp_mn[i] - (1.28 * samp_se[i]), 
         lwd = 4, angle = 90, length = 0.1);
  arrows(x0 = i, x1 = i, y0 = samp_mn[i], y1 = samp_mn[i] + (1.28 * samp_se[i]),
         lwd = 4, angle = 90, length = 0.1);
}
text(x = 4, y = 7.88, labels = "Population mean = 7.2", cex = 1.75, 
     col = "blue");
```



## Confidence in a sampled mean value (80%)

```{r, echo = FALSE}
samps_mn <- rep(x = NA, times = 20);
samp_mn  <- rep(x = NA, times = 20);
samp_se  <- rep(x = NA, times = 20);
for(i in 1:20){
  the_samp    <- rnorm(n = 40, mean = 1, sd = 1);
  std_err     <- sd(the_samp) / sqrt(40);
  samp_mn[i]  <- mean(the_samp);
  samp_se[i]  <- std_err;
}
samp_mn <- samp_mn + 6.2
par(mar = c(6, 6, 2, 2));
plot(x = 1:20, y = samp_mn, ylim = c(6.3, 7.9), pch = 20, cex = 3, xaxt = "n",
     xlab = "Sample from the population (N = 40)", ylab = "Mean of the sample",
     cex.lab = 2, cex.axis = 2);
axis(side = 1, at = 1:20, labels = 1:20, cex.lab = 1.75);
abline(h = 7.2, lwd = 3, lty = "dashed", col = "blue");
for(i in 1:20){
  arrows(x0 = i, x1 = i, y0 = samp_mn[i], y1 = samp_mn[i] - (1.28 * samp_se[i]), 
         lwd = 4, angle = 90, length = 0.1);
  arrows(x0 = i, x1 = i, y0 = samp_mn[i], y1 = samp_mn[i] + (1.28 * samp_se[i]),
         lwd = 4, angle = 90, length = 0.1);
}
text(x = 4, y = 7.88, labels = "Population mean = 7.2", cex = 1.75, 
     col = "blue");
```



## Confidence in a sampled mean value

\Huge 

**The goal of CIs is to contain uncertainty:**

\LARGE

- How frequently will the CIs that we draw *contain* the population mean ($\mu_{x}$)?
- Distribution probabilities (e.g., z-scores)
- **The standard error ($\mathbf{s/\sqrt{N}}$)**







## Confidence in a sampled mean value

\Huge

**Calculating CIs**

\vspace{8mm}

- LCI = $\bar{x} - z \left(\frac{s}{\sqrt{N}}\right)$ \vspace{6mm}
- UCI = $\bar{x} + z \left(\frac{s}{\sqrt{N}}\right)$


## Probabilities calculated from the normal distribution

```{r, echo = FALSE, fig.alt = "A plot of a bell curve, with a range of the curve shaded in grey between negative sigma and positive sigma, symmetrical around the peak of the curve", fig.cap = "", out.width="100%"}
par(mar = c(6, 6, 2, 2));
xx       <- seq(from = -5, to = 5, by = 0.0001);
pr_norm  <- dnorm(x = xx, mean = 0, sd = 1);
plot(x = xx, y = pr_norm, type = "l", ylim = c(0, 0.48), xaxt = "n",
     ylab = "Probability density",  xlab = "x", lwd = 3, xlim = c(-3, 3),
     cex.lab = 2, cex.axis = 2, yaxs = "i");
xx_vals <- 30001:70001;
polygon(c(xx[xx_vals], xx[70001], xx[30001]), 
        c(pr_norm[xx_vals], 0, 0), col="blue");
yy_mean1 <- seq(from = 0, to = 0.15, length = 1000);
xx_mean1 <- rep(0, times = 1000);
yy_mean2 <- seq(from = 0.25, to = max(pr_norm), length = 1000);
xx_mean2 <- rep(0, times = 1000);
points(x = xx_mean1, y = yy_mean1, type = "l", lwd = 2, lty = "dotted");
points(x = xx_mean2, y = yy_mean2, type = "l", lwd = 2, lty = "dotted");
distr_lab <- c(expression(mu-1.96*sigma), expression(mu), expression(mu+1.96*sigma));
axis(side = 1, at = c(-2, 0, 2), labels = distr_lab, cex.axis = 2);
text(x = 0,  y = 0.2, labels = "95%", cex = 3);
```


\footnotetext[1]{\href{https://bradduthie.github.io/stats/app/zandp/}{https://bradduthie.github.io/stats/app/zandp/}}



## Confidence in a sampled mean value

\Huge

**Really assuming: $\mathbf{s = \sigma}$**

\vspace{8mm}

- LCI = $\bar{x} - z \left(\frac{\sigma}{\sqrt{N}}\right)$ \vspace{6mm}
- UCI = $\bar{x} + z \left(\frac{\sigma}{\sqrt{N}}\right)$



## Confidence in a sampled mean value {.t}

\Huge

**Problem assuming: $\mathbf{s = \sigma}$**

\vspace{8mm}

- Assuming no uncertainty in estimation of standard deviation ($\sigma$)
- If uncertainty in $\sigma$, our CIs will be wrong



## Confidence in a sampled mean value {.t}

\Huge

**Problem assuming: $\mathbf{s = \sigma}$**

\vspace{4mm}

- Problem less relevant$^{1}$ for large (N > 30) sample sizes
- Low sample size increases uncertainty of standard deviation

\footnotetext[1]{Sokal, RR \& FJ Rohlf. 1995. Biometry. 3rd ed. WH Freeman \& Company, New York, USA.}


## Confidence in a sampled mean value {.t}

\begin{columns}[T] % Align columns at the top
\begin{column}{0.5\textwidth}
\includegraphics[width=\textwidth]{img/tukki_Confused.png}
\end{column}
\begin{column}{0.5\textwidth}
\Huge \vspace{2mm}

Uncertainty in the standard deviation?

\vspace{6mm}

Need to adjust the normal distribution

\end{column}
\end{columns}



## The normal distribution {.t}

\Huge
\vspace{-15mm}

$$f\left(x\right) = \frac{1}{\sigma\sqrt{2\pi}}e^{-\frac{1}{2}\left(\frac{x - \mu}{\sigma}\right)^{2}}$$

\footnotetext[1]{You do not need to know this equation.}

## The t-distribution {.t}

\Huge
\vspace{-15mm}

$$f(t) = \frac{\Gamma\left(\frac{v + 1}{2} \right)}{\sqrt{\pi v} \Gamma{\left(\frac{v}{2}\right)}}\left(1 + \frac{t^{2}}{v} \right)^{-\frac{v + 1}{2}}$$

\hrule\pause

```{r, echo = FALSE, fig.alt = "An angry big cat on a windowsill", fig.cap = "", out.width="70%"}
knitr::include_graphics("img/tukki_angry.png")
```



\footnotetext[1]{Forget the equation, see: \href{https://bradduthie.github.io/stats/app/t_score/}{https://bradduthie.github.io/stats/app/t\_score/}}


## Confidence intervals with a normal distribution

\LARGE

\vspace{4mm}

**Verbally**

- LCI = Mean -- (z-score $\times$ standard error)
- UCI = Mean + (z-score $\times$ standard error)

\vspace{2mm}

**Mathematically**

- LCI = $\bar{x} - z \left(\frac{s}{\sqrt{N}}\right)$
- UCI = $\bar{x} + z \left(\frac{s}{\sqrt{N}}\right)$


\footnotetext[1]{\href{https://bradduthie.github.io/stats/app/zandp/}{https://bradduthie.github.io/stats/app/zandp/}}


## Confidence intervals with a t-distribution

\LARGE

\vspace{4mm}

**Verbally**

- LCI = Mean -- (t-score $\times$ standard error)
- UCI = Mean + (t-score $\times$ standard error)

\vspace{2mm}

**Mathematically**

- LCI = $\bar{x} - t \left(\frac{s}{\sqrt{N}}\right)$
- UCI = $\bar{x} + t \left(\frac{s}{\sqrt{N}}\right)$


\footnotetext[1]{\href{https://bradduthie.github.io/stats/app/t_score/}{https://bradduthie.github.io/stats/app/t\_score/}}



## Confidence intervals with normal distribution {.t}

\LARGE

| $x_{1}$ | $x_{2}$ | $x_{3}$ | $x_{4}$ | $x_{5}$ | $x_{6}$ | $x_{7}$ |
|---------|---------|---------|---------|---------|---------|---------|
| 17.1    | 15.2    |  14.9   | 12.6    | 15.2    |  10.3   | 12.7    |

\vspace{-6mm}
\Large

$N = 7$ \hspace{2mm} $\bar{x} = 14$ \hspace{2mm} $s = 2.26$ \hspace{2mm} $z = 1.96$ \hspace{2mm} $df = \infty$

\vspace{2mm}
\hrule 
\vspace{2mm}
\begin{align*}
LCI &= \bar{x} - z\left(\frac{s}{\sqrt{N}}\right) &&&&&& \\ \\
    &= 14 - 1.96\left(\frac{2.26}{\sqrt{7}} \right) &&&&&& \\  
    &= 12.34 &&&&&&
\end{align*}



## Confidence intervals with t-distribution {.t}

\LARGE

| $x_{1}$ | $x_{2}$ | $x_{3}$ | $x_{4}$ | $x_{5}$ | $x_{6}$ | $x_{7}$ |
|---------|---------|---------|---------|---------|---------|---------|
| 17.1    | 15.2    |  14.9   | 12.6    | 15.2    |  10.3   | 12.7    |

\vspace{-6mm}
\Large

$N = 7$ \hspace{2mm} $\bar{x} = 14$ \hspace{2mm} $s = 2.26$ \hspace{2mm} $t = 2.45$ \hspace{2mm} $df = 6$

\vspace{2mm}
\hrule 
\vspace{2mm}
\begin{align*}
LCI &= \bar{x} - t\left(\frac{s}{\sqrt{N}}\right) &&&&&& \\ \\
    &= 14 - 2.45\left(\frac{2.26}{\sqrt{7}} \right) &&&&&& \\  
    &= 11.91 &&&&&&
\end{align*}



## Standard normal distribution

```{r, echo = FALSE, fig.alt = "A plot of a bell curve, shaded in grey and centered at the x-axis on a value of zero is shown. The x axis is labelled 'z-score'.", fig.cap = "", out.width="100%"}
par(mar = c(6, 6, 2, 2));
xx       <- seq(from = -5, to = 5, by = 0.001);
pr_norm  <- dnorm(x = xx, mean = 0, sd = 1);
plot(x = xx, y = pr_norm, type = "l", ylim = c(0, 0.48), 
     ylab = "Probability density",  xlab = "z-score", lwd = 3, xlim = c(-4, 4),
     cex.lab = 2, cex.axis = 2, yaxs = "i");
polygon(c(xx, max(xx)), c(pr_norm, 0), col="blue");
```


\footnotetext[1]{\href{https://bradduthie.github.io/stats/app/zandp/}{https://bradduthie.github.io/stats/app/zandp/}}



## The t-distribution (6 df)

```{r, echo = FALSE, fig.alt = "A plot of a bell curve, shaded in grey and centered at the x-axis on a value of zero is shown. The x axis is labelled 'z-score'.", fig.cap = "", out.width="100%"}
par(mar = c(6, 6, 2, 2));
xx       <- seq(from = -5, to = 5, by = 0.001);
pr_norm  <- dt(xx, df = 6);
plot(x = xx, y = pr_norm, type = "l", ylim = c(0, 0.48), 
     ylab = "Probability density",  xlab = "t-score", lwd = 3, xlim = c(-4, 4),
     cex.lab = 2, cex.axis = 2, yaxs = "i");
polygon(c(xx, max(xx)), c(pr_norm, 0), col="blue");
```


\footnotetext[1]{\href{https://bradduthie.github.io/stats/app/t_score/}{https://bradduthie.github.io/stats/app/t\_score/}}




## Turning a value into z-score


```{r, echo = FALSE, fig.alt = "A two panel figure, the first shows a curve of the standard normal distribution, to the right of which there is a histogram of data centred at a mean of 5 with a relatively high standard deviation. In the second figure, the histogram data fit nicely within the normal distribution", fig.cap = "", out.width="100%"}
par(mar = c(6, 6, 2, 2));
xvals <- c(7.53, 7.05, 8.11, 7.69, 7.81, 7.69, 7.33, 8, 8.20, 8.11, 8.08, 
           8.22, 8.18, 7.66, 7.82, 6.54, 8.04, 7.31, 7.51, 8.45, 7.58, 7.82, 
           8.03, 7.79, 7.95, 8.02); # Scotland temp 1995-2020 (degrees C)
xx       <- seq(from = -5, to = 5, by = 0.001);
pr_norm  <- dnorm(x = xx, mean = 0, sd = 1);
hist(x = xvals, col = "blue", cex.axis = 2, cex.lab = 2, main = "",
     xlab = "Annual mean temperature in Scotland (degrees C)", breaks = 20,
     xlim = c(-4, 8.6), ylim = c(0, 1.9), freq = FALSE);
points(x = xx, y = pr_norm, type = "l", lty = "solid", lwd = 3);
arrows(x0 = 6, x1 = 1, y0 = 0.9, y1 = 0.3, lwd = 5, col = "black");
```


## Translating a variable into a standard normal distribution

\Huge

- Subtract mean
- Divide by standard deviation

$$z = \frac{x - \bar{x}}{s}$$

\vspace{6mm}

Can turn x-values into z-scores



## What if the population mean is really 8.0?


```{r, echo = FALSE, fig.alt = "A histogram with normally distributed data.", out.width="90%"}
xvals <- c(7.53, 7.05, 8.11, 7.69, 7.81, 7.69, 7.33, 8, 8.20, 8.11, 8.08, 
           8.22, 8.18, 7.66, 7.82, 6.54, 8.04, 7.31, 7.51, 8.45, 7.58, 7.82, 
           8.03, 7.79, 7.95, 8.02); # Scotland temp 1995-2020 (degrees C)
par(mar = c(6, 6, 2, 2));
hist(x = xvals, col = "blue", cex.axis = 2, cex.lab = 2, main = "",
     xlab = "Annual mean temperature in Scotland (degrees C)", breaks = 20,
     xlim = c(6.4, 8.6), ylim = c(0, 1.9), freq = FALSE);
```

\Large

$N = 26$ \hspace{6mm} $\bar{x} = 7.789$ \hspace{6mm} $s = 0.4124844$

\footnotetext[1]{Met Office. (July 1, 2021). Annual mean temperature in Scotland from 1995 to 2020. In Statista. Retrieved February 10, 2026, from \href{https://www.statista.com/statistics/367853/scotland-average-temperature/}{https://www.statista.com/statistics/367853/scotland-average-temperature/}}



## Testing a hypothesis with the t-distribution

\Huge

- Subtract hypothesised mean
- Divide by standard deviation

$$t = \frac{\bar{x} - \mu}{SE(x)}$$

\vspace{4mm}\LARGE

If $\mu$ is the population mean, what is the probability of getting $\bar{x}$?



## Testing a hypothesis with the t-distribution

\Huge

- Subtract hypothesised mean
- Divide by standard deviation

$$t = \frac{7.789 - 8}{\left(0.4124844/\sqrt{26} \right)} = -2.61$$

\vspace{4mm}\LARGE

If 8 is the population mean, what is the probability of getting 7.789?




## The t-distribution (25 df)

```{r, echo = FALSE, fig.alt = "A plot of a bell curve, shaded in grey and centered at the x-axis on a value of zero is shown. The x axis is labelled 'z-score'.", fig.cap = "", out.width="100%"}
par(mar = c(6, 6, 2, 2));
xx       <- seq(from = -5, to = 5, by = 0.001);
pr_norm  <- dt(xx, df = 25);
plot(x = xx, y = pr_norm, type = "l", ylim = c(0, 0.48), 
     ylab = "Probability density",  xlab = "t-score", lwd = 3, xlim = c(-4, 4),
     cex.lab = 2, cex.axis = 2, yaxs = "i");
polygon(c(xx, max(xx)), c(pr_norm, 0), col="blue");
arrows(x0 = -2.61, x1 = -2.61, y0 = 0.12, y1 = 0, lwd = 5);
```


\footnotetext[1]{\href{https://bradduthie.github.io/stats/app/t_score/}{https://bradduthie.github.io/stats/app/t\_score/}}


