0

I am estimating a multinomial logit using mlogit in r. Is there a way to use a list of variables in the mlogit function, instead of enumerating all the variables in it? Here is a reproducible example. My question is how can I avoid typing AGE + SEX in the mlogit function?

zz <- "Choice AGE SEX
        1  42   2
        1   8   1
        1  37   1
        1   8   2
        1   7   2
        3  50   1
        3  69   1
        3  63   2
        3  37   2
        3  37   1
        3   9   1
        3   6   2
        3  32   2
        3  42   1
        2  62   1
        2  60   1
        2  68   2
        2  69   1
        2  60   2
        2  45   1"

data <- read.table(text = zz, header = TRUE)
Model <- mlogit(Choice ~ 0 | AGE + SEX | 0  , data, shape ="wide", choice = "Choice")
Milad
  • 339
  • 2
  • 12
  • It does seem that mlogit uses a non-standard formula evaluation. So while using a dot would work for most other regression models, it will not work for mlogit. – Milad Jul 30 '14 at 00:50

1 Answers1

1

OK. So it seems that mlogit has it's own custom formula format that's based on the Formula package which seems to make it difficult to use the standard . symbol you can usually use in formulas to specify "all the other variables." I did find a messy way to expand these special formulas. Here's the helper function

expandFormula <- function(object, data) {
    object<-Formula(object)
    nrhs<-lapply(sapply(lapply(attr(object, "rhs"), function(x)
        eval(as.call(
            list(quote(`~`), 
            attr(object, "lhs")[[1]], 
            x)
         )))), terms, data=data, simplify=T), 
    `[[`, 3)
    attr(object, "rhs")<-nrhs
    object
}

Now this function isn't very defensive as far as error checking goes. It assumes one response and allows for multiple "right hand sides" and assumes that no part of the formula is empty. We would use this function on your data with

ff <- Choice ~ 0 | . | 0 
Model <- mlogit(expandFormula(ff, data) , data, shape ="wide", choice = "Choice")

And this would normally be the preferred way to work with formulas. However, since this function doesn't play that nicely, you can also build formulas as strings. You could do

resp <- names(data)[1]
covar <- names(data)[-1]
ff <- as.formula(paste(resp, "~", "0","|", paste(covar, collapse="+"),"|","0"))
Model <- mlogit(ff, data, shape ="wide", choice = "Choice")

Which will end up running the same model.

MrFlick
  • 195,160
  • 17
  • 277
  • 295
  • Honestly I did not check the first suggestion. Actually I was looking for something like what you offered using paste function. I implemented it and it works properly. Thank you very much. – Milad Jul 30 '14 at 07:10