2

I am creating a table with tab_model from the package sjPlot (https://cran.r-project.org/web/packages/sjPlot/vignettes/tab_model_estimates.html).

However, when I use a negative binomial rstanarm model object, tab_model re-runs MCMC chains. My actual model takes many hours to run, so this is not ideal for tab_model to be doing this, but it doesn't seem to do it for other models (such as with glmer in lme4).

library(rstanarm)
library(lme4)

dat.nb<-data.frame(x=rnorm(200),z=rep(c("A","B","C","D"),50),
                y=rnbinom(200,size=1,prob = .5))

mod1<-glmer.nb(y~x+(1|z),data=dat.nb)

options(mc.cores = parallel::detectCores())
mod2<-stan_glmer.nb(y~x+(1|z),data=dat.nb)

Now to create the model tables:

library(sjPlot)
tab_model(mod1)

enter image description here

The output is quick, and as expected (although the original model also ran quick, so it is possible that tab_model is re-running the model here too).

Now when I try

tab_model(mod2)

It begins re-running MCMC. Is this normal behavior, and if so, is anyone familiar with a way to turn this off, and just use the model object already created, rather than re-running the model?

Ben Bolker
  • 211,554
  • 25
  • 370
  • 453
Dylan_Gomes
  • 2,066
  • 14
  • 29
  • I tried to dig my way through and debug this, but the code was just too chunky for me to figure it out. https://github.com/strengejacke/sjPlot/blob/7f8aa7c3dd7471634359b585312aa5e03edff62c/R/tab_model.R – Ben Bolker Oct 13 '20 at 23:12
  • I briefly looked at it as well, and just thought I had no chance.. – Dylan_Gomes Oct 13 '20 at 23:26

1 Answers1

3

tl;dr I think this is going to be hard to avoid without hacking both the insight package and this one, or asking the package maintainer for an edit, unless you want to forgo printing the ICC, R^2, and the random-effects variance. Here, tab_model() calls insight::get_variance(), which tries to compute variances for the null model so it can compute the ICC and R^2. Computing these variances requires re-running the model. (When it does it for the glmer.nb, it goes via lme4:::update.merMod() and is quick enough that you don't notice the computation time.)

So

tab_model(mod2,show.r2=FALSE,show.icc=FALSE,show.re.var=FALSE)

doesn't recompute anything. In theory I think it should be possible to skip the resampling/recomputation step with just show.r2=FALSE, show.icc=FALSE (i.e. it shouldn't be necessary to get the RE var), but this would take some hacking/participation by the maintainer.


Digging in (by using debug(rstan::sampling) to stop inside the Stan sampling function, then where to see the call stack ...

  • tab_model() calls insight::get_variance() here
  • the insight::get_variance.stanreg() method calls insight:::.compute_variances()
  • ... which calls insight:::.compute_variance_distribution()
  • ... which (for a log-link count distribution) calls insight:::.variance_distributional()
  • ... which calls null_model
  • ... which calls .null_model_mixed()
  • ... which calls stats::update()
Ben Bolker
  • 211,554
  • 25
  • 370
  • 453
  • so am I correct in that it appears to be running a null model then, not re-fitting the same model? I am not sure what the function uses the null model for, but it seems as if this should only be useful for some of the output. I will try to look through and see if there is an argument that if turned off would bypass the need for this updating. – Dylan_Gomes Oct 13 '20 at 23:39
  • The package maintainer is generally quite responsive, it might be worth opening an issue about this. I think wanting the RE var but being willing to do without the R^2 or the ICC is a not-unusual use case ... – Ben Bolker Oct 13 '20 at 23:46
  • Thank you for this, I haven't opened an issue before, but I will look into it. – Dylan_Gomes Oct 13 '20 at 23:53
  • 1
    For anyone following this thread: https://github.com/strengejacke/sjPlot/issues/697 – Dylan_Gomes Oct 14 '20 at 00:02