3
votes

Stata allows for fixed effects and random effects specification of the logistic regression through the xtlogit fe and xtlogit re commands accordingly. I was wondering what are the equivalent commands for these specifications in R.

The only similar specification I am aware of is the mixed effects logistic regression

mymixedlogit <- glmer(y ~ x1 + x2 +  x3 + (1 | x4), data = d, family = binomial)

but I am not sure whether this maps to any of the aforementioned commands.

1
it's hard to know without more details on the meaning of the equivalent Stata function: can you edit your question to provide a link? - Ben Bolker
My experience with reading the Stata documentation was very painful. Compared to its opaque character, reading the R documentation in the help pages was a piece of cake. (And people do complain about R help pages.) I would think a link to a full tutorial would be more helpful than a link to Stata help pages. - IRTFM
melogit and xtmelogit (superseded by meqrlogit in Stata 13), are the commands for mixed effects regression for binary/binomial responses. - Roberto Ferrer
@BondedDust Why did you find Stata documentation painful? I actually think it's one of its strong features and by that I mean that it's really good. Straightforward, clear, concise, with relevant examples. And I'm just talking about help <command>. The manuals elaborate with details and offer methods, additional examples and commentaries. - Roberto Ferrer
@BondedDust Your profile says "Learned R on my own and have used it daily for last 4+ years." Give us the equivalent statement for Stata and we will be clear on why you find Stata documentation more difficult. FWIW, I learned Stata on my own and have used it daily for last 23+ years, have used R much, much less and find it harder work, but the plausible explanation starts with myself, not the documentation. - Nick Cox

1 Answers

3
votes

The glmer command is used to quickly fit logistic regression models with varying intercepts and varying slopes (or, equivalently, a mixed model with fixed and random effects).

To fit a varying intercept multilevel logistic regression model in R (that is, a random effects logistic regression model), you can run the following using the in-built "mtcars" data set:

data(mtcars)
head(mtcars)
m <- glmer(mtcars$am ~ 1 + mtcars$wt + (1|mtcars$gear), family="binomial")
summary(m)   

# and you can examine the fixed and random effects
fixef(m); ranef(m)

To fit a varying-intercept slope model in Stata, you of course use the xtlogit command (using the similar but not identical in-built "auto" data set in Stata):

sysuse auto
xtset gear_ratio
xtlogit foreign weight, re

I'll add that I find the entire reference to "fixed" versus "random" effects ambiguous, and I prefer to refer to the structure of the model itself (e.g., are the intercepts varying? which slopes are varying, if any? is the model nested in 2 levels or more? are the levels cross-classified or not?). For a similar view, see Andrew Gelman's thoughts on "fixed" versus "random" effects.

Update: Ben Bolker's excellent comment below points out that in R it's more informative when using predict commands to use the data=mtcars option instead of, say, the dollar notation:

data(mtcars)
m1 <- glmer(mtcars$am ~ 1 + mtcars$wt + (1|mtcars$gear), family="binomial")
m2 <- glmer(am ~ 1 + wt + (1|gear), family="binomial", data=mtcars)
p1 <- predict(m1); p2 <- predict(m2)
names(p1) # not that informative...
names(p2) # very informative!