Fixing computationally singular error in R mlogit package

Hi everyone!
I am using mlogit package in R for random utility model: [Estimation of Random Utility Models in R: The mlogit Package | Journal of Statistical Software]
The dependent variable "choice" depends on alternative specific variables: logPCI, logopenness, loglabour3, logunem1, loglabour3 and 4 other variables: logMAinside1, logMAoutside1, logSAinside1, logSAoutside1. I am having some problems and appreciate your help. I attach my data sample at the end of this post.

Q1. I have a question regarding 4 variables: logMAinside1, logMAoutside1, logSAinside1, logSAoutside1. They are industry-specific variables because firms belonging to the same "sector" in the same province (alt) will have the same values of these 4 variables (for example, firm id 108582716 and firm id 108594870 in sampel dataset). If firms belonging to the same sector but in different provinces will have different values of these four variables. Can I consider them as individual specific variables when I run mlogit model and use this code:
ml1.mydata2<- mlogit(choice ~ logPCI + logopenness + logseaport3 + loglabour3 + logunem1| logMAinside1 + logMAoutside1 + logSAinside1 + logSAoutside1, mydata2, reflevel = "Hanoi_city") ?

Q2. When I run mlogit model:

library(mlogit)
library(dfidx)
mydata2 <- dfidx(mydata, idx = c("firm_id", "alt"),drop.index = FALSE)

library(mlogit)
ml1.mydata2<- mlogit(choice ~ logPCI + logopenness + logseaport3 + loglabour3 + logunem1| logMAinside1 + logMAoutside1 + logSAinside1 + logSAoutside1, mydata2, reflevel = "Hanoi_city")

I have the errors: Error in solve.default(H, g[!fixed]) : system is computationally singular: reciprocal condition number = 2.98946e-2

I think that the error comes from firms belonging to same industries in the same province (alt) have the same values of logMAinside1, logMAoutside1, logSAinside1, logSAoutside1. Would you mind suggesting a solution to my problem? I am interested in the effect of 4 variables: logMAinside1, logMAoutside1, logSAinside1, logSAoutside1 on dependent variable.

Q3. My real dataset is quite big: 97167 observations with 35 alternatives. Surprisingly, When I run the real dataset, the code has no error. Is that because the large number of observations correct for problem in Q2. However, as I add heterosc = TRUE to the model:

ml1.mydata2<- mlogit(choice ~ logPCI + logopenness + logseaport3 + loglabour3 + logunem1| logMAinside1 + logMAoutside1 + logSAinside1 + logSAoutside1, mydata2, reflevel = "Hanoi_city", heterosc = TRUE)

I got the error " Error in if (abs(chi2) < tol) { : missing value where TRUE/FALSE needed"

Q4. When I run nested logit model with the codes

mydata3 <- dfidx(mydata, idx = list("firm_id", c("alt", "group")), idnames = c("chid", "alt"), drop.index = FALSE)
nl.mydata3 <- mlogit(choice ~ logPCI + logopenness + logseaport3 + loglabour3 + logunem1, data = mydata3, nests = TRUE, un.nest.el = FALSE)

in the sample, I still get computational singular error but surprisingly, the same code applied in real dataset give me result. However, as I add 4 variables: logMAinside1, logMAoutside1, logSAinside1, logSAoutside1 on the nested logit model on both real sample and sample data set:

nl2.mydata3 <- mlogit(choice ~ logPCI + logopenness + logseaport3 + loglabour3 + logunem1|logMAinside1 logMAoutside1 logSAinside1 logSAoutsie1, data = mydata3, nests = TRUE, un.nest.el = FALSE)

I get the same errors: Error in solve.default(crossprod(attr(x, "gradi")[, !fixed])) : system is computationally singular: reciprocal condition number = 9.95274e-29

Here is a sample of my dataset.

mydata <- tibble::tribble(
                  ~firm_id,              ~alt, ~choice, ~year_operation, ~sector, ~technology, ~logMAinside1, ~logMAoutside1, ~logSAinside1, ~logSAoutside1, ~logseaport3,   ~logpop, ~logopenness,   ~logPCI, ~loglabour3, ~logunem1,  ~logroad,      ~group,
                108582716L,   "Haiphong_city",      0L,           2019L,    "SA",       "Low",     8.2224178,      8.1201506,     7.7692056,      7.8650966,    1.4350846,  7.161622,    5.5514636,  4.230186,   8.9138956,  5.857933, 8.8258247, "Key_North",
                108582716L, "Phutho_province",      0L,           2019L,    "SA",       "Low",     8.2224178,      8.1201506,     7.7692056,      7.8650966,    5.3082676, 5.9839363,    4.7599664, 4.1826606,   8.7382259, 5.2470241, 6.5759125, "Key_North",
                108582716L,      "Hanoi_city",      1L,           2019L,    "SA",       "Low",     8.2224178,      8.1201506,     7.7692056,      7.8650966,    4.8202815, 7.7137847,     4.702239, 4.2312036,   9.1257057, 5.2983174, 8.9816189, "Key_North",
                108594870L,   "Haiphong_city",      0L,           2019L,    "SA",       "Low",     8.2224178,      8.1201506,     7.7692056,      7.8650966,    1.4350846,  7.161622,    5.5514636,  4.230186,   8.9138956,  5.857933, 8.8258247, "Key_North",
                108594870L, "Phutho_province",      0L,           2019L,    "SA",       "Low",     8.2224178,      8.1201506,     7.7692056,      7.8650966,    5.3082676, 5.9839363,    4.7599664, 4.1826606,   8.7382259, 5.2470241, 6.5759125, "Key_North",
                108594870L,      "Hanoi_city",      1L,           2019L,    "SA",       "Low",     8.2224178,      8.1201506,     7.7692056,      7.8650966,    4.8202815, 7.7137847,     4.702239, 4.2312036,   9.1257057, 5.2983174, 8.9816189, "Key_North",
                108595095L,   "Haiphong_city",      0L,           2019L,    "SB",       "Low",     8.0093489,      8.2819128,       7.93539,      8.2279396,    1.4350846,  7.161622,    5.5514636,  4.230186,   8.9138956,  5.857933, 8.8258247, "Key_North",
                108595095L, "Phutho_province",      0L,           2019L,    "SB",       "Low",     8.0093489,      8.2819128,       7.93539,      8.2279396,    5.3082676, 5.9839363,    4.7599664, 4.1826606,   8.7382259, 5.2470241, 6.5759125, "Key_North",
                108595095L,      "Hanoi_city",      1L,           2019L,    "SB",       "Low",     8.0093489,      8.2819128,       7.93539,      8.2279396,    4.8202815, 7.7137847,     4.702239, 4.2312036,   9.1257057, 5.2983174, 8.9816189, "Key_North",
                108605233L,   "Haiphong_city",      0L,           2019L,    "SM",      "High",     8.2393103,        9.70644,     11.101022,      9.4778214,    1.4350846,  7.161622,    5.5514636,  4.230186,   8.9138956,  5.857933, 8.8258247, "Key_North",
                108605233L, "Phutho_province",      0L,           2019L,    "SM",      "High",     8.2393103,        9.70644,     11.101022,      9.4778214,    5.3082676, 5.9839363,    4.7599664, 4.1826606,   8.7382259, 5.2470241, 6.5759125, "Key_North",
                108605233L,      "Hanoi_city",      1L,           2019L,    "SM",      "High",     8.2393103,        9.70644,     11.101022,      9.4778214,    4.8202815, 7.7137847,     4.702239, 4.2312036,   9.1257057, 5.2983174, 8.9816189, "Key_North",
                201761149L,      "Hanoi_city",      0L,           2017L,    "SA",       "Low",     6.7310853,      8.0759678,      6.561851,      7.7405944,    4.8202815, 7.6879973,    4.8924828, 4.1066027,   8.9457035, 5.7365723,  8.830514, "Key_North",
                201761149L,   "Haiphong_city",      1L,           2017L,    "SA",       "Low",     6.7310853,      8.0759678,      6.561851,      7.7405944,    1.4350846,  7.145196,    5.1715345, 4.0960097,   8.8061895, 5.8289456, 8.5009623, "Key_North",
                201761149L, "Phutho_province",      0L,           2017L,    "SA",       "Low",     6.7310853,      8.0759678,      6.561851,      7.7405944,    5.3082676, 5.9687076,    4.6572061, 4.0707345,   8.6069441, 5.2983174, 6.4081993, "Key_North",
                201762784L,   "Haiphong_city",      1L,           2017L,    "SN",      "High",     8.9314318,      8.5643978,     11.221056,      8.3962173,    1.4350846,  7.145196,    5.1715345, 4.0960097,   8.8061895, 5.8289456, 8.5009623, "Key_North",
                201762784L,      "Hanoi_city",      0L,           2017L,    "SN",      "High",     8.9314318,      8.5643978,     11.221056,      8.3962173,    4.8202815, 7.6879973,    4.8924828, 4.1066027,   8.9457035, 5.7365723,  8.830514, "Key_North",
                201762784L, "Phutho_province",      0L,           2017L,    "SN",      "High",     8.9314318,      8.5643978,     11.221056,      8.3962173,    5.3082676, 5.9687076,    4.6572061, 4.0707345,   8.6069441, 5.2983174, 6.4081993, "Key_North",
                201764559L,   "Haiphong_city",      1L,           2017L,    "SD",       "Low",     7.2551007,      7.8877192,     9.6826878,      8.0652342,    1.4350846,  7.145196,    5.1715345, 4.0960097,   8.8061895, 5.8289456, 8.5009623, "Key_North",
                201764559L,      "Hanoi_city",      0L,           2017L,    "SD",       "Low",     7.2551007,      7.8877192,     9.6826878,      8.0652342,    4.8202815, 7.6879973,    4.8924828, 4.1066027,   8.9457035, 5.7365723,  8.830514, "Key_North",
                201764559L, "Phutho_province",      0L,           2017L,    "SD",       "Low",     7.2551007,      7.8877192,     9.6826878,      8.0652342,    5.3082676, 5.9687076,    4.6572061, 4.0707345,   8.6069441, 5.2983174, 6.4081993, "Key_North"
                )

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

This topic was automatically closed 42 days after the last reply. New replies are no longer allowed.

If you have a query related to it or one of the replies, start a new topic and refer back with a link.