1
$\begingroup$

I am trying to simulate multi-level data for repeated measurements. My design includes just one within subjects factor, no between-subject factor. Consider the case of three treatment conditions with 10 trials in each. This is what I've written so far (using R):

##### repeated measures analysed using lme



rm(list = ls())
set.seed(374)

library(lme4)
library(reshape2)

trials_per_subject <- 30
number_of_subjects <- 30
treatment_conditions <- 3

Ntotal <- number_of_subjects*trials_per_subject

test.df <- data.frame(
                  subject = sort(rep(c(1:number_of_subjects),trials_per_subject)), 
                  trial = rep(c(1:trials_per_subject),number_of_subjects) , 
                  x1 = rep(rep(c(1,0),c(10,20)),number_of_subjects),
                  x2 = rep(rep(c(0,1,0),each=10),number_of_subjects))

# random slopes
beta1 <- rnorm(number_of_subjects,40,10)
beta2 <- rnorm(number_of_subjects,40,10)
# random intercept (subject effect)

subject_effect  <- rnorm(number_of_subjects,400,50)
#trial specific errors
errors  <- rnorm(Ntotal,0,5)




test.df$beta1 <- beta1[test.df$subject]
test.df$beta2 <- beta2[test.df$subject]
test.df$int <- subject_effect[test.df$subject]

# factor variable for further computing in lmer()  
test.df$treatment <- rep(c(1,2,3),each=10,length=Ntotal)
    test.df$treatment <- as.factor(test.df$treatment)

# generate response times
test.df$y <- test.df$int + test.df$x1 * test.df$beta1 + test.df$x2 *  test.df$beta2 + errors




# get correlation matrix
Df <- dcast(test.df,subject~treatment,value.var="y",fun.aggregate=mean)[,-1]
cor(Df)

# relevel so that treatment 3 becomes the reference
test.df <- within(test.df, treatment <- relevel(treatment, ref = 3))

# fit model (random intercept + slope model)
re.lm <- lmer(y ~ treatment + (1+treatment|subject), data = test.df) 
summary(re.lm)

Because for repeated measurements, the correlation structure is important (sphericity), it would be necessary to be able to (indirectly) control for the assumption of sphericity/compound symmetry. To be more concise, I want manipulate whether the variances and covariances are equal (assumption met) or different (assumption violated), see dataset DF above.

$\endgroup$
  • $\begingroup$ One way is to generate the data in wide format, so each row is a subject and you have 30 columns for the repeated measures. In this format you can specify the correlations between columns to satisfy the sphericity requirement and then reshape to long format. See an example of using the rmvnorm function here. $\endgroup$ – Andy W May 25 '14 at 14:00
  • $\begingroup$ thanks, if I just needed multivariate data with a specific correlation, your approach would be totally fine. But in my case, the data (columns in wide format) should be generated by a regression model. $\endgroup$ – beginneR May 25 '14 at 14:10
  • $\begingroup$ Your treatments appear to be additive, so just generate the multivariate normal data (under the case of no treatment) and then add in the fixed and random treatment effects per subject that you want. $\endgroup$ – Andy W May 25 '14 at 14:51
  • $\begingroup$ thank you, this would work, but since I want to vary different parameters, such as trials per condition, this is not the best way unfortunately. Because then, I would have to generate the multivariate data every time using a different variance-covariance matrix. It isn't necessary for me to know exactly what the (population) var-cov matrix looks like. I just need to roughly manipulate the structure so that variances and covariances are similar or not. $\endgroup$ – beginneR May 25 '14 at 15:48
  • $\begingroup$ This blog post may help. It provides clear step-by-step matrix algebra in R for simulating data with different fixed and random effects: psychometroscar.wordpress.com/… $\endgroup$ – John Flournoy Oct 23 '16 at 23:31

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Browse other questions tagged or ask your own question.