rvlenth / emmeans

Estimated marginal means
https://rvlenth.github.io/emmeans/
340 stars 30 forks source link

Error in t(OO[[rr]]) : object 'unpackedMatrix_transpose' not found #456

Closed Florentin85 closed 7 months ago

Florentin85 commented 7 months ago

Describe the bug

I fit a model: lmer_richness_tot_log <- lme4::lmer(richness_tot_log ~ species * h2o + (1 | bloc), data = div_species, REML = TRUE) Then, I try to obtain the emmeans for this model: emm_lmer_richness_tot_log <- emmeans::emmeans(lmer_richness_tot_log, specs = list(pairwise ~ h2o | species), type = "response", adjust = "tukey") ... and R gives me the following error message: Error in t(OO[[rr]]) : object 'unpackedMatrix_transpose' not found The error occurs only for particular models from my R environment. For example this model works: glmer_richness_patho <- lme4::glmer(richness_rarfy_patho ~ sp * h2o + (1 | bloc), data = agg_dat_df, family = poisson) I also tried a lme4::lmer() type of model in a different R environment and this model also worked with the emmeans::emmeans function. I searched on the internet and used ChatGPT to try to solve this error, but was unsuccessful so far. I updated all my R packages. Perhaps, this error is connected to the recent update of emmeans? I did not try to downgrade to an older version of emmeans yet.

To reproduce

div_species.csv

lmer_richness_tot_log <- lme4::lmer(richness_tot_log ~ species * h2o + (1 | bloc), data = div_species, REML = TRUE)

emm_lmer_richness_tot_log <- emmeans::emmeans(lmer_richness_tot_log, specs = list(pairwise ~ h2o | species), type = "response", adjust = "tukey")

Expected behavior

I expect to see the pairwise comparisons of each species by H2O treatment in the R console when I run the resulting emmeans object.

Additional context

I run on the operating system Pop!_OS 22.04 LTS (similar to Ubuntu)

Ground rules

IgnRL commented 7 months ago

Hi, I have the exact same problem, and our models are very similar since I am also working with diversity indexes:

lmer.richness <- lmer(richness ~ time*treatment +(1|treat.pot), data= data)

If it helps to solve the bug, I am working on Windows 10 x64 (build 19045), and all my packages and R versions are updated.

It doesn't show any problem with other models like lm():

lm1->lm(richness ~soil*acc, data= sample.data.rar.NS) emmeans.lm1 <- emmeans(lm1, ~ soil)

I hope this helps to solve or reproduce the problem.

rvlenth commented 7 months ago

There is nothing at all unusual about this model, and I do not experience this problem on my system...

> emmeans(lmer_richness_tot_log, specs = list(pairwise ~ h2o | species), type = "response", adjust = "tukey")
$`emmeans of h2o | species`
species = Acer saccharum:
 h2o  emmean    SE   df lower.CL upper.CL
 High   5.22 0.129 27.1     4.95     5.48
 Low    4.71 0.130 26.9     4.44     4.97

species = Betula papyrifera:
 h2o  emmean    SE   df lower.CL upper.CL
 High   4.41 0.114 23.2     4.17     4.64
 Low    4.78 0.114 23.2     4.54     5.01

species = Larix laricina:
 h2o  emmean    SE   df lower.CL upper.CL
 High   4.74 0.114 23.2     4.50     4.97
 Low    4.35 0.114 23.2     4.12     4.59

species = Picea glauca:
 h2o  emmean    SE   df lower.CL upper.CL
 High   4.38 0.114 23.2     4.15     4.62
 Low    5.00 0.156 30.0     4.68     5.32

species = Pinus strobus:
 h2o  emmean    SE   df lower.CL upper.CL
 High   4.19 0.114 23.2     3.95     4.43
 Low    3.96 0.129 27.0     3.69     4.22

species = Quercus rubra:
 h2o  emmean    SE   df lower.CL upper.CL
 High   4.50 0.114 23.2     4.27     4.74
 Low    4.43 0.114 23.2     4.19     4.66

Degrees-of-freedom method: kenward-roger 
Confidence level used: 0.95 

$`pairwise differences of h2o | species`
species = Acer saccharum:
 2          estimate    SE   df t.ratio p.value
 High - Low   0.5109 0.183 27.0   2.795  0.0095

species = Betula papyrifera:
 2          estimate    SE   df t.ratio p.value
 High - Low  -0.3696 0.161 23.2  -2.296  0.0311

species = Larix laricina:
 2          estimate    SE   df t.ratio p.value
 High - Low   0.3838 0.161 23.2   2.384  0.0257

species = Picea glauca:
 2          estimate    SE   df t.ratio p.value
 High - Low  -0.6202 0.193 28.1  -3.215  0.0033

species = Pinus strobus:
 2          estimate    SE   df t.ratio p.value
 High - Low   0.2312 0.172 25.4   1.342  0.1914

species = Quercus rubra:
 2          estimate    SE   df t.ratio p.value
 High - Low   0.0762 0.161 23.2   0.473  0.6406

Degrees-of-freedom method: kenward-roger 

so I don't think this is a bug in emmeans. From the error message, it looks like the t() method does not exist for class unpackedMatrix_transpose does not exist

> packageVersion("lme4")
[1] ‘1.1.34’

> methods("t")
 [1] t,ANY-method            t,CsparseMatrix-method  t,diagonalMatrix-method
 [4] t,indMatrix-method      t,packedMatrix-method   t,pMatrix-method       
 [7] t,RsparseMatrix-method  t,sparseVector-method   t,TsparseMatrix-method 
[10] t,unpackedMatrix-method t.data.frame            t.default              
[13] t.fractions*            t.gtable*               t.Surv*                
[16] t.Surv2*                t.trellis*              t.ts*                  
[19] t.vctrs_sclr*           t.vctrs_vctr*           t.zoo*                 
see '?methods' for accessing help and source code

I tried updating lme4 and Matrix, and I still can't reproduce this error.

rvlenth commented 7 months ago

PS you might try running emmeans with df = Inf added to the arguments. If that does not error, then it'd mean the issue is in the pbkrtest package (or lmerTest?)

Florentin85 commented 7 months ago

Dear rvlenth, thanks for your quick reply! I tried adding the df = Inf to the emmeans call without success. I also tried to update my lme4, Matrix, pbkrtest, and lmerTest packages again. However, this did also not help to solve the problem. I also looked up the methods and indeed unpackedMatrix_transpose does not seem to exist > methods("t") [1] t,ANY-method t,CsparseMatrix-method t,DataFrame-method [4] t,denseMatrix-method t,diagonalMatrix-method t,Hits-method [7] t,HitsList-method t,indMatrix-method t,otu_table-method [10] t,packedMatrix-method t,Pairs-method t,phyloseq-method [13] t,pMatrix-method t,RsparseMatrix-method t,sparseVector-method [16] t,TransposedDataFrame-method t,TsparseMatrix-method t,unpackedMatrix-method [19] t.data.frame t.DataFrame* t.default [22] t.dudi* t.fractions* t.gtable* [25] t.Hits* t.HitsList* t.igraph* [28] t.ktab* t.Surv* t.Surv2* [31] t.TransposedDataFrame* t.trellis* t.ts* [34] t.vctrs_sclr* t.vctrs_vctr* t.zoo* see '?methods' for accessing help and source code

How could I fix this problem? Do you happen to have any idea? Thanks for your support!

rvlenth commented 7 months ago

No suggestions, but wonder if downgrading to R 4.3.1 might help.

Florentin85 commented 7 months ago

This problem remains weird. I can fit lme4::lmer() type of models and run them with the emmeans function in another workspace (environment) without a problem. I tried wiping my workspace except for the data.frame "div_species", fitted the lme4::lmer() type model and ran emmeans again, without success. Mmmh, I guess downgrading might help, but I would like to avoid downgrading my R.

rvlenth commented 7 months ago

might be hard to tell the difference between upgrading and downgrading...

rvlenth commented 7 months ago

I'm gonna close this as I think it comes from somewhere outside of emmeans -- at least it looks like it

Florentin85 commented 7 months ago

Yes, it could come from outside of emmeans! After removing and re-installing my R and all packages, the error disappeared.