knitr::opts_chunk$set( collapse = TRUE, comment = "#>", fig.path = "man/figures/README-", out.width = "100%" )
emlogit
is a R package that implements the Expectation and Conditional-Maximization (ECM) algorithm for the multinomial logistic regression.
| Installation | Examples | References | |-|-|-|
You can install the development version from GitHub with:
# install.packages("devtools") devtools::install_github("soichiroy/emlogit")
Car
data from mlogit
package.
require(emlogit) require(dplyr) data(Car, package = 'mlogit') ## prepare data: only allow for indiviual specific covariates y <- Car %>% select(choice) Y <- model.matrix(~choice-1, data = y) X <- Car %>% select(college, hsg2, coml5) %>% data.matrix()
## fit fit <- emlogit(Y = Y, X = X) summary(fit) ## predicted probability prob <- predict(fit)
head(prob) %>% knitr::kable(digit = 3)
japan
data from MNP
package.
## multinomial outcome data(japan, package = "MNP") head(japan) # prepare datainput Y <- japan %>% select(LDP, NFP, SKG, JCP) %>% data.matrix() X <- japan %>% select(gender, education, age) %>% data.matrix() set.seed(1234) fit <- emlogit(Y = Y, X = X) summary(fit) pred <- predict(fit)
head(pred) %>% knitr::kable(digit = 3)
Since the binomial outcome is a special case of multinomial outcomes,
emlogit
can handle the binomial outcome.
set.seed(1234) ## generate X and pi(X) betas <- rnorm(4) X <- MASS::mvrnorm(5000, mu = rep(0, length(betas)), Sigma = diag(length(betas))) prob <- 1 / (1 + exp(- 0.5 - X %*% betas)) ## generate outcome ybin <- rbinom(n = 5000, size = 1, prob = prob) Y <- model.matrix(~ factor(ybin) - 1) ## fit fit <- emlogit(Y = Y, X = X) ## check betas <- c(0.5, betas) cbind(true = betas, estimate = summary(fit)$estimate) %>% knitr::kable(digit = 3)
The ECM algorithm used in this package utilizes the Polya-Gamma augmentation scheme originally developed by Polson et al. (2013). Applications to the multinomial outcome in the context of deriving the EM algorithm based on Polya-Gamma representation can be found in Durante et al. (2019) and Goplerud (2019), among others.
Add the following code to your website.
For more information on customizing the embed code, read Embedding Snippets.