Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

R mlogit() function: Error in if (abs(x - oldx) < ftol) { : missing value where TRUE/FALSE needed

Tags:

r

mlogit

I am having trouble with mlogit() function. I am trying to predict which variables in a given set are the most preferred amongst people who took our survey. I am trying to predict the optimal combination of variables to create the most preferred option. Basically, we are measuring "Name", "Logo Size", "Design", "Theme","Flavor", and "Color".

To do this, we have a large data set and are trying to run it through mlogit.data() and mlogit(), although we keep getting the same error:

Error in if (abs(x - oldx) < ftol) { : missing value where TRUE/FALSE needed

None of my data is negative or missing, so this is very confusing. My syntax is:

#Process data in mlogit.data()

data2 <- 
  mlogit.data(data=data, choice="Choice", 
              shape="long", varying=5:10, 
              alt.levels=paste("pos",1:3))

#Make character columns factors and "choice" column (the one we are 
#measuring) a numeric.

data2$Name <- as.factor(data2$Name)
data2$Logo.Size <- as.factor(data2$Logo.Size)
data2$Design <- as.factor(data2$Design)
data2$Theme <- as.factor(data2$Theme)
data2$Color <- as.factor(data2$Color)
data2$Choice <- as.numeric(as.character(data2$Choice))

##### RUN MODEL ##### 
m1 <- mlogit(Choice ~ 0 + Name + Logo.Size + Design + Theme + Flavor 
+ Color, data = data2)

m1

Does it look like there is a problem with my syntax, or is it likely my data that is the problem?

like image 626
Andrew Colin Avatar asked Oct 17 '25 18:10

Andrew Colin


2 Answers

In a panel setting, it is potentially the case that one or more of your choice cards does not have a TRUE value. One fix would be to drop choice cards that are missing a choice.

## Use data.table
library(data.table)

## Drop choice cards that received no choice
data.table[, full := sum(Choice), by=Choice_id]
data.table.full <- data.table[full!=0,]

This is an issue specific to mlogit(). For example, STATA's mixed logit approach ignores missing response variables, R views this as more of an issue that needs to be addressed.

like image 175
bpar Avatar answered Oct 20 '25 08:10

bpar


I had similar issue, but eventually figured out. In my case, it is due to missing value of the covariates not the choice response.

like image 36
SLi Avatar answered Oct 20 '25 06:10

SLi



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!