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