# Simulating Multinomial Logit Data with R

I'm looking to generate fake data to fit a multinomial logit in R? Any code/suggestions on material to look at would be very much appreciated...

Thanks.

• Can you say a little more? Do you need to control probabilities for each outcome? Do you need to specify particular predictor relationships? Stuff like that may help guide response to your needs. – Alexis Jun 17 '14 at 17:29
• Sure. I am looking to generate simulation data that fits a particular relationship with known parameters. That is, I have vector of X's and a set of parameters and want to generate fake data that I can predict from a multinomial logit with the known parameters..hope that makes sense...! – user3749518 Jun 18 '14 at 22:48

It is really simple to generate multinomial logit regression data. All you need to keep in mind are the normalizing assumptions.

# covariate matrix
mX = matrix(rnorm(1000), 200, 5)

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

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

# multinomial draws
mChoices = t(apply(vProb, 1, rmultinom, n = 1, size = 1))
dfM = cbind.data.frame(y = apply(mChoices, 1, function(x) which(x==1)), mX)


Here mChoices and dfM\$y encode the same information differently.

• One thing that threw me off for a second: vProb is not a probability, but non-negative number. However, rmultinom internally normalizes so they sum to 1. – andybega Aug 4 '15 at 7:59
• @andybega It does indeed -- and that was intended. – tchakravarty Aug 4 '15 at 10:33
• It was just surprising to me. I did something similar but where I manually normalized probabilities to 1 before sampling outcomes without using rmultinorm. Makes perfect sense though. – andybega Aug 5 '15 at 7:18

This wikibooks link describes generating multinomial ordered logit data. The mlogit package seems to have some existing data sets as well.

#Genarating 500 random numbers with zero mean
x = rnorm(500,0)
#Assigning the values of beta1 and beta2
Beta1 = 2
Beta2 = .5
#Calculation of denominator for probability calculation
Denominator= 1+exp(Beta1*x)+exp(Beta2*x)
#Calculating the matrix of probabilities for three choices
vProb = cbind(1/Denominator, exp(x*Beta1)/Denominator, exp(x*Beta2)/Denominator )
# Assigning the value one to maximum probability and zero for rest to get the appropriate choices for value of x
mChoices = t(apply(vProb, 1, rmultinom, n = 1, size = 1))
# Value of Y and X together
dfM = cbind.data.frame(y = apply(mChoices, 1, function(x) which(x==1)), x)
#Adding library for multinomial logit regression
library("nnet")
#We want zero intercept hence x+0 hence the foumula of regression as below
fit<-(multinom(y ~ x + 0, dfM))
#This function uses first y as base class
#hence upper probability calculation is changed
summary(fit)
#In case we do not keep intercept as zero
fit2<-multinom(y ~ x, dfM)
summary(fit2)
#This also result intercept very close to zero and non significant
#and value of beta as modeled earlier and significant
#running from mlogit package
library(mlogit)
DM<-mlogit.data(dfM, shape="wide",sep="",choice="y",alt.levels=1:3)
#Do not know why -1 is used at two places. I will appreciate if some one can explain
fit3<-mlogit(y~-1|-1+x,data=DM)
summary(fit3)

• I think this data generating process is kind more straightforward. Because of the ML estimation of coefficients(Beta1, Beta2) in most of the cases provides the coefficients of alternative levels comparing to the baseline level, these estimators will not be consistent with the true coefficients of DGP(data generating process) in the above answers. However, this DGP generates baseline level (denominator) first so that the estimation of most packages will provide the comparable estimation of the coefficients of alternative levels. – Xiaorui Zhu Jan 12 at 11:22
1. Generation independent variables and error term by respectively using rnorm(), rgumbel() in package {FAdist}.
2. Setting truth parameters
3. Calculation of utility (latent variables) of each alternatives and choice result (dependent variable)

Fin.

• There are two approaches to generate multinomial logit process, I think. Which is better, Mr. fg nu's way and my way? – Pavel Nedved Dec 28 '14 at 18:18