rhierMnlRwMixture {bayesm} | R Documentation |
rhierMnlRwMixture
is a MCMC algorithm for a hierarchical multinomial logit with a mixture of normals
heterogeneity distribution. This is a hybrid Gibbs Sampler with a RW Metropolis step for the MNL
coefficients for each panel unit.
rhierMnlRwMixture(Data, Prior, Mcmc)
Data |
list(p,lgtdata,Z) ( Z is optional) |
Prior |
list(a,deltabar,Ad,mubar,Amu,nu,V,ncomp) (all but ncomp are optional) |
Mcmc |
list(s,w,R,keep) (R required) |
Model:
y_i ~ MNL(X_i,beta_i). i=1,..., length(lgtdata). theta_i is nvar x 1.
beta_i= ZDelta[i,] + u_i.
Note: here ZDelta refers to Z%*%D, ZDelta[i,] is ith row of this product.
Delta is an nz x nvar array.
u_i ~ N(mu_{ind},Sigma_{ind}). ind ~ multinomial(pvec).
Priors:
pvec ~ dirichlet (a)
delta= vec(Delta) ~ N(deltabar,A_d^{-1})
mu_j ~ N(mubar,Sigma_j (x) Amu^{-1})
Sigma_j ~ IW(nu,V)
Lists contain:
p
lgtdata
lgtdata[[i]]$y
lgtdata[[i]]$X
a
deltabar
Ad
mubar
Amu
nu
V
ncomp
s
w
R
keep
a list containing:
Deltadraw |
R/keep x nz*nvar matrix of draws of Delta, first row is initial value |
betadraw |
nlgt x nvar x R/keep array of draws of betas |
nmix |
list of 3 components, probdraw, NULL, compdraw |
loglike |
log-likelihood for each kept draw (length R/keep) |
More on probdraw
component of nmix list:
R/keep x ncomp matrix of draws of probs of mixture components (pvec)
More on compdraw
component of return value list:
Note: Z should not include an intercept and is centered for ease of interpretation.
Be careful in assessing prior parameter, Amu. .01 is too small for many applications. See
Rossi et al, chapter 5 for full discussion.
Note: as of version 2.0-2 of bayesm
, the fractional weight parameter has been changed
to a weight between 0 and 1. w is the fractional weight on the normalized pooled likelihood.
This differs from what is in Rossi et al chapter 5, i.e.
like_i^(1-w) x like_pooled^((n_i/N)*w)
Large R values may be required (>20,000).
Peter Rossi, Graduate School of Business, University of Chicago, Peter.Rossi@ChicagoGsb.edu.
For further discussion, see Bayesian Statistics and Marketing
by Rossi, Allenby and McCulloch, Chapter 5.
http://faculty.chicagogsb.edu/peter.rossi/research/bsm.html
## if(nchar(Sys.getenv("LONG_TEST")) != 0) {R=10000} else {R=10} set.seed(66) p=3 # num of choice alterns ncoef=3 nlgt=300 # num of cross sectional units nz=2 Z=matrix(runif(nz*nlgt),ncol=nz) Z=t(t(Z)-apply(Z,2,mean)) # demean Z ncomp=3 # no of mixture components Delta=matrix(c(1,0,1,0,1,2),ncol=2) comps=NULL comps[[1]]=list(mu=c(0,-1,-2),rooti=diag(rep(1,3))) comps[[2]]=list(mu=c(0,-1,-2)*2,rooti=diag(rep(1,3))) comps[[3]]=list(mu=c(0,-1,-2)*4,rooti=diag(rep(1,3))) pvec=c(.4,.2,.4) simmnlwX= function(n,X,beta) { ## simulate from MNL model conditional on X matrix k=length(beta) Xbeta=X%*%beta j=nrow(Xbeta)/n Xbeta=matrix(Xbeta,byrow=TRUE,ncol=j) Prob=exp(Xbeta) iota=c(rep(1,j)) denom=Prob%*%iota Prob=Prob/as.vector(denom) y=vector("double",n) ind=1:j for (i in 1:n) {yvec=rmultinom(1,1,Prob[i,]); y[i]=ind%*%yvec} return(list(y=y,X=X,beta=beta,prob=Prob)) } ## simulate data simlgtdata=NULL ni=rep(50,300) for (i in 1:nlgt) { betai=Delta%*%Z[i,]+as.vector(rmixture(1,pvec,comps)$x) Xa=matrix(runif(ni[i]*p,min=-1.5,max=0),ncol=p) X=createX(p,na=1,nd=NULL,Xa=Xa,Xd=NULL,base=1) outa=simmnlwX(ni[i],X,betai) simlgtdata[[i]]=list(y=outa$y,X=X,beta=betai) } ## plot betas if(0){ ## set if(1) above to produce plots bmat=matrix(0,nlgt,ncoef) for(i in 1:nlgt) {bmat[i,]=simlgtdata[[i]]$beta} par(mfrow=c(ncoef,1)) for(i in 1:ncoef) hist(bmat[,i],breaks=30,col="magenta") } ## set parms for priors and Z Prior1=list(ncomp=5) keep=5 Mcmc1=list(R=R,keep=keep) Data1=list(p=p,lgtdata=simlgtdata,Z=Z) out=rhierMnlRwMixture(Data=Data1,Prior=Prior1,Mcmc=Mcmc1) cat("Summary of Delta draws",fill=TRUE) summary(out$Deltadraw,tvalues=as.vector(Delta)) cat("Summary of Normal Mixture Distribution",fill=TRUE) summary(out$nmix) if(0) { ## plotting examples plot(out$betadraw) plot(out$nmix) }