r - 有序 logit R polr 的聚类标准误差 - 估计中删除的值

标签 r regression logistic-regression

我对 R 很陌生,并且习惯了非常基本的应用程序。 现在我遇到了一个问题需要帮助:

我正在寻找一种方法来为有序逻辑回归聚类标准误差(我的估计类似于 this 示例)

我已经尝试过 robcovvcovCL,它们给了我类似的错误消息:

  • meatCL(x, cluster = cluster, type = type, ...) 中的错误:数字 “cluster”和“estfun()”中的观测值不匹配
  • u[, ii] 中的错误<- ui:要替换的项目数不是替换长度的倍数

非常感谢!

编辑: 我找到了一些有关缺失值的更多信息,但这似乎不是问题 - 因为即使我使用 this 解决它,它仍然存在。答案,或者当使用没有 NA 的数据集时。就像下面的例子一样。问题似乎是 polr 没有将残差作为输出的一部分提供给我。我该如何解决这个问题?

 dat <- read.dta("https://stats.idre.ucla.edu/stat/data/ologit.dta")
    length(dat$apply)
    twenty <- seq(from=1, to=20, by=1)
    dat$clustervar<-sample(twenty, size=400, replace=TRUE)



    m <- polr(apply ~ pared + public + gpa, data = dat, Hess=TRUE)
    vcovCL <- function(x, cluster.by, type="sss", dfcw=1){
      # R-codes (www.r-project.org) for computing
      # clustered-standard errors. Mahmood Arai, Jan 26, 2008.

      # The arguments of the function are:
      # fitted model, cluster1 and cluster2
      # You need to install libraries `sandwich' and `lmtest'

      # reweighting the var-cov matrix for the within model
      require(sandwich)
      cluster <- cluster.by
      M <- length(unique(cluster))
      N <- length(cluster)
      stopifnot(N == length(x$residuals))
      K <- x$rank
      ##only Stata small-sample correction supported right now
      ##see plm >= 1.5-4
      stopifnot(type=="sss")
      if(type=="sss"){
        dfc <- (M/(M-1))*((N-1)/(N-K))
      }
      uj  <- apply(estfun(x), 2, function(y) tapply(y, cluster, sum))
      mycov <- dfc * sandwich(x, meat=crossprod(uj)/N) * dfcw
      return(mycov)
    }
    vcovCL(dat, m, dat$clustervar)

这给了我:

Error: N == length(x$residuals) is not TRUE
Called from: vcovCL(dat, m, dat$clustervar)

最佳答案

我在 ?sandwich::vcovCL 的帮助页面上取得了成功,该页面显示该函数的第一个参数是模型对象。需要使用 :: 运算符来屏蔽您提供的函数:

 m <- polr(apply ~ pared + public + gpa, data = dat, Hess=TRUE)
 ( clval <- sandwich::vcovCL(m, dat$clustervar) )
                                  pared       public         gpa unlikely|somewhat likely
pared                       0.085218306  0.005588259  0.04584255               0.15545404
public                      0.005588259  0.092283173 -0.01890725              -0.05875859
gpa                         0.045842552 -0.018907254  0.07067573               0.22455931
unlikely|somewhat likely    0.155454041 -0.058758588  0.22455931               0.72408670
somewhat likely|very likely 0.165079639 -0.058282514  0.23631756               0.75713049
                            somewhat likely|very likely
pared                                        0.16507964
public                                      -0.05828251
gpa                                          0.23631756
unlikely|somewhat likely                     0.75713049
somewhat likely|very likely                  0.80749182

如果您想要 Wald 测试,您可能需要使用该矩阵的 diag。我认为这就是 coeftest 将提供的:

coeftest( m, vcov = clval)

t test of coefficients:

        Estimate Std. Error t value  Pr(>|t|)    
pared   1.047690   0.291922  3.5889 0.0003738 ***
public -0.058786   0.303781 -0.1935 0.8466565    
gpa     0.615941   0.265849  2.3169 0.0210210 *  
---
Signif. codes:  0 ‘***’ 0.001 ‘**’ 0.01 ‘*’ 0.05 ‘.’ 0.1 ‘ ’ 1

促使 Achim Zeileis 成功搜索 Rhelp 并找到答案的另一个问题是 here

关于r - 有序 logit R polr 的聚类标准误差 - 估计中删除的值,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/47798401/

相关文章:

r - 如何在一个 ggplot2 图中为两个 geom 图层添加图例?

python - 高斯过程回归的内核参数 : How to get them in Scikit-learn?

python - 如何在 python 中执行逻辑套索?

python - 如何高效地将数据从 R 上传到 SQL 数据库(Snowflake)?

r - 如何使用 purrr :map function? 使用动态变量改变多列

python - LASSO 中的单热编码分类变量,如何比较变量重要性?

python - 逻辑回归统计模型概率预测的置信区间

python - 如何在Python中找到实际的逻辑回归模型?

r - 在 R Shiny 服务器上保存临时文件

statistics - 如何通过多元回归获得 WinBUGS 中的多项概率