atahk / pscl

Political Science Computational Laboratory
65 stars 14 forks source link

Error in `vcov.zeroinfl` when 1 x 1 matrix is the result #16

Closed rvlenth closed 1 year ago

rvlenth commented 1 year ago

I ran into an issue with the vcov method for zeroinfl objects. Here is a reproducible example (the 1st example for zeroinfl())

require(pscl)
## ... messages deleted ...

data("bioChemists", package = "pscl")
fm_zip <- zeroinfl(art ~ . | 1, data = bioChemists)
vcov(fm_zip, "zero")
## Error in `rownames<-`(`*tmp*`, value = names(cf)): attempt to set 'rownames' on an object with no dimensions

Created on 2023-01-13 with reprex v2.0.2

The problem is that we subset the entire covariance matrix; and if a 1x1 matrix is the result, then it drops to a simple numeric value rather than a matrix. So you need to add drop = FALSE in a couple of places. The code, with my annotations added, is

> pscl:::vcov.zeroinfl
function (object, model = c("full", "count", "zero"), ...) 
{
    model <- match.arg(model)
    rval <- object$vcov
    if (model == "full") 
        return(rval)
    cf <- object$coefficients[[model]]
    wi <- seq(along = object$coefficients$count)
    rval <- if (model == "count") 
        rval[wi, wi]         ### should be rval[wi, wi, drop = FALSE]
    else rval[-wi, -wi]      ### should be rval[-wi, -wi, drop = FALSE]
    colnames(rval) <- rownames(rval) <- names(cf)
    return(rval)
}
<bytecode: 0x000001e6efea00d8>
<environment: namespace:pscl>

I took a look at vcov.hurdle and it appears to have the same vulnerability.

zmeers commented 1 year ago

Thank you! This has been solved with the most recent push to the master branch.