1

I am trying to estimate a multinomial logit model with the mlogit package. I don't know how to deal with variables that don't apply to all alternatives. For example, I have four transportation modes (walk, bike, public transport and car) and the variable "cost" applies only to the car and public transport. When I import the csv file into R and try to estimate the model I become this error message:

Error in reshapeLong(data, idvar = idvar, timevar = timevar, varying = varying,  : 
  'varying' arguments must be the same length

How can I make R understand that the cost variable describes only two of the alternatives and not all of them?

amonk
  • 1,769
  • 2
  • 18
  • 27
sbb2018
  • 11
  • 3
  • 1
    Fill in 0's for the the ones it doesn't apply to. – Gregor Thomas Jan 24 '18 at 22:31
  • That would work I guess for the cost variable, but I also have categorical variables like comfort with two levels: 1 class (coded with 1) and 2 class (coded with 0). This variable describes only the airplane and the train but not to the car. If I fill in 0's for the car that would mean that the car has comfort of the 2. class which is not exactly correct since the car has only one level for comfort. – sbb2018 Jan 24 '18 at 22:52
  • Make it a 3-level factor - 1 class, 2 class, unapplicable. – Gregor Thomas Jan 25 '18 at 02:44

1 Answers1

1

Use constPar=c("bike:cost", "walk:cost") to treat cost as a constant variable for bike and walk.

Check the example on page 31 on the mlogit package.

M_M
  • 899
  • 8
  • 21
  • I tried this but it didn't work. The error message comes when I try to reshape the data which is in the wide format. The input table does not contain the variable cost for the alternatives "foot" and "bike". Probably I need to add two columns "cost:foot" and "cost:bike" and fill in 0's. In this case the cost variable is defined for all alternatives and the program can reshape the data. This method wouldn't work for categorical variables. – sbb2018 Jan 26 '18 at 12:15
  • Yes - add two columns "cost:foot" and "cost:bike" and fill in 0's. Can you provide a reproducible example? – M_M Jan 26 '18 at 17:58
  • I added the columns for the foot and bike alternatives and filled in 0's. The code worked properly. The problem is that I also have categorical variables like comfort which I don't know how to model. The only way that sounds reasonable to me is the one mentioned above - to make a 3-level factor (unapplicable, 2st class and 2nd class). I am still not sure if that is the right way to model categorical variable. – sbb2018 Jan 26 '18 at 20:51
  • Is the comfort variable = 0 for all cases of unapplicable modes? and =1 for all cases of applicable modes? Or some individuals have it as 1 and some others have it as 2? If it is the latter, you probably need to code it as two variables: comfort1 comfort2 (0 inapplicable or unavailable and 1 if available). – M_M Jan 29 '18 at 22:31
  • You are more likely to get better help if you provide a sample dataset and a reproducible example. – M_M Jan 29 '18 at 22:32