我对 R 很陌生,并且习惯了非常基本的应用程序。
现在我遇到了一个问题需要帮助:
我正在寻找一种方法聚类标准误 for an 有序逻辑回归 (my estimation is similar to this https://stats.idre.ucla.edu/r/dae/ordinal-logistic-regression/例子)
我已经尝试过了robcov and vcovCL他们给我类似的错误消息:
- eatCL(x, cluster = cluster, type = type, ...) 中的错误:数字
“cluster”和“estfun()”中的观测值不匹配
- u[ ii]
提前谢谢了!
编辑:
我发现了一些有关缺失值的更多信息,但这似乎不是问题 - 因为即使我使用它来解决它,它仍然存在this https://stackoverflow.com/questions/23313907/clustered-standard-errors-with-data-containing-nas答案,或者当使用没有 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)