emmeans R package error: "Error in t(OO[[rr]]) : object 'unpackedMatrix_transpose' not found"

251 Views Asked by At

I am attempting to use the emmeans package in RStudio, which I have done successfully many times, but now it is giving me an error that I can't solve:

shannon_site_emm <- emmeans(shannon_site, factor(site))
Error in t(OO[[rr]]) : object 'unpackedMatrix_transpose' not found

Specifically, I am re-running a previously used script with an updated dataset, so I know the emmeans package worked before with this project, and the nature of the updates to my dataset aren't dramatic enough that they should be the issue here.

Has anyone come across this error before and know how to fix it?

What I did:

#fit a model:
shannon_site <- lmer(Shannon ~ site + (1|bird_name), data = alpha_metadata)

# inspected parameters:
summary(shannon_site)

Linear mixed model fit by REML. t-tests use Satterthwaite's method ['lmerModLmerTest']
Formula: Shannon ~ site + (1 | bird_name)
   Data: alpha_metadata

REML criterion at convergence: 889.7

Scaled residuals: 
    Min      1Q  Median      3Q     Max 
-2.4094 -0.6484  0.0143  0.5922  2.6900 

Random effects:
 Groups    Name        Variance Std.Dev.
 bird_name (Intercept) 0.1652   0.4065  
 Residual              1.2518   1.1188  
Number of obs: 281, groups:  bird_name, 142

**Fixed effects:
                  Estimate Std. Error       df t value Pr(>|t|)    
(Intercept)         3.4142     0.8894 150.4461   3.839 0.000182 ***
siteFt Lauderdale  -1.3439     1.0893 150.4461  -1.234 0.219234    
siteJDSP            0.3794     0.9889 162.3981   0.384 0.701773    
siteSFWC           -1.3423     0.9080 152.7540  -1.478 0.141394    
siteTTP            -0.4431     0.8937 150.0042  -0.496 0.620771    
---**
Signif. codes:  0 ‘***’ 0.001 ‘**’ 0.01 ‘*’ 0.05 ‘.’ 0.1 ‘ ’ 1

Correlation of Fixed Effects:
            (Intr) stFtLd stJDSP stSFWC
siteFtLdrdl -0.816                     
siteJDSP    -0.899  0.734              
siteSFWC    -0.980  0.800  0.881       
siteTTP     -0.995  0.813  0.895  0.975
3

There are 3 best solutions below

1
Russ Lenth On

I have seen this issue before in an issue report. It is not a problem in emmeans. It appears to be some glitch in the transition between R versions and the Matrix package support, possibly. I think if you reinstall some packages (maybe Matrix?) and restart R, the problem will go away. Sorry I can't be more specific, but I do have user-reported success.

0
Mikael Jagan On

Your question is more or less a duplicate of this GitHub issue. A package namespace loaded in your R process caches a method for generic function base::t from a version of Matrix different from the one that you have installed. The cached method is "stale" because it does not work correctly under the installed version of Matrix, in this case because the installed version of Matrix does not define the symbol unpackedMatrix_transpose.

The solution is to re-install from sources the package whose namespace caches the stale method. The updated cache will be fully compatible with the version of Matrix currently installed.

The culprit package is very likely phyloseq, hence:

install.packages("phyloseq", type = "source")

Why do I suspect phyloseq? For each package p in loadedNamespaces(), look for a symbol called .__T__t:base in asNamespace(p). If it exists, then it is an environment caching methods for base::t. For each such method f, test whether environment(f) is the Matrix namespace. If so, then package p caches methods for base::t exported by Matrix, hence ideally you would re-install it from sources whenever you update (or downdate) Matrix.

hasCachedMethods <- function(packages, generic, generic.from, methods.from) {
    ans <- logical(length(packages))
    names(ans) <- packages
    .__T__ <- paste0(".__T__", generic, ":", generic.from)
    ns.from <- asNamespace(methods.from)
    for (i in seq_along(packages)) {
        e <- asNamespace(packages[i])[[.__T__]]
        if (!is.environment(e))
            next
        for (s in names(e)) {
            if (identical(environment(e[[s]]), ns.from)) {
                ans[i] <- TRUE
                break
            }
        }
    }
    ans
}
> loadedNamespaces()
[1] "compiler"  "graphics"  "utils"     "grDevices" "stats"     "datasets" 
[7] "methods"   "base"
> loadNamespace("phyloseq")
<environment: namespace:phyloseq>
> (packages <- loadedNamespaces())
 [1] "methods"          "generics"         "utf8"             "graphics"        
 [5] "bitops"           "stringi"          "lattice"          "digest"          
 [9] "magrittr"         "grid"             "grDevices"        "iterators"       
[13] "foreach"          "plyr"             "jsonlite"         "Matrix"          
[17] "ape"              "GenomeInfoDb"     "survival"         "mgcv"            
[21] "fansi"            "scales"           "permute"          "Biostrings"      
[25] "codetools"        "ade4"             "cli"              "rlang"           
[29] "crayon"           "XVector"          "Biobase"          "splines"         
[33] "munsell"          "utils"            "vegan"            "stats"           
[37] "tools"            "base"             "parallel"         "reshape2"        
[41] "phyloseq"         "dplyr"            "colorspace"       "Rhdf5lib"        
[45] "ggplot2"          "GenomeInfoDbData" "multtest"         "BiocGenerics"    
[49] "vctrs"            "R6"               "stats4"           "lifecycle"       
[53] "rhdf5"            "zlibbioc"         "stringr"          "biomformat"      
[57] "S4Vectors"        "IRanges"          "MASS"             "cluster"         
[61] "pkgconfig"        "pillar"           "gtable"           "glue"            
[65] "data.table"       "Rcpp"             "tidyselect"       "tibble"          
[69] "rhdf5filters"     "datasets"         "igraph"           "nlme"            
[73] "compiler"         "RCurl"           
> h <- hasCachedMethods(packages, "t", "base", "Matrix")
> h[h]
  Matrix phyloseq 
    TRUE     TRUE 

This kind of analysis is rather too much to expect of every user. Ideally, the authors of Matrix would devise machinery to systematically detect cache invalidation, then somehow publish this information ahead of updates. (It's happening, slowly ...) Even more ideally, we would investigate the effect of patching methods to cache classes and methods at load (rather than install) time. That would prevent cache invalidation altogether, but we (mainly I) have simply not found the time.

0
Kevin Jerez On

I had the same problem. I closed RStudio without saving the workspace, and re-opened it as an administrator. Then I updated all packages, and when asked if I should reinstall source packages, I answered yes.

After installation, I ran the script again and it worked this time.