1

I want to run the mlogit function on my discrete choice dataset. Below I have provided the data and the lines of R code utilizing the package mlogit that are producing the errors. I am getting an error when running the full dataset about the dimensions for mlogit.data. When running mlogit, I am getting an error around coercing the "choice" variable to a logical.

Here is a snapshot of the code and the structure to reproduce a subset of it in R.

How the data Looks in R

structure(list(row_id = c("1a", "1b", "1c", "1d", "1e", "1g"), choice = structure(c(2L, 1L, 2L, 1L, 2L, 1L), .Label = c("1", "2"), class = "factor"), alt_var = structure(c(1L, 2L, 1L, 2L, 1L, 2L), .Label = c("1", "2"), class = "factor"), meal_choice = structure(c(1L, 2L, 1L, 1L, 1L, 2L), .Label = c("1", "2"), class = "factor"), transport_choice = structure(c(1L, 2L, 1L, 2L, 2L, 2L), .Label = c("1", "2"), class = "factor"), packaging_source = structure(c(1L, 2L, 1L, 1L, 2L, 2L), .Label = c("1", "2"), class = "factor"), disposal_choice = structure(c(1L, 2L, 1L, 1L, 2L, 2L), .Label = c("1", "2"), class = "factor")), row.names = c(NA, -6L), class = c("data.table", "data.frame"), .internal.selfref = <pointer: 0x7fa6d980f8e0>)

mlogit.data(data = df, choice = "choice", shape = "long", alt.var = "alt_var", id.var = "row_id", drop.index = TRUE)
Error in `$<-.data.frame`(x, name, value) : replacement has 964 rows, data has 965

mlogit(choice ~ meal_choice + transport_choice + packaging_source + disposal_choice, df, reflevel = "car")
`Error in dfidx::dfidx(data = data, dfa$idx, drop.index = dfa$drop.index,  : 
  impossible to coerce the choice variable to a logical`
Fahim Uz Zaman
  • 444
  • 3
  • 6
chaimocha
  • 27
  • 1
  • 6
  • 1
    In the long format, your choice variable `choice` should be equal to 1 or `TRUE` for the chosen alternative and 0 or `FALSE` for the non-chosen alternatives. In your data, it is either 1 or 2. The error means that the `dfidx` fails to guess what indicates the chosen alternative. TLDR; you need to recode your choice variable. – edsandorf Nov 10 '20 at 12:15

0 Answers0