1

I want to fit a very simple mixed-effects model, with a couple of fixed effects and random intercepts (no random slopes), using the mlogit package in R. My categorical outcome variable has three levels, so I cannot use the lme4 package.

However, I keep googling and stack-ing and CRAN-ing (?) about this, but nowhere am I able to find a good solution. Any help out there on how to do this with the mlogit package? -- Or are there any similar alternatives in other R packages (or in SPSS, Stata or Minitab, or via packages in Python/Julia)?

See code below for my data structure and what type of model I would like to fit (I know how to fit a fixed-effects only model with mlogit (cf. fixed_model below); I just want to add random intercepts):

library(mlogit)
library(dfidx)

# Make variables: 
Outcome = c("y","z","y","z","x","z","y","x","x","x","z",
"y","z","x","x","y","z","x", "x", "y")
Predictor = rep(c("M", "F"), 10)
RandomIntercept = rep(c("A", "B", "C", "D"), 5)

# Make data frame
df <- data.frame(Outcome, Predictor, RandomIntercept) 

# Make mlogit-ready dataframe:
df_mlogit <- dfidx(df, choice = "Outcome", shape = "wide", id.var = "RandomIntercept")

# Display first observations: 
head(df_mlogit)

# Make fixed-effect-only model: 
fixed_model <- mlogit::mlogit(Outcome ~ 1 | Predictor, data = df_mlogit, reflevel = "x")

#Display results: 
fixed_model

# The kind of model I want, in lme4-syntax: 
dream_model <- lme4::glmer(Outcome ~ Predictor + (1|RandomIntercept), family = "binomial")
persboe
  • 71
  • 1
  • 5
  • Have you looked at the `mclogit` package, which is specifically for multinomial mixed effects models? – Allan Cameron Jan 27 '22 at 09:16
  • Hi! I have seen it mentioned, yes, when I think about it. But I haven't looked at it closely yet; you wouldn't happen to know (like, right of the bat) how to fit a model that looks something like the following? `Outcome ~ Predictor + (1|RandomIntercept)` – persboe Jan 27 '22 at 11:24
  • You would probably do something like `mclogit(cbind(factor(Outcome), seq(nrow(df))) ~ Predictor, random = ~1|RandomIntercept, data = df)`, but your dummy data is too small to fit such a model. – Allan Cameron Jan 27 '22 at 11:41
  • Thanks a lot for this, @AllanCameron! I tried to use your code with my actual data (N ≈ 5000), but I get a warning that says `No predictor variable remains in model` (even if I add several (categorical/continuous) predictors). I also get a warning saying `removing "[Predictor X]" from model due to insufficient within-choice set variance`. It may seem that my data simply have to be modeled differently (?) – persboe Jan 27 '22 at 12:52

0 Answers0