1
$\begingroup$

Is there a way to simulate a linear mixed model where the estimated fixed effects match the fixed effects one specifies in the simulation?

At first I assumed that the following code would achieve this. Please note: I set empirical = TRUE within the MASS:mvrnom function.

library("lme4")
set.seed(123)

d <- data.frame("subject" = gl(10, 6),
                "condition" = gl(2, 1, 60, labels = c("A", "B")),
                "X" = rep(0:1, 30),
                "b1" = 50)
d$subject_intercept <- MASS::mvrnorm(nlevels(d$subject), 
                                     mu = 0, 
                                     Sigma = 5^2,
                                     empirical = TRUE) [d$subject]
d$noise <- MASS::mvrnorm(nrow(d), 
                         mu = 0, 
                         Sigma = 3^2, 
                         empirical = TRUE)

d <- within(d, y <- b1 * X + subject_intercept + noise)

print(m1 <- lmer(y ~ condition + (1|subject), d))
Linear mixed model fit by REML ['lmerMod']
Formula: y ~ condition + (1 | subject)
   Data: d
REML criterion at convergence: 323.7271
Random effects:
 Groups   Name        Std.Dev.
 subject  (Intercept) 4.412   
 Residual             3.035   
Number of obs: 60, groups:  subject, 10
Fixed Effects:
(Intercept)   conditionB  
    -0.2248      50.4496 

However, the fixed effect estimates for the intercept and the contrast ("conditionB") are:

  • intercept: the simulated (constant) effect of condition A + the mean of the simulated residuals within this condition
  • contrast: the simulated (constant) effect of condition B + the mean of the simulated residuals within this condition - the intercept (because of contrast.treatment)

effect_A <- with(subset(d, condition == "A"), mean(b1 * X + noise))
all.equal(fixef(m1)[[1]], effect_A)
[1] TRUE
effect_B <- with(subset(d, condition == "B"), mean(b1 * X + noise))
all.equal(fixef(m1)[[2]], effect_B - effect_A)
[1] TRUE

This seems to be a problem because AFAIK one usually only specifies the residual variation and the mean of the residuals (0) across conditions.

$\endgroup$
2
$\begingroup$

Since the mean of the simulated residuals is not exactly zero, the model cannot possibly disentangle the mean residual from the true effect. Nevertheless, the model clearly does a good job in taking sampling variability into account, because both your estimates are less than one standard error from their true values. And if you increase the number of observations you can get as close as you want to the original values used in the simulation.

Edit: In your code the mean of the residual is zero but only when summed over the whole dataset, i.e. pooling together the two conditions A and B. The residuals' means are not zero when averaged separately in the two conditions, hence the difference between the estimates and the true generative values. I don't think it is possible to obtain the same exact values of the fixed-effects used in the simulation, unless you force the residuals to sum to zero within conditions, e.g. code below.

library("lme4")
set.seed(123)

d <- data.frame("subject" = gl(10, 6),
                "condition" = gl(2, 1, 60, labels = c("A", "B")),
                "X" = rep(0:1, 30),
                "b1" = 50)
d$subject_intercept <- MASS::mvrnorm(nlevels(d$subject), 
                                     mu = 0, 
                                     Sigma = 5^2,
                                     empirical = TRUE) [d$subject]

# simulate residuals separately to make sure they sum to zero within conditions
d$noise[d$condition=="A"] <- MASS::mvrnorm(nrow(d[d$condition=="A",]), 
                         mu = 0, 
                         Sigma = 3^2*(60-1)/(60-2), 
                         empirical = TRUE)

d$noise[d$condition=="B"] <- MASS::mvrnorm(nrow(d[d$condition=="B",]), 
                         mu = 0, 
                         Sigma = 3^2*(60-1)/(60-2), 
                         empirical = TRUE)

d <- within(d, y <- b1 * X + subject_intercept + noise)

# now the fixed-effect estimates are the same as the true values, up to some numerical error
print(m1 <- lmer(y ~ condition + (1|subject), d))

Linear mixed model fit by REML ['lmerMod']
Formula: y ~ condition + (1 | subject)
   Data: d
REML criterion at convergence: 327.9733
Random effects:
 Groups   Name        Std.Dev.
 subject  (Intercept) 4.635   
 Residual             3.141   
Number of obs: 60, groups:  subject, 10
Fixed Effects:
(Intercept)   conditionB  
  -3.44e-14     5.00e+01 

Edit 2: When setting empirical=TRUE the values generated are such that the sample variance computed using Bessel correction (that is by using of $n-1$ instead of $n$ in the denominator) correspond exactly to the one used for generating the values. In the code above the residuals are generated for two smaller vectors of length 30 instead of a single one of length 60, hence the estimated sample variance would be different from the intended value, unless this value is corrected as pointed out by @amoeba in his comment. In this particular case one needs to multiply the desired value by $\frac{60-1}{60-2}$, and more generally if one wants to generate a sample of length $n$ by concatenating $k$ vectors of equal length, the standard deviation used to generate each of the $k$ vectors should be multiplied by the factor $\frac{n-1}{n-k}$.

$\endgroup$
  • $\begingroup$ Doesn't empirical = TRUE guarantee that the mean of simulated residuals is exactly zero? $\endgroup$ – amoeba May 30 '18 at 9:09
  • $\begingroup$ @amoeba: Yes, it does. That's why I'm asking. $\endgroup$ – statmerkur May 30 '18 at 9:50
  • $\begingroup$ it is zero if you sum over the whole simulated dataset, mean(d$noise) $\approx 0$. Why would you expect them to sum to zero for any particular subset? $\endgroup$ – matteo May 30 '18 at 9:54
  • 1
    $\begingroup$ @statmerkur If sample A has mean 0 and variance sigma^2, and sample B has mean 0 and variance sigma^2, then shouldn't the combined sample {A,B} have mean 0 and variance sigma^2? $\endgroup$ – amoeba May 30 '18 at 11:24
  • 2
    $\begingroup$ @statmerkur Yeah, that's because of the Bessel's correction: (9*(30-1) + 9*(30-1))/(2*30-1) = 8.847458. This means that if you want to get 9 as var(d$noise) then you should supply 9*(60-1)/(60-2) = 9.155172 as the variance for A and B. $\endgroup$ – amoeba May 30 '18 at 12:16

Your Answer

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

Not the answer you're looking for? Browse other questions tagged or ask your own question.