-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathpaired_t_test_as_model.Rmd
More file actions
65 lines (51 loc) · 1.38 KB
/
paired_t_test_as_model.Rmd
File metadata and controls
65 lines (51 loc) · 1.38 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
---
title: "Paired T Test as a Model"
author: "Clay Ford"
date: "`r Sys.Date()`"
output: html_document
---
```{r setup, include=FALSE}
knitr::opts_chunk$set(echo = TRUE)
```
## Generate some data
Generate something resembling data collected at five stations:
```{r}
set.seed(1234)
time <- seq.Date(from = as.Date('2024-01-01'), to = as.Date('2024-03-01'),
by = "days")
station <- rep(LETTERS[1:5], each = length(time))
d <- data.frame(time = rep(time, 5), station = station)
# random time effect
z <- rnorm(length(time), sd = 0.4)
# generate some outcome measure
d$y <- (10 + z[rep(1:length(time), 5)]) +
(station == "B")*0.5 +
(station == "C")*2.3 +
(station == "D")*-0.7 +
(station == "E")*0.4 +
rnorm(nrow(d), sd = 0.3)
```
Quick visual of data:
```{r}
library(ggplot2)
ggplot(d) +
aes(x = time, y = y, color = station) +
geom_line()
```
## Fit model
Instead of individual paired t-tests we can fit one model.
```{r message=FALSE}
library(lme4)
m <- lmer(y ~ station + (1|time), data = d)
summary(m, corr = F)
```
Now run all the paired t-tests at once. Notice p-value corrections are automatically made for the multiple tests.
```{r message=FALSE}
library(emmeans)
emmeans(m, specs = pairwise ~ station)$contrasts
```
And here are the confidence intervals on the average differences.
```{r}
emmeans(m, specs = pairwise ~ station)$contrast |>
confint()
```