0

I have been trying to run mlogit model on R and its giving me an error. I am new to R and can't find a solution for it. The code runs fine when I remove the Q23* columns from it.

My dataset: enter image description here


library(mlogit)
library(reshape2)
library(dfidx)

#Data Reading and Cleaning
Data <- read.csv('test.csv')
#Data <- Data[,c(-2,-17:-24)]
Data <- Data[,c(-2)]
#Data



Data <- na.omit(Data)

#Manipulation to prepare Data
Data$id <- 1:nrow(Data)
DataWide = melt(Data, id.vars = c('Sex','AgeA','AgeB','AgeC','AgeD','HouseholdIncomeA','HouseholdIncomeB','HouseholdIncomeC','HouseholdIncomeD','HouseholdIncomeE','JobA','JobB','JobC','Q23S1','Q23S2','Q23S3','Q23S4','Q23S5','Q23S6','Q23S7','Q23S8','Q23S9','Q23S10','id'))
names(DataWide)[names(DataWide) == 'value'] <- 'choice'
DataWide <- DataWide[order(DataWide$id), ]
rownames(DataWide) <- NULL
DataWide$choiceid <- 1:nrow(DataWide)
DataWide <- DataWide[,-25]



#Coercion of Data to dfidx object for panel data
DFIDX <- dfidx(DataWide, choice = "choice", shape = "wide", 
               idx = list(c("choiceid", "id")), idnames = c("chid", "alt"))
head(DFIDX, 10)

dataset <- mlogit.data(DataWide, choice = "choice", shape="wide")



#Multinomial Logistic Regression
Model1 <- mlogit(choice ~ 0 | Sex + AgeA+AgeB+AgeC+ HouseholdIncomeA+HouseholdIncomeB+HouseholdIncomeC+HouseholdIncomeD +JobA+JobB+Q23S1+Q23S2+Q23S3+Q23S4+Q23S5+Q23S6+Q23S7+Q23S8+Q23S9+Q23S10 , data = DFIDX)
summary(Model1)

Error:

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



Sex,Age,Job,Area,Q23S1,Q23S2,Q23S3,Q23S4,Q23S5,Q23S6,Q23S7,Q23S8,Q23S9,Q23S10,JobA,JobB,JobC,AgeA,AgeB,AgeC,AgeD,HouseholdIncomeA,HouseholdIncomeB,HouseholdIncomeC,HouseholdIncomeD,HouseholdIncomeE
1,69,2,3,3,1,1,1,1,1,1,1,1,1,0,1,0,0,0,0,1,0,0,0,0,1
1,48,1,3,3,1,1,1,3,3,1,3,3,2,1,0,0,0,0,1,0,0,0,0,1,0
1,54,1,3,1,1,1,1,1,1,1,1,1,1,1,0,0,0,0,1,0,0,0,1,0,0
1,44,1,3,1,1,1,1,1,1,1,1,1,1,0,0,0,0,1,0,0,1,0,0,0,1
1,60,2,4,3,1,1,1,2,2,1,3,3,3,0,1,0,0,0,0,1,1,0,0,0,0
1,53,1,3,3,1,1,1,3,3,1,3,3,1,1,0,0,0,0,1,0,1,0,0,0,0
2,57,3,4,3,1,1,1,3,3,3,3,3,3,0,0,1,0,0,0,1,0,0,1,0,0
1,66,1,3,3,1,1,1,3,3,3,3,2,1,1,0,0,0,0,0,1,0,0,0,0,1
2,58,3,3,3,1,1,1,2,2,1,2,2,1,0,0,1,0,0,0,1,0,0,0,1,0
1,50,1,4,3,1,1,1,3,3,2,3,3,2,1,0,0,0,0,1,0,0,1,0,0,0
1,57,1,4,3,1,1,1,1,1,1,1,1,1,1,0,0,0,0,0,1,0,0,0,1,0
2,60,3,4,3,1,1,1,2,2,1,2,1,1,0,0,1,0,0,0,1,0,0,1,0,0


  • It looks to me like your model might not be of full rank. Check your indicator variables. Just as you’ve omitted one of the `AgeX` columns, I suspect you might need to the same for `JobX` and `HouseholdX`. – Limey Jun 24 '22 at 11:30
  • @Limey I've already omitted one column for Job and Household too – Wahab Aftab Jun 24 '22 at 11:49
  • Not according to your screenshot. That’s all we have to go on. – Limey Jun 24 '22 at 11:51
  • @Limey oh yes they are in the dataset but in the final model, I'm not passing HouseholdE and JobC. Look at the last line of code – Wahab Aftab Jun 24 '22 at 11:53
  • Ok. You are correct. Apologies. But we can’t feed a screenshot into your modelling code, and you’ve not given us your full dataset, so you’re probably on your own. – Limey Jun 24 '22 at 11:56
  • @Limey I have added a sample dataset which you can use in the model. – Wahab Aftab Jun 24 '22 at 12:10

0 Answers0