使用mlogit R函数时出错:“两个​​索引未定义唯一的观察结果". [英] Error with using mlogit R function: "The two indexes don't define unique observations"

查看:156
本文介绍了使用mlogit R函数时出错:“两个​​索引未定义唯一的观察结果".的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我的数据集如下

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
.....

每次我运行mlogit函数的代码

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

我收到以下错误:

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

问题来自转换后的数据:DCE_data ?

The problem is from the transformed data : DCE_data ?

提前谢谢!

推荐答案

对我来说,您的代码有效:

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

这篇关于使用mlogit R函数时出错:“两个​​索引未定义唯一的观察结果".的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆