Initialize repository

This commit is contained in:
2026-01-09 16:55:02 +01:00
commit e2b548c00c
34 changed files with 3885 additions and 0 deletions
+81
View File
@@ -0,0 +1,81 @@
Exercises: Data simulation for crossed random-effects models
================
- Change the data simulation by Baayen, Davidson, and Bates (2008) for
$N = 30$ subjects instead of only 3
- You can use the following script and adjust it accordingly
- You can choose if you want to use model matrices or create the vectors
“manually”
``` r
library(lattice)
library(lme4)
#--------------- (1) Create data frame ----------------------------------------
datsim <- expand.grid(subject = factor(c("s1" , "s2" , "s3" )),
item = factor(c("w1" , "w2" , "w3" )),
soa = factor(c("long" , "short" ))) |>
sort_by(~ subject)
#--------------- (2) Define parameters ----------------------------------------
beta0 <- 522.22
beta1 <- -19
sw <- 21
sy0 <- 24
sy1 <- 7
ry <- -0.7
se <- 9
#--------------- (3) Create vectors and simulate data -------------------------
# Fixed effects
b0 <- rep(beta0, 18)
b1 <- rep(rep(c(0, beta1), each = 3), 3)
# Draw random effects
w <- rep(rnorm(3, mean = 0, sd = sw), 6)
e <- rnorm(18, mean = 0, sd = se)
# Bivariate normal distribution
sig <- matrix(c(sy0^2, ry * sy0 * sy1, ry * sy0 * sy1, sy1^2), 2, 2)
y01 <- MASS::mvrnorm(3, mu = c(0, 0), Sigma = sig)
y0 <- rep(y01[,1], each = 6)
y1 <- rep(c(0, y01[1,2],
0, y01[2,2],
0, y01[3,2]), each = 3)
datsim$rt <- b0 + b1 + w + y0 + y1 + e
#--------------- (4) Simulate data using model matrices -----------------------
X <- model.matrix( ~ soa, datsim)
Z <- model.matrix( ~ 0 + item + subject + subject:soa, datsim,
contrasts.arg = list(subject = contrasts(datsim$subject,
contrasts = FALSE)))
# Fixed effects
beta <- c(beta0, beta1)
# Random effects
u <- c(w = unique(w),
y0 = y01[,1],
y1 = y01[,2])
datsim$rt2 <- X %*% beta + Z %*% u + e
#--------------- (5) Visualize simulated data ---------------------------------
xyplot(rt ~ soa | subject, datsim, group = item, type = "b", layout = c(3, 1))
```
### Reference
<div id="refs" class="references csl-bib-body hanging-indent">
<div id="ref-Baayen08" class="csl-entry">
Baayen, R. H., D. J. Davidson, and D. M. Bates. 2008. “Mixed-Effects
Modeling with Crossed Random Effects for Subjects and Items.” *Journal
of Memory and Language* 59 (4): 390412.
<https://doi.org/10.1016/j.jml.2007.12.005>.
</div>
</div>
+67
View File
@@ -0,0 +1,67 @@
Exercise: Power simulation for LMM
================
## Physical healing as a function of perceived time
Aungle and Langer (2023) investigate how perceived time influences
physical healing
- They used cupping to induce bruises on 33 subjects, then took a
picture, waited for 28 min and took another picture
- Subjective time was manipulated to feel like 14, 28, or 56 min
- The pre and post pictures were presented to 25 raters who rated the
amount of healing on a 10-point-scale with 0 = not at all healed, 5 =
somewhat healed, 10 = completely healed
- Subjects participated in all three conditions over a two week period
Data: [healing.RData](../data/healing.RData)
``` r
load("../data/healing.RData")
str(dat)
# Subject ID
dat$Subject <- factor(dat$Subject)
# Rater ID
dat$ResponseId <- factor(dat$ResponseId)
```
1. Visualize the data.
- Aggregate the data over Raters and plot the data for each subject
using `lattice::xyplot()`
- Aggregate the data over Subjects and plot one panel for each rater
- How would you choose the random effects for a model testing
healing over the three conditions
2. Fit the model you think fits the experimental design best
3. Test the effects of condition
4. Run a power simulation for a replication study:
- Set up a data frame containing the study design and sample size.
- Specify the minimum relevant effects.
- Set the fixed effects and variance components to plausible values.
- How many participants are required to detect the specified effect
with a power of 80%?
- Recover the parameters of the model for one simulated data set.
### Reference
<div id="refs" class="references csl-bib-body hanging-indent">
<div id="ref-Aungle23" class="csl-entry">
Aungle, P., and E. Langer. 2023. “Physical Healing as a Function of
Perceived Time.” *Scientific Reports* 13 (1): 22432.
<https://doi.org/10.1038/s41598-023-50009-3>.
</div>
</div>
Binary file not shown.