knitr::opts_chunk$set(
  collapse = TRUE,
  comment = "#>",
  fig.path = "man/figures/README-",
  out.width = "100%"
)

emlogit

R build status

emlogit is a R package that implements the Expectation and Conditional-Maximization (ECM) algorithm for the multinomial logistic regression.

Table of Contents

| Installation | Examples | References | |-|-|-|

Installation

You can install the development version from GitHub with:

# install.packages("devtools")
devtools::install_github("soichiroy/emlogit")

Examples

Categorical outcome

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)

Multinomial outcome

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)

Binary outcome

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)

References

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.



soichiroy/emlogit documentation built on Sept. 24, 2021, 5:22 p.m.