Error with using mlogit R function: “The two indexes don't define unique observations”

扶醉桌前 提交于 2020-07-10 09:00:08

问题


My dataset look like this

ID  choice_situation    Alternative Attr1   Attr2   Attr3   choice
ID_1    1   1   0   0   0   0
ID_1    1   2   1   1   0   1
ID_1    2   1   1   1   0   0
ID_1    2   2   1   1   1   1
ID_1    3   1   2   1   0   1
ID_1    3   2   3   1   0   0
ID_2    1   1   3   0   1   1
ID_2    1   2   0   0   0   0
ID_2    2   1   2   1   1   0
ID_2    2   2   2   1   1   1
ID_2    3   1   0   0   0   1
ID_2    3   2   0   0   1   0
.....

Every time I run the code of mlogit function

DCE_data<- mlogit.data(data=dataset, choice = "choice", shape = "long", alt.var = "Alternative", id.var = "ID") #ok
model<- mlogit(choice ~ Attr1 + Attr2 + Attr3 | 0, DCE_data)#error

I get the error below :

Error in dfidx(x, .idx, pkg = pkg) : 
  the two indexes don't define unique observations

The problem is from the transformed data : DCE_data ?

Thanks in advance!


回答1:


For me your code works:

library(tidyverse)
df <- tibble::tribble(
           ~ID, ~choice_situation, ~Alternative, ~Attr1, ~Attr2, ~Attr3, ~choice,
        "ID_1",                1L,           1L,     0L,     0L,     0L,      0L,
        "ID_1",                1L,           2L,     1L,     1L,     0L,      1L,
        "ID_1",                2L,           1L,     1L,     1L,     0L,      0L,
        "ID_1",                2L,           2L,     1L,     1L,     1L,      1L,
        "ID_1",                3L,           1L,     2L,     1L,     0L,      1L,
        "ID_1",                3L,           2L,     3L,     1L,     0L,      0L,
        "ID_2",                1L,           1L,     3L,     0L,     1L,      1L,
        "ID_2",                1L,           2L,     0L,     0L,     0L,      0L,
        "ID_2",                2L,           1L,     2L,     1L,     1L,      0L,
        "ID_2",                2L,           2L,     2L,     1L,     1L,      1L,
        "ID_2",                3L,           1L,     0L,     0L,     0L,      1L,
        "ID_2",                3L,           2L,     0L,     0L,     1L,      0L
        )

library(mlogit)
DCE_data<- mlogit.data(data=df, choice = "choice", shape = "long", alt.var = "Alternative", id.var = "ID") #ok
model<- mlogit(choice ~ Attr1 + Attr2 + Attr3 | 0, DCE_data)#error
summary(model)

> model

Call:
mlogit(formula = choice ~ Attr1 + Attr2 + Attr3 | 0, data = DCE_data,     method = "nr")

Coefficients:
   Attr1     Attr2     Attr3  
 0.34137  14.86152   0.39473


来源:https://stackoverflow.com/questions/62181059/error-with-using-mlogit-r-function-the-two-indexes-dont-define-unique-observa

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!