ropensci / GLMMcosinor

An R package for flexible cosinor modelling using the glmmTMB framework
https://docs.ropensci.org/GLMMcosinor/
GNU General Public License v3.0
1 stars 0 forks source link

[bug fix] `autoplot()` doesn't work with models that include a ziformula/dispformula. #14

Closed RWParsons closed 1 month ago

RWParsons commented 7 months ago

Currently the autoplot() method doesn't work for models with dispersion of zero-inflated formulas.

Suggest adding the following test to test-autoplot.R:

test_that("autoplot works with simple inputs and ziformula and dispformula", {
  object_zi <- cglmm(
    vit_d ~ X + amp_acro(time, group = "X", period = 12),
    data = vitamind,
    ziformula = ~ 0 + amp_acro(time, group = "X", period = 12)
  )

  object_disp <- cglmm(
    vit_d ~ X + amp_acro(time, group = "X", period = 12),
    data = vitamind,
    dispformula = ~ 0 + amp_acro(time, group = "X", period = 12),
  )

  vdiffr::expect_doppelganger(
    "plot with dispformula",
    autoplot(object_disp)
  )
  vdiffr::expect_doppelganger(
    "plot with ziformula",
    autoplot(object_zi)
  )
})

I suspect that this is because predict.cglmm() only passes the main formula to nd <- update_formula_and_data(...) leading to the final predict line to have nd = and returning the original dataset, not the new dataset.

I think it would be optimal to make the cglmm model object consistent with the formula included. This would make passing it to the predict function clearer and consistent rather than having to include checks to see whether the zi or disp formula were included. For example, in line 171 - 216 of R/data_processor.R, we could ALWAYS include the zi_formula and disp_formula in the output rather than having it as NULL. There is, after all, a formula passed to glmmTMB::glmmTMB(), but it's the default (~0 and ~1, respectively), so I think it makes sense to keep this with the cglmm object and any time predictions are being made or newdata is being created given a dataset and a set of formulas, all formulas are passed.

RWParsons commented 1 month ago

fixed with https://github.com/ropensci/GLMMcosinor/pull/22