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...
Cross Validated is a question and answer site for people interested in statistics, machine learning, data analysis, data mining, and data visualization. It only takes a minute to sign up.Sign up to join this community
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)
dfM$y encode the same information differently.
#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)