Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

R error: Error in `row.names<-.data.frame`(`*tmp*`, value = value)

I just make up a data set to test the function "mlogit" which stands for "multinomial logistic regression model"

The data is simply:

head(dat)
   y x1 x2 x3
 1 4  1 18  4
 2 5  1 20  5
 3 2  1 25  3
 4 3  0 26  6
 5 4  0 26  8
 6 3  1 27  4

Then when I type

fit <- mlogit(y ~ x1 + x2 + x3, data=dat)

the following Message appears:

Error in `row.names<-.data.frame`(`*tmp*`, value = value) : 
  invalid 'row.names' length

Does anyone know why or how to solve it?

like image 645
Daniel Avatar asked Mar 20 '23 17:03

Daniel


1 Answers

The help states:

The ‘data’ argument may be an ordinary ‘data.frame’. In this case, some supplementary arguments should be provided and are passed to ‘mlogit.data’.

You have not given any supplementary arguments. Note that I consider this poor documentation because it does not state which supplementary arguments should be provided.

From the examples, it seems that "shape" and "choice" should at least be set:

 # a data.frame in wide format with two missing prices
 Fishing2 <- Fishing
 Fishing2[1, "price.pier"] <- Fishing2[3, "price.beach"] <- NA
 mlogit(mode~price+catch|income, Fishing2, shape="wide", choice="mode", varying = 2:9)

 # a data.frame in long format with three missing lines
 data("TravelMode", package = "AER")
 Tr2 <- TravelMode[-c(2, 7, 9),]
 mlogit(choice~wait+gcost|income+size, Tr2, shape = "long",
        chid.var = "individual", alt.var="mode", choice = "choice")

By the way, welcome to stackoverflow! Here are some tips on writing a better question and thus increasing the chance of a good answer.

  1. you should state the package from which your command comes. I'm assuming it is from the mlogit package, but the mlogit command is in every package.
  2. you should give a minimal example. You give the output of the head command, but it's not clear if the error can be reproduced with that. library(mlogit) should also be given in your minimal example.
  3. you should read the help for the command. Help files can be intimidating and very technical, but you don't have to understand everything in them. In your example, I'm guessing that some supplementary arguments should be provided would have jumped out at you. In case you're not sure how to access help for the command mlogit, you can use ?mlogit or help(mlogit).
like image 126
scottkosty Avatar answered Mar 31 '23 16:03

scottkosty