-1

I am going to use mlogit package. I run it like example but it still gives me an error and I don't know what to do about it.

data72 = mlogit.data(data, choice ="Hour72motor", shape ="long", ,alt.levels="mode")
simple = mlogit(Hour72motor ~ Hemisphere +Lesionlocation + gender + Age+ DoesHematoma+study, data=data72)

The error is:

Error in contrasts<-(tmp, value = contr.funs[1 + isOF[nn]]) : contrasts can be applied only to factors with 2 or more levels

Thank you for your support.

I add the comment which is recommended; to examine the counts of non-numeric variables in the model

testdat <- na.omit(data72[c("Hour72motor", "Hemisphere", "Lesionlocation", 
      "gender","Age", "DoesHematoma", "study")]) ; 

lapply(testdat[!sapply(testdat, is.numeric)], table)
# $Hour72motor
# FALSE  TRUE 
#   128    16 
user20650
  • 24,654
  • 5
  • 56
  • 91
Maryam Gh
  • 39
  • 6
  • can you include the first few rows of `data72`, at least the columns you are using in your model? It looks like you have a factor with only one level, but you need to do something like `lapply(data72, function(x) length(unique(x))` to check – C8H10N4O2 Jul 08 '15 at 00:42
  • [how to make a great R reproducible example](http://stackoverflow.com/questions/5963269/how-to-make-a-great-r-reproducible-example) – C8H10N4O2 Jul 08 '15 at 00:43

1 Answers1

1

In my experience, that error message usually means that the data object you're trying to use at the modeling stage is empty or corrupted, or the variable type you're using as the y in your model isn't the type it needs to be. Have you inspected the data frame to make sure your first line of code produced what you wanted it to produce? Try:

str(data72)
table(data72$Hour72motor)

Does it have the number of columns and rows you expected? Does your model target (Hour72motor) take the values you expected? If no to either one, try tinkering with the first line to make sure you pull the data you need in the required format.

ulfelder
  • 5,305
  • 1
  • 22
  • 40
  • Thanks for your quick response: – Maryam Gh Jul 07 '15 at 21:54
  • I did what you said but nothing wrong: – Maryam Gh Jul 07 '15 at 21:55
  • Do you have any suggestion? – Maryam Gh Jul 07 '15 at 21:56
  • More than likely one of the variables in your model has only one levels - remember any rows with missing data will be removed. Try `testdat <- na.omit(data72[c("Hour72motor", "Hemisphere", "Lesionlocation", "gender","Age", "DoesHematoma", "study")]) ; lapply(testdat[!sapply(testdat, is.numeric)], table)` – user20650 Jul 07 '15 at 22:06
  • Thank you, but it still gives me same answer with table(data72$Hour72motor) – Maryam Gh Jul 07 '15 at 22:16
  • From my understanding if there is missing or one level response it should give me different answer, right? – Maryam Gh Jul 07 '15 at 22:18
  • Can you edit your question with the results from the above commands please : you can do this by posting the results of `dput(lapply(testdat[!sapply(testdat, is.numeric)], table))` – user20650 Jul 07 '15 at 22:47
  • testdat <- na.omit(data72[c("Hour72motor", "Hemisphere", "Lesionlocation", "gender","Age", "DoesHematoma", "study")]) ; lapply(testdat[!sapply(testdat, is.numeric)], table) $Hour72motor FALSE TRUE 128 16 > dput(lapply(testdat[!sapply(testdat, is.numeric)], table)) structure(list(Hour72motor = structure(c(128L, 16L), .Dim = 2L, .Dimnames = structure(list( c("FALSE", "TRUE")), .Names = ""), class = "table")), .Names = "Hour72motor") – Maryam Gh Jul 07 '15 at 23:32
  • Thank you. ok i m not sure, but i suspect that separation may be the issue, as there are only 16 cases. Try running the model with one predictor at a time, and gradually build up the model, to see when it starts throwing an error. [Aside: I would say just from looking at the names of the predictors in your model, it is surprising that they are all numeric] – user20650 Jul 07 '15 at 23:44
  • Thank you @user20650. I changed my data set in different way and it finally worked – Maryam Gh Jul 09 '15 at 21:34
  • Great stuff Maryam, I dont think i helped much, but glad you got it working – user20650 Jul 09 '15 at 22:06