I am using the mlogit package with R.
After importing my data using:
t <-read.csv('junk.csv',header=TRUE, sep=",", dec=".")
and call:
x <- mlogit.data(t,choice="D",shape="long",id.var="key",alt.var="altkey")
I am getting the following error:
Error in `row.names<-.data.frame`(`*tmp*`, value = c("1.1", "1.2", "1.3", :
duplicate 'row.names' are not allowed
In addition: Warning message:
non-unique values when setting 'row.names': ‘1.1’, ‘1.2’, ‘1.3’, ‘1.4’, ‘1.5’, ‘1.6’
Any ideas how to fix it?
My data exist in the following format in a csv file:
[junk.csv]
key,altkey,A,B,C,D
201005131,1,2.6,118.17,117,0
201005131,2,1.4,117.11,115,0
201005131,3,1.1,117.38,122,1
201005131,4,24.6,,122,0
201005131,5,48.6,91.90,122,0
201005131,6,59.8,,122,0
201005132,1,20.2,118.23,113,0
201005132,2,2.5,123.67,120,1
201005132,3,7.4,116.30,120,0
201005132,4,2.8,118.86,120,0
201005132,5,6.9,124.72,120,0
201005132,6,2.5,123.81,120,0
201005132,7,8.5,119.23,115,
My experience of
mlogitis that it isn’t very forgiving about data that isn’t exactly the way it should be.In your case, I notice that the first respondent has 6 alternatives, while the second respondent has 7 alternatives. If you format your data to have an equal number of alternatives for each respondent the
mlogit.datafunction works:Running
mlogiton all of the data reproduces the error:However, removing line number 13, i.e. the 7th alternative, works:
Of course, this isn’t very satisfactory, since it destroys some of the data. A better solution is to construct the data in a format that
mlogit()expects, and then callmlogit()directly:Now the data looks like this:
And you can call
mlogit()directly:Result: