## simulate data
# \donttest{
data <- simulate_mlogit_dataset()
# }
## estimate model without misclassification error
# \donttest{
library(mlogit)
data2 <- mlogit.data(data, varying = NULL, choice = "Y", shape = "wide")
summary(mlogit(Y ~ 1 | X + M2, data2, reflevel = "3"))
# }
## estimate model with misclassification error
# \donttest{
summary(mlogit(Y ~ 1 | X + M, data2, reflevel = "3"))
# }
## estimate misclassification probabilities
# \donttest{
Pmodel <- glm(M2 ~ M + X, data = data, family = binomial("logit"))
summary(Pmodel)
# }
## construct a-posteriori probabilities from Pmodel
# \donttest{
P <- predict(Pmodel, newdata = data, type = "response")
P <- cbind(1 - P, P)
dimnames(P)[[2]] <- c("M0", "M1") ## speaking names
# }
## estimate misclassGLM
# \donttest{
Yneu <- matrix(rep.int(0, nrow(data) * 3), ncol = 3)
for (i in 1:nrow(data)) Yneu[i, data$Y[i]] <- 1
est <- misclassMlogit(Y = Yneu,
X = as.matrix(data[, 2, drop = FALSE]),
setM = matrix(c(0, 1), nrow = 2),
P = P)
summary(est)
# }
## and bootstrapping the results from dataset
if (FALSE) {
summary(boot.misclassMlogit(est,
Y = Yneu,
X = data.matrix(data[, 2, drop = FALSE]),
Pmodel = Pmodel,
PX = data,
repetitions = 100))
}
Run the code above in your browser using DataLab