# Random effects

Another little simulation.

Set up the number of people and how many data points:

people = 10

obs = 4

Noise, the random intercept per person:

sub_noise = data.frame(id = 1:people, rand_int = rnorm(people,0,60))

Set up a dataframe and a relation such that y_ij = 23x_i + 20 + b_j + e_ij

id = rep(1:people,obs)

thedata = data.frame(id)

thedata = merge(sub_noise, thedata)

thedata$x = rep(1:obs,people)

thedata$y = 23*thedata$x + 20 + thedata$rand_int + rnorm(people*obs, 0, 20)

Here’s a bit of the table:

> thedata id rand_int x y 1 1 -84.9 1 -58.15 2 1 -84.9 2 -3.09 3 1 -84.9 3 14.81 4 1 -84.9 4 16.95 5 2 -69.8 1 -9.75 6 2 -69.8 2 3.73 7 2 -69.8 3 18.86 8 2 -69.8 4 59.62 9 3 72.6 1 101.65 10 3 72.6 2 120.65 11 3 72.6 3 142.71 12 3 72.6 4 208.24…

Fit a mixed effect model with random intercept for participants.

lmer1 = lmer(y ~ x + (1|id), data=thedata)

And a gaussian regression where responses are assumed to be independent.

glm1 = glm(y ~ x, data=thedata)

> summary(lmer1)

Linear mixed-effects model fit by REML

Formula: y ~ x + (1 | id)

Data: thedata

AIC BIC logLik MLdeviance REMLdeviance

379 385 -187 385 373

Random effects:

Groups Name Variance Std.Dev.

id (Intercept) 2404 49.0

Residual 419 20.5

number of obs: 40, groups: id, 10Fixed effects:

Estimate Std. Error t value

(Intercept) 10.57 17.41 0.61

x 23.51 2.89 8.12Correlation of Fixed Effects:

(Intr)

x -0.416

> summary(glm1)Call:

glm(formula = y ~ x, data = thedata)Deviance Residuals:

Min 1Q Median 3Q Max

-92.23 -44.13 -1.10 47.89 103.61Coefficients:

Estimate Std. Error t value Pr(>|t|)

(Intercept) 10.57 20.11 0.53 0.6022

x 23.51 7.34 3.20 0.0028(Dispersion parameter for gaussian family taken to be 2697)

Null deviance: 130117 on 39 degrees of freedom

Residual deviance: 102472 on 38 degrees of freedom

AIC: 433.5Number of Fisher Scoring iterations: 2

> sd(glm1$residuals)

[1] 51

Note how the sd of the residuals in the mixed effects model is 20.5, whereas in the model with only fixed effects is 51. The estimates are identical, but the standard errors are reduced.