# Simulating data for mixed models

Simulating data for linear regression is fairly easy. You generate a vector of X values of some range, you decide on some beta value and you generate random errors, and build a linear equation.

Now I want to do the same for a mixed model, with a random effect, and repeated measures. Why I want to do this ? For sample size estimation, I want to check how large should N be in my coming study. I do not know how to create the simulation. I don't mind using R or SAS, whatever you guys can help me with, fine with me.

Some words on my design. I have a continuous response variable Y. I have a fixed factor with two levels, I will call it X1 (treatment vs. control). These treatments are some operating techniques. There are two kind of operations these treatments can be applied on, A and B. This is a random factor, because there could be more, but only 2 will be examined. I will call the random factor Z1 (A vs. B). Each subject will have 2 operating procedures (repeated measures) of either type A or B (both repeated measures from the SAME type - both using SAME treatment, i.e., the new treatment or control).

I assume a correlation of 0.8 between repeated measures of the same subject, for all subjects. Let's also say I know or predict the means and standard deviations of the groups of X1, and the variance of Z1.

How do I simulate or generate data to fit this description so I can run a mixed model on it using various sample sizes and calculate the power ?

Thank you !

P.S My main problem is generating the correlations and to take into account the random effect.

• Do you know mvrnorm() from the MASS library in R (stat.ethz.ch/R-manual/R-devel/library/MASS/html/mvrnorm.html)? It enables to generate a vector of values from a multivariate normal distribution. Then, you can proceed as for simple linear regression, conditional on the random effects. – ocram Nov 10 '13 at 9:47
• If I understand you correctly, I will generate correlated data using the mentioned function, and then do what I did for linear regression. How will I take into account the random effect, just add another random variable like I did with the residuals in regression ? Should I add it only for intercept or also for slope ? – user34592 Nov 11 '13 at 7:20
• @user34592 you will get some idea from stats.stackexchange.com/questions/154909/… – time Jul 24 '15 at 4:20
• and also from stats.stackexchange.com/questions/154917/… – time Jul 24 '15 at 4:22