multinomial mixed model in r

To fit a multinomial mixed model in R, we can use the mlogit package for model specification and lme4 package for model fitting.

Here's an example code:

main.r
library(mlogit)
library(lme4)

# Load data
data("Electricity", package = "mlogit")

# Convert data to wide format
Electricity_wide <- mlogit.data(Electricity, shape = "wide", choice = "choice")

# Specify model formula, where choice is the dependent variable
# and price and income are the independent variables
mlogit_model <- mlogit(choice ~ price | income | 0,
                       data = Electricity_wide)

# Fit the model using lme4's glmer function
mm <- glmer(formula = formula(mlogit_model),
            data = Electricity_wide,
            family = binomial(),
            REML = FALSE)

# View summary of model output
summary(mm)
642 chars
23 lines

In this example code, the Electricity dataset from the mlogit package is loaded and converted to wide format using mlogit.data(). We then specify the model formula using mlogit() function, indicating that choice is the dependent variable and price and income are the independent variables. A random intercept for each individual is included with the | 0 notation. The model is then fitted using glmer() function from lme4 package with binomial family. Finally, we can view the summary of model output using summary() function.

gistlibby LogSnag