Code
subj_n <- 40 # total number of subjects
b0 <- 0 # intercept
b1 <- 0.5 # fixed effect of condition
u0s_sd <- 0.5 # random intercept SD for subjects
sigma_sd <- 2 # error SD
# set up data structure
nested_data <- function(sn, intercept, slope, varinterc, sigma) {
add_random(subj = sn) %>%
# add and recode categorical variables
add_between("subj", cond = c("control", "test")) %>%
add_recode("cond", "cond.t", control = 0, test = 1) %>%
# add random effects
add_ranef("subj", u0s = u0s_sd) %>%
add_ranef(sigma = sigma_sd) %>%
# calculate DV
mutate(dv = b0 + u0s + b1 * cond.t + sigma) ->
nd
return(nd)
}
nest_dat <- nested_data(subj_n, b0, b1, u0s, sigma_sd)