NigelH
NigelH

Reputation: 1

R: Specifying random effects using glmer command

I am analyzing categorical data from a questionnaire conducted in different schools to see what factors might have influenced pupil's responses. I am therefore building a mixed model using the glmer command from R's lme4 package. For each survey question response I have six predictor variables and I want to include School as a random effect in such a way as both the intercept and slope vary by school. I have searched long and hard both online and offline and have found conflicting accounts concerning the correct way to code for this and, being an R novice, am not sure which is right! Here is what I've come up with (where Like is the response variable):

LikeM1 <- glmer(Like ~ Treatment + Regularity + Learn + Age + Gender + 
  Organisation_Membership_Summary + (1 + Like|School), 
  data = MagpieData, na.action = "na.omit", family = binomial(logit))

Have I specified School as a random effect correctly so that both the intercept and slope vary by School, or not? I should perhaps mention that being categorical data, all my variables are factors in R.

Upvotes: 0

Views: 6331

Answers (2)

mikey
mikey

Reputation: 1210

I think you want to do this:

LikeM1 <-glmer(Like ~ Treatment + Regularity + Learn + Age + Gender + Organisation_Membership_Summary + (1 | School) + (0 + Treatment + Regularity + Learn + Age + Gender + Organisation_Membership_Summary | School), data = MagpieData, na.action = "na.omit",family = binomial(logit))

The first part of the formula in parentheses is the random intercept and the second is the random slope. This link provides a really good explanation.

Upvotes: 0

Sarah Tannenbaum
Sarah Tannenbaum

Reputation: 31

If you want both the slope and the intercept to vary by group, the general form is: y ~ x + (1 + x | group). In the parentheses, the 1 indicates that the intercept should vary by group, and the x indicates that the coefficient of predictor x should vary by group. You've got a lot of predictors in your model. I'd start with one predictor at a time to make interpretation a bit easier.

Upvotes: 1

Related Questions