Heteroskedastic logit Model in R

Hi everyone,

I am a beginner of R and just joined this group. I am trying to run Heteroskedastic logit in R. I followed the instruction on this link: https://cran.r-project.org/web/packages/mlogit/vignettes/c4.relaxiid.html
Here is a picture of my data


I run the code
library(mlogit)
hl.fdi <- mlogit(province_chosen ~ Z1 + Z2 + Z3 + Z4 + Z5 + Z6|X1 + X2 + X3 + X4,sample1,reflevel = 'Hanoi_city', heterosc = TRUE)
coef(summary(hl.fdi) (as I want to know the scale parameter; sample1 is the name of my dataset which I imported from csv)

However, I got the error
Error in dfidx::dfidx(data = data, dfa$idx, drop.index = dfa$drop.index, :
the two indexes don't define unique observations

Do I need to indicate reference level here? Can you help me with the codes? Is there a way for us to extract part of my data sample and upload here? I am very new to R. I really appreciate any help. Thanks so much!

Can't help with specifics without a reprex. See the FAQ.

Run the example given in the vignette you linked. Before running mlogit(), data must first be transformed with dfidx().

From help(mlogit)

The data argument may be an ordinary data.frame . In this case, some supplementary arguments should be provided and are passed to mlogit.data() . Note that it is not necessary to indicate the choice argument as it is deduced from the formula.

mlogit.data() is not deprecated and dfidx:: dfidx() is now required to prepare the required idnames arguments. For a data frame object, those must be prepared manually.

Thank so much @technocrat for your reply ! I really want to show you my dataset but I am struggling with datapasta and reprex although I have read and followed the instruction.
I followed scenario 1 in this website: Using datapasta with reprex • reprex (tidyverse.org)
but do not know how to paste my data from clip board to this group (as the video does not show).

Hi! Here is my data sample. Really look forward to your help

sample3 <- tibble::tribble(
                                  ~id, ~province_chosen,           ~province_name,       ~X1,       ~X2,       ~X3,       ~X4,       ~Z1,       ~Z2,        ~Z3,       ~Z4,       ~Z5,       ~Z6,
                           108582716L,               0L,      "Ninhbinh_province", 8.2224178, 8.1201506, 7.7692056, 7.8650966, 4.8283138, 6.5539336,  5.0262208, 4.1679049, 8.5716619, 4.3820267,
                           108582716L,               0L,       "Travinh_province", 8.2224178, 8.1201506, 7.7692056, 7.8650966,   4.89784, 6.0980744,  3.1784184, 4.1463041, 8.5822763, 5.7037826,
                           108582716L,               0L,      "Vinhlong_province", 8.2224178, 8.1201506, 7.7692056, 7.8650966, 4.9126549, 6.5352411,  3.5308795, 4.2668962, 8.5671768,  5.480639,
                           108582716L,               0L,          "Haiphong_city", 8.2224178, 8.1201506, 7.7692056, 7.8650966, 1.4350846,  7.161622,  5.5514636,  4.230186, 8.9138956,  5.857933,
                           108582716L,               0L,        "Phutho_province", 8.2224178, 8.1201506, 7.7692056, 7.8650966, 5.3082676, 5.9839363,  4.7599664, 4.1826606, 8.7382259, 5.2470241,
                           108582716L,               1L,             "Hanoi_city", 8.2224178, 8.1201506, 7.7692056, 7.8650966, 4.8202815, 7.7137847,   4.702239, 4.2312036, 9.1257057, 5.2983174,
                           108582716L,               0L,  "Thuathienhue_province", 8.2224178, 8.1201506, 7.7692056, 7.8650966, 4.6249728,   5.46806,  4.2329168, 4.1972017, 8.5623245, 6.0867748,
                           108582716L,               0L,        "Longan_province", 8.2224178, 8.1201506, 7.7692056, 7.8650966, 4.0055132,  5.811141,  5.3210664, 4.2314944, 8.8020086, 5.4380794,
                           108582716L,               0L,        "Nghean_province", 8.2224178, 8.1201506, 7.7692056, 7.8650966, 5.9506426, 5.2574954,  3.3081558, 4.1993051, 8.5572729, 4.8675346,
                           108582716L,               0L,         "Camau_province", 8.2224178, 8.1201506, 7.7692056, 7.8650966, 5.7462034, 5.4638319,  4.0027852, 4.1604443,   8.75842,  5.521461,
                           108582716L,               0L,     "Quangninh_province", 8.2224178, 8.1201506, 7.7692056, 7.8650966, 3.8501475,   5.32301,  4.2263217, 4.2959237, 9.0632677,  5.768321,
                           108582716L,               0L,       "Dongnai_province", 8.2224178, 8.1201506, 7.7692056, 7.8650966, 3.4594662, 6.2653012,  5.5089636,  4.186924, 9.0895538, 5.5606818,
                           108582716L,               0L,     "Binhthuan_province", 8.2224178, 8.1201506, 7.7692056, 7.8650966, 5.1704841, 5.0498562,  3.8159115, 4.1794515, 8.5931168, 5.6698809,
                           108582716L,               0L,       "Tayninh_province", 8.2224178, 8.1201506, 7.7692056, 7.8650966, 4.7361984, 5.6347895,  5.4272904, 4.2054386, 8.8453484, 5.0106354,
                           108582716L,               0L,       "Namdinh_province", 8.2224178, 8.1201506, 7.7692056, 7.8650966, 4.4875121, 7.0130157,  4.6440415, 4.1757708, 8.5558758, 5.0106354,
                           108582716L,               0L,     "Quangngai_province", 8.2224178, 8.1201506, 7.7692056, 7.8650966, 4.9972124, 5.5093884,  3.8801143, 4.1640263, 8.9127951, 5.2983174,
                           108582716L,               0L,    "Thainguyen_province", 8.2224178, 8.1201506, 7.7692056, 7.8650966, 5.1817837, 5.8861041,  6.7543707, 4.2152338, 9.0023937, 4.7874918,
                           108582716L,               0L,     "Binhduong_province", 8.2224178, 8.1201506, 7.7692056, 7.8650966, 3.5973122, 6.6883545,  5.8385348, 4.2103481, 8.9690628, 5.3936276,
                           108582716L,               0L,      "Thanhhoa_province", 8.2224178, 8.1201506, 7.7692056, 7.8650966, 5.2522736,  5.768321,  4.5135665,  4.184185, 8.4405375, 5.1357985,
                           108582716L,               0L,       "Hungyen_province", 8.2224178, 8.1201506, 7.7692056, 7.8650966, 4.5315237, 7.1530519,  5.4823933, 4.1526136, 8.9107132, 4.7004805,
                           108582716L,               0L,         "Hanam_province", 8.2224178, 8.1201506, 7.7692056, 7.8650966, 4.8520303,   6.84375,    5.30164, 4.1754637, 8.7301798, 5.2470241,
                           108582716L,               0L,      "Haiduong_province", 8.2224178, 8.1201506, 7.7692056, 7.8650966, 3.8110971, 6.9874902,  5.6670299, 4.1565366, 8.8397036, 5.5984221,
                           108582716L,               0L,        "Hatinh_province", 8.2224178, 8.1201506, 7.7692056, 7.8650966, 6.0776424, 5.3612924,   4.708425, 4.1814394, 8.6849604, 6.1944056,
                           108582716L,               0L,    "Tuyenquang_province", 8.2224178, 8.1201506, 7.7692056, 7.8650966,  5.556828, 4.8903489,  2.4108267, 4.1763854, 8.6335611, 4.8675346,
                           108582716L,               0L,            "Danang_city", 8.2224178, 8.1201506, 7.7692056, 7.8650966,  2.587764, 6.7345915,  4.3072119, 4.2506356, 8.8469181, 6.0637851,
                           108582716L,               0L,      "Haugiang_province", 8.2224178, 8.1201506, 7.7692056, 7.8650966, 5.3706379, 6.1717005,  4.5786986,  4.161068, 8.5612812, 5.5984221,
                           108582716L,               0L, "Baria-vungtau_province", 8.2224178, 8.1201506, 7.7692056, 7.8650966, 4.2297492,  6.331502,  4.4369946, 4.2040954, 9.0418806, 5.4380794,
                           108582716L,               0L,      "Thaibinh_province", 8.2224178, 8.1201506, 7.7692056, 7.8650966, 4.2456341,  7.029973,  4.5747805, 4.1802163, 8.5892353, 4.9416423,
                           108582716L,               0L,      "Vinhphuc_province", 8.2224178, 8.1201506, 7.7692056, 7.8650966, 5.1239638, 6.7844572,  5.3757849, 4.2009544, 8.9477854, 5.1357985,
                           108582716L,               0L,       "Bacninh_province", 8.2224178, 8.1201506, 7.7692056, 7.8650966, 4.5549288, 7.3238306,  6.6909885, 4.2597179, 9.0587311, 5.3471074,
                           108582716L,               0L,        "Backan_province", 8.2224178, 8.1201506, 7.7692056, 7.8650966, 4.7361984, 4.2046928, -1.7439516,  4.139955, 8.6858816, 4.3820267,
                           108582716L,               0L,      "Quangnam_province", 8.2224178, 8.1201506, 7.7692056, 7.8650966,  4.383276, 4.9558272,  4.1683974, 4.2401752, 8.6177435, 5.7365723,
                           108582716L,               0L,         "Hochiminh_city", 8.2224178, 8.1201506, 7.7692056, 7.8650966, 2.6390574, 8.3359108,  5.0551434,  4.207078, 9.1471338, 5.6698809,
                           108582716L,               0L,     "Binhphuoc_province", 8.2224178, 8.1201506, 7.7692056, 7.8650966, 4.6634393, 4.9558272,  4.9517002, 4.1305156, 8.7598467, 5.7037826,
                           108582716L,               0L,      "Bacgiang_province", 8.2224178, 8.1201506, 7.7692056, 7.8650966, 4.5890408, 6.0730443,  5.6537271, 4.1662002, 8.8224411, 4.7004805,
                           108594870L,               0L,        "Longan_province", 8.2224178, 8.1201506, 7.7692056, 7.8650966, 4.0055132,  5.811141,  5.3210664, 4.2314944, 8.8020086, 5.4380794,
                           108594870L,               0L,      "Bacgiang_province", 8.2224178, 8.1201506, 7.7692056, 7.8650966, 4.5890408, 6.0730443,  5.6537271, 4.1662002, 8.8224411, 4.7004805,
                           108594870L,               0L,       "Dongnai_province", 8.2224178, 8.1201506, 7.7692056, 7.8650966, 3.4594662, 6.2653012,  5.5089636,  4.186924, 9.0895538, 5.5606818,
                           108594870L,               0L,  "Thuathienhue_province", 8.2224178, 8.1201506, 7.7692056, 7.8650966, 4.6249728,   5.46806,  4.2329168, 4.1972017, 8.5623245, 6.0867748,
                           108594870L,               0L,      "Haugiang_province", 8.2224178, 8.1201506, 7.7692056, 7.8650966, 5.3706379, 6.1717005,  4.5786986,  4.161068, 8.5612812, 5.5984221,
                           108594870L,               0L,       "Hungyen_province", 8.2224178, 8.1201506, 7.7692056, 7.8650966, 4.5315237, 7.1530519,  5.4823933, 4.1526136, 8.9107132, 4.7004805,
                           108594870L,               0L, "Baria-vungtau_province", 8.2224178, 8.1201506, 7.7692056, 7.8650966, 4.2297492,  6.331502,  4.4369946, 4.2040954, 9.0418806, 5.4380794,
                           108594870L,               0L,     "Quangninh_province", 8.2224178, 8.1201506, 7.7692056, 7.8650966, 3.8501475,   5.32301,  4.2263217, 4.2959237, 9.0632677,  5.768321,
                           108594870L,               0L,            "Danang_city", 8.2224178, 8.1201506, 7.7692056, 7.8650966,  2.587764, 6.7345915,  4.3072119, 4.2506356, 8.8469181, 6.0637851,
                           108594870L,               0L,        "Hatinh_province", 8.2224178, 8.1201506, 7.7692056, 7.8650966, 6.0776424, 5.3612924,   4.708425, 4.1814394, 8.6849604, 6.1944056,
                           108594870L,               0L,      "Thanhhoa_province", 8.2224178, 8.1201506, 7.7692056, 7.8650966, 5.2522736,  5.768321,  4.5135665,  4.184185, 8.4405375, 5.1357985,
                           108594870L,               0L,     "Binhduong_province", 8.2224178, 8.1201506, 7.7692056, 7.8650966, 3.5973122, 6.6883545,  5.8385348, 4.2103481, 8.9690628, 5.3936276,
                           108594870L,               0L,       "Bacninh_province", 8.2224178, 8.1201506, 7.7692056, 7.8650966, 4.5549288, 7.3238306,  6.6909885, 4.2597179, 9.0587311, 5.3471074,
                           108594870L,               0L,         "Hochiminh_city", 8.2224178, 8.1201506, 7.7692056, 7.8650966, 2.6390574, 8.3359108,  5.0551434,  4.207078, 9.1471338, 5.6698809,
                           108594870L,               0L,          "Haiphong_city", 8.2224178, 8.1201506, 7.7692056, 7.8650966, 1.4350846,  7.161622,  5.5514636,  4.230186, 8.9138956,  5.857933,
                           108594870L,               0L,      "Quangnam_province", 8.2224178, 8.1201506, 7.7692056, 7.8650966,  4.383276, 4.9558272,  4.1683974, 4.2401752, 8.6177435, 5.7365723,
                           108594870L,               0L,        "Backan_province", 8.2224178, 8.1201506, 7.7692056, 7.8650966, 4.7361984, 4.2046928, -1.7439516,  4.139955, 8.6858816, 4.3820267,
                           108594870L,               0L,    "Thainguyen_province", 8.2224178, 8.1201506, 7.7692056, 7.8650966, 5.1817837, 5.8861041,  6.7543707, 4.2152338, 9.0023937, 4.7874918,
                           108594870L,               0L,         "Hanam_province", 8.2224178, 8.1201506, 7.7692056, 7.8650966, 4.8520303,   6.84375,    5.30164, 4.1754637, 8.7301798, 5.2470241,
                           108594870L,               0L,        "Phutho_province", 8.2224178, 8.1201506, 7.7692056, 7.8650966, 5.3082676, 5.9839363,  4.7599664, 4.1826606, 8.7382259, 5.2470241,
                           108594870L,               0L,      "Vinhphuc_province", 8.2224178, 8.1201506, 7.7692056, 7.8650966, 5.1239638, 6.7844572,  5.3757849, 4.2009544, 8.9477854, 5.1357985,
                           108594870L,               0L,      "Vinhlong_province", 8.2224178, 8.1201506, 7.7692056, 7.8650966, 4.9126549, 6.5352411,  3.5308795, 4.2668962, 8.5671768,  5.480639,
                           108594870L,               1L,             "Hanoi_city", 8.2224178, 8.1201506, 7.7692056, 7.8650966, 4.8202815, 7.7137847,   4.702239, 4.2312036, 9.1257057, 5.2983174,
                           108594870L,               0L,       "Travinh_province", 8.2224178, 8.1201506, 7.7692056, 7.8650966,   4.89784, 6.0980744,  3.1784184, 4.1463041, 8.5822763, 5.7037826,
                           108594870L,               0L,      "Ninhbinh_province", 8.2224178, 8.1201506, 7.7692056, 7.8650966, 4.8283138, 6.5539336,  5.0262208, 4.1679049, 8.5716619, 4.3820267,
                           108594870L,               0L,     "Quangngai_province", 8.2224178, 8.1201506, 7.7692056, 7.8650966, 4.9972124, 5.5093884,  3.8801143, 4.1640263, 8.9127951, 5.2983174,
                           108594870L,               0L,       "Tayninh_province", 8.2224178, 8.1201506, 7.7692056, 7.8650966, 4.7361984, 5.6347895,  5.4272904, 4.2054386, 8.8453484, 5.0106354,
                           108594870L,               0L,      "Thaibinh_province", 8.2224178, 8.1201506, 7.7692056, 7.8650966, 4.2456341,  7.029973,  4.5747805, 4.1802163, 8.5892353, 4.9416423,
                           108594870L,               0L,        "Nghean_province", 8.2224178, 8.1201506, 7.7692056, 7.8650966, 5.9506426, 5.2574954,  3.3081558, 4.1993051, 8.5572729, 4.8675346,
                           108594870L,               0L,      "Haiduong_province", 8.2224178, 8.1201506, 7.7692056, 7.8650966, 3.8110971, 6.9874902,  5.6670299, 4.1565366, 8.8397036, 5.5984221,
                           108594870L,               0L,       "Namdinh_province", 8.2224178, 8.1201506, 7.7692056, 7.8650966, 4.4875121, 7.0130157,  4.6440415, 4.1757708, 8.5558758, 5.0106354,
                           108594870L,               0L,     "Binhthuan_province", 8.2224178, 8.1201506, 7.7692056, 7.8650966, 5.1704841, 5.0498562,  3.8159115, 4.1794515, 8.5931168, 5.6698809,
                           108594870L,               0L,         "Camau_province", 8.2224178, 8.1201506, 7.7692056, 7.8650966, 5.7462034, 5.4638319,  4.0027852, 4.1604443,   8.75842,  5.521461,
                           108594870L,               0L,    "Tuyenquang_province", 8.2224178, 8.1201506, 7.7692056, 7.8650966,  5.556828, 4.8903489,  2.4108267, 4.1763854, 8.6335611, 4.8675346,
                           108594870L,               0L,     "Binhphuoc_province", 8.2224178, 8.1201506, 7.7692056, 7.8650966, 4.6634393, 4.9558272,  4.9517002, 4.1305156, 8.7598467, 5.7037826,
                           108595095L,               0L,      "Haugiang_province", 8.0093489, 8.2819128,   7.93539, 8.2279396, 5.3706379, 6.1717005,  4.5786986,  4.161068, 8.5612812, 5.5984221,
                           108595095L,               0L,         "Hochiminh_city", 8.0093489, 8.2819128,   7.93539, 8.2279396, 2.6390574, 8.3359108,  5.0551434,  4.207078, 9.1471338, 5.6698809,
                           108595095L,               0L,      "Vinhlong_province", 8.0093489, 8.2819128,   7.93539, 8.2279396, 4.9126549, 6.5352411,  3.5308795, 4.2668962, 8.5671768,  5.480639,
                           108595095L,               0L,            "Danang_city", 8.0093489, 8.2819128,   7.93539, 8.2279396,  2.587764, 6.7345915,  4.3072119, 4.2506356, 8.8469181, 6.0637851,
                           108595095L,               0L,       "Bacninh_province", 8.0093489, 8.2819128,   7.93539, 8.2279396, 4.5549288, 7.3238306,  6.6909885, 4.2597179, 9.0587311, 5.3471074,
                           108595095L,               0L,  "Thuathienhue_province", 8.0093489, 8.2819128,   7.93539, 8.2279396, 4.6249728,   5.46806,  4.2329168, 4.1972017, 8.5623245, 6.0867748,
                           108595095L,               0L,      "Thanhhoa_province", 8.0093489, 8.2819128,   7.93539, 8.2279396, 5.2522736,  5.768321,  4.5135665,  4.184185, 8.4405375, 5.1357985,
                           108595095L,               0L,     "Binhduong_province", 8.0093489, 8.2819128,   7.93539, 8.2279396, 3.5973122, 6.6883545,  5.8385348, 4.2103481, 8.9690628, 5.3936276,
                           108595095L,               0L,      "Vinhphuc_province", 8.0093489, 8.2819128,   7.93539, 8.2279396, 5.1239638, 6.7844572,  5.3757849, 4.2009544, 8.9477854, 5.1357985,
                           108595095L,               0L,     "Binhphuoc_province", 8.0093489, 8.2819128,   7.93539, 8.2279396, 4.6634393, 4.9558272,  4.9517002, 4.1305156, 8.7598467, 5.7037826,
                           108595095L,               0L,       "Hungyen_province", 8.0093489, 8.2819128,   7.93539, 8.2279396, 4.5315237, 7.1530519,  5.4823933, 4.1526136, 8.9107132, 4.7004805,
                           108595095L,               0L,        "Phutho_province", 8.0093489, 8.2819128,   7.93539, 8.2279396, 5.3082676, 5.9839363,  4.7599664, 4.1826606, 8.7382259, 5.2470241,
                           108595095L,               0L,         "Camau_province", 8.0093489, 8.2819128,   7.93539, 8.2279396, 5.7462034, 5.4638319,  4.0027852, 4.1604443,   8.75842,  5.521461,
                           108595095L,               0L,     "Quangngai_province", 8.0093489, 8.2819128,   7.93539, 8.2279396, 4.9972124, 5.5093884,  3.8801143, 4.1640263, 8.9127951, 5.2983174,
                           108595095L,               0L,        "Hatinh_province", 8.0093489, 8.2819128,   7.93539, 8.2279396, 6.0776424, 5.3612924,   4.708425, 4.1814394, 8.6849604, 6.1944056,
                           108595095L,               0L,      "Thaibinh_province", 8.0093489, 8.2819128,   7.93539, 8.2279396, 4.2456341,  7.029973,  4.5747805, 4.1802163, 8.5892353, 4.9416423,
                           108595095L,               0L,        "Nghean_province", 8.0093489, 8.2819128,   7.93539, 8.2279396, 5.9506426, 5.2574954,  3.3081558, 4.1993051, 8.5572729, 4.8675346,
                           108595095L,               0L,         "Hanam_province", 8.0093489, 8.2819128,   7.93539, 8.2279396, 4.8520303,   6.84375,    5.30164, 4.1754637, 8.7301798, 5.2470241,
                           108595095L,               0L,     "Quangninh_province", 8.0093489, 8.2819128,   7.93539, 8.2279396, 3.8501475,   5.32301,  4.2263217, 4.2959237, 9.0632677,  5.768321,
                           108595095L,               0L,        "Backan_province", 8.0093489, 8.2819128,   7.93539, 8.2279396, 4.7361984, 4.2046928, -1.7439516,  4.139955, 8.6858816, 4.3820267,
                           108595095L,               0L,    "Thainguyen_province", 8.0093489, 8.2819128,   7.93539, 8.2279396, 5.1817837, 5.8861041,  6.7543707, 4.2152338, 9.0023937, 4.7874918,
                           108595095L,               0L,       "Dongnai_province", 8.0093489, 8.2819128,   7.93539, 8.2279396, 3.4594662, 6.2653012,  5.5089636,  4.186924, 9.0895538, 5.5606818,
                           108595095L,               0L,      "Haiduong_province", 8.0093489, 8.2819128,   7.93539, 8.2279396, 3.8110971, 6.9874902,  5.6670299, 4.1565366, 8.8397036, 5.5984221,
                           108595095L,               0L,      "Ninhbinh_province", 8.0093489, 8.2819128,   7.93539, 8.2279396, 4.8283138, 6.5539336,  5.0262208, 4.1679049, 8.5716619, 4.3820267,
                           108595095L,               0L,      "Bacgiang_province", 8.0093489, 8.2819128,   7.93539, 8.2279396, 4.5890408, 6.0730443,  5.6537271, 4.1662002, 8.8224411, 4.7004805,
                           108595095L,               0L,       "Tayninh_province", 8.0093489, 8.2819128,   7.93539, 8.2279396, 4.7361984, 5.6347895,  5.4272904, 4.2054386, 8.8453484, 5.0106354,
                           108595095L,               0L,          "Haiphong_city", 8.0093489, 8.2819128,   7.93539, 8.2279396, 1.4350846,  7.161622,  5.5514636,  4.230186, 8.9138956,  5.857933,
                           108595095L,               0L,       "Travinh_province", 8.0093489, 8.2819128,   7.93539, 8.2279396,   4.89784, 6.0980744,  3.1784184, 4.1463041, 8.5822763, 5.7037826,
                           108595095L,               0L,      "Quangnam_province", 8.0093489, 8.2819128,   7.93539, 8.2279396,  4.383276, 4.9558272,  4.1683974, 4.2401752, 8.6177435, 5.7365723,
                           108595095L,               0L,     "Binhthuan_province", 8.0093489, 8.2819128,   7.93539, 8.2279396, 5.1704841, 5.0498562,  3.8159115, 4.1794515, 8.5931168, 5.6698809,
                           108595095L,               0L, "Baria-vungtau_province", 8.0093489, 8.2819128,   7.93539, 8.2279396, 4.2297492,  6.331502,  4.4369946, 4.2040954, 9.0418806, 5.4380794,
                           108595095L,               1L,             "Hanoi_city", 8.0093489, 8.2819128,   7.93539, 8.2279396, 4.8202815, 7.7137847,   4.702239, 4.2312036, 9.1257057, 5.2983174,
                           108595095L,               0L,    "Tuyenquang_province", 8.0093489, 8.2819128,   7.93539, 8.2279396,  5.556828, 4.8903489,  2.4108267, 4.1763854, 8.6335611, 4.8675346,
                           108595095L,               0L,       "Namdinh_province", 8.0093489, 8.2819128,   7.93539, 8.2279396, 4.4875121, 7.0130157,  4.6440415, 4.1757708, 8.5558758, 5.0106354,
                           108595095L,               0L,        "Longan_province", 8.0093489, 8.2819128,   7.93539, 8.2279396, 4.0055132,  5.811141,  5.3210664, 4.2314944, 8.8020086, 5.4380794,
                           108605233L,               0L,     "Binhduong_province", 8.2393103,   9.70644, 11.101022, 9.4778214, 3.5973122, 6.6883545,  5.8385348, 4.2103481, 8.9690628, 5.3936276,
                           108605233L,               0L,         "Camau_province", 8.2393103,   9.70644, 11.101022, 9.4778214, 5.7462034, 5.4638319,  4.0027852, 4.1604443,   8.75842,  5.521461,
                           108605233L,               0L,     "Quangninh_province", 8.2393103,   9.70644, 11.101022, 9.4778214, 3.8501475,   5.32301,  4.2263217, 4.2959237, 9.0632677,  5.768321,
                           108605233L,               0L,      "Bacgiang_province", 8.2393103,   9.70644, 11.101022, 9.4778214, 4.5890408, 6.0730443,  5.6537271, 4.1662002, 8.8224411, 4.7004805,
                           108605233L,               0L,        "Backan_province", 8.2393103,   9.70644, 11.101022, 9.4778214, 4.7361984, 4.2046928, -1.7439516,  4.139955, 8.6858816, 4.3820267,
                           108605233L,               0L,       "Hungyen_province", 8.2393103,   9.70644, 11.101022, 9.4778214, 4.5315237, 7.1530519,  5.4823933, 4.1526136, 8.9107132, 4.7004805,
                           108605233L,               0L,        "Hatinh_province", 8.2393103,   9.70644, 11.101022, 9.4778214, 6.0776424, 5.3612924,   4.708425, 4.1814394, 8.6849604, 6.1944056,
                           108605233L,               0L, "Baria-vungtau_province", 8.2393103,   9.70644, 11.101022, 9.4778214, 4.2297492,  6.331502,  4.4369946, 4.2040954, 9.0418806, 5.4380794,
                           108605233L,               0L,       "Travinh_province", 8.2393103,   9.70644, 11.101022, 9.4778214,   4.89784, 6.0980744,  3.1784184, 4.1463041, 8.5822763, 5.7037826,
                           108605233L,               0L,      "Thanhhoa_province", 8.2393103,   9.70644, 11.101022, 9.4778214, 5.2522736,  5.768321,  4.5135665,  4.184185, 8.4405375, 5.1357985,
                           108605233L,               0L,     "Binhphuoc_province", 8.2393103,   9.70644, 11.101022, 9.4778214, 4.6634393, 4.9558272,  4.9517002, 4.1305156, 8.7598467, 5.7037826,
                           108605233L,               0L,       "Bacninh_province", 8.2393103,   9.70644, 11.101022, 9.4778214, 4.5549288, 7.3238306,  6.6909885, 4.2597179, 9.0587311, 5.3471074,
                           108605233L,               0L,    "Thainguyen_province", 8.2393103,   9.70644, 11.101022, 9.4778214, 5.1817837, 5.8861041,  6.7543707, 4.2152338, 9.0023937, 4.7874918,
                           108605233L,               0L,       "Tayninh_province", 8.2393103,   9.70644, 11.101022, 9.4778214, 4.7361984, 5.6347895,  5.4272904, 4.2054386, 8.8453484, 5.0106354,
                           108605233L,               0L,       "Dongnai_province", 8.2393103,   9.70644, 11.101022, 9.4778214, 3.4594662, 6.2653012,  5.5089636,  4.186924, 9.0895538, 5.5606818,
                           108605233L,               0L,        "Nghean_province", 8.2393103,   9.70644, 11.101022, 9.4778214, 5.9506426, 5.2574954,  3.3081558, 4.1993051, 8.5572729, 4.8675346,
                           108605233L,               0L,      "Vinhlong_province", 8.2393103,   9.70644, 11.101022, 9.4778214, 4.9126549, 6.5352411,  3.5308795, 4.2668962, 8.5671768,  5.480639,
                           108605233L,               0L,      "Ninhbinh_province", 8.2393103,   9.70644, 11.101022, 9.4778214, 4.8283138, 6.5539336,  5.0262208, 4.1679049, 8.5716619, 4.3820267,
                           108605233L,               0L,    "Tuyenquang_province", 8.2393103,   9.70644, 11.101022, 9.4778214,  5.556828, 4.8903489,  2.4108267, 4.1763854, 8.6335611, 4.8675346,
                           108605233L,               0L,            "Danang_city", 8.2393103,   9.70644, 11.101022, 9.4778214,  2.587764, 6.7345915,  4.3072119, 4.2506356, 8.8469181, 6.0637851,
                           108605233L,               0L,      "Vinhphuc_province", 8.2393103,   9.70644, 11.101022, 9.4778214, 5.1239638, 6.7844572,  5.3757849, 4.2009544, 8.9477854, 5.1357985,
                           108605233L,               0L,      "Haiduong_province", 8.2393103,   9.70644, 11.101022, 9.4778214, 3.8110971, 6.9874902,  5.6670299, 4.1565366, 8.8397036, 5.5984221,
                           108605233L,               0L,      "Quangnam_province", 8.2393103,   9.70644, 11.101022, 9.4778214,  4.383276, 4.9558272,  4.1683974, 4.2401752, 8.6177435, 5.7365723,
                           108605233L,               0L,      "Haugiang_province", 8.2393103,   9.70644, 11.101022, 9.4778214, 5.3706379, 6.1717005,  4.5786986,  4.161068, 8.5612812, 5.5984221,
                           108605233L,               0L,     "Quangngai_province", 8.2393103,   9.70644, 11.101022, 9.4778214, 4.9972124, 5.5093884,  3.8801143, 4.1640263, 8.9127951, 5.2983174,
                           108605233L,               0L,  "Thuathienhue_province", 8.2393103,   9.70644, 11.101022, 9.4778214, 4.6249728,   5.46806,  4.2329168, 4.1972017, 8.5623245, 6.0867748,
                           108605233L,               0L,          "Haiphong_city", 8.2393103,   9.70644, 11.101022, 9.4778214, 1.4350846,  7.161622,  5.5514636,  4.230186, 8.9138956,  5.857933,
                           108605233L,               0L,     "Binhthuan_province", 8.2393103,   9.70644, 11.101022, 9.4778214, 5.1704841, 5.0498562,  3.8159115, 4.1794515, 8.5931168, 5.6698809,
                           108605233L,               0L,         "Hanam_province", 8.2393103,   9.70644, 11.101022, 9.4778214, 4.8520303,   6.84375,    5.30164, 4.1754637, 8.7301798, 5.2470241,
                           108605233L,               0L,         "Hochiminh_city", 8.2393103,   9.70644, 11.101022, 9.4778214, 2.6390574, 8.3359108,  5.0551434,  4.207078, 9.1471338, 5.6698809,
                           108605233L,               0L,        "Phutho_province", 8.2393103,   9.70644, 11.101022, 9.4778214, 5.3082676, 5.9839363,  4.7599664, 4.1826606, 8.7382259, 5.2470241,
                           108605233L,               0L,      "Thaibinh_province", 8.2393103,   9.70644, 11.101022, 9.4778214, 4.2456341,  7.029973,  4.5747805, 4.1802163, 8.5892353, 4.9416423,
                           108605233L,               1L,             "Hanoi_city", 8.2393103,   9.70644, 11.101022, 9.4778214, 4.8202815, 7.7137847,   4.702239, 4.2312036, 9.1257057, 5.2983174,
                           108605233L,               0L,        "Longan_province", 8.2393103,   9.70644, 11.101022, 9.4778214, 4.0055132,  5.811141,  5.3210664, 4.2314944, 8.8020086, 5.4380794,
                           108605233L,               0L,       "Namdinh_province", 8.2393103,   9.70644, 11.101022, 9.4778214, 4.4875121, 7.0130157,  4.6440415, 4.1757708, 8.5558758, 5.0106354
                           )
              
                    
                    head(sample3)
#> # A tibble: 6 Ă— 13
#>         id provi…¹ provi…²    X1    X2    X3    X4    Z1    Z2    Z3    Z4    Z5
#>      <int>   <int> <chr>   <dbl> <dbl> <dbl> <dbl> <dbl> <dbl> <dbl> <dbl> <dbl>
#> 1   1.09e8       0 Ninhbi…  8.22  8.12  7.77  7.87  4.83  6.55  5.03  4.17  8.57
#> 2   1.09e8       0 Travin…  8.22  8.12  7.77  7.87  4.90  6.10  3.18  4.15  8.58
#> 3   1.09e8       0 Vinhlo…  8.22  8.12  7.77  7.87  4.91  6.54  3.53  4.27  8.57
#> 4   1.09e8       0 Haipho…  8.22  8.12  7.77  7.87  1.44  7.16  5.55  4.23  8.91
#> 5   1.09e8       0 Phutho…  8.22  8.12  7.77  7.87  5.31  5.98  4.76  4.18  8.74
#> 6   1.09e8       1 Hanoi_…  8.22  8.12  7.77  7.87  4.82  7.71  4.70  4.23  9.13
#> # … with 1 more variable: Z6 <dbl>, and abbreviated variable names
#> #   ¹​province_chosen, ²​province_name

Created on 2023-04-02 with reprex v2.0.2

and here is my code (
X1...X4: individual specific vars
Z1...Z7: alternative specific vars) but the error is still the same: Error in dfidx::dfidx(data = data, dfa$idx, drop.index = dfa$drop.index, :
the two indexes don't define unique observations
I read about "- dfidx" but still confused how to use in this situation.

library(mlogit)
hl.fdi <- mlogit(province_chosen ~ Z1 + Z2 + Z3 + Z4 + Z5 + Z6|X1 + X2 + X3 + X4,sample3,reflevel = 'Hanoi_city', heterosc = TRUE)```

You can also use the dput(YOURDATA) function and then cut and paste the output from the console pane. If there’s a lot, just need to include enough rows so your code works the same. Like this

dput(head(YOURDATA,100)) # for the first hundred rows only.

Hi @technocrat for replying to me. I posted my data in another post but I was notified by the system that it went into spam, so I wrote another one.

sample3 <- tibble::tribble(
                    ~id, ~province_chosen,           ~province_name,       ~X1,       ~X2,       ~X3,       ~X4,       ~Z1,       ~Z2,        ~Z3,       ~Z4,       ~Z5,       ~Z6,
             108582716L,               0L,      "Ninhbinh_province", 8.2224178, 8.1201506, 7.7692056, 7.8650966, 4.8283138, 6.5539336,  5.0262208, 4.1679049, 8.5716619, 4.3820267,
             108582716L,               0L,       "Travinh_province", 8.2224178, 8.1201506, 7.7692056, 7.8650966,   4.89784, 6.0980744,  3.1784184, 4.1463041, 8.5822763, 5.7037826,
             108582716L,               0L,      "Vinhlong_province", 8.2224178, 8.1201506, 7.7692056, 7.8650966, 4.9126549, 6.5352411,  3.5308795, 4.2668962, 8.5671768,  5.480639,
             108582716L,               0L,          "Haiphong_city", 8.2224178, 8.1201506, 7.7692056, 7.8650966, 1.4350846,  7.161622,  5.5514636,  4.230186, 8.9138956,  5.857933,
             108582716L,               0L,        "Phutho_province", 8.2224178, 8.1201506, 7.7692056, 7.8650966, 5.3082676, 5.9839363,  4.7599664, 4.1826606, 8.7382259, 5.2470241,
             108582716L,               1L,             "Hanoi_city", 8.2224178, 8.1201506, 7.7692056, 7.8650966, 4.8202815, 7.7137847,   4.702239, 4.2312036, 9.1257057, 5.2983174,
             108582716L,               0L,  "Thuathienhue_province", 8.2224178, 8.1201506, 7.7692056, 7.8650966, 4.6249728,   5.46806,  4.2329168, 4.1972017, 8.5623245, 6.0867748,
             108582716L,               0L,        "Longan_province", 8.2224178, 8.1201506, 7.7692056, 7.8650966, 4.0055132,  5.811141,  5.3210664, 4.2314944, 8.8020086, 5.4380794,
             108582716L,               0L,        "Nghean_province", 8.2224178, 8.1201506, 7.7692056, 7.8650966, 5.9506426, 5.2574954,  3.3081558, 4.1993051, 8.5572729, 4.8675346,
             108582716L,               0L,         "Camau_province", 8.2224178, 8.1201506, 7.7692056, 7.8650966, 5.7462034, 5.4638319,  4.0027852, 4.1604443,   8.75842,  5.521461,
             108582716L,               0L,     "Quangninh_province", 8.2224178, 8.1201506, 7.7692056, 7.8650966, 3.8501475,   5.32301,  4.2263217, 4.2959237, 9.0632677,  5.768321,
             108582716L,               0L,       "Dongnai_province", 8.2224178, 8.1201506, 7.7692056, 7.8650966, 3.4594662, 6.2653012,  5.5089636,  4.186924, 9.0895538, 5.5606818,
             108582716L,               0L,     "Binhthuan_province", 8.2224178, 8.1201506, 7.7692056, 7.8650966, 5.1704841, 5.0498562,  3.8159115, 4.1794515, 8.5931168, 5.6698809,
             108582716L,               0L,       "Tayninh_province", 8.2224178, 8.1201506, 7.7692056, 7.8650966, 4.7361984, 5.6347895,  5.4272904, 4.2054386, 8.8453484, 5.0106354,
             108582716L,               0L,       "Namdinh_province", 8.2224178, 8.1201506, 7.7692056, 7.8650966, 4.4875121, 7.0130157,  4.6440415, 4.1757708, 8.5558758, 5.0106354,
             108582716L,               0L,     "Quangngai_province", 8.2224178, 8.1201506, 7.7692056, 7.8650966, 4.9972124, 5.5093884,  3.8801143, 4.1640263, 8.9127951, 5.2983174,
             108582716L,               0L,    "Thainguyen_province", 8.2224178, 8.1201506, 7.7692056, 7.8650966, 5.1817837, 5.8861041,  6.7543707, 4.2152338, 9.0023937, 4.7874918,
             108582716L,               0L,     "Binhduong_province", 8.2224178, 8.1201506, 7.7692056, 7.8650966, 3.5973122, 6.6883545,  5.8385348, 4.2103481, 8.9690628, 5.3936276,
             108582716L,               0L,      "Thanhhoa_province", 8.2224178, 8.1201506, 7.7692056, 7.8650966, 5.2522736,  5.768321,  4.5135665,  4.184185, 8.4405375, 5.1357985,
             108582716L,               0L,       "Hungyen_province", 8.2224178, 8.1201506, 7.7692056, 7.8650966, 4.5315237, 7.1530519,  5.4823933, 4.1526136, 8.9107132, 4.7004805,
             108582716L,               0L,         "Hanam_province", 8.2224178, 8.1201506, 7.7692056, 7.8650966, 4.8520303,   6.84375,    5.30164, 4.1754637, 8.7301798, 5.2470241,
             108582716L,               0L,      "Haiduong_province", 8.2224178, 8.1201506, 7.7692056, 7.8650966, 3.8110971, 6.9874902,  5.6670299, 4.1565366, 8.8397036, 5.5984221,
             108582716L,               0L,        "Hatinh_province", 8.2224178, 8.1201506, 7.7692056, 7.8650966, 6.0776424, 5.3612924,   4.708425, 4.1814394, 8.6849604, 6.1944056,
             108582716L,               0L,    "Tuyenquang_province", 8.2224178, 8.1201506, 7.7692056, 7.8650966,  5.556828, 4.8903489,  2.4108267, 4.1763854, 8.6335611, 4.8675346,
             108582716L,               0L,            "Danang_city", 8.2224178, 8.1201506, 7.7692056, 7.8650966,  2.587764, 6.7345915,  4.3072119, 4.2506356, 8.8469181, 6.0637851,
             108582716L,               0L,      "Haugiang_province", 8.2224178, 8.1201506, 7.7692056, 7.8650966, 5.3706379, 6.1717005,  4.5786986,  4.161068, 8.5612812, 5.5984221,
             108582716L,               0L, "Baria-vungtau_province", 8.2224178, 8.1201506, 7.7692056, 7.8650966, 4.2297492,  6.331502,  4.4369946, 4.2040954, 9.0418806, 5.4380794,
             108582716L,               0L,      "Thaibinh_province", 8.2224178, 8.1201506, 7.7692056, 7.8650966, 4.2456341,  7.029973,  4.5747805, 4.1802163, 8.5892353, 4.9416423,
             108582716L,               0L,      "Vinhphuc_province", 8.2224178, 8.1201506, 7.7692056, 7.8650966, 5.1239638, 6.7844572,  5.3757849, 4.2009544, 8.9477854, 5.1357985,
             108582716L,               0L,       "Bacninh_province", 8.2224178, 8.1201506, 7.7692056, 7.8650966, 4.5549288, 7.3238306,  6.6909885, 4.2597179, 9.0587311, 5.3471074,
             108582716L,               0L,        "Backan_province", 8.2224178, 8.1201506, 7.7692056, 7.8650966, 4.7361984, 4.2046928, -1.7439516,  4.139955, 8.6858816, 4.3820267,
             108582716L,               0L,      "Quangnam_province", 8.2224178, 8.1201506, 7.7692056, 7.8650966,  4.383276, 4.9558272,  4.1683974, 4.2401752, 8.6177435, 5.7365723,
             108582716L,               0L,         "Hochiminh_city", 8.2224178, 8.1201506, 7.7692056, 7.8650966, 2.6390574, 8.3359108,  5.0551434,  4.207078, 9.1471338, 5.6698809,
             108582716L,               0L,     "Binhphuoc_province", 8.2224178, 8.1201506, 7.7692056, 7.8650966, 4.6634393, 4.9558272,  4.9517002, 4.1305156, 8.7598467, 5.7037826,
             108582716L,               0L,      "Bacgiang_province", 8.2224178, 8.1201506, 7.7692056, 7.8650966, 4.5890408, 6.0730443,  5.6537271, 4.1662002, 8.8224411, 4.7004805,
             108594870L,               0L,        "Longan_province", 8.2224178, 8.1201506, 7.7692056, 7.8650966, 4.0055132,  5.811141,  5.3210664, 4.2314944, 8.8020086, 5.4380794,
             108594870L,               0L,      "Bacgiang_province", 8.2224178, 8.1201506, 7.7692056, 7.8650966, 4.5890408, 6.0730443,  5.6537271, 4.1662002, 8.8224411, 4.7004805,
             108594870L,               0L,       "Dongnai_province", 8.2224178, 8.1201506, 7.7692056, 7.8650966, 3.4594662, 6.2653012,  5.5089636,  4.186924, 9.0895538, 5.5606818,
             108594870L,               0L,  "Thuathienhue_province", 8.2224178, 8.1201506, 7.7692056, 7.8650966, 4.6249728,   5.46806,  4.2329168, 4.1972017, 8.5623245, 6.0867748,
             108594870L,               0L,      "Haugiang_province", 8.2224178, 8.1201506, 7.7692056, 7.8650966, 5.3706379, 6.1717005,  4.5786986,  4.161068, 8.5612812, 5.5984221,
             108594870L,               0L,       "Hungyen_province", 8.2224178, 8.1201506, 7.7692056, 7.8650966, 4.5315237, 7.1530519,  5.4823933, 4.1526136, 8.9107132, 4.7004805,
             108594870L,               0L, "Baria-vungtau_province", 8.2224178, 8.1201506, 7.7692056, 7.8650966, 4.2297492,  6.331502,  4.4369946, 4.2040954, 9.0418806, 5.4380794,
             108594870L,               0L,     "Quangninh_province", 8.2224178, 8.1201506, 7.7692056, 7.8650966, 3.8501475,   5.32301,  4.2263217, 4.2959237, 9.0632677,  5.768321,
             108594870L,               0L,            "Danang_city", 8.2224178, 8.1201506, 7.7692056, 7.8650966,  2.587764, 6.7345915,  4.3072119, 4.2506356, 8.8469181, 6.0637851,
             108594870L,               0L,        "Hatinh_province", 8.2224178, 8.1201506, 7.7692056, 7.8650966, 6.0776424, 5.3612924,   4.708425, 4.1814394, 8.6849604, 6.1944056,
             108594870L,               0L,      "Thanhhoa_province", 8.2224178, 8.1201506, 7.7692056, 7.8650966, 5.2522736,  5.768321,  4.5135665,  4.184185, 8.4405375, 5.1357985,
             108594870L,               0L,     "Binhduong_province", 8.2224178, 8.1201506, 7.7692056, 7.8650966, 3.5973122, 6.6883545,  5.8385348, 4.2103481, 8.9690628, 5.3936276,
             108594870L,               0L,       "Bacninh_province", 8.2224178, 8.1201506, 7.7692056, 7.8650966, 4.5549288, 7.3238306,  6.6909885, 4.2597179, 9.0587311, 5.3471074,
             108594870L,               0L,         "Hochiminh_city", 8.2224178, 8.1201506, 7.7692056, 7.8650966, 2.6390574, 8.3359108,  5.0551434,  4.207078, 9.1471338, 5.6698809,
             108594870L,               0L,          "Haiphong_city", 8.2224178, 8.1201506, 7.7692056, 7.8650966, 1.4350846,  7.161622,  5.5514636,  4.230186, 8.9138956,  5.857933,
             108594870L,               0L,      "Quangnam_province", 8.2224178, 8.1201506, 7.7692056, 7.8650966,  4.383276, 4.9558272,  4.1683974, 4.2401752, 8.6177435, 5.7365723,
             108594870L,               0L,        "Backan_province", 8.2224178, 8.1201506, 7.7692056, 7.8650966, 4.7361984, 4.2046928, -1.7439516,  4.139955, 8.6858816, 4.3820267,
             108594870L,               0L,    "Thainguyen_province", 8.2224178, 8.1201506, 7.7692056, 7.8650966, 5.1817837, 5.8861041,  6.7543707, 4.2152338, 9.0023937, 4.7874918,
             108594870L,               0L,         "Hanam_province", 8.2224178, 8.1201506, 7.7692056, 7.8650966, 4.8520303,   6.84375,    5.30164, 4.1754637, 8.7301798, 5.2470241,
             108594870L,               0L,        "Phutho_province", 8.2224178, 8.1201506, 7.7692056, 7.8650966, 5.3082676, 5.9839363,  4.7599664, 4.1826606, 8.7382259, 5.2470241,
             108594870L,               0L,      "Vinhphuc_province", 8.2224178, 8.1201506, 7.7692056, 7.8650966, 5.1239638, 6.7844572,  5.3757849, 4.2009544, 8.9477854, 5.1357985,
             108594870L,               0L,      "Vinhlong_province", 8.2224178, 8.1201506, 7.7692056, 7.8650966, 4.9126549, 6.5352411,  3.5308795, 4.2668962, 8.5671768,  5.480639,
             108594870L,               1L,             "Hanoi_city", 8.2224178, 8.1201506, 7.7692056, 7.8650966, 4.8202815, 7.7137847,   4.702239, 4.2312036, 9.1257057, 5.2983174,
             108594870L,               0L,       "Travinh_province", 8.2224178, 8.1201506, 7.7692056, 7.8650966,   4.89784, 6.0980744,  3.1784184, 4.1463041, 8.5822763, 5.7037826,
             108594870L,               0L,      "Ninhbinh_province", 8.2224178, 8.1201506, 7.7692056, 7.8650966, 4.8283138, 6.5539336,  5.0262208, 4.1679049, 8.5716619, 4.3820267,
             108594870L,               0L,     "Quangngai_province", 8.2224178, 8.1201506, 7.7692056, 7.8650966, 4.9972124, 5.5093884,  3.8801143, 4.1640263, 8.9127951, 5.2983174,
             108594870L,               0L,       "Tayninh_province", 8.2224178, 8.1201506, 7.7692056, 7.8650966, 4.7361984, 5.6347895,  5.4272904, 4.2054386, 8.8453484, 5.0106354,
             108594870L,               0L,      "Thaibinh_province", 8.2224178, 8.1201506, 7.7692056, 7.8650966, 4.2456341,  7.029973,  4.5747805, 4.1802163, 8.5892353, 4.9416423,
             108594870L,               0L,        "Nghean_province", 8.2224178, 8.1201506, 7.7692056, 7.8650966, 5.9506426, 5.2574954,  3.3081558, 4.1993051, 8.5572729, 4.8675346,
             108594870L,               0L,      "Haiduong_province", 8.2224178, 8.1201506, 7.7692056, 7.8650966, 3.8110971, 6.9874902,  5.6670299, 4.1565366, 8.8397036, 5.5984221,
             108594870L,               0L,       "Namdinh_province", 8.2224178, 8.1201506, 7.7692056, 7.8650966, 4.4875121, 7.0130157,  4.6440415, 4.1757708, 8.5558758, 5.0106354,
             108594870L,               0L,     "Binhthuan_province", 8.2224178, 8.1201506, 7.7692056, 7.8650966, 5.1704841, 5.0498562,  3.8159115, 4.1794515, 8.5931168, 5.6698809,
             108594870L,               0L,         "Camau_province", 8.2224178, 8.1201506, 7.7692056, 7.8650966, 5.7462034, 5.4638319,  4.0027852, 4.1604443,   8.75842,  5.521461,
             108594870L,               0L,    "Tuyenquang_province", 8.2224178, 8.1201506, 7.7692056, 7.8650966,  5.556828, 4.8903489,  2.4108267, 4.1763854, 8.6335611, 4.8675346,
             108594870L,               0L,     "Binhphuoc_province", 8.2224178, 8.1201506, 7.7692056, 7.8650966, 4.6634393, 4.9558272,  4.9517002, 4.1305156, 8.7598467, 5.7037826,
             108595095L,               0L,      "Haugiang_province", 8.0093489, 8.2819128,   7.93539, 8.2279396, 5.3706379, 6.1717005,  4.5786986,  4.161068, 8.5612812, 5.5984221,
             108595095L,               0L,         "Hochiminh_city", 8.0093489, 8.2819128,   7.93539, 8.2279396, 2.6390574, 8.3359108,  5.0551434,  4.207078, 9.1471338, 5.6698809,
             108595095L,               0L,      "Vinhlong_province", 8.0093489, 8.2819128,   7.93539, 8.2279396, 4.9126549, 6.5352411,  3.5308795, 4.2668962, 8.5671768,  5.480639,
             108595095L,               0L,            "Danang_city", 8.0093489, 8.2819128,   7.93539, 8.2279396,  2.587764, 6.7345915,  4.3072119, 4.2506356, 8.8469181, 6.0637851,
             108595095L,               0L,       "Bacninh_province", 8.0093489, 8.2819128,   7.93539, 8.2279396, 4.5549288, 7.3238306,  6.6909885, 4.2597179, 9.0587311, 5.3471074,
             108595095L,               0L,  "Thuathienhue_province", 8.0093489, 8.2819128,   7.93539, 8.2279396, 4.6249728,   5.46806,  4.2329168, 4.1972017, 8.5623245, 6.0867748,
             108595095L,               0L,      "Thanhhoa_province", 8.0093489, 8.2819128,   7.93539, 8.2279396, 5.2522736,  5.768321,  4.5135665,  4.184185, 8.4405375, 5.1357985,
             108595095L,               0L,     "Binhduong_province", 8.0093489, 8.2819128,   7.93539, 8.2279396, 3.5973122, 6.6883545,  5.8385348, 4.2103481, 8.9690628, 5.3936276,
             108595095L,               0L,      "Vinhphuc_province", 8.0093489, 8.2819128,   7.93539, 8.2279396, 5.1239638, 6.7844572,  5.3757849, 4.2009544, 8.9477854, 5.1357985,
             108595095L,               0L,     "Binhphuoc_province", 8.0093489, 8.2819128,   7.93539, 8.2279396, 4.6634393, 4.9558272,  4.9517002, 4.1305156, 8.7598467, 5.7037826,
             108595095L,               0L,       "Hungyen_province", 8.0093489, 8.2819128,   7.93539, 8.2279396, 4.5315237, 7.1530519,  5.4823933, 4.1526136, 8.9107132, 4.7004805,
             108595095L,               0L,        "Phutho_province", 8.0093489, 8.2819128,   7.93539, 8.2279396, 5.3082676, 5.9839363,  4.7599664, 4.1826606, 8.7382259, 5.2470241,
             108595095L,               0L,         "Camau_province", 8.0093489, 8.2819128,   7.93539, 8.2279396, 5.7462034, 5.4638319,  4.0027852, 4.1604443,   8.75842,  5.521461,
             108595095L,               0L,     "Quangngai_province", 8.0093489, 8.2819128,   7.93539, 8.2279396, 4.9972124, 5.5093884,  3.8801143, 4.1640263, 8.9127951, 5.2983174,
             108595095L,               0L,        "Hatinh_province", 8.0093489, 8.2819128,   7.93539, 8.2279396, 6.0776424, 5.3612924,   4.708425, 4.1814394, 8.6849604, 6.1944056,
             108595095L,               0L,      "Thaibinh_province", 8.0093489, 8.2819128,   7.93539, 8.2279396, 4.2456341,  7.029973,  4.5747805, 4.1802163, 8.5892353, 4.9416423,
             108595095L,               0L,        "Nghean_province", 8.0093489, 8.2819128,   7.93539, 8.2279396, 5.9506426, 5.2574954,  3.3081558, 4.1993051, 8.5572729, 4.8675346,
             108595095L,               0L,         "Hanam_province", 8.0093489, 8.2819128,   7.93539, 8.2279396, 4.8520303,   6.84375,    5.30164, 4.1754637, 8.7301798, 5.2470241,
             108595095L,               0L,     "Quangninh_province", 8.0093489, 8.2819128,   7.93539, 8.2279396, 3.8501475,   5.32301,  4.2263217, 4.2959237, 9.0632677,  5.768321,
             108595095L,               0L,        "Backan_province", 8.0093489, 8.2819128,   7.93539, 8.2279396, 4.7361984, 4.2046928, -1.7439516,  4.139955, 8.6858816, 4.3820267,
             108595095L,               0L,    "Thainguyen_province", 8.0093489, 8.2819128,   7.93539, 8.2279396, 5.1817837, 5.8861041,  6.7543707, 4.2152338, 9.0023937, 4.7874918,
             108595095L,               0L,       "Dongnai_province", 8.0093489, 8.2819128,   7.93539, 8.2279396, 3.4594662, 6.2653012,  5.5089636,  4.186924, 9.0895538, 5.5606818,
             108595095L,               0L,      "Haiduong_province", 8.0093489, 8.2819128,   7.93539, 8.2279396, 3.8110971, 6.9874902,  5.6670299, 4.1565366, 8.8397036, 5.5984221,
             108595095L,               0L,      "Ninhbinh_province", 8.0093489, 8.2819128,   7.93539, 8.2279396, 4.8283138, 6.5539336,  5.0262208, 4.1679049, 8.5716619, 4.3820267,
             108595095L,               0L,      "Bacgiang_province", 8.0093489, 8.2819128,   7.93539, 8.2279396, 4.5890408, 6.0730443,  5.6537271, 4.1662002, 8.8224411, 4.7004805,
             108595095L,               0L,       "Tayninh_province", 8.0093489, 8.2819128,   7.93539, 8.2279396, 4.7361984, 5.6347895,  5.4272904, 4.2054386, 8.8453484, 5.0106354,
             108595095L,               0L,          "Haiphong_city", 8.0093489, 8.2819128,   7.93539, 8.2279396, 1.4350846,  7.161622,  5.5514636,  4.230186, 8.9138956,  5.857933,
             108595095L,               0L,       "Travinh_province", 8.0093489, 8.2819128,   7.93539, 8.2279396,   4.89784, 6.0980744,  3.1784184, 4.1463041, 8.5822763, 5.7037826,
             108595095L,               0L,      "Quangnam_province", 8.0093489, 8.2819128,   7.93539, 8.2279396,  4.383276, 4.9558272,  4.1683974, 4.2401752, 8.6177435, 5.7365723,
             108595095L,               0L,     "Binhthuan_province", 8.0093489, 8.2819128,   7.93539, 8.2279396, 5.1704841, 5.0498562,  3.8159115, 4.1794515, 8.5931168, 5.6698809,
             108595095L,               0L, "Baria-vungtau_province", 8.0093489, 8.2819128,   7.93539, 8.2279396, 4.2297492,  6.331502,  4.4369946, 4.2040954, 9.0418806, 5.4380794,
             108595095L,               1L,             "Hanoi_city", 8.0093489, 8.2819128,   7.93539, 8.2279396, 4.8202815, 7.7137847,   4.702239, 4.2312036, 9.1257057, 5.2983174,
             108595095L,               0L,    "Tuyenquang_province", 8.0093489, 8.2819128,   7.93539, 8.2279396,  5.556828, 4.8903489,  2.4108267, 4.1763854, 8.6335611, 4.8675346,
             108595095L,               0L,       "Namdinh_province", 8.0093489, 8.2819128,   7.93539, 8.2279396, 4.4875121, 7.0130157,  4.6440415, 4.1757708, 8.5558758, 5.0106354,
             108595095L,               0L,        "Longan_province", 8.0093489, 8.2819128,   7.93539, 8.2279396, 4.0055132,  5.811141,  5.3210664, 4.2314944, 8.8020086, 5.4380794,
             108605233L,               0L,     "Binhduong_province", 8.2393103,   9.70644, 11.101022, 9.4778214, 3.5973122, 6.6883545,  5.8385348, 4.2103481, 8.9690628, 5.3936276,
             108605233L,               0L,         "Camau_province", 8.2393103,   9.70644, 11.101022, 9.4778214, 5.7462034, 5.4638319,  4.0027852, 4.1604443,   8.75842,  5.521461,
             108605233L,               0L,     "Quangninh_province", 8.2393103,   9.70644, 11.101022, 9.4778214, 3.8501475,   5.32301,  4.2263217, 4.2959237, 9.0632677,  5.768321,
             108605233L,               0L,      "Bacgiang_province", 8.2393103,   9.70644, 11.101022, 9.4778214, 4.5890408, 6.0730443,  5.6537271, 4.1662002, 8.8224411, 4.7004805,
             108605233L,               0L,        "Backan_province", 8.2393103,   9.70644, 11.101022, 9.4778214, 4.7361984, 4.2046928, -1.7439516,  4.139955, 8.6858816, 4.3820267,
             108605233L,               0L,       "Hungyen_province", 8.2393103,   9.70644, 11.101022, 9.4778214, 4.5315237, 7.1530519,  5.4823933, 4.1526136, 8.9107132, 4.7004805,
             108605233L,               0L,        "Hatinh_province", 8.2393103,   9.70644, 11.101022, 9.4778214, 6.0776424, 5.3612924,   4.708425, 4.1814394, 8.6849604, 6.1944056,
             108605233L,               0L, "Baria-vungtau_province", 8.2393103,   9.70644, 11.101022, 9.4778214, 4.2297492,  6.331502,  4.4369946, 4.2040954, 9.0418806, 5.4380794,
             108605233L,               0L,       "Travinh_province", 8.2393103,   9.70644, 11.101022, 9.4778214,   4.89784, 6.0980744,  3.1784184, 4.1463041, 8.5822763, 5.7037826,
             108605233L,               0L,      "Thanhhoa_province", 8.2393103,   9.70644, 11.101022, 9.4778214, 5.2522736,  5.768321,  4.5135665,  4.184185, 8.4405375, 5.1357985,
             108605233L,               0L,     "Binhphuoc_province", 8.2393103,   9.70644, 11.101022, 9.4778214, 4.6634393, 4.9558272,  4.9517002, 4.1305156, 8.7598467, 5.7037826,
             108605233L,               0L,       "Bacninh_province", 8.2393103,   9.70644, 11.101022, 9.4778214, 4.5549288, 7.3238306,  6.6909885, 4.2597179, 9.0587311, 5.3471074,
             108605233L,               0L,    "Thainguyen_province", 8.2393103,   9.70644, 11.101022, 9.4778214, 5.1817837, 5.8861041,  6.7543707, 4.2152338, 9.0023937, 4.7874918,
             108605233L,               0L,       "Tayninh_province", 8.2393103,   9.70644, 11.101022, 9.4778214, 4.7361984, 5.6347895,  5.4272904, 4.2054386, 8.8453484, 5.0106354,
             108605233L,               0L,       "Dongnai_province", 8.2393103,   9.70644, 11.101022, 9.4778214, 3.4594662, 6.2653012,  5.5089636,  4.186924, 9.0895538, 5.5606818,
             108605233L,               0L,        "Nghean_province", 8.2393103,   9.70644, 11.101022, 9.4778214, 5.9506426, 5.2574954,  3.3081558, 4.1993051, 8.5572729, 4.8675346,
             108605233L,               0L,      "Vinhlong_province", 8.2393103,   9.70644, 11.101022, 9.4778214, 4.9126549, 6.5352411,  3.5308795, 4.2668962, 8.5671768,  5.480639,
             108605233L,               0L,      "Ninhbinh_province", 8.2393103,   9.70644, 11.101022, 9.4778214, 4.8283138, 6.5539336,  5.0262208, 4.1679049, 8.5716619, 4.3820267,
             108605233L,               0L,    "Tuyenquang_province", 8.2393103,   9.70644, 11.101022, 9.4778214,  5.556828, 4.8903489,  2.4108267, 4.1763854, 8.6335611, 4.8675346,
             108605233L,               0L,            "Danang_city", 8.2393103,   9.70644, 11.101022, 9.4778214,  2.587764, 6.7345915,  4.3072119, 4.2506356, 8.8469181, 6.0637851,
             108605233L,               0L,      "Vinhphuc_province", 8.2393103,   9.70644, 11.101022, 9.4778214, 5.1239638, 6.7844572,  5.3757849, 4.2009544, 8.9477854, 5.1357985,
             108605233L,               0L,      "Haiduong_province", 8.2393103,   9.70644, 11.101022, 9.4778214, 3.8110971, 6.9874902,  5.6670299, 4.1565366, 8.8397036, 5.5984221,
             108605233L,               0L,      "Quangnam_province", 8.2393103,   9.70644, 11.101022, 9.4778214,  4.383276, 4.9558272,  4.1683974, 4.2401752, 8.6177435, 5.7365723,
             108605233L,               0L,      "Haugiang_province", 8.2393103,   9.70644, 11.101022, 9.4778214, 5.3706379, 6.1717005,  4.5786986,  4.161068, 8.5612812, 5.5984221,
             108605233L,               0L,     "Quangngai_province", 8.2393103,   9.70644, 11.101022, 9.4778214, 4.9972124, 5.5093884,  3.8801143, 4.1640263, 8.9127951, 5.2983174,
             108605233L,               0L,  "Thuathienhue_province", 8.2393103,   9.70644, 11.101022, 9.4778214, 4.6249728,   5.46806,  4.2329168, 4.1972017, 8.5623245, 6.0867748,
             108605233L,               0L,          "Haiphong_city", 8.2393103,   9.70644, 11.101022, 9.4778214, 1.4350846,  7.161622,  5.5514636,  4.230186, 8.9138956,  5.857933,
             108605233L,               0L,     "Binhthuan_province", 8.2393103,   9.70644, 11.101022, 9.4778214, 5.1704841, 5.0498562,  3.8159115, 4.1794515, 8.5931168, 5.6698809,
             108605233L,               0L,         "Hanam_province", 8.2393103,   9.70644, 11.101022, 9.4778214, 4.8520303,   6.84375,    5.30164, 4.1754637, 8.7301798, 5.2470241,
             108605233L,               0L,         "Hochiminh_city", 8.2393103,   9.70644, 11.101022, 9.4778214, 2.6390574, 8.3359108,  5.0551434,  4.207078, 9.1471338, 5.6698809,
             108605233L,               0L,        "Phutho_province", 8.2393103,   9.70644, 11.101022, 9.4778214, 5.3082676, 5.9839363,  4.7599664, 4.1826606, 8.7382259, 5.2470241,
             108605233L,               0L,      "Thaibinh_province", 8.2393103,   9.70644, 11.101022, 9.4778214, 4.2456341,  7.029973,  4.5747805, 4.1802163, 8.5892353, 4.9416423,
             108605233L,               1L,             "Hanoi_city", 8.2393103,   9.70644, 11.101022, 9.4778214, 4.8202815, 7.7137847,   4.702239, 4.2312036, 9.1257057, 5.2983174,
             108605233L,               0L,        "Longan_province", 8.2393103,   9.70644, 11.101022, 9.4778214, 4.0055132,  5.811141,  5.3210664, 4.2314944, 8.8020086, 5.4380794,
             108605233L,               0L,       "Namdinh_province", 8.2393103,   9.70644, 11.101022, 9.4778214, 4.4875121, 7.0130157,  4.6440415, 4.1757708, 8.5558758, 5.0106354
             )
head(sample3)
#> # A tibble: 6 Ă— 13
#>         id provi…¹ provi…²    X1    X2    X3    X4    Z1    Z2    Z3    Z4    Z5
#>      <int>   <int> <chr>   <dbl> <dbl> <dbl> <dbl> <dbl> <dbl> <dbl> <dbl> <dbl>
#> 1   1.09e8       0 Ninhbi…  8.22  8.12  7.77  7.87  4.83  6.55  5.03  4.17  8.57
#> 2   1.09e8       0 Travin…  8.22  8.12  7.77  7.87  4.90  6.10  3.18  4.15  8.58
#> 3   1.09e8       0 Vinhlo…  8.22  8.12  7.77  7.87  4.91  6.54  3.53  4.27  8.57
#> 4   1.09e8       0 Haipho…  8.22  8.12  7.77  7.87  1.44  7.16  5.55  4.23  8.91
#> 5   1.09e8       0 Phutho…  8.22  8.12  7.77  7.87  5.31  5.98  4.76  4.18  8.74
#> 6   1.09e8       1 Hanoi_…  8.22  8.12  7.77  7.87  4.82  7.71  4.70  4.23  9.13
#> # … with 1 more variable: Z6 <dbl>, and abbreviated variable names
#> #   ¹​province_chosen, ²​province_name

Created on 2023-04-02 with reprex v2.0.2
Here is my code and the error is still the same as my first post:
library(mlogit)
hl <- mlogit(province_chosen ~ Z1 + Z2 + Z3 + Z4 + Z5 + Z6|X1 + X2 + X3 + X4,sample3,reflevel = 'Hanoi_city', heterosc = TRUE)

  1. I also want to get scale parameter of the Heteroskedastic logit by a code similar to the code provided in this link https://cran.r-project.org/web/packages/mlogit/vignettes/c4.relaxiid.html: Here is the code they provided and I want a similar code for my data, but I do not know how to create.

I really look forward to your help. Thank you so much!

coef(summary(hl.MC))[11:12, ]
```:

I've been approaching this as a functional analysis problem in finding the right arguments needed to resolve the error messages and have understood a couple of the underlying issues—the province_name variable needs two be a factor and there must be two columns identified as index columns in the dfidx() function. However, I was still having difficulty resolving an error involving the "reference level."

Going back to read more of the documentation, I saw that this is a model developed in the econometrics field and is designed for specific types of data sets

Data sets used for multinomial logit estimation concern some individuals ... that make one or a sequential choice of one alternative among a set of mutually exclusive alternatives. The determinants of these choices are covariates that can depend on the alternative and the choice situation, only on the alternative or only on the choice situation.

In the data for this question, the choices are a list of localities as 35 mutually exclusive alternatives to be selected by a list of 4 individuals, and 9 numeric variables that are assumed to be relevant to the choice.

However, all individuals made the same choice, effectively focusing attention on a much smaller data object

(d <- data.frame(
  id = c(108582716, 108594870, 108595095, 108605233), 
  province_chosen = c(1, 1, 1, 1), 
  province_name = rep("Hanoi_city",4),
  X1 = c(8.2224178, 8.2224178, 8.0093489, 8.2393103), X2 = c(
    8.1201506,
    8.1201506, 8.2819128, 9.70644
  ), X3 = c(
    7.7692056, 7.7692056,
    7.93539, 11.101022
  ), X4 = c(
    7.8650966, 7.8650966, 8.2279396,
    9.4778214
  ), Z1 = c(4.8202815, 4.8202815, 4.8202815, 4.8202815), Z2 = c(7.7137847, 7.7137847, 7.7137847, 7.7137847), Z3 = c(
    4.702239,
    4.702239, 4.702239, 4.702239
  ), Z4 = c(
    4.2312036, 4.2312036,
    4.2312036, 4.2312036
  ), Z5 = c(
    9.1257057, 9.1257057, 9.1257057,
    9.1257057
  ), Z6 = c(5.2983174, 5.2983174, 5.2983174, 5.2983174)
))
#>          id province_chosen province_name       X1       X2        X3       X4
#> 1 108582716               1    Hanoi_city 8.222418 8.120151  7.769206 7.865097
#> 2 108594870               1    Hanoi_city 8.222418 8.120151  7.769206 7.865097
#> 3 108595095               1    Hanoi_city 8.009349 8.281913  7.935390 8.227940
#> 4 108605233               1    Hanoi_city 8.239310 9.706440 11.101022 9.477821
#>         Z1       Z2       Z3       Z4       Z5       Z6
#> 1 4.820282 7.713785 4.702239 4.231204 9.125706 5.298317
#> 2 4.820282 7.713785 4.702239 4.231204 9.125706 5.298317
#> 3 4.820282 7.713785 4.702239 4.231204 9.125706 5.298317
#> 4 4.820282 7.713785 4.702239 4.231204 9.125706 5.298317

Created on 2023-04-03 with reprex v2.0.2

Noticing the the Z variables were a constant among all individuals, we are left with only the X variables to look to for insight.

#>         Z1       Z2       Z3       Z4       Z5       Z6
#> 1 4.820282 7.713785 4.702239 4.231204 9.125706 5.298317
#> 2 4.820282 7.713785 4.702239 4.231204 9.125706 5.298317
#> 3 4.820282 7.713785 4.702239 4.231204 9.125706 5.298317
#> 4 4.820282 7.713785 4.702239 4.231204 9.125706 5.298317

(xs <- d[4:7])
#>         X1       X2        X3       X4
#> 1 8.222418 8.120151  7.769206 7.865097
#> 2 8.222418 8.120151  7.769206 7.865097
#> 3 8.009349 8.281913  7.935390 8.227940
#> 4 8.239310 9.706440 11.101022 9.477821
GGally::ggpairs(xs)
#> Registered S3 method overwritten by 'GGally':
#>   method from   
#>   +.gg   ggplot2

And among the X variables, two individuals report identical values. The remaining eight data points don't leave much to go on.

This means I need better to understand if this is the extent of the data and, if it is, what question the mlogit approach is meant to illuminate?

Hi. Thank you for being considerate. Z1.....Z6 are alternative specific variable and X1...X4 are individual specific. I am most interested in examining the effect of X1...X4 on the probability of choosing alternatives. Really appreciate your help. It seems I need a lot to learn about R and this methodology.

@nhungntk1005, let me ask a follow-up question from my reading of @technocrat's reply. @technocrat writes "all individuals made the same choice." If the dependent variable is identical for all observations, then there is no way to use multinomial logit--or any other statistical method--to find out what influenced the choice. Hopefully, I am misunderstanding the data.

2 Likes