4
$\begingroup$

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.

$\endgroup$
2
  • 2
    $\begingroup$ 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. $\endgroup$
    – Alexis
    Jun 17, 2014 at 17:29
  • $\begingroup$ 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...! $\endgroup$ Jun 18, 2014 at 22:48

4 Answers 4

6
$\begingroup$

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.

$\endgroup$
6
  • 1
    $\begingroup$ 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. $\endgroup$
    – andybega
    Aug 4, 2015 at 7:59
  • $\begingroup$ @andybega It does indeed -- and that was intended. $\endgroup$ Aug 4, 2015 at 10:33
  • $\begingroup$ 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. $\endgroup$
    – andybega
    Aug 5, 2015 at 7:18
  • 1
    $\begingroup$ Thanks for posting this - I've been struggling with randomly generated multinomial logistic regression data for the past few days. Generating binomial logistic regression observations can be done in one line of code: a <- rbinom(n=200, size=1, prob=exp(X%*%Coef)/(1 + exp(X%*%Coef))), but multinomial is a headache. $\endgroup$
    – RobertF
    Mar 28, 2020 at 16:31
  • 1
    $\begingroup$ And if you want to add a nonzero intercept to the multinomial DGF, modify the above code: mX = cbind(rep(1, 200), matrix(rnorm(1000), 200, 5)); vCoef1 = rep(0, 6); vCoef2 = rnorm(6); vCoef3 = rnorm(6) then run the rest of the code as written. For a final check run a multinomial logistic regression on the generated data (remove the intercept coefficient column): m <- multinom(y ~ ., data = dfM[,-2]); summary(m). The values in vCoef2 and vCoef3 ought to closely match the coefficients from the regression output. $\endgroup$
    – RobertF
    Mar 31, 2020 at 18:28
2
$\begingroup$
#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)
$\endgroup$
1
  • 1
    $\begingroup$ 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. $\endgroup$ Jan 12, 2019 at 11:22
1
$\begingroup$

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

$\endgroup$
-2
$\begingroup$
  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.

$\endgroup$
1
  • $\begingroup$ There are two approaches to generate multinomial logit process, I think. Which is better, Mr. fg nu's way and my way? $\endgroup$
    – Tky
    Dec 28, 2014 at 18:18

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.