marginal effects of mlogit in R

后端 未结 2 1223
情话喂你
情话喂你 2020-12-18 14:32

I am new to R, and I don\'t understand yet completely the logic of its calculations...

I cannot overcome my problem with the help from previous posts either.

2条回答
  •  一整个雨季
    2020-12-18 14:58

    Ok, seems like I have kind of solution with the help of:

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

    and

    How can I view the source code for a function?

    apparently it was enough to see for the:

    > getAnywhere(effects.mlogit)
    A single object matching ‘effects.mlogit’ was found
    It was found in the following places
      registered S3 method for effects from namespace mlogit
      namespace:mlogit
    with value
    
    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 <- lapply(attr(formula(object), "rhs"), as.character)
        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
    }
    
    

    Then to copy the function and modyfy its 16 line:

    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
    }
    

    Now the results are as of:

     > myeffects(mlogit.data2,covariate="RSVS",data=zz)
                2             0             1             4             5             6 
     3.612318e-03  5.368693e-04 -4.903995e-08 -5.382731e-03  1.238768e-03 -5.175053e-06
    

提交回复
热议问题