ERROR : system is exactly singular

Hi,
I am trying to run multinominal logistic regression. Please follow the link below for HTML file:
file:///Users/mtk/Desktop/RQ1.html

but receiving the following error:

Error in solve.default(H, g[!fixed]) : Lapack routine dgesv: system is exactly singular: U[5,5] = 0

Can anyone tell me what the problem is? and what is the solution?

We don't really have enough info to help you out, the link you have posted is for a location in your local system so we don't have access to it. Could you ask this with a minimal REPRoducible EXample (reprex)? A reprex makes it much easier for others to understand your issue and figure out how to help.

If you've never heard of a reprex before, you might want to start by reading this FAQ:

Please share your sample data on a copy/paste friendly format, ideally use datapasta and reprex packages to make a proper reproducible example as described in the link I gave you before.

I have never used the mlogit package but looking at the documentation, you should not specify an intercept in the first part of the formula. The formula consists of three parts:

first one contains the alternative specific variables with generic coefficient,i.e.a unique coefficient for all the alternatives ; the second one contains the individual specific variables for which one coefficient is estimated for all the alternatives except one of them ; the third one contains the alternative specific variables with alternative specific coefficients. The different parts are separeted by a | sign.

The documentation also says:

The intercept is necessarely alternative specific (a generic intercept is not identified because only utility differences are relevant). Therefore, it deals with the second part of the formula. As it is usual inR, the default behaviour is to include an intercept.

Therefore, I suggest removing the explicit intercept from your call to mlogit

mlogit(Happiness ~ 1| Access, data = RQdata2, reflevel = 2)

Data Frame:

## Constructing variables:
Access <- c(1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,
1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1
,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,
1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,
1,1,1,1,1,1,1,2,2,2,2,2,3)

Happiness <- c(1,1,1, 1,1, 1 ,1 ,1, 1, 1, 1, 1, 1,
 1, 1, 1, 1, 1, 1, 1, 1 ,1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 
1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 
1, 1, 1, 1, 1, 1,1,1,2,2,2,2,2,2,2,2,2,2, 2,2,2,
2,2,2,2,2,2,2,2,2,2,2,2,2,2,2,2,2,2,2,2,2,3,3,
3,3,3,NA,NA,NA,NA)

## Change variables to factor:
Access <- factor(Access , levels= c(1,2,3,4), labels = c("Access_to_both
", "Access_to_str ", " Access_to_cog", "Access_to_none") )

Happiness <- factor(Happiness , levels= c(1,2,3), labels = c("Happy", "Neither_Happy_or_Unhappy",
 "Unhappy"))

## Constructing dataframe:
RQdata2 <- data.frame(Access,Happiness)

Converting data to wide format and run multinominal Logistic regression:

# Change long format to wide format
library(mlogit)
RQdata2 <- mlogit.data(RQdata2, choice = "Happiness", shape = "wide")

# Run multinominal logistic regression:
RQmodel <- mlogit(Happiness ~ 1| Access, data = RQdata2, reflevel = 2)

Error in solve.default(H, g[!fixed]) : Lapack routine dgesv: system is exactly singular: U[5,5] = 0

Looking at your data, Access effectively only has 2 levels, 1 & 2. Level 4 never appears and the one occurrence of level 3 is matched with a Happiness of NA. If I trim your data to remove observations where Happiness = NA and also limit the levels of Access to those actually present, I do not get an error.

## Constructing variables:
Access2 <- c(1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,
            1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1
            ,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,
            1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,
            1,1,1,1,1,1,1,2,2)

Happiness2 <- c(1,1,1, 1,1, 1 ,1 ,1, 1, 1, 1, 1, 1,
               1, 1, 1, 1, 1, 1, 1, 1 ,1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 
               1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 
               1, 1, 1, 1, 1, 1,1,1,2,2,2,2,2,2,2,2,2,2, 2,2,2,
               2,2,2,2,2,2,2,2,2,2,2,2,2,2,2,2,2,2,2,2,2,3,3,
               3,3,3)

## Change variables to factor:
Access2 <- factor(Access2 , levels= c(1,2), labels = c("Access_to_both", "Access_to_str"))

Happiness2 <- factor(Happiness2 , levels= c(1,2,3), 
                     labels = c("Happy", "Neither_Happy_or_Unhappy", "Unhappy"))

## Constructing dataframe:
RQdata2_2 <- data.frame(Access2,Happiness2)


# Change long format to wide format
library(mlogit)
#> Warning: package 'mlogit' was built under R version 3.5.3
#> Loading required package: Formula
#> Warning: package 'Formula' was built under R version 3.5.2
#> Loading required package: zoo
#> Warning: package 'zoo' was built under R version 3.5.2
#> 
#> Attaching package: 'zoo'
#> The following objects are masked from 'package:base':
#> 
#>     as.Date, as.Date.numeric
#> Loading required package: lmtest
#> Warning: package 'lmtest' was built under R version 3.5.3
RQdata2_2 <- mlogit.data(RQdata2_2, choice = "Happiness2", shape = "wide")

# Run multinominal logistic regression:
RQmodel <- mlogit(Happiness2 ~ 1| Access2, data = RQdata2_2, reflevel = 2)
RQmodel
#> 
#> Call:
#> mlogit(formula = Happiness2 ~ 1 | Access2, data = RQdata2_2,     reflevel = 2, method = "nr")
#> 
#> Coefficients:
#>            Happy:(intercept)           Unhappy:(intercept)  
#>                      0.51669                      -2.42775  
#>   Happy:Access2Access_to_str  Unhappy:Access2Access_to_str  
#>                     -0.51669                      19.57906

Created on 2019-03-26 by the reprex package (v0.2.1)

1 Like