I am quite new to R and used to pretty basic application. Now I have encountered a problem I need help with:
I am looking for a way to cluster standard errors for an ordered logistic regression (my estimation is similar to this example)
I already tried robcov and vcovCL and they give me similar error messages:
- Error in meatCL(x, cluster = cluster, type = type, ...) : number of observations in 'cluster' and 'estfun()' do not match
- Error in u[, ii] <- ui : number of items to replace is not a multiple of replacement length
Many thanks in advance!
Edit: I found some more information about the missing values but that does not seem to be the problem - because it persists even if I work around it using this answer, or when use a dataset without NA's. Just as in the example below. The problem seems to be that polr does not give me the residuals as part of the output. How can I work around this?
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)
This gives me:
Error: N == length(x$residuals) is not TRUE
Called from: vcovCL(dat, m, dat$clustervar)