1

I have been able to make a mlogit.data object to input into the gmnl package for latent class analysis but I keep running into the problem where it says the choiced variable has no index. I saw there was another question about the same problem but there was no answer to that question. I have included a sample of the test choices data. I appreciate any help into the matter.

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

I am not sure why here there are so many multiples of the variables idx.alt, idx.personid and idx.choiceid. This was specified in the dfidx line but this does not show up there.

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

Thank you very much for looking over this

dan1st
  • 12,568
  • 8
  • 34
  • 67
Miha04
  • 23
  • 5

0 Answers0