Bayesian logit model with Pólya Gamma prior with MCMC
Usage
mnlogit(X, Y, baseline = ncol(Y), niter = 1000, nburn = 500, A0 = 10^4)
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
- nburn
Burn-in draws for MCMC
- A0
Prior variance scalar for all slope coefficients
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)
#>
|
| | 0%
|
| | 1%
|
|= | 1%
|
|= | 2%
|
|== | 2%
|
|== | 3%
|
|== | 4%
|
|=== | 4%
|
|=== | 5%
|
|==== | 5%
|
|==== | 6%
|
|===== | 6%
|
|===== | 7%
|
|===== | 8%
|
|====== | 8%
|
|====== | 9%
|
|======= | 9%
|
|======= | 10%
|
|======= | 11%
|
|======== | 11%
|
|======== | 12%
|
|========= | 12%
|
|========= | 13%
|
|========= | 14%
|
|========== | 14%
|
|========== | 15%
|
|=========== | 15%
|
|=========== | 16%
|
|============ | 16%
|
|============ | 17%
|
|============ | 18%
|
|============= | 18%
|
|============= | 19%
|
|============== | 19%
|
|============== | 20%
|
|============== | 21%
|
|=============== | 21%
|
|=============== | 22%
|
|================ | 22%
|
|================ | 23%
|
|================ | 24%
|
|================= | 24%
|
|================= | 25%
|
|================== | 25%
|
|================== | 26%
|
|=================== | 26%
|
|=================== | 27%
|
|=================== | 28%
|
|==================== | 28%
|
|==================== | 29%
|
|===================== | 29%
|
|===================== | 30%
|
|===================== | 31%
|
|====================== | 31%
|
|====================== | 32%
|
|======================= | 32%
|
|======================= | 33%
|
|======================= | 34%
|
|======================== | 34%
|
|======================== | 35%
|
|========================= | 35%
|
|========================= | 36%
|
|========================== | 36%
|
|========================== | 37%
|
|========================== | 38%
|
|=========================== | 38%
|
|=========================== | 39%
|
|============================ | 39%
|
|============================ | 40%
|
|============================ | 41%
|
|============================= | 41%
|
|============================= | 42%
|
|============================== | 42%
|
|============================== | 43%
|
|============================== | 44%
|
|=============================== | 44%
|
|=============================== | 45%
|
|================================ | 45%
|
|================================ | 46%
|
|================================= | 46%
|
|================================= | 47%
|
|================================= | 48%
|
|================================== | 48%
|
|================================== | 49%
|
|=================================== | 49%
|
|=================================== | 50%
|
|=================================== | 51%
|
|==================================== | 51%
|
|==================================== | 52%
|
|===================================== | 52%
|
|===================================== | 53%
|
|===================================== | 54%
|
|====================================== | 54%
|
|====================================== | 55%
|
|======================================= | 55%
|
|======================================= | 56%
|
|======================================== | 56%
|
|======================================== | 57%
|
|======================================== | 58%
|
|========================================= | 58%
|
|========================================= | 59%
|
|========================================== | 59%
|
|========================================== | 60%
|
|========================================== | 61%
|
|=========================================== | 61%
|
|=========================================== | 62%
|
|============================================ | 62%
|
|============================================ | 63%
|
|============================================ | 64%
|
|============================================= | 64%
|
|============================================= | 65%
|
|============================================== | 65%
|
|============================================== | 66%
|
|=============================================== | 66%
|
|=============================================== | 67%
|
|=============================================== | 68%
|
|================================================ | 68%
|
|================================================ | 69%
|
|================================================= | 69%
|
|================================================= | 70%
|
|================================================= | 71%
|
|================================================== | 71%
|
|================================================== | 72%
|
|=================================================== | 72%
|
|=================================================== | 73%
|
|=================================================== | 74%
|
|==================================================== | 74%
|
|==================================================== | 75%
|
|===================================================== | 75%
|
|===================================================== | 76%
|
|====================================================== | 76%
|
|====================================================== | 77%
|
|====================================================== | 78%
|
|======================================================= | 78%
|
|======================================================= | 79%
|
|======================================================== | 79%
|
|======================================================== | 80%
|
|======================================================== | 81%
|
|========================================================= | 81%
|
|========================================================= | 82%
|
|========================================================== | 82%
|
|========================================================== | 83%
|
|========================================================== | 84%
|
|=========================================================== | 84%
|
|=========================================================== | 85%
|
|============================================================ | 85%
|
|============================================================ | 86%
|
|============================================================= | 86%
|
|============================================================= | 87%
|
|============================================================= | 88%
|
|============================================================== | 88%
|
|============================================================== | 89%
|
|=============================================================== | 89%
|
|=============================================================== | 90%
|
|=============================================================== | 91%
|
|================================================================ | 91%
|
|================================================================ | 92%
|
|================================================================= | 92%
|
|================================================================= | 93%
|
|================================================================= | 94%
|
|================================================================== | 94%
|
|================================================================== | 95%
|
|=================================================================== | 95%
|
|=================================================================== | 96%
|
|==================================================================== | 96%
|
|==================================================================== | 97%
|
|==================================================================== | 98%
|
|===================================================================== | 98%
|
|===================================================================== | 99%
|
|======================================================================| 99%
|
|======================================================================| 100%
print(BETA)
#> [,1] [,2] [,3]
#> [1,] -3 1 0
#> [2,] 3 -3 0
print(apply(res1$postb, c(1, 2), mean))
#> [,1] [,2] [,3]
#> [1,] -3.309630 1.235649 0
#> [2,] 3.245753 -3.852727 0
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)
#>
|
| | 0%
|
|= | 1%
|
|= | 2%
|
|== | 3%
|
|=== | 4%
|
|==== | 5%
|
|==== | 6%
|
|===== | 7%
|
|====== | 8%
|
|====== | 9%
|
|======= | 10%
|
|======== | 11%
|
|======== | 12%
|
|========= | 13%
|
|========== | 14%
|
|========== | 15%
|
|=========== | 16%
|
|============ | 17%
|
|============= | 18%
|
|============= | 19%
|
|============== | 20%
|
|=============== | 21%
|
|=============== | 22%
|
|================ | 23%
|
|================= | 24%
|
|================== | 25%
|
|================== | 26%
|
|=================== | 27%
|
|==================== | 28%
|
|==================== | 29%
|
|===================== | 30%
|
|====================== | 31%
|
|====================== | 32%
|
|======================= | 33%
|
|======================== | 34%
|
|======================== | 35%
|
|========================= | 36%
|
|========================== | 37%
|
|=========================== | 38%
|
|=========================== | 39%
|
|============================ | 40%
|
|============================= | 41%
|
|============================= | 42%
|
|============================== | 43%
|
|=============================== | 44%
|
|================================ | 45%
|
|================================ | 46%
|
|================================= | 47%
|
|================================== | 48%
|
|================================== | 49%
|
|=================================== | 50%
|
|==================================== | 51%
|
|==================================== | 52%
|
|===================================== | 53%
|
|====================================== | 54%
|
|====================================== | 55%
|
|======================================= | 56%
|
|======================================== | 57%
|
|========================================= | 58%
|
|========================================= | 59%
|
|========================================== | 60%
|
|=========================================== | 61%
|
|=========================================== | 62%
|
|============================================ | 63%
|
|============================================= | 64%
|
|============================================== | 65%
|
|============================================== | 66%
|
|=============================================== | 67%
|
|================================================ | 68%
|
|================================================ | 69%
|
|================================================= | 70%
|
|================================================== | 71%
|
|================================================== | 72%
|
|=================================================== | 73%
|
|==================================================== | 74%
|
|==================================================== | 75%
|
|===================================================== | 76%
|
|====================================================== | 77%
|
|======================================================= | 78%
|
|======================================================= | 79%
|
|======================================================== | 80%
|
|========================================================= | 81%
|
|========================================================= | 82%
|
|========================================================== | 83%
|
|=========================================================== | 84%
|
|============================================================ | 85%
|
|============================================================ | 86%
|
|============================================================= | 87%
|
|============================================================== | 88%
|
|============================================================== | 89%
|
|=============================================================== | 90%
|
|================================================================ | 91%
|
|================================================================ | 92%
|
|================================================================= | 93%
|
|================================================================== | 94%
|
|================================================================== | 95%
|
|=================================================================== | 96%
|
|==================================================================== | 97%
|
|===================================================================== | 98%
|
|===================================================================== | 99%
|
|======================================================================| 100%
print(apply(res1$postb, c(1, 2), mean))
#> Cropland Forest OtherLand Pasture
#> Intercept 0 -3.796285e+00 -5.8772331073 -7.656935e+00
#> Cropland 0 -6.183014e-03 0.0031790380 -2.351784e-02
#> Forest 0 -2.170406e-02 0.0052990212 5.178866e-03
#> OtherLand 0 8.710393e-03 -0.0125493881 -9.181244e-03
#> Pasture 0 2.100400e-03 0.0004128899 4.825130e-03
#> Plantations 0 -2.582314e-02 -0.2219133925 -3.710696e-02
#> Urban 0 -1.014442e-03 -0.8361251782 -4.874117e+00
#> MeanTemp 0 -7.260722e-03 0.0273925055 -2.245136e-03
#> MeanPrecip 0 -5.410051e-04 -0.0004399593 -2.266767e-04
#> HarvWood 0 1.142146e-02 0.5454130018 3.392572e-02
#> HarvCost 0 3.737684e-04 -0.0032157449 5.899610e-04
#> GRASYLD 0 -1.903381e-01 -0.0214090318 5.002190e-02
#> meanTimeToMarket 0 -3.871487e-03 -0.0006335591 9.403948e-04
#> totPop 0 -2.707342e-04 -0.0001508931 -1.520544e-04
#> ruralPop 0 2.623296e-01 -0.7562659797 -9.096202e-01
#> Altitude 0 -1.026486e-03 0.0005487423 -2.940524e-04
#> Slope 0 2.796321e-02 0.0254912940 -3.140357e-02
#> Soil2_Medium 0 -1.134372e+00 -0.6925383063 2.659172e+00
#> Soil3_Heavy 0 4.065724e-01 -0.9644933834 3.973145e+00
#> Soil4_Stony 0 -1.009990e+00 -0.2342204054 -2.249419e+00
#> MEANYLD 0 -5.795987e+01 7.7719622311 -3.258236e+01
#> HI_MEAN 0 1.666235e+01 -4.2353022799 1.781134e+01
#> LI_MEAN 0 1.225341e+01 0.5377789658 -4.120644e+00
#> IR_MEAN 0 1.394622e+01 -2.1287763538 7.549424e+00
#> SS_MEAN 0 1.540385e+01 -1.6446134950 1.141858e+01
#> Plantations Urban
#> Intercept -1.408002e+01 -1.528958e+01
#> Cropland -1.136966e-02 1.246410e-02
#> Forest 4.585718e-03 -9.783224e-03
#> OtherLand 3.803265e-03 5.789315e-03
#> Pasture -5.408957e-03 -2.935421e-03
#> Plantations -9.436381e-03 -4.095200e-01
#> Urban -5.209868e-01 -6.680181e-01
#> MeanTemp 7.575193e-03 7.062805e-04
#> MeanPrecip -1.017311e-04 -1.656599e-04
#> HarvWood 5.762479e-01 1.747155e+00
#> HarvCost -2.430452e-03 -2.265119e-02
#> GRASYLD 1.626919e-02 -1.181236e-01
#> meanTimeToMarket 1.362148e-03 5.523938e-04
#> totPop 1.181234e-05 -1.398674e-04
#> ruralPop -1.517320e+00 7.038877e-01
#> Altitude -3.016830e-04 -7.122637e-04
#> Slope -3.609381e-02 -1.489167e-01
#> Soil2_Medium 9.190299e+00 9.486333e+00
#> Soil3_Heavy 1.092553e+01 1.091791e+01
#> Soil4_Stony 8.473508e+00 9.945717e+00
#> MEANYLD 7.976525e+00 -2.504058e+01
#> HI_MEAN 1.460317e+00 -7.083513e+00
#> LI_MEAN -3.323083e+00 2.206426e+01
#> IR_MEAN -3.411697e+00 6.138944e+00
#> SS_MEAN -2.656811e+00 4.157459e+00