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")