简体   繁体   中英

issue with mlogit package related to modalities

I am using the mlogit package to run a multinomial mixed effect model. The data are in the long format to begin with. I am trying to determine whether a variable is related to differences in a categorical outcome, sleep duration, that has 3 categories (0,1,2). Sample data are:

id  sleep  var1
1    0     1.5
1    2     1.5
2    1      2
2    1      2
3    0      1

I tried running the following code:

mldata <- mlogit.data(df, choice="sleep", shape ="long")

mlogit.model1 <- mlogit(sleep ~ 1 | var1, data = df, reflevel = "0")

Error in dfidx::dfidx(data = data, dfa$idx, drop.index = dfa$drop.index,  :
  The choice variable must have exactly two modalities

In a previous post, I read that this message was caused by including the "varying" function of the mlogit package. However, I didn't include that so I don't understand what is going on.

Can someone please help?

I had the same issue but it seems like it has to do with some confusion about the shape argument. From one of the vignette:

Data sets can have two different shapes: a wide shape (one row for each choice situation) or a long shape (one row for each alternative and, therefore, as many rows as there are alternatives for each choice situation).

Thus, while your data is in long format in the traditional sense it is in the wide format in the mlogit package sense as there is a choice in each row.

I find this to be very confusing and the error message does not help at all. Two examples from the package are

data("Fishing", package = "mlogit")
head(Fishing[, 1:4])
#R>      mode price.beach price.pier price.boat
#R> 1 charter     157.930    157.930    157.930
#R> 2 charter      15.114     15.114     10.534
#R> 3    boat     161.874    161.874     24.334
#R> 4    pier      15.134     15.134     55.930
#R> 5    boat     106.930    106.930     41.514
#R> 6 charter     192.474    192.474     28.934
Fish <- dfidx(Fishing, varying = 2:9, shape = "wide", choice = "mode")

for wide format and

data("TravelMode", package = "AER")
head(TravelMode)
#R>   individual  mode choice wait vcost travel gcost income size
#R> 1          1   air     no   69    59    100    70     35    1
#R> 2          1 train     no   34    31    372    71     35    1
#R> 3          1   bus     no   35    25    417    70     35    1
#R> 4          1   car    yes    0    10    180    30     35    1
#R> 5          2   air     no   64    58     68    68     30    2
#R> 6          2 train     no   44    31    354    84     30    2

# default for dfidx's shape is long
TM2 <- dfidx(TravelMode, idx = c("individual", "mode"))

for long format. Notice the mode and the choice column with the no / yes .

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM