pitakakariki / simr

Power Analysis of Generalised Linear Mixed Models by Simulation
70 stars 20 forks source link

power simulation from scratch with repeated measures lmm design #128

Open Psygn opened 6 years ago

Psygn commented 6 years ago

I am having trouble figuring out how to generate data from scratch for a power simulation with powerSim, for a repeated measure LMM. Say, I want 100 cases (id) with 5 measurement points each (t) and one individual-level variable (m). The model I would like to create would be model1 <- makeLmer(y ~ t + m + m*t + (t | id), fixef=b, VarCorr=V2, sigma=s, data=X).

How do I generate the data matrix to simulate this model? I understand I need to create variables, but am unsure how to create m (say, a scale score from 1 to 7), as an individual-level variable that 'belongs' to each id. I assume id and t are correct this way, but I am drawing a blank on m.

id <- rep(1:100) t <- rep(1:5) m <- ??? X <- expand.grid(id=id, t=t, m=m)

Sorry if this is a silly question.

pitakakariki commented 6 years ago

These kinds of questions are good for giving me a sense of "gaps" in the package and documentation. I didn't think many people would use the "from scratch" features so I didn't spend as long making them as user-friendly as they could be.

nb: id <- rep(1:100) should just be id <- 1:100. Are you reading an old version of the vignette? I had the rep typo in there for a while before I spotted it.


Onto your question - I'd start by randomly generating some values for m. This is probably the tricky part - this might not be a variable you have control over in your design, so you'll need some idea of the likely distribution of scores in your sample.

For an even distribution you could use e.g.:

m <- sample(1:7, 100, replace=TRUE)

Or if you want a normal-ish distribution:

m <- 1 + rbinom(100, 6, 0.5)

Put these in a data frame with the ids at t=1:

X1 <- data.frame(id=1:100, t=1, m=m)

Then use extend to make 5 copies of this initial set of measurements:

X <- extend(X1, along="t", n=5)
> str(X)
'data.frame':   500 obs. of  3 variables:
 $ id: int  1 2 3 4 5 6 7 8 9 10 ...
 $ t : num  1 1 1 1 1 1 1 1 1 1 ...
 $ m : num  6 3 5 3 6 5 3 3 4 3 ...
Psygn commented 6 years ago

Thank you for your quick reply, very helpful! This worked!