mnlogit: Bayesian logit model with Pólya Gamma prior with MCMC

View source: R/mnlogit.R

mnlogitR Documentation

Bayesian logit model with Pólya Gamma prior with MCMC

Description

Bayesian logit model with Pólya Gamma prior with MCMC

Usage

mnlogit(
  X,
  Y,
  baseline = ncol(Y),
  niter = 1000,
  nburn = 500,
  A0 = 10^4,
  calc_marginal_fx = FALSE
)

Arguments

X

An n by k matrix of explanatory variables

Y

An n by p matrix of dependent variables

baseline

Baseline class for estimation. Parameters will be set to zero. Defaults to the p-th column.

niter

Total number of MCMC draws. Defaults to 1000.

nburn

Burn-in draws for MCMC. Note: nburn has to be lower than niter. Defaults to 500.

A0

Prior variance scalar for all slope coefficients

calc_marginal_fx

Should marginal effects be calculated? Defaults to FALSE.

Details

MCMC estimation of a multinomial logit model following Polson et al. (2013).

Value

A list containing

  • postb A k x p x (niter - nburn dimensions) array containing posterior draws of the slope coefficients.

  • marginal_fx A k x p x (niter - nburn dimensions) array containing posterior draws of marginal effects.

  • X, Y, baseline The matrices of explanatory and dependent variables, as defined above and the baseline class.

References

Nicholas G. Polson, James G. Scott, and Jesse Windle. Bayesian inference for logistic models using Polya-Gamma latent variables. Journal of the American statistical Association 108.504 (2013): 1339-1349.

Examples

n <- 100
p <- 3
k <- 2
X <- cbind(1, matrix(rnorm(n * (k - 1), 0, 2), n, k - 1))
BETA <- matrix(sample(c(-3:3), k * p, replace = TRUE), k, p)
BETA[, p] <- 0
Y <- exp(X %*% BETA) / rowSums(exp(X %*% BETA))
res1 <- mnlogit(X, Y)
print(BETA)
print(apply(res1$postb, c(1, 2), mean))

require(tidyr)
Y = dplyr::filter(argentina_luc,lu.from == "Cropland" & Ts == 2000) %>%
   pivot_wider(names_from = lu.to)
X = argentina_df$xmat %>% tidyr::pivot_wider(names_from = "ks") %>%
   dplyr::arrange(match(ns,Y$ns))
Y = Y %>% dplyr::select(-c(lu.from,Ts,ns))
X = X %>% dplyr::select(-c(ns))
res1 <- mnlogit(as.matrix(X), as.matrix(Y),baseline = which(colnames(Y) == "Cropland"),
          niter = 100,nburn = 50)
print(apply(res1$postb, c(1, 2), mean))

tkrisztin/downscalr documentation built on June 2, 2025, 1:16 a.m.