library(faux)
library(dplyr)
library(tidyr)
library(broom.mixed)
library(lme4)

sim_mixed_cc

This function produces a data table for a basic cross-classified design with random intercepts for subjects and items.

For example, the following code produces the data for 100 subjects responding to 50 items where the response has an overall mean (grand_i) of 10. Subjects vary in their average response with an SD of 1, items vary in their average response with an SD of 2, and the residual error term has an SD of 3.

You can then see how changing these numbers affects the random effects in an intercept-only mixed effects model.

lme4::lmer(y ~ 1 + (1 | sub_id) + (1 | item_id), data = dat_cc) %>%
  broom.mixed::tidy() %>%
  knitr::kable(digits = 3)
effect group term estimate std.error statistic
fixed NA (Intercept) 9.867 0.312 31.633
ran_pars sub_id sd__(Intercept) 1.157 NA NA
ran_pars item_id sd__(Intercept) 2.026 NA NA
ran_pars Residual sd__Observation 3.010 NA NA

For example, changing grand_i to 0 changes the estimate for the fixed effect of the intercept (Intercept). Changing sub_sd to 1.5 and item_sd to 3 change the estimate for the SD of their corresponding random effects. Changing error_sd to 10 changes the estimate from the Residual SD.

dat_cc <- sim_mixed_cc(100, 50, 0, 1.5, 3, 10)

lme4::lmer(y ~ 1 + (1 | sub_id) + (1 | item_id), data = dat_cc) %>%
  broom.mixed::tidy() %>%
  knitr::kable(digits = 3)
effect group term estimate std.error statistic
fixed NA (Intercept) 0.084 0.544 0.154
ran_pars sub_id sd__(Intercept) 1.223 NA NA
ran_pars item_id sd__(Intercept) 3.611 NA NA
ran_pars Residual sd__Observation 10.010 NA NA

sim_mixed_df

This function uses lme4::lmer() to get subject, item and error SDs from an existing dataset and simulates a new dataset with the specified number of subjects and items with distributions drawn from the example data.

This example uses the fr4 dataset from this package to simulate 100 new subjects viewing 50 new faces.