if (FALSE) {
data(Nethvote)
## just a choice-specific X var
post1 <- MCMCmnl(vote ~
choicevar(distD66, "sqdist", "D66") +
choicevar(distPvdA, "sqdist", "PvdA") +
choicevar(distVVD, "sqdist", "VVD") +
choicevar(distCDA, "sqdist", "CDA"),
baseline="D66", mcmc.method="IndMH", B0=0,
verbose=500, mcmc=100000, thin=10, tune=1.0,
data=Nethvote)
plot(post1)
summary(post1)
## just individual-specific X vars
post2<- MCMCmnl(vote ~
relig + class + income + educ + age + urban,
baseline="D66", mcmc.method="IndMH", B0=0,
verbose=500, mcmc=100000, thin=10, tune=0.5,
data=Nethvote)
plot(post2)
summary(post2)
## both choice-specific and individual-specific X vars
post3 <- MCMCmnl(vote ~
choicevar(distD66, "sqdist", "D66") +
choicevar(distPvdA, "sqdist", "PvdA") +
choicevar(distVVD, "sqdist", "VVD") +
choicevar(distCDA, "sqdist", "CDA") +
relig + class + income + educ + age + urban,
baseline="D66", mcmc.method="IndMH", B0=0,
verbose=500, mcmc=100000, thin=10, tune=0.5,
data=Nethvote)
plot(post3)
summary(post3)
## numeric y variable
nethvote$vote <- as.numeric(nethvote$vote)
post4 <- MCMCmnl(vote ~
choicevar(distD66, "sqdist", "2") +
choicevar(distPvdA, "sqdist", "3") +
choicevar(distVVD, "sqdist", "4") +
choicevar(distCDA, "sqdist", "1") +
relig + class + income + educ + age + urban,
baseline="2", mcmc.method="IndMH", B0=0,
verbose=500, mcmc=100000, thin=10, tune=0.5,
data=Nethvote)
plot(post4)
summary(post4)
## Simulated data example with nonconstant choiceset
n <- 1000
y <- matrix(0, n, 4)
colnames(y) <- c("a", "b", "c", "d")
xa <- rnorm(n)
xb <- rnorm(n)
xc <- rnorm(n)
xd <- rnorm(n)
xchoice <- cbind(xa, xb, xc, xd)
z <- rnorm(n)
for (i in 1:n){
## randomly determine choiceset (c is always in choiceset)
choiceset <- c(3, sample(c(1,2,4), 2, replace=FALSE))
numer <- matrix(0, 4, 1)
for (j in choiceset){
if (j == 3){
numer[j] <- exp(xchoice[i, j] )
}
else {
numer[j] <- exp(xchoice[i, j] - z[i] )
}
}
p <- numer / sum(numer)
y[i,] <- rmultinom(1, 1, p)
y[i,-choiceset] <- -999
}
post5 <- MCMCmnl(y~choicevar(xa, "x", "a") +
choicevar(xb, "x", "b") +
choicevar(xc, "x", "c") +
choicevar(xd, "x", "d") + z,
baseline="c", verbose=500,
mcmc=100000, thin=10, tune=.85)
plot(post5)
summary(post5)
}
Run the code above in your browser using DataLab