如何在R中编写Bootstrap Probit模型的代码选择

时间:2014-09-19 18:28:38

标签: r regression feature-selection

这个问题涉及如何在具有边际效应的概率模型中编码变量选择(直接或通过调用一些预先存在的包)。

我对这些电影的免费和商业可用性影响进行了一点概率回归,以及与TLAPD相关的电影的盗版水平blog post

在R中运行probit的简单方法通常是glm,即:

probit <- glm(y ~ x1 + x2, data=data, family =binomial(link = "probit"))

但这对解释来说有问题,因为它不会产生边际效应。

通常,如果我想要从概率回归中得到边际效应,我会定义这个函数(我不记得原始来源,但它是一个很受欢迎的函数,可以重新发布):

mfxboot <- function(modform,dist,data,boot=500,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)
}

然后像这样运行回归:

mfxboot(modform = "y ~ x1 + x2",
        dist = "probit",
        data = piracy)

但是使用这种方法我不知道我可以运行任何变量选择算法,如前进,后退,逐步等。

解决此问题的最佳方法是什么?是否有更好的方法在R中运行问题报告边际效应并允许自动模型选择?或者我应该专注于使用mfxboot并使用该函数进行变量选择吗?

谢谢!

1 个答案:

答案 0 :(得分:1)

目前尚不清楚为什么会出现问题。模型(变量)选择和计算给定模型的边际效应是连续的,没有理由尝试将两者结合起来。

以下是在模型(变量)选择后计算边际效应及其自举标准效果的方法:

  1. 使用您首选的模型选择程序执行变量选择(包括下面讨论的引导模型选择技术,与您将用于计算边际标准误差的引导程序混淆对最终模型的影响)。

    以下是this question中提供的数据集的示例。另请注意,这是以 no 方式认可逐步变量选择方法的使用。

  2. #================================================
    # read in data, and perform variable selection for
    #   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)
    
    # perform model (variable) selection
    glmStepE = step(object = glmE)
    
    1. 现在将选定的模型传递给计算边际效应的函数。
    2. #================================================
      # 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(glmStepE)
      

      这是输出:

      > fnMargEffBin(glmStepE)
           genre    filiere 
      0.06951617 0.04571239
      
      1. 要获得边际效应的标准误差,您可以使用Boot函数中的car函数来引导边际效应,因为它为引导程序提供了这样一个简洁的接口来自glm估算的派生统计数据。
      2. #================================================
        # compute bootstrap std. err. for the marginal effects
        #================================================
        margEffBootE = Boot(object = glmStepE, f = fnMargEffBin, 
             labels = names(coef(glmE))[-1], R = 100)
        summary(margEffBootE)
        

        这是输出:

        > summary(margEffBootE)
                  R original  bootBias   bootSE  bootMed
        genre   100 0.069516 0.0049706 0.045032 0.065125
        filiere 100 0.045712 0.0013197 0.011714 0.048900
        

        附录:

        作为理论上的兴趣,有两种方法可以解释你的引导变量选择问题。

        1. 您可以使用自举模型拟合标准作为拟合度量来执行模型选择(变量选择)。 Shao (1996)概述了这一理论,并要求采用子采样方法 然后,您可以根据上面选择的最佳模型计算边际效应及其引导标准误差。

        2. 您可以对多个引导程序样本执行变量选择,并通过查看多个引导程序模型选择中保留的变量来获得任一个最佳模型,或使用模型平均估计器。 Austin and Tu (2004)中讨论了这一理论 然后,您可以根据上面选择的最佳模型计算边际效应及其引导标准误差。