-
Notifications
You must be signed in to change notification settings - Fork 92
estimate censoring probabilities #855
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
Merged
Merged
Changes from 8 commits
Commits
Show all changes
14 commits
Select commit
Hold shift + click to select a range
45db39f
add reverse KM curve estimate
topepo 0d7effb
documention uodate
topepo 5297de8
fix typos
topepo 0376b41
fix formula name
topepo b737edf
Apply suggestions from code review
topepo a6dfa09
put censoring functions in their own file
topepo 2126975
generalize the censoring model interface for future use
topepo 9df1f10
nocov and fix logic when NAs are there
topepo b8a31f7
Merge branch 'main' into censor-probs
topepo 34644a5
updated on reviewer code
topepo e245c72
Merge branch 'main' into censor-probs
topepo 17b763c
Merge branch 'censor-probs' of https://github.com/tidymodels/parsnip …
topepo 8a24b67
testing notes
topepo 009ab36
updated number
topepo File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,6 +1,6 @@ | ||
Package: parsnip | ||
Title: A Common API to Modeling and Analysis Functions | ||
Version: 1.0.3.9000 | ||
Version: 1.0.3.9001 | ||
Authors@R: c( | ||
person("Max", "Kuhn", , "[email protected]", role = c("aut", "cre")), | ||
person("Davis", "Vaughan", , "[email protected]", role = "aut"), | ||
|
@@ -54,6 +54,7 @@ Suggests: | |
mgcv, | ||
modeldata, | ||
nlme, | ||
prodlim, | ||
ranger (>= 0.12.0), | ||
remotes, | ||
rmarkdown, | ||
|
@@ -75,4 +76,4 @@ Config/testthat/edition: 3 | |
Encoding: UTF-8 | ||
LazyData: true | ||
Roxygen: list(markdown = TRUE) | ||
RoxygenNote: 7.2.1.9000 | ||
RoxygenNote: 7.2.3 |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,107 @@ | ||
# nocov start | ||
# tested in the extratests repo | ||
|
||
new_censoring_model <- | ||
function(formula, | ||
object, | ||
pkgs = character(0), | ||
label = character(0), | ||
extra_cls = character(0)) { | ||
res <- list(formula = formula, fit = object, label = label, required_pkgs = pkgs) | ||
class(res) <- c(paste0("censoring_model_", label), "censoring_model", extra_cls) | ||
res | ||
} | ||
|
||
# ------------------------------------------------------------------------------ | ||
# estimate the reverse km curve for censored regresison models | ||
|
||
make_cens_prob_model <- function(obj, eval_env) { | ||
if (obj$mode != "censored regression") { | ||
return(list()) | ||
} | ||
rlang::check_installed("prodlim") | ||
|
||
# Note: even when fit_xy() is called, eval_env will still have | ||
# objects data and formula in them | ||
f <- eval_env$formula | ||
km_form <- stats::update(f, ~ 1) | ||
cl <- | ||
rlang::call2( | ||
"prodlim", | ||
formula = km_form, | ||
.ns = "prodlim", | ||
reverse = TRUE, | ||
type = "surv", | ||
x = FALSE, | ||
data = rlang::expr(eval_env$data) | ||
) | ||
|
||
if (!is.null(eval_env$weights)) { | ||
cl <- rlang::call_modify(cl, caseweights = rlang::expr(eval_env$weights)) | ||
} | ||
rkm <- try(rlang::eval_tidy(cl), silent = TRUE) | ||
if (!inherits(rkm, "try-error")) { | ||
attr(f, ".Environment") <- rlang::base_env() | ||
} | ||
attr(rkm$formula, ".Environment") <- rlang::base_env() | ||
topepo marked this conversation as resolved.
Show resolved
Hide resolved
|
||
new_censoring_model(f, object = rkm, label = "reverse_km", pkgs = "prodlim") | ||
} | ||
|
||
# ------------------------------------------------------------------------------ | ||
# Basic S3 methods | ||
|
||
print.censoring_model <- function(x, ...) { | ||
cat(x$label, "model for predicting the probability of censoring\n") | ||
invisible(x) | ||
} | ||
|
||
predict.censoring_model <- function(object, ...) { | ||
rlang::abort( | ||
paste("Don't know how to predict with a censoring model of type:", object$label) | ||
) | ||
invisible(NULL) | ||
} | ||
|
||
#' @export | ||
predict.censoring_model_reverse_km <- function(object, new_data = NULL, time, as_vector = FALSE, ...) { | ||
rlang::check_installed("prodlim") | ||
|
||
res <- rep(NA_real_, length(time)) | ||
|
||
# Some time values might be NA (for Graf category 2) | ||
is_na <- which(is.na(time)) | ||
if (length(is_na) > 0) { | ||
time <- time[-is_na] | ||
} | ||
|
||
if (length(time) > 0) { | ||
topepo marked this conversation as resolved.
Show resolved
Hide resolved
|
||
if (is.null(new_data)) { | ||
tmp <- | ||
purrr::map_dbl(time, ~ predict(object$fit, times = .x, type = "surv")) | ||
} else { | ||
tmp <- | ||
purrr::map_dbl(time, ~ predict(object$fit, newdata = new_data, times = .x, type = "surv")) | ||
} | ||
zero_prob <- purrr::map_lgl(tmp, ~ !is.na(.x) && .x == 0) | ||
if (any(zero_prob)) { | ||
# Don't want censoring probabilities of zero so add an epsilon | ||
# Either use 1/n or half of the minimum survival probability | ||
n <- max(object$fit$n.risk) | ||
half_min_surv_prob <- min(object$fit$surv[object$fit$surv > 0]) / 2 | ||
eps <- min(1 / n, half_min_surv_prob) | ||
tmp[zero_prob] <- eps | ||
} | ||
|
||
if (length(is_na) > 0) { | ||
res[-is_na] <- tmp | ||
} else { | ||
res <- tmp | ||
} | ||
} | ||
if (!as_vector) { | ||
res <- tibble::tibble(.prob_censored = unname(res)) | ||
} | ||
res | ||
} | ||
|
||
# nocov end |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -197,5 +197,3 @@ xy_form <- function(object, env, control, ...) { | |
res$preproc <- data_obj[c("x_var", "y_var")] | ||
res | ||
} | ||
|
||
|
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,2 @@ | ||
Note that some functionality in parsnip is tested outside of the package. Due to a high degree of dependencies, many additional tests are in the [extratexts](https://github.com/tidymodels/extratests/tree/main/tests/testthat) repository. These are run nightly with CRAN and Github versions of parsnip as well as other tidymodels packages. | ||
|
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.