I received some good help getting my data formatted properly produce a multinomial logistic model with mlogit here (Formatting data for mlogit)
However, I'm trying now to analyze the effects of covariates in my model. I find the help file in mlogit.effects()
to be not very informative. One of the problems is that the model appears to produce a lot of rows of NAs (see below, index(mod1)
).
- Can anyone clarify why my data is producing those NAs?
- Can anyone help me get
mlogit.effects
to work with the data below? - I would consider shifting the analysis to
multinom()
. However, I can't figure out how to format the data to fit the formula for usemultinom()
. My data is a series of rankings of seven different items (Accessible, Information, Trade offs, Debate, Social and Responsive) Would I just model whatever they picked as their first rank and ignore what they chose in other ranks? I can get that information.
Reproducible code is below:
#Loadpackages
library(RCurl)
library(mlogit)
library(tidyr)
library(dplyr)
#URL where data is stored
dat.url <- 'https://raw.githubusercontent.com/sjkiss/Survey/master/mlogit.out.csv'
#Get data
dat <- read.csv(dat.url)
#Complete cases only as it seems mlogit cannot handle missing values or tied data which in this case you might get because of median imputation
dat <- dat[complete.cases(dat),]
#Change the choice index variable (X) to have no interruptions, as a result of removing some incomplete cases
dat$X <- seq(1,nrow(dat),1)
#Tidy data to get it into long format
dat.out <- dat %>%
gather(Open, Rank, -c(1,9:12)) %>%
arrange(X, Open, Rank)
#Create mlogit object
mlogit.out <- mlogit.data(dat.out, shape='long',alt.var='Open',choice='Rank', ranked=TRUE,chid.var='X')
#Fit Model
mod1 <- mlogit(Rank~1|gender+age+economic+Job,data=mlogit.out)
Here is my attempt to set up a data frame similar to the one portrayed in the help file. It doesnt work. I confess although I know the apply family pretty well, tapply is murky to me.
with(mlogit.out, data.frame(economic=tapply(economic, index(mod1)$alt, mean)))
Compare from the help:
data("Fishing", package = "mlogit")
Fish <- mlogit.data(Fishing, varying = c(2:9), shape = "wide", choice = "mode")
m <- mlogit(mode ~ price | income | catch, data = Fish)
# compute a data.frame containing the mean value of the covariates in
# the sample data in the help file for effects
z <- with(Fish, data.frame(price = tapply(price, index(m)$alt, mean),
catch = tapply(catch, index(m)$alt, mean),
income = mean(income)))
# compute the marginal effects (the second one is an elasticity
effects(m, covariate = "income", data = z)
mod1 <- mlogit(...
it works fine. When I look atsummary(mod1)
it looks good. Looking at?index
, the help page points tomlogit.data
, which sounds like it is intended for use on data, not on a model, the description is: "shape a data.frame in a suitable form for the use of the mlogit function." Nor do I seeindex
used on a model in the help. Maybe you need to update yourmlogit
package? – Diametricallymlogit
hasn't been updated since December 2013, so that's probably not your problem. The onlyindex
object I find in my namespace is from thezoo
package. So, if you don't useindex
on your model (usesummary()
instead), do you still have a question? – DiametricallyFish
is a data frame, not a model! – DiametricallyNA
in theindex(mod1)
code. Did you try usingeffects
on the Game model from the mlogit vignette to see if you encounter the same issue on another ranked order model? – Appliedcode
dat<-dat[complete.cases(dat),]code
It seems like I get a similar error with the Game data:code
library(mlogit) #Load data data('Game2') #format game.dat<-mlogit.data(Game2, choice='ch', shape='long', alt.var='platform', ranked=TRUE) #Model game.mod<-mlogit(ch ~ own|hours, data=Game2, alt.var='platform', ranked=TRUE, shape='long', reflevel='PC') summary(game.mod) index(game.mod) #Sample data sample.dat<-expand.grid(platform=levels(dat$platform), hours=2, own=c(1)) #effects effects(mod, covariate='hours', data=test)code
– Whitleathereffects
with rank-ordered models? – Applied