Strange behavior of the mlogit effects command in R

I am evaluating a multicomponent logit model and would like to report the marginal effect. I am running into difficulties because when I use a large version of the model I get an error.

Here's an example of reproducibility. The following code with two covariates works great.

library(mlogit)

df = data.frame(c(0,1,1,2,0,1,0), c(1,6,7,4,2,2,1), c(683,276,756,487,776,100,982))
colnames(df) <- c('y', 'col1', 'col3')
df$col2<-df$col1^2
mydata = df

mldata <- mlogit.data(mydata, choice="y", shape="wide")
mlogit.model1 <- mlogit(y ~ 1| col1+col2, data=mldata)
m <- mlogit(y ~ 1| col1+col2, data = mldata)
z <- with(mldata, data.frame(col1 = tapply(col1, index(m)$alt, mean), 
                             col2 = tapply(col2, index(m)$alt, mean) ) )
effects(mlogit.model1, covariate = "col1", data = z)

      

Now that I have three covariates:

mlogit.model1 <- mlogit(y ~ 1| col1+col2+col3, data=mldata)
m <- mlogit(y ~ 1| col1+col2+col3, data = mldata)
z <- with(mldata, data.frame(col1 = tapply(col1, index(m)$alt, mean), 
                             col2 = tapply(col2, index(m)$alt, mean), 
                             col3 = tapply(col3, index(m)$alt, mean) ) )
effects(mlogit.model1, covariate = "col1", data = z)

      

The last line displays the following error:

Error in if (rhs% in% c (1, 3)) {: argument has length 0

But if I run

effects(mlogit.model1, covariate = "col3", data = z)

      

then it works fine to provide marginal effects col3

. Why not give marginal effects col1

?

Note that all columns do not contain NULL

and are the same length. Can anyone explain the reason for this behavior?

+3


source to share


1 answer


I believe this will help you find a solution.

Link: http://www.talkstats.com/showthread.php/44314-calculate-marginal-effects-using-mlogit-package

> methods(effects)
[1] effects.glm*    effects.lm*     effects.mlogit*
see '?methods' for accessing help and source code 
Note: Non-visible functions are asterisked

      

Explanation:

Little conversion is needed in the effects.mlogit source code.



On line 16, you must replace "cov.list <- lapply (attr (formula (object)," rhs "), as.character)" with "cov.list <- strsplit (as.character (attr (formula (object) , "rhs")), "+", fixed = TRUE) "

Fix the result:

> effects(mlogit.model1, covariate = "col1", data = z)
            0             1             2 
-4.135459e-01  4.135459e-01  9.958986e-12 

> myeffects(mlogit.model2, covariate = "col1", data = z2)
           0            1            2 
 1.156729129 -1.157014778  0.000285649 

      

code

require(mlogit)

myeffects<-function (object, covariate = NULL, type = c("aa", "ar", "rr", 
                                                        "ra"), data = NULL, ...) 
{
  type <- match.arg(type)
  if (is.null(data)) {
    P <- predict(object, returnData = TRUE)
    data <- attr(P, "data")
    attr(P, "data") <- NULL
  }
  else P <- predict(object, data)
  newdata <- data
  J <- length(P)
  alt.levels <- names(P)
  pVar <- substr(type, 1, 1)
  xVar <- substr(type, 2, 2)
  cov.list <- strsplit(as.character(attr(formula(object), "rhs")), " + ", fixed = TRUE)
  rhs <- sapply(cov.list, function(x) length(na.omit(match(x, 
                                                           covariate))) > 0)
  rhs <- (1:length(cov.list))[rhs]
  eps <- 1e-05
  if (rhs %in% c(1, 3)) {
    if (rhs == 3) {
      theCoef <- paste(alt.levels, covariate, sep = ":")
      theCoef <- coef(object)[theCoef]
    }
    else theCoef <- coef(object)[covariate]
    me <- c()
    for (l in 1:J) {
      newdata[l, covariate] <- data[l, covariate] + eps
      newP <- predict(object, newdata)
      me <- rbind(me, (newP - P)/eps)
      newdata <- data
    }
    if (pVar == "r") 
      me <- t(t(me)/P)
    if (xVar == "r") 
      me <- me * matrix(rep(data[[covariate]], J), J)
    dimnames(me) <- list(alt.levels, alt.levels)
  }
  if (rhs == 2) {
    newdata[, covariate] <- data[, covariate] + eps
    newP <- predict(object, newdata)
    me <- (newP - P)/eps
    if (pVar == "r") 
      me <- me/P
    if (xVar == "r") 
      me <- me * data[[covariate]]
    names(me) <- alt.levels
  }
  me
}

df = data.frame(c(0,1,1,2,0,1,0), c(1,6,7,4,2,2,1), c(683,276,756,487,776,100,982))
colnames(df) <- c('y', 'col1', 'col3')
df$col2<-df$col1^2
mydata = df

mldata <- mlogit.data(mydata, choice="y", shape="wide")
mlogit.model1 <- mlogit(y ~ 1| col1+col2, data=mldata)
m <- mlogit(y ~ 1| col1+col2, data = mldata)
z <- with(mldata, data.frame(col1 = tapply(col1, index(m)$alt, mean), 
                             col2 = tapply(col2, index(m)$alt, mean) ) )

mldata2 <- mlogit.data(mydata, choice="y", shape="wide")
mlogit.model2 <- mlogit(y ~ 1| col1+col2+col3, data=mldata2)
m2 <- mlogit(y ~ 1| col1+col2+col3, data = mldata2)
z2 <- with(mldata, data.frame(col1 = tapply(col1, index(m2)$alt, mean), 
                             col2 = tapply(col2, index(m2)$alt, mean), 
                             col3 = tapply(col3, index(m2)$alt, mean) ) )

effects(mlogit.model1, covariate = "col1", data = z)
myeffects(mlogit.model2, covariate = "col1", data = z2)

      

+1


source







All Articles