1
$\begingroup$

I referred to Simulating Multinomial Logit Data with R post and have a working code that generates a categorical DV variable with 3 levels, but I wish to have 4 levels and also specify the proportion of each level, how should I modify the code?

set.seed(888)
# covariate matrix
mX <- rnorm_multi(n = 410,
                  mu = c(4.11, 4.17, 4.34),
                  sd = c(.64, .59, .63),
                  r = c(-.29, -.17, -.03),
                  varnames = c("Negative_affect",
                               "Effortful_control", "Extraversion"),
                  empirical = FALSE)

mX<-data.matrix(mX, rownames.force = NA)

# coefficients for each choice
vCoef1 = rep(0, 3)
vCoef2 = rnorm(3)
vCoef3 = rnorm(3)

# vector of probabilities
vProb = cbind(exp(mX%*%vCoef1), exp(mX%*%vCoef2), exp(mX%*%vCoef3))

# multinomial draws
strategy = t(apply(vProb, 1, rmultinom, n = 1, size = 1))
dfM = cbind.data.frame(strategy = apply(strategy, 1, function(x) which(x==1)), mX)
$\endgroup$

0

Browse other questions tagged or ask your own question.