Data Analysis for Psychology in R 1 (2026/27)
Psychology, PPLS
University of Edinburgh
When we don’t have access to the population we want to compare our sample to, how can we run a statistical analysis?
What kind of statistical test lets us compare our sample to a specific hypothesised value?
Last week, we knew the population that gave us our null distribution:
In real life, we’ll basically never know the population that we want to compare any of our samples to.
???
It looks a lot like a normal distribution (overlaid in blue), but it’s less pointy in the middle and fatter at the tails.
The fat tails put more probability on extreme values, so that we don’t rule them out as much, since after all we don’t know what the population might look like.
All of these are t-distributions with different degrees of freedom (df):
You don’t need to know exactly what df is. Just know that it’s closely related related to sample size:
So our sample size determines the shape of the t-distribution that we will use as our null distribution.
Why does it work this way? Because the more data we have, the better a guess we can make about the population.
Recall from last week the five steps of NHST:
We’ll go through them one by one as we walk through the logic of a one-sample t-test.
RQ: In healthy adults, is average body temperature different from the standard reference value of 37C?
The null hypothesis (H0) is always that any difference or effect we’re interested in does not exist.
\[ H_0 : \mu = 37 \]
H0: mu = 37
The alternative hypothesis (H1) is that the difference or effect we’re interested in does exist.
\[ H_1 : \mu \neq 37 \]
H0: mu != 37
Rows: 50
Columns: 2
$ BodyTemp <dbl> 36.4, 37.4, 37.2, 37.1, 36.7, 37.2, 37.2, 36.6, 36.0, 37.2, 3~
$ Pulse <dbl> 69, 77, 75, 84, 71, 76, 81, 77, 75, 81, NA, 78, 71, 80, 70, 7~
mean_temp <- mean(temp_sample$BodyTemp)
temp_sample |>
ggplot(aes(x = BodyTemp)) +
geom_density(linewidth = 0, fill = '#0F4C81') +
geom_vline(xintercept = mean_temp, colour = 'tomato1', linewidth = 2) +
annotate(geom = 'label', x = mean_temp+0.05, y = 0.4, label = paste0('mean:\n', round(mean_temp, 2)), colour = 'tomato1', size = 8, hjust = 0, linewidth = 1)Our key question: How likely would it be to get a sample mean like this one, if H0 were true? (That is, if body temps were truly 37C on average?)
If H0 is true—if body temperatures in reality do have a mean of 37C—then:
How much variability around zero should we expect, exactly, if the H0 is true?
The answer to that question is the t-distribution:
Not degrees Celsius—if H0 were true, we would expect differences of \(\pm\) 2-ish t-values.
You don’t need to know how to calculate a t-value yourself!
R will translate our observed sample mean into a t-value and compare it to this t-distribution for us, using the function t.test().
To use the function t.test(), we give it four arguments:
To use the function t.test(), we give it four arguments:
One Sample t-test
data: temp_sample$BodyTemp
t = -3.14, df = 49, p-value = 0.0029
alternative hypothesis: true mean is not equal to 37
95 percent confidence interval:
36.690 36.932
sample estimates:
mean of x
36.811
data: temp_sample$BodyTemp
t = -3.14, df = 49, p-value = 0.0029
Our sample’s t-value is –3.14 (that means it’s three-and-a-bit SEs below the hypothesised population mean of 37).
The null distribution has 49 df (good sense check).
The p-value tells us that, if H0 were true, the probability of observing a t-value of –3.14 or more extreme, or +3.14 or more extreme, is .003, or 0.3%.
alternative hypothesis: true mean is not equal to 37
This line says in words what we wrote earlier as \(\mu \neq 37\).
A non-directional alternative hypothesis, because the RQ didn’t specify a difference in one direction or the other.
95 percent confidence interval:
36.690 36.932
This is the range of values within which we are 95% confident that the true population mean body temperature lies. (Careful: not the range of values that, with 95% probability, contains that true value.)
sample estimates:
mean of x
36.811
The mean body temperature observed in our sample is 36.81 degrees C.
This is the value that’s three SEs below the hypothesised population mean of 37.
One Sample t-test
data: temp_sample$BodyTemp
t = -3.14, df = 49, p-value = 0.0029
alternative hypothesis: true mean is not equal to 37
95 percent confidence interval:
36.690 36.932
sample estimates:
mean of x
36.811
Our significance level, or alpha level, is .05 (or 5%).
Recall: When \(\alpha = .05\), we will reject H0, even when H0 is true, one time in twenty.
Our p-value .003 is below .05. Therefore we decide: Reject H0.
RQ: In healthy adults, is average body temperature different from the standard reference value of 37C?
APA-formatted write-up:
To investigate whether \(\mu\), the mean body temperature of healthy humans, differs from the commonly thought value of 37°C, we performed a one-sample t-test of \(H_0 : \mu = 37\) against \(H_1 : \mu \neq 37\) at the 5% significance level.
The sample results (M = 36.81, SD = 0.43) provide evidence against the null hypothesis (\(t\)(49) = –3.14, \(p\) = .003, two-sided). We therefore conclude that the mean body temperature of healthy humans differs from 37°C and, with 95% confidence, is between 36.69°C and 36.93°C.
APA notation format for reporting the key inferential values:
\(t\)([degrees of freedom]) = [t-statistic], \(p\) = [p-value], [one-sided/two-sided]
Note that this difference is statistically significant, but it’s not practically significant: no one will rewrite the medical textbooks to change 37 to 36.81!
In Block 3, you’ll learn a way to estimate how practically significant a difference might be.
What questions do you have right now?
The day and time of DAPR1 lectures in Semester 2 is different from Semester 1.
Starting on January 12, 2027, all Semester 2 lectures are on Tuesdays from 13:10 to 14:00.
The location stays the same, and Elizabeth will be your lecturer for all of Block 3.
Thank you all for your hard work this semester.
Umberto, Josiah, and I wish you a restful winter break and a lovely new year when it comes.
When we don’t have access to the population we want to compare our sample to, how can we run a statistical analysis?
What kind of statistical test lets us compare our sample to a specific hypothesised value?
Uncollapse code for the basic standard normal distribution plot.
dapr1blue <- '#0F4C81'
m <- 0
sd <- 1
nx <- seq(m-4*sd, m+4*sd, length.out = 1000)
p_stdnorm <- tibble(
x = nx,
n_dens = dnorm(nx, m = m, sd = sd)
) |>
ggplot(aes(x = x, y = n_dens)) +
geom_line(linewidth = 1.5, colour = dapr1blue) +
labs(
y = 'Density',
x = 'Standard normal distribution'
) +
theme_classic() +
theme(
panel.grid = element_blank(),
text = element_text(family = 'Source Sans 3', size = 24),
axis.title = ggplot2::element_text(color = dapr1blue)
) +
scale_x_continuous(breaks = seq(
(m - 4*sd), (m + 4*sd), by = sd)
) +
NULLUncollapse code for the following plot with the z-statistic and shaded area.
z <- abs(1.1)
p_stdnorm +
geom_ribbon(
aes(
x = ifelse(x <= -z, x, NA),
ymin = 0, ymax = n_dens),
alpha = 0.3, fill = dapr1blue
) +
geom_ribbon(
aes(
x = ifelse(x >= z, x, NA),
ymin = 0, ymax = n_dens),
alpha = 0.3, fill = dapr1blue
) +
geom_vline(xintercept = -z, colour = 'tomato1', linewidth = 2, linetype = 'dashed') +
geom_vline(xintercept = z, colour = 'tomato1', linewidth = 2) +
NULL