I am trying to run a nested logit using mlogit in R to analyze data from choices people made. There are 4 possible alternatives they could choose from, but in any given choice situation a person had to choice between only 2 of the 4 total types. I'm having trouble getting my data in the right format to run the nested logit.

My data is currently in long format, where each row is one choice alternative with the following fields (chid is the trial index for that choice, type can be A, B, C, or D, chosen is the DV, order is 1 or 2 for what option in what is the list, person is the unique person ID, and alternative is the other option available for that trial):


The problem I seem to be having is how to format the data and how to create the nest list. I want to be able to look at how the types compare in getting chosen, but conditioned on what the alternative choice was available for each trial.

I've tried variations of this command, but I don't know what to define as my list of nests:

nested.logit <- mlogit(chosen ~ type + order, my.data, shape='long', alt.var='type', 

The nest I want to use is the alternative column, but I'm not sure this is the right way to format my data. I get an error:

'Error in `row.names<-.data.frame`(`*tmp*`, value  
 ... duplicate row.names are not allowed error

Can anyone help me figure out how to format my data to run the nested logit?

PS - I am not using mlogit.data to format my data, because I have gotten errors:

>mydata <- mlogit.data(data, choice="chosen",shape="long",id.var="chid",alt.var="type")
Error in `row.names<-.data.frame`(`*tmp*`, value = c("1.A", "1.B",  : 

duplicate 'row.names' are not allowed

From other posts (ie https://stackoverflow.com/questions/9355535/how-should-i-format-my-data-for-the-r-mlogit-package) I've gotten the impression that what mlogit data is made for is alternative sets that stay exactly the same for each choice. I stopped trying to use the command because my alternative sets change (since each choice only presents 2 of the 4 options).

When I run the following on my data (as presented above), I get the same error:

>alternative.list <- c("_A", "_B", "_C", "_D")
>nested.logit <- mlogit(chosen ~ type + order, data, shape='long', alt.var='type', nests=list(alternative.list))
Error in `row.names<-.data.frame`(`*tmp*`, value = c("1.A", "1.B") : duplicate 'row.names' are not allowed

All the examples I've seen have used nests that are more straightforward, such as where two types are always in one nest, two other types are always in a second nest, etc, which makes me wonder if I can use mlogit at all for what I have. I've looked at clogit in the survival package, but again my data doesn't map exactly to what its used for in the examples.

Thanks ...

  • $\begingroup$ Have you set up the data using the mlogit.data command? And do you have a MWE for me to debug? $\endgroup$ – gregmacfarlane Jun 19 '13 at 15:27
  • $\begingroup$ I have not used mlogit on my data, because I get an error: $\endgroup$ – user27040 Jun 20 '13 at 0:59
  • $\begingroup$ The first step is to change your data.frame into an mlogit object with the mlogit.data() function. Your question is very similar to stackoverflow.com/questions/5906272/… $\endgroup$ – gregmacfarlane Jun 20 '13 at 13:59

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Browse other questions tagged or ask your own question.