带有集群标准错误的probit - 相当于Stata命令

时间:2015-03-06 22:30:45

标签: r stata logistic-regression standard-error

我在Stata中有以下probit命令,并在R中查找等效代码:

probit mediation viol ethniccomp  lncrisisdur  lncapratio  lnten_mean durable_avg neighbors totaldem_nbrhd geostr medprev jointdem if newcrisis==1, cluster(crisno)

我能够复制系数的估计结果,但不是更正的标准误差(聚集的)

probit.3.1_1 <- glm(mediation ~           viol+ethniccomp+lncrisisdur+lncapratio+lnten_mean+durable_avg+neighbors+
                    totaldem_nbrhd+geostr+medprev+jointdem,
                    data=as.data.frame(basedata[basedata$newcrisis==1,]), family=binomial (link=probit)) 

我基本上在R中寻找Stata选项cluster(crisno)中的等价物。

我见过这个reply,但据我所知,建议的解决方案仅指logit,而不是probit。

1 个答案:

答案 0 :(得分:1)

我不知道分析解决方案,因此我将使用boot包中的boot在R中使用块引导程序。

这是我将基准测试的Stata代码。

cd "C:\Users\Richard\Desktop\"
use "http://www.ats.ucla.edu/stat/stata/dae/binary.dta", clear
generate group = int((_n - 1) / 20) + 1
probit admit gpa gre, vce(cluster group)
outsheet using "binary.txt", replace

这里是R中的等价物。第二个块提供group上的块引导,这是我在Stata中制作的随机聚类变量。

setwd("C:/Users/Richard/Desktop/")
df <- read.delim("binary.txt")

# homoskedastic
probit <- glm(admit ~ gpa + gre, data=df, family=binomial(link=probit)) 

# with block bootstrap using `boot` package
library(boot)
myProbit <- function(x, y) {
    myDf <- do.call("rbind", lapply(y, function(n) subset(df, group == x[n])))
    myModel <- glm(admit ~ gpa + gre, data=myDf, family=binomial(link=probit))
    coefficients(myModel)
}
groups <- unique(df$group)
probitBS <- boot(groups, myProbit, 500)

# comparison
summary(probit)
probitBS

它们非常接近(Stata结果后跟R块引导结果)。

Probit regression                                 Number of obs   =        400
                                                  Wald chi2(2)    =      24.03
                                                  Prob > chi2     =     0.0000
Log pseudolikelihood =   -240.094                 Pseudo R2       =     0.0396

                                 (Std. Err. adjusted for 20 clusters in group)
------------------------------------------------------------------------------
             |               Robust
       admit |      Coef.   Std. Err.      z    P>|z|     [95% Conf. Interval]
-------------+----------------------------------------------------------------
         gpa |    .454575   .1531717     2.97   0.003     .1543641    .7547859
         gre |   .0016425   .0006404     2.56   0.010     .0003873    .0028977
       _cons |  -3.003536    .520864    -5.77   0.000    -4.024411   -1.982662
------------------------------------------------------------------------------

> probitBS

ORDINARY NONPARAMETRIC BOOTSTRAP


Call:
boot(data = groups, statistic = myProbit, R = 500)


Bootstrap Statistics :
        original        bias     std. error
t1* -3.003535745 -3.976856e-02 0.5420935780
t2*  0.454574799  3.781773e-03 0.1530609943
t3*  0.001642537  4.200797e-05 0.0006210689