3

After successfully running the mlogit model in R, I get an error trying to obtain marginal effects that says:

"Error in predict.mlogit(object, data) : the number of rows of the data.frame should be a multiple of the number of alternatives"

I have even tried to change line number 16 in the source code as explained in a different post and still get the same error. Any help would be appreciated. All my variables are NOT alternative specific. I have 4 alternatives.

#**model:**(ran successfully) 
m<-summary(mlogit(TypOfCr~1|OneOrTwoVeh|1,data=mnldata,reflevel="PDO"))
#**means:**(ran successfully)
z <- with(mnldata, data.frame(OneOrTwoVeh=mean(OneOrTwoVeh)))
#**effects**: effects(m,covariate="OneOrTwoVeh",data=z)
effects(m,covariate="OneOrTwoVeh",data=z)

Error in predict.mlogit(object, data) : 
  the number of rows of the data.frame should be a multiple of the number of alternatives

The following is a link to a similar question posted on the web but I am having hard time with the solution posted there. marginal effects of mlogit in R Thanks in advance

jay.sf
  • 60,139
  • 8
  • 53
  • 110
Ed.
  • 31
  • 4
  • It would be helpful having the link to the other post you're mentionning. Also, making a reproducible example is always a good idea (see http://stackoverflow.com/questions/5963269/how-to-make-a-great-r-reproducible-example) – Dominic Comtois Mar 23 '15 at 07:33
  • here is a link for a similar question that the solution in it did not help me. http://stackoverflow.com/questions/25831729/marginal-effects-of-mlogit-in-r – Ed. Mar 23 '15 at 13:21

2 Answers2

0

effects() expects a model object as its first argument. But m is not a model object. Rather, it is the output of running the summary() function on a model object. Instead try

m <- mlogit(TypOfCr~1|OneOrTwoVeh|1,data=mnldata,reflevel="PDO") 

(i.e., create a model object, not a summary of a model object) and feed that to effects().

eipi10
  • 91,525
  • 24
  • 209
  • 285
  • Hi, thank you for your response. I just tried that exactly as you said and still got the same error; the number of rows in the data.frame should be a multiple of the number of alternatives. In a different post with a problem similar to my problem, a person actually overcame this problem by "copying" the "means" row as many times as needed to create a matrix that has the number of rows equal to the number of alternatives, unfortunately, I do not know how to do that. Any suggestions? – Ed. Mar 23 '15 at 04:50
0

Try this:

z <- with(mnldata, data.frame(OneOrTwoVeh = tapply(OneOrTwoVeh, index(m)$alt, mean)))
jkt
  • 946
  • 1
  • 7
  • 18
  • Ended up here randomly and realised this post was more than a year old after posting my answer. Anyways, this gives you the mean as a row for each alternative. – jkt Jun 11 '16 at 18:08