Here is an example using an artificial data set as pilot data to estimate power for a random intercepts model. The z variable is a count dependent variable, while x is a time variable going from 1 to 10 (i.e. there are ten measurements per person), and g is the group factor splitting the data into three groups a, b, and c, which for this example we will assume are different people. In this simulation study, we want to estimate the sample size required for detecting an effect for time (i.e. the x variable) of -.045. That is, we want to know the number of participants (i.e. the number of groups in variable g) to estimate an average change in the dependent variable, z, over time to have a power of .8 that would be able to detect a slope of -.045 (i.e. 80% chance of not making a type two error).

We begin by setting up the model, which is a standard multilevel model with g as the hieratical variable with three levels a, b, c (i.e. three different people) getting their own intercepts (i.e. random intercepts) and x with its own slope per group. Then we change x’s slope to -.045 and run the powerCurve model to estimate the number of people needed to achieve a desired level of power (usually .8). The powerCurve function usually runs 1,000 simulations, which in real data analysis the user should use; however, because 1000 simulations can take a long time, I have set the number of simulations (nsim) to 10. Additionally, because in this example, we are interested in understanding the number of participants (i.e. the g variable), I specify along “g”. The default is along the x variable, which, in this example, would provide the power for different amounts of measurements take across three participants needed to achieve a certain level of power.

As the reader can see the power is very low (.4) to detect a slope of -045 with three participants each receiving ten measurements.

library(simr)

data("simdata")
head(simdata)
##          y x g z
## 1 8.139081 1 a 3
## 2 7.947861 2 a 3
## 3 9.283638 3 a 3
## 4 7.779489 4 a 2
## 5 5.803512 5 a 3
## 6 6.131720 6 a 2
model = glmer(z~x + (1 + x |g), family = "poisson", data = simdata)

fixef(model)["x"] = -.045

set.seed(123)
powerCurve(model , along = "g", nsim =  10)
## Simulating: |                                                             |Simulating: |======                                                       |Simulating: |============                                                 |Simulating: |==================                                           |Simulating: |========================                                     |Simulating: |==============================                               |Simulating: |====================================                         |Simulating: |==========================================                   |Simulating: |================================================             |Simulating: |======================================================       |Simulating: |=============================================================|(1/1) (1/1) Simulating: |                                                       |(1/1) Simulating: |=====                                                  |(1/1) Simulating: |===========                                            |(1/1) Simulating: |================                                       |(1/1) Simulating: |======================                                 |(1/1) Simulating: |===========================                            |(1/1) Simulating: |=================================                      |(1/1) Simulating: |======================================                 |(1/1) Simulating: |============================================           |(1/1) Simulating: |=================================================      |(1/1) Simulating: |=======================================================|(1/1) 
## Power for predictor 'x', (95% confidence interval),
## by number of levels in g:
##       3: 40.00% (12.16, 73.76) - 30 rows
##
## Time elapsed: 0 h 0 m 2 s

Therefore, we need to simulate increases in the sample to evaluate how many people are needed across ten measurements to achieve 80% power. We can do this by using the extend function. With the extend function, we set the simulation to simulate the model with participants 15 participants (i.e. 150 data points), because we specified the extension along the participant variable g with n = 15. Then we run the powerCurve function along g again. The results show that with ten measurements per person, we would need at least 15 people to have 80% power in our model.

model13 = extend(model, along = "g", n = 15)
set.seed(123)
powerCurve(model13, nsim = 10, along = "g")
## Simulating: |                                                             |Simulating: |======                                                       |Simulating: |============                                                 |Simulating: |==================                                           |Simulating: |========================                                     |Simulating: |==============================                               |Simulating: |====================================                         |Simulating: |==========================================                   |Simulating: |================================================             |Simulating: |======================================================       |Simulating: |=============================================================|( 1/10) ( 1/10) Simulating: |                                                     |( 1/10) Simulating: |=====                                                |( 1/10) Simulating: |==========                                           |( 1/10) Simulating: |===============                                      |( 1/10) Simulating: |=====================                                |( 1/10) Simulating: |==========================                           |( 1/10) Simulating: |===============================                      |( 1/10) Simulating: |=====================================                |( 1/10) Simulating: |==========================================           |( 1/10) Simulating: |===============================================      |( 1/10) Simulating: |=====================================================|( 1/10) ( 2/10) ( 2/10) Simulating: |                                                     |( 2/10) Simulating: |=====                                                |( 2/10) Simulating: |==========                                           |( 2/10) Simulating: |===============                                      |( 2/10) Simulating: |=====================                                |( 2/10) Simulating: |==========================                           |( 2/10) Simulating: |===============================                      |( 2/10) Simulating: |=====================================                |( 2/10) Simulating: |==========================================           |( 2/10) Simulating: |===============================================      |( 2/10) Simulating: |=====================================================|( 2/10) ( 3/10) ( 3/10) Simulating: |                                                     |( 3/10) Simulating: |=====                                                |( 3/10) Simulating: |==========                                           |( 3/10) Simulating: |===============                                      |( 3/10) Simulating: |=====================                                |( 3/10) Simulating: |==========================                           |( 3/10) Simulating: |===============================                      |( 3/10) Simulating: |=====================================                |( 3/10) Simulating: |==========================================           |( 3/10) Simulating: |===============================================      |( 3/10) Simulating: |=====================================================|( 3/10) ( 4/10) ( 4/10) Simulating: |                                                     |( 4/10) Simulating: |=====                                                |( 4/10) Simulating: |==========                                           |( 4/10) Simulating: |===============                                      |( 4/10) Simulating: |=====================                                |( 4/10) Simulating: |==========================                           |( 4/10) Simulating: |===============================                      |( 4/10) Simulating: |=====================================                |( 4/10) Simulating: |==========================================           |( 4/10) Simulating: |===============================================      |( 4/10) Simulating: |=====================================================|( 4/10) ( 5/10) ( 5/10) Simulating: |                                                     |( 5/10) Simulating: |=====                                                |( 5/10) Simulating: |==========                                           |( 5/10) Simulating: |===============                                      |( 5/10) Simulating: |=====================                                |( 5/10) Simulating: |==========================                           |( 5/10) Simulating: |===============================                      |( 5/10) Simulating: |=====================================                |( 5/10) Simulating: |==========================================           |( 5/10) Simulating: |===============================================      |( 5/10) Simulating: |=====================================================|( 5/10) ( 6/10) ( 6/10) Simulating: |                                                     |( 6/10) Simulating: |=====                                                |( 6/10) Simulating: |==========                                           |( 6/10) Simulating: |===============                                      |( 6/10) Simulating: |=====================                                |( 6/10) Simulating: |==========================                           |( 6/10) Simulating: |===============================                      |( 6/10) Simulating: |=====================================                |( 6/10) Simulating: |==========================================           |( 6/10) Simulating: |===============================================      |( 6/10) Simulating: |=====================================================|( 6/10) ( 7/10) ( 7/10) Simulating: |                                                     |( 7/10) Simulating: |=====                                                |( 7/10) Simulating: |==========                                           |( 7/10) Simulating: |===============                                      |( 7/10) Simulating: |=====================                                |( 7/10) Simulating: |==========================                           |( 7/10) Simulating: |===============================                      |( 7/10) Simulating: |=====================================                |( 7/10) Simulating: |==========================================           |( 7/10) Simulating: |===============================================      |( 7/10) Simulating: |=====================================================|( 7/10) ( 8/10) ( 8/10) Simulating: |                                                     |( 8/10) Simulating: |=====                                                |( 8/10) Simulating: |==========                                           |( 8/10) Simulating: |===============                                      |( 8/10) Simulating: |=====================                                |( 8/10) Simulating: |==========================                           |( 8/10) Simulating: |===============================                      |( 8/10) Simulating: |=====================================                |( 8/10) Simulating: |==========================================           |( 8/10) Simulating: |===============================================      |( 8/10) Simulating: |=====================================================|( 8/10) ( 9/10) ( 9/10) Simulating: |                                                     |( 9/10) Simulating: |=====                                                |( 9/10) Simulating: |==========                                           |( 9/10) Simulating: |===============                                      |( 9/10) Simulating: |=====================                                |( 9/10) Simulating: |==========================                           |( 9/10) Simulating: |===============================                      |( 9/10) Simulating: |=====================================                |( 9/10) Simulating: |==========================================           |( 9/10) Simulating: |===============================================      |( 9/10) Simulating: |=====================================================|( 9/10) (10/10) (10/10) Simulating: |                                                     |(10/10) Simulating: |=====                                                |(10/10) Simulating: |==========                                           |(10/10) Simulating: |===============                                      |(10/10) Simulating: |=====================                                |(10/10) Simulating: |==========================                           |(10/10) Simulating: |===============================                      |(10/10) Simulating: |=====================================                |(10/10) Simulating: |==========================================           |(10/10) Simulating: |===============================================      |(10/10) Simulating: |=====================================================|(10/10) 
## Power for predictor 'x', (95% confidence interval),
## by number of levels in g:
##       3: 30.00% ( 6.67, 65.25) - 30 rows
##       4: 30.00% ( 6.67, 65.25) - 40 rows
##       6: 50.00% (18.71, 81.29) - 60 rows
##       7: 60.00% (26.24, 87.84) - 70 rows
##       8: 60.00% (26.24, 87.84) - 80 rows
##      10: 60.00% (26.24, 87.84) - 100 rows
##      11: 60.00% (26.24, 87.84) - 110 rows
##      12: 60.00% (26.24, 87.84) - 120 rows
##      14: 70.00% (34.75, 93.33) - 140 rows
##      15: 80.00% (44.39, 97.48) - 150 rows
##
## Time elapsed: 0 h 0 m 25 s