1

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!

Joshua Mire
  • 736
  • 1
  • 6
  • 17
Ruser-lab9
  • 193
  • 1
  • 12
  • The `ÌD` column does not index the rows, so (Thats why you get the error messages). Why didi you choose to include the ID this way? – dario Jun 03 '20 at 19:32
  • In `mlogit.data` the `ID` indexes individuals that face several different choice situations. – Ahorn Jun 03 '20 at 19:58

1 Answers1

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
Ahorn
  • 3,686
  • 1
  • 10
  • 17
  • Thanks so much! actually when i changed the type of attributes from factor to integer and rerun the code, i had no error message. – Ruser-lab9 Jun 04 '20 at 15:44