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

Support for cch models #243

Merged
merged 13 commits into from
Jan 24, 2024
2 changes: 1 addition & 1 deletion .github/workflows/R-CMD-check.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -41,7 +41,7 @@ jobs:

- uses: r-lib/actions/setup-r-dependencies@v2
with:
extra-packages: any::rcmdcheck, glmmTMB=?ignore
extra-packages: any::rcmdcheck, glmmTMB=?source, lme4=?source
needs: check

- uses: r-lib/actions/check-r-package@v2
Expand Down
3 changes: 3 additions & 0 deletions NAMESPACE
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ S3method(model_get_assign,model_fit)
S3method(model_get_assign,vglm)
S3method(model_get_coefficients_type,LORgee)
S3method(model_get_coefficients_type,biglm)
S3method(model_get_coefficients_type,cch)
S3method(model_get_coefficients_type,clm)
S3method(model_get_coefficients_type,clmm)
S3method(model_get_coefficients_type,clogit)
Expand Down Expand Up @@ -39,6 +40,7 @@ S3method(model_get_model_matrix,LORgee)
S3method(model_get_model_matrix,betareg)
S3method(model_get_model_matrix,biglm)
S3method(model_get_model_matrix,brmsfit)
S3method(model_get_model_matrix,cch)
S3method(model_get_model_matrix,clm)
S3method(model_get_model_matrix,default)
S3method(model_get_model_matrix,fixest)
Expand Down Expand Up @@ -68,6 +70,7 @@ S3method(model_get_response,model_fit)
S3method(model_get_response_variable,default)
S3method(model_get_terms,betareg)
S3method(model_get_terms,brmsfit)
S3method(model_get_terms,cch)
S3method(model_get_terms,default)
S3method(model_get_terms,glmmTMB)
S3method(model_get_terms,model_fit)
Expand Down
1 change: 1 addition & 0 deletions NEWS.md
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
**New supported models**

- Support for `mmrm::mmrm()` models (#228)
- Support for `survival::cch()` models (#242)

**New features**

Expand Down
23 changes: 23 additions & 0 deletions R/custom_tidiers.R
Original file line number Diff line number Diff line change
Expand Up @@ -79,6 +79,8 @@
#' @export
#' @family custom_tieders
tidy_with_broom_or_parameters <- function(x, conf.int = TRUE, conf.level = .95, ...) {
exponentiate_later <- FALSE

# load broom.mixed if available
if (any(c("glmerMod", "lmerMod", "glmmTMB", "glmmadmb", "stanreg", "brmsfit") %in% class(x))) {
.assert_package("broom.mixed", fn = "broom.helpers::tidy_with_broom_or_parameters()")
Expand Down Expand Up @@ -126,6 +128,16 @@
}
}

# specific case for cch models
# exponentiate and conf.int not supported by broom::tidy()
if (inherits(x, "cch")) {
if (isTRUE(tidy_args$exponentiate)) {
exponentiate_later <- TRUE

Check warning on line 135 in R/custom_tidiers.R

View check run for this annotation

Codecov / codecov/patch

R/custom_tidiers.R#L134-L135

Added lines #L134 - L135 were not covered by tests
}
tidy_args$exponentiate <- NULL
tidy_args$conf.int <- NULL

Check warning on line 138 in R/custom_tidiers.R

View check run for this annotation

Codecov / codecov/patch

R/custom_tidiers.R#L137-L138

Added lines #L137 - L138 were not covered by tests
}

# for betareg, if exponentiate = TRUE, forcing tidy_parameters,
# by adding `component = "all" to the arguments`
if (inherits(x, "betareg")) {
Expand Down Expand Up @@ -210,6 +222,17 @@
)
}
}

# cleaning in conf.int = FALSE
if (isFALSE(conf.int)) {
res <- res %>%
dplyr::select(-dplyr::any_of(c("conf.low", "conf.high")))
}

if (exponentiate_later) {
res <- .exponentiate(res)

Check warning on line 233 in R/custom_tidiers.R

View check run for this annotation

Codecov / codecov/patch

R/custom_tidiers.R#L233

Added line #L233 was not covered by tests
}

res
}

Expand Down
16 changes: 16 additions & 0 deletions R/helpers.R
Original file line number Diff line number Diff line change
Expand Up @@ -53,3 +53,19 @@
names(x) <- saved_names
x
}

# copied from broom
.exponentiate <- function (data, col = "estimate")

Check warning on line 58 in R/helpers.R

View workflow job for this annotation

GitHub Actions / lint

file=R/helpers.R,line=58,col=26,[function_left_parentheses_linter] Remove spaces before the left parenthesis in a function definition.
{

Check warning on line 59 in R/helpers.R

View workflow job for this annotation

GitHub Actions / lint

file=R/helpers.R,line=59,col=1,[brace_linter] Opening curly braces should never go on their own line and should always be followed by a new line.
data <- data %>%
dplyr::mutate(
dplyr::across(dplyr::all_of(col), exp)

Check warning on line 62 in R/helpers.R

View check run for this annotation

Codecov / codecov/patch

R/helpers.R#L60-L62

Added lines #L60 - L62 were not covered by tests
)
if ("conf.low" %in% colnames(data)) {
data <- data %>%
dplyr::mutate(
dplyr::across(dplyr::any_of(c("conf.low", "conf.high")), exp)

Check warning on line 67 in R/helpers.R

View check run for this annotation

Codecov / codecov/patch

R/helpers.R#L64-L67

Added lines #L64 - L67 were not covered by tests
)
}
data

Check warning on line 70 in R/helpers.R

View check run for this annotation

Codecov / codecov/patch

R/helpers.R#L70

Added line #L70 was not covered by tests
}
6 changes: 6 additions & 0 deletions R/model_get_coefficients_type.R
Original file line number Diff line number Diff line change
Expand Up @@ -146,6 +146,12 @@
"prop_hazard"
}

#' @export
#' @rdname model_get_coefficients_type
model_get_coefficients_type.cch <- function(model) {
"prop_hazard"

Check warning on line 152 in R/model_get_coefficients_type.R

View check run for this annotation

Codecov / codecov/patch

R/model_get_coefficients_type.R#L152

Added line #L152 was not covered by tests
}

#' @export
#' @rdname model_get_coefficients_type
model_get_coefficients_type.model_fit <- function(model) {
Expand Down
9 changes: 9 additions & 0 deletions R/model_get_model_matrix.R
Original file line number Diff line number Diff line change
Expand Up @@ -130,3 +130,12 @@
data = model %>% model_get_model_frame()
)
}

#' @export
#' @rdname model_get_model_matrix
model_get_model_matrix.cch <- function(model, ...) {
stats::model.matrix.default(
model$call$formula %>% stats::formula(),
data = model %>% model_get_model_frame()

Check warning on line 139 in R/model_get_model_matrix.R

View check run for this annotation

Codecov / codecov/patch

R/model_get_model_matrix.R#L137-L139

Added lines #L137 - L139 were not covered by tests
)
}
15 changes: 15 additions & 0 deletions R/model_get_terms.R
Original file line number Diff line number Diff line change
Expand Up @@ -55,3 +55,18 @@
model_get_terms.betareg <- function(model) {
model_get_terms(model$terms$full)
}

#' @export
#' @rdname model_get_terms
model_get_terms.betareg <- function(model) {
model_get_terms(model$terms$full)
}

#' @export
#' @rdname model_get_model_matrix
model_get_terms.cch <- function(model, ...) {
stats::terms.formula(
model$call$formula %>% stats::formula(),
data = model %>% model_get_model_frame()

Check warning on line 70 in R/model_get_terms.R

View check run for this annotation

Codecov / codecov/patch

R/model_get_terms.R#L68-L70

Added lines #L68 - L70 were not covered by tests
)
}
3 changes: 2 additions & 1 deletion data-raw/DATASET.R
Original file line number Diff line number Diff line change
Expand Up @@ -53,7 +53,8 @@ supported_models <-
"`mmrm::mmrm()`", "",
"`pscl::zeroinfl()`", "Use `tidy_zeroinfl()` as `tidy_fun`.",
"`pscl::hurdle()`", "Use `tidy_zeroinfl()` as `tidy_fun`.",
"`betareg::betareg()`", "Use `tidy_parameters()` as `tidy_fun` with `component` argument to control with coefficients to return. `broom::tidy()` does not support the `exponentiate` argument for betareg models, use `tidy_parameters()` instead." # nolint
"`betareg::betareg()`", "Use `tidy_parameters()` as `tidy_fun` with `component` argument to control with coefficients to return. `broom::tidy()` does not support the `exponentiate` argument for betareg models, use `tidy_parameters()` instead.", # nolint
"`survival::cch()`", "`Experimental support."
) %>%
dplyr::arrange(.data$model, .locale = "en")

Expand Down
Binary file modified data/supported_models.rda
Binary file not shown.
3 changes: 3 additions & 0 deletions man/model_get_coefficients_type.Rd

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

8 changes: 7 additions & 1 deletion man/model_get_model_matrix.Rd

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 2 additions & 0 deletions man/model_get_terms.Rd

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

3 changes: 3 additions & 0 deletions tests/testthat/test-tidy_plus_plus.R
Original file line number Diff line number Diff line change
Expand Up @@ -966,3 +966,6 @@ test_that("tidy_post_fun argument of `tidy_plus_plus()`", {
)
expect_equal(nrow(res), 2L)
})

# test for survival::cch() not working, model.frame() not working
# in the test_that environment for this type of model
Loading