mlogit.data() Error: Assigned data `ids` must be compatible with existing data
Asked Answered
K

1

6

I have been working hours on that and I simply cannot find any solution to the problem. Hopefully someone here can help.

I'm trying to create a personal choice matrix for some data with the following structure:

# A tibble: 2,152 x 32
     age choice canton  lr_s dist_svp dist_fdp dist_bdp dist_cvp dist_glp dist_sp
   <dbl> <fct>  <fct>  <dbl>    <dbl>    <dbl>    <dbl>    <dbl>    <dbl>   <dbl>
 1    39 sp     GE         3       49       25       25        4       16       1
 2    67 sp     ZH         0      100       49       64        4       25       0
 3    42 svp    ZH         7        4        4        1       36        4      36

   dist_gps pid_svp pid_fdp pid_bdp pid_cvp pid_glp pid_sp pid_gps french italian
      <dbl>   <dbl>   <dbl>   <dbl>   <dbl>   <dbl>  <dbl>   <dbl>  <dbl>   <dbl>
 1        0       0       0       0       0       0      0       1      1       0
 2        9       0       0       0       0       0      1       0      0       0
 3       36       0       0       0       0       0      1       0      0       0

Now, I need to create a personal choice matrix with the 7 alternatives that are indicated by dist_* / pid_* in the columns.

This should, according to my understanding, work with the following code:

work.pc <- mlogit.data(work,
                       varying = c(5:11, 12:18),
                       choice = "choice",
                       shape = "wide",
                       sep = "_")

However, when I run this code, I get the following Error message and a few Warning messages:

Error: Assigned data `ids` must be compatible with existing data.
x Existing data has 15064 rows.
x Assigned data has 2152 rows.
ℹ Only vectors of size 1 are recycled.
Run `rlang::last_error()` to see where the error occurred.
In addition: Warning messages:
1: Setting row names on a tibble is deprecated. 
2: Setting row names on a tibble is deprecated. 
3: Setting row names on a tibble is deprecated. 
4: Setting row names on a tibble is deprecated. 
5: Setting row names on a tibble is deprecated. 
6: Setting row names on a tibble is deprecated. 
7: Setting row names on a tibble is deprecated. 

What's the issue here? I'm grateful for any help! I've tried everything.

Kilburn answered 1/5, 2020 at 8:50 Comment(2)
sorry, I am not too familiar with tibbles.. how can I copy this data into r? Wouldnt dput be a lot easier?Riccardo
E.g. dput(work[1:20,])Riccardo
K
8

Problem solved: the tibble "work" has to be converted into dataframe.

After using

work <- as.data.frame(work)

the code functions properly i.e. the Error message is eliminated.

Kilburn answered 1/5, 2020 at 13:18 Comment(2)
A follow up to this solution: this seems to work on sf objects (which produce the same problem) but this is not entirely desirable as it removes the geographic component to the sf. Furthermore, sf objects are supposed to abide by the tidyverse. Does anyone have a workaround to produce the same solution for an sf object but to keep the geometry (i.e. not necessitating conversion to a dataframe)?Jugurtha
@Jugurtha - in general, you can use sf_use_s2(FALSE) for issues like this when manipulating sf objects. So run sf_use_s2(FALSE) prior to whatever code throws the error, and sf_use_s2(TRUE) to turn it back on.Integrated

© 2022 - 2024 — McMap. All rights reserved.