Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Modernize registration of S3 method for projpred generic #1525

Merged
merged 4 commits into from
Jul 6, 2023

Conversation

fweber144
Copy link
Contributor

@fweber144 fweber144 commented Jul 6, 2023

I'm not sure if you are willing to increase the required R version to >= 3.6.0 (until now: >= 3.5.0), but if you are, then this PR provides a more modern way to register the S3 method get_refmodel.brmsfit() for the projpred::get_refmodel() generic.

This was already discussed in #1222, but at that time (more precisely, at #1222 (comment)), I didn't realize that you don't need to call library(projpred) before documenting via devtools::document() if you can live with having get_refmodel.brmsfit( in man/get_refmodel.brmsfit.Rd (as is the case already now) instead of \method{get_refmodel}{brmsfit}( (the latter would render the typical ## S3 method for class 'brmsfit' header in the "Usage" section of HTML and PDF documentation, but as I said, we don't have that header at the moment either). In other words, in #1222, I didn't realize that calling library(projpred) before documenting only provides a minor cosmetic improvement compared to the current state.

Another reason why I would switch to this more modern S3 registration is that in https://github.com/r-lib/vctrs/blob/master/R/register-s3.R, the code for s3_register() has changed, so it should probably be updated if you don't want to adopt the more modern scheme. However, when copying the new vctrs::s3_register() code, you have to keep in mind that this would require at least one new helper function (vctrs:::.rlang_s3_register_compat()), which increases the amount of code that has to be copied to and maintained in brms. This also shows that keeping the current S3 registration up to date is kind of tedious, because you regularly have to check vctrs::s3_register() for updates.

I have tested this PR locally with the following code (where I restarted the R session between each section):

# Attach ------------------------------------------------------------------

library(brms)
options(mc.cores = parallel::detectCores())
ref_fit <- brm(
  formula = rate ~ conc + state,
  data = Puromycin,
  backend = "cmdstanr",
  seed = 2052109,
  refresh = 0
)

library(projpred)
prj <- project(ref_fit, solution_terms = "conc", ndraws = 40)
prjmat <- as.matrix(prj)

# Load --------------------------------------------------------------------

options(mc.cores = parallel::detectCores())
ref_fit <- brms::brm(
  formula = rate ~ conc + state,
  data = Puromycin,
  backend = "cmdstanr",
  seed = 2052109,
  refresh = 0
)

prj <- projpred::project(ref_fit, solution_terms = "conc", ndraws = 40)
prjmat <- as.matrix(prj)

# Attach brms, load projpred ----------------------------------------------

library(brms)
options(mc.cores = parallel::detectCores())
ref_fit <- brm(
  formula = rate ~ conc + state,
  data = Puromycin,
  backend = "cmdstanr",
  seed = 2052109,
  refresh = 0
)

prj <- projpred::project(ref_fit, solution_terms = "conc", ndraws = 40)
prjmat <- as.matrix(prj)

# Load brms, attach projpred ----------------------------------------------

options(mc.cores = parallel::detectCores())
ref_fit <- brms::brm(
  formula = rate ~ conc + state,
  data = Puromycin,
  backend = "cmdstanr",
  seed = 2052109,
  refresh = 0
)

library(projpred)
prj <- project(ref_fit, solution_terms = "conc", ndraws = 40)
prjmat <- as.matrix(prj)

as well as with a couple of hands-on examples as well as within projpred's unit tests (and I have also run R CMD check for brms, of course).

methods whose generic isn't imported (e.g., because the corresponding package is
in `Suggests`, not `Imports`).
calling `library(projpred)` beforehand).

If the re-documentation would have taken place by calling `library(projpred)`
and then `devtools::document(roclets = c('rd', 'namespace'))` in the console
(the `roclets = c('rd', 'namespace')` part is due to the `PackageRoxygenize:
rd,namespace` entry in the `brms.Rproj` file), then in
`man/get_refmodel.brmsfit.Rd`, line `get_refmodel.brmsfit(` would have been
replaced by `\method{get_refmodel}{brmsfit}(`.
@fweber144
Copy link
Contributor Author

If you are also willing to always run library(projpred) before documenting, then I could push a commit that updates the documentation correspondingly (i.e., which provides the cosmetic improvement mentioned above). But in #1222, I understood you in the way that this complicates your build toolchain, so I sticked with the "standard" way of documenting (i.e., without calling library(projpred) beforehand).

@paul-buerkner
Copy link
Owner

Thank you! Yes, I would prefer not to load projpred before documenting. I will check out the PR now.

@paul-buerkner
Copy link
Owner

Looks good. Merging now.

@paul-buerkner paul-buerkner merged commit 40d0587 into paul-buerkner:master Jul 6, 2023
4 of 6 checks passed
@fweber144 fweber144 deleted the projpred_registerS3 branch July 6, 2023 08:32
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants