【问题标题】:mfxboot function for marginal effects for probit regressions?mfxboot 函数用于概率回归的边际效应?
【发布时间】:2014-12-16 20:44:49
【问题描述】:

数据:Data

代码:

#function that calculates ‘the average of the sample marginal effects’.
mfxboot <- function(modform,dist,data,boot=1000,digits=3){
x <- glm(modform, family=binomial(link=dist),data)
# get marginal effects
pdf <- ifelse(dist=="probit",
            mean(dnorm(predict(x, type = "link"))),
            mean(dlogis(predict(x, type = "link"))))
marginal.effects <- pdf*coef(x)
# start bootstrap
bootvals <- matrix(rep(NA,boot*length(coef(x))), nrow=boot)
set.seed(1111)
for(i in 1:boot){
samp1 <- data[sample(1:dim(data)[1],replace=T,dim(data)[1]),]
x1 <- glm(modform, family=binomial(link=dist),samp1)
pdf1 <- ifelse(dist=="probit",
               mean(dnorm(predict(x, type = "link"))),
               mean(dlogis(predict(x, type = "link"))))
bootvals[i,] <- pdf1*coef(x1)
}
res <- cbind(marginal.effects,apply(bootvals,2,sd),marginal.effects/apply(bootvals,2,sd))
if(names(x$coefficients[1])=="(Intercept)"){
res1 <- res[2:nrow(res),]
res2 <-   matrix(as.numeric(sprintf(paste("%.",paste(digits,"f",sep=""),sep=""),res1)),nrow=dim(res1)[1])    
rownames(res2) <- rownames(res1)
} else {
res2 <- matrix(as.numeric(sprintf(paste("%.",paste(digits,"f",sep=""),sep="")),nrow=dim(res)[1]))
rownames(res2) <- rownames(res)
}
colnames(res2) <- c("marginal.effect","standard.error","z.ratio") 
return(res2)
}

## Regression
probit_enae = glm(emploi ~ genre + filiere + satisfaction + competence + anglais, family=binomial(link="probit"),
              data=ENAE_Probit.df)
summary(probit_enae) #Summary output of the regression
confint(probit_enae) #Gives the 95% confidence interval for the estimated coefficients

## Running the mfxboot for Marginal effects
mfx_enae = mfxboot(emploi ~ genre + filiere + satisfaction + competence + anglais,"probit",ENAE_Probit.df)

问题:

当我运行 mfxboot 函数时,我收到以下错误消息:

bootvals[i, ]

知道为什么会这样吗?以及如何解决这个问题的任何建议?

谢谢。

【问题讨论】:

    标签: r marginal-effects


    【解决方案1】:

    我无法重现您的错误。也许您应该将sessionInfo() 输出添加到您的问题中。尽管如此,下面还是对mfxboot 函数的建议增强。


    我的建议是将mfxboot 函数重构为两个函数——一个 返回给定 glm 对象的边际效应,第二个是 引导它。

    您可以使用 car 包中的 Boot 函数轻松完成此操作,因为 这是一个很好的引导glm 对象的前端。

    这里有一些代码演示了这个过程,阅读起来更清晰:

    第 1 步:估计概率模型

    library(car)
    
    #================================================
    # read in data, and estimate a probit model
    #================================================
    dfE = read.csv("ENAE_Probit.csv")
    formE = emploi ~ genre + 
      filiere + satisfaction + competence + anglais
    glmE = glm(formula = formE, 
               family = binomial(link = "probit"),
               data = dfE)
    

    第 2 步:编写一个返回边际效应的函数

    以下函数将binomial 系列的glm 对象作为输入,并计算logit 和probit 链接的适当边际效应。

    #================================================
    # function: compute marginal effects for logit and probit models
    # NOTE: this assumes that an intercept has been included by default
    #================================================
    fnMargEffBin = function(objBinGLM) {
      stopifnot(objBinGLM$family$family == "binomial")
      vMargEff = switch(objBinGLM$family$link, 
                        probit = colMeans(outer(dnorm(predict(objBinGLM, 
                                                             type = "link")),
                                               coef(objBinGLM))[, -1]),
                        logit = colMeans(outer(dlogis(predict(objBinGLM, 
                                                            type = "link")),
                                              coef(objBinGLM))[, -1])
      )
      return(vMargEff)
    }
    
    # test the function
    fnMargEffBin(glmE)
    

    第 3 步:引导边际效应

    以下代码使用 car 包中的 Boot 函数来引导边际效应。请注意Boot 的接口如何针对从lmglm 对象派生的统计信息进行优化。

    #================================================
    # compute bootstrap std. err. for the marginal effects
    #================================================
    margEffBootE = Boot(object = glmE, f = fnMargEffBin, 
         labels = names(coef(glmE))[-1], R = 100)
    summary(margEffBootE)
    

    这是输出:

    > summary(margEffBootE)
                   R  original    bootBias   bootSE   bootMed
    genre        100  0.070733  0.00654606 0.042162  0.074563
    filiere      100  0.043173  0.00060356 0.014064  0.043486
    satisfaction 100  0.050773 -0.00110501 0.037737  0.048310
    competence   100 -0.020144  0.00407027 0.034194 -0.014987
    anglais      100 -0.018906 -0.00170887 0.033522 -0.019164
    

    【讨论】:

      猜你喜欢
      • 2011-08-13
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2019-11-16
      • 2019-07-15
      • 2020-06-07
      • 2021-05-03
      • 2019-03-14
      相关资源
      最近更新 更多