ggplotting power curves from the 'simr' package

The R package ‘simr’ has greatly facilitated power analysis for mixed-effects models using Monte Carlo simulation (which involves running hundreds or thousands of tests under slight variations of the data). The powerCurve function is used to estimate the statistical power for various sample sizes in one go. Since the tests are run serially, they can take a VERY long time; approximately, the time it takes to run the model supplied once (say, a few hours) times the number of simulations (nsim, which should be higher than 200), and times the number of different sample sizes examined. While there isn’t a built-in parallel method, the power curves for different sample sizes can be run separately, and the results can be progressively combined as each component finishes running (see tutorial). The power curves produced by simr are so good they deserve ‘ggplot2’ rendering. So, here’s a function for it.

A usage example


# Toy model with data from 'simr' package
fit = lmer(y ~ x + (x | g), data = simdata)

# Extend sample size of `g`
fit_extended_g = extend(fit, along = 'g', n = 12)

fit_powercurve = 
  powerCurve(fit_extended_g, fixed('x'), 
             along = 'g', breaks = c(4, 6, 8, 10, 12), 
             nsim = 50, seed = 123, progress = FALSE)

# Read in custom function to ggplot results from simr::powerCurve


powercurvePlot(fit_powercurve, number_x_axis_levels = 6) +
  # Change some defaults
  xlab("Number of levels in 'g'") +
  theme(plot.title = element_blank(),
        axis.title.x = element_text(size = 18), 
        axis.title.y = element_text(size = 18), 
        axis.text = element_text(size = 17))

comments powered by Disqus