1

我已经能够制作一个 mlogit.data 对象以输入到 gmnl 包中以进行潜在类分析,但我一直遇到它说choiced变量没有索引的问题。我看到有关于同一问题的另一个问题,但该问题没有答案。我已经包含了测试选择数据的样本。感谢您对此事的任何帮助。

personid    choiceid    alt payment management  assessment  crop
1   1   1   3   2   2   3
1   2   2   2   2   1   3
1   3   1   3   2   1   3
1   4   1   2   1   3   1
1   5   1   2   1   3   1
1   6   2   1   1   2   1
1   7   2   3   1   2   3
1   8   2   3   1   2   3
1   9   2   3   1   1   2
1   10  2   3   1   1   2
1   11  2   3   1   2   1
1   12  2   2   1   1   3
1   13  3   1   2   1   1
1   14  2   1   1   2   3
1   15  2   2   1   2   2
1   16  2   1   1   1   3
2   17  3   1   2   1   2
2   18  3   1   3   1   2
2   19  1   3   1   1   3


library("gmnl")
library("mlogit")
library("readr")
library("Formula")


testchoices <- testchoices <- read_csv("~/Documents/year 3/DISS/MODELS/choice/testchoices.csv")
#> 
#> ── Column specification ────────────────────────────────────────────────────────
#> cols(
#>   personid = col_double(),
#>   choiceid = col_double(),
#>   alt = col_double(),
#>   payment = col_double(),
#>   management = col_double(),
#>   assessment = col_double(),
#>   crop = col_double()
#> )
test <- as.data.frame(testchoices)
test$choiceid <- 1:nrow(test)
head(test, 19)
#>    personid choiceid alt payment management assessment crop
#> 1         1        1   1       3          2          2    3
#> 2         1        2   2       2          2          1    3
#> 3         1        3   1       3          2          1    3
#> 4         1        4   1       2          1          3    1
#> 5         1        5   1       2          1          3    1
#> 6         1        6   2       1          1          2    1
#> 7         1        7   2       3          1          2    3
#> 8         1        8   2       3          1          2    3
#> 9         1        9   2       3          1          1    2
#> 10        1       10   2       3          1          1    2

nrow(test)
#> [1] 19

MC <- dfidx::dfidx(test, alt.levels = c("payment", "management", "assessment", "crop"), 
                  idx = list(c("choiceid", "personid"), "alt"), drop.index = FALSE, package = "mlogit")

print(MC)
#> ~~~~~~~
#>  first 10 observations out of 19 
#> ~~~~~~~
#>    personid choiceid alt payment management assessment crop  idx
#> 1         1        1   1       3          2          2    3  1:1
#> 2         1        2   2       2          2          1    3  2:2
#> 3         1        3   1       3          2          1    3  3:1
#> 4         1        4   1       2          1          3    1  4:1
#> 5         1        5   1       2          1          3    1  5:1
#> 6         1        6   2       1          1          2    1  6:2
#> 7         1        7   2       3          1          2    3  7:2
#> 8         1        8   2       3          1          2    3  8:2
#> 9         1        9   2       3          1          1    2  9:2
#> 10        1       10   2       3          1          1    2 10:2
#> 
#> ~~~ indexes ~~~~
#>    choiceid personid alt
#> 1         1        1   1
#> 2         2        1   2
#> 3         3        1   1
#> 4         4        1   1
#> 5         5        1   1
#> 6         6        1   2
#> 7         7        1   2
#> 8         8        1   2
#> 9         9        1   2
#> 10       10        1   2
#> indexes:  1, 1, 2

final4 <- mlogit.data(MC, choice = "choiceid", shape = "wide", drop.index = TRUE)
head(final4)
#> ~~~~~~~
#>  first 10 observations out of 361 
#> ~~~~~~~
#>    personid choiceid payment management assessment crop idx.choiceid
#> 1         1     TRUE       3          2          2    3            1
#> 2         1    FALSE       3          2          2    3            1
#> 3         1    FALSE       3          2          2    3            1
#> 4         1    FALSE       3          2          2    3            1
#> 5         1    FALSE       3          2          2    3            1
#> 6         1    FALSE       3          2          2    3            1
#> 7         1    FALSE       3          2          2    3            1
#> 8         1    FALSE       3          2          2    3            1
#> 9         1    FALSE       3          2          2    3            1
#> 10        1    FALSE       3          2          2    3            1
#>    idx.personid idx.alt idx.choiceid.1 idx.personid.1 idx.alt.1 idx.choiceid.2
#> 1             1       1              1              1         1              1
#> 2             1       1              1              1         1              1
#> 3             1       1              1              1         1              1
#> 4             1       1              1              1         1              1
#> 5             1       1              1              1         1              1
#> 6             1       1              1              1         1              1
#> 7             1       1              1              1         1              1
#> 8             1       1              1              1         1              1
#> 9             1       1              1              1         1              1
#> 10            1       1              1              1         1              1
#>    idx.personid.2 idx.alt.2 idx.choiceid.3 idx.personid.3 idx.alt.3
#> 1               1         1              1              1         1
#> 2               1         1              1              1         1
#> 3               1         1              1              1         1
#> 4               1         1              1              1         1
#> 5               1         1              1              1         1
#> 6               1         1              1              1         1
#> 7               1         1              1              1         1
#> 8               1         1              1              1         1
#> 9               1         1              1              1         1
#> 10              1         1              1              1         1
#>    idx.choiceid.4 idx.personid.4 idx.alt.4 idx.choiceid.5 idx.personid.5
#> 1               1              1         1              1              1
#> 2               1              1         1              1              1
#> 3               1              1         1              1              1
#> 4               1              1         1              1              1
#> 5               1              1         1              1              1
#> 6               1              1         1              1              1
#> 7               1              1         1              1              1
#> 8               1              1         1              1              1
#> 9               1              1         1              1              1
#> 10              1              1         1              1              1

#> 
#> ~~~ indexes ~~~~
#>    chid alt
#> 1     1   1
#> 2     1  10
#> 3     1  11
#> 4     1  12
#> 5     1  13
#> 6     1  14
#> 7     1  15
#> 8     1  16
#> 9     1  17
#> 10    1  18
#> indexes:  1, 2

我不知道为什么这里有这么多变量的倍数idx.altidx.personididx.choiceid。这是在该dfidx行中指定的,但没有显示在那里。

View(final4)

lc <- gmnl(choiceid ~ payment | management + assessment + crop, 
           data = final4,
           model = 'lc', 
           Q = 4, 
           panel = TRUE,
           method = "bhhh")

#> Error in gmnl(choiceid ~ payment | management + assessment + crop, data = final4, : No individual index

非常感谢您查看此内容

4

0 回答 0