stan-dev / rstanarm

rstanarm R package for Bayesian applied regression modeling
https://mc-stan.org/rstanarm
GNU General Public License v3.0
385 stars 132 forks source link

`posterior_linpred()` for `stan_gamm4()` errors with 'Matrix' v1.6-4 #610

Closed fweber144 closed 8 months ago

fweber144 commented 9 months ago

Summary:

Calling posterior_linpred() on a stan_gamm4() fit errors if version 1.6-4 of the Matrix package is installed.

Description:

See "Summary" above.

Reproducible Steps:

# Based on the example section of `?rstanarm::stan_gamm4`.
library(rstanarm)
nobs <- 400
ngrp <- 20
dat <- mgcv::gamSim(1, n = nobs, scale = 2)
dat$fac <- as.factor(sample.int(ngrp, size = nobs, replace = TRUE))
dat$y <- dat$y + model.matrix(~ fac - 1, data = dat) %*% rnorm(ngrp) * 0.5
rfit <- stan_gamm4(y ~ s(x0) + x1 + s(x2), data = dat, random = ~ (1 | fac),
                   chains = 1, iter = 500, seed = 198, refresh = 0)
pl <- posterior_linpred(rfit)

The last line succeeds with 'Matrix' v1.6-3, but errors with 'Matrix' v1.6-4:

Error in initializePtr() :
  function 'chm_factor_ldetL2' not provided by package 'Matrix'

RStanARM Version:

2.26.1 (from CRAN)

R Version:

4.3.2

Operating System:

Ubuntu 22.04.3 LTS

jgabry commented 9 months ago

Thanks for letting us know, that's not good. I debugged this just now and it seems to happen at this line

https://github.com/stan-dev/rstanarm/blob/a9889e6237a113cd17c387540e7f825b74211f85/R/pp_data.R#L232

which means it's when ranef is called. This is actually a gamm4 issue because it happens even outside of rstanarm when calling ranef on a gamm4 object:

ranef(gamm4(y ~ s(x0) + x1 + s(x2), data = dat, random = ~ (1 | fac)))

gives me the same error. So I think this needs to be fixed in gamm4. @bgoodri @avehtari it doesn't look like gamm4 has a GitHub repo, so we can send the maintainer an email.

avehtari commented 9 months ago

Here's the reproducible example without rstanarm

library(gamm4)
nobs <- 400
ngrp <- 20
dat <- mgcv::gamSim(1, n = nobs, scale = 2)
dat$fac <- as.factor(sample.int(ngrp, size = nobs, replace = TRUE))
dat$y <- dat$y + model.matrix(~ fac - 1, data = dat) %*% rnorm(ngrp) * 0.5
fit <- gamm4(y ~ s(x0) + x1 + s(x2), data = dat, random = ~ (1 | fac))
jgabry commented 9 months ago

Thanks, just sent Simon Wood an email using the email address on the CRAN page for gamm4

jgabry commented 9 months ago

I corresponded with Simon and it seems like an issue with incompatible versions of lme4 and Matrix (for some reason there isn't backwards compatibility and both need to updated to the latest versions at the same time). If you update lme4 does this error go away? That got it working for me.

fweber144 commented 9 months ago

Hm, that's strange. At the time of my initial comment, my lme4 version was already at 1.1-35.1 (the current CRAN version) and it still is. Do you refer to the most recent GitHub version of lme4?

To be safe, I have now updated all my R packages (except for tikzDevice, but that shouldn't matter here) to their most recent CRAN versions (and StanHeaders to its most recent https://mc-stan.org/r-packages/ version, but again, that shouldn't matter here), but the issue still occurs.

jgabry commented 9 months ago

Hmm, very strange. I wasn't referring to the GitHub version of lme4, just the latest CRAN version. According to Simon Wood gamm4 should work with any pair of lme4 and Matrix that are compatible with each other, but that seems not to be the case for you (although it works for me now). Not sure what's going on, but I definitely think it's related to lme4/Matrix/gamm4 and not anything specific to rstanarm.

fweber144 commented 9 months ago

Yes, seems to be unrelated to rstanarm. If you want, you can close this issue here.

avehtari commented 8 months ago

In case someone gets hits by this and finds this issue, it's about binary incompatibility, and installing lme4 and Matrix from source solves this (+ restart R session). See https://stackoverflow.com/questions/77481539/error-in-initializeptr-function-cholmod-factor-ldeta-not-provided-by-pack

fweber144 commented 8 months ago

In case someone gets hits by this and finds this issue, it's about binary incompatibility, and installing lme4 and Matrix from source solves this (+ restart R session). See https://stackoverflow.com/questions/77481539/error-in-initializeptr-function-cholmod-factor-ldeta-not-provided-by-pack

Thanks, works indeed.