I'm fitting a multilevel model that has a random effect nested within a fixed effect:
df <- tibble(
id = seq(1:500)
) %>%
mutate(treatment = rbinom(n = 500, size = 1, prob = 8/9))
df <- as.data.frame(lapply(df, rep, 6)) %>%
arrange(id, treatment) %>%
mutate(scenario = rep(seq(1:6), 500)) %>%
mutate(magnitude_manipulation = case_when(
treatment == 1 & scenario == 1 ~ "very_low",
treatment == 1 & scenario == 2 ~ "mid_low",
treatment == 1 & scenario == 3 ~ "low",
treatment == 1 & scenario == 4 ~ "high",
treatment == 1 & scenario == 5 ~ "mid_high",
treatment == 1 & scenario == 6 ~ "very_high"
),
magnitude_manipulation = factor(magnitude_manipulation, ordered = TRUE, levels = c("very_low", "mid_low", "low", "high", "mid_high", "very_high")),
outcome = round(rnorm(3000, 100, 15)))
That is, magnitude_manipulation is nested in treatment - there are no levels of magnitude_manipulation when treatment is equal to 0, but 6 levels when treatment is equal to 1. What would be the appropriate way to model this data with lmer?
This is my best shot so far:
lmer(outcome ~ treatment + (1 | magnitude_manipulation) + (1 | scenario) + (1 | id), data = df)
I also have random effects for scenario and person because these are repeated factors. Any assistance would be greatly appreciated because although this topic has been addressed before, I'm not sure I'm applying it correctly.