Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
38 changes: 30 additions & 8 deletions R/metric_generators.R
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

[air] reported by reviewdog 🐶

stop("rf (ranger) prediction type not supported or unknown prediction structure.")

Original file line number Diff line number Diff line change
Expand Up @@ -149,16 +149,34 @@ predict_custom <- function(x, y = NULL, fit, model, type = "response") {
}

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

[air] reported by reviewdog 🐶

Suggested change

# Survival: ranger returns a matrix of survival probabilities by timepoint
if (is.matrix(preds) && inherits(fit, "ranger") && fit$treetype == "Surv") {
# If user asks for survival probabilities, return the matrix
if (is.matrix(pr$survival) && inherits(fit, "ranger") && fit$treetype == "Survival") {
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

[air] reported by reviewdog 🐶

Suggested change
if (is.matrix(pr$survival) && inherits(fit, "ranger") && fit$treetype == "Survival") {
if (
is.matrix(pr$survival) &&
inherits(fit, "ranger") &&
fit$treetype == "Survival"
) {

# If user asks for survival probabilities, return the survival matrix
if (type == "survival") {
return(preds)

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

[air] reported by reviewdog 🐶

Suggested change

times <- pr$unique.death.times
surv_matrix <- pr$survival

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

[air] reported by reviewdog 🐶

Suggested change

return(surv_matrix)
}
# For linear predictor / risk score, convert survival curves to a summary risk:
# pragmatic approach: use negative mean survival (higher => worse risk)
# For linear predictor / risk score, convert survival to linear predictor using
# logit = (S(t) / 1- S(t)) or
# lp = log(H(t)) where H(t) = sum(h(t))
if (type == "lp") {
lp <- -rowMeans(preds, na.rm = TRUE)
return(as.numeric(lp))

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

[air] reported by reviewdog 🐶

Suggested change

#times <- pr$unique.death.times
#surv_matrix <- pr$survival

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

[air] reported by reviewdog 🐶

Suggested change

# obtain the survival at an ith observation unique time
#surv_vec <- sapply(1:nrow(x_df), function(i) {
# t_i <- x_df$time[i]
# idx <- which.min(abs(times - t_i))
# surv_matrix[i, idx]
# })

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

[air] reported by reviewdog 🐶

Suggested change

# get lp from survival vector + adjust S(t) = 1 by subtracting 1e-8
# surv_vec_to_lp <- qlogis(surv_vec - 1e-8)
# chf n x times matrix: summing over times gives cumulative hazard
return(log(rowSums(pr$chf)))
}
}

Expand Down Expand Up @@ -306,7 +324,9 @@ continuous_calib_itl <- function(data, fit, model) {

survival_cindex <- function(data, fit, model) {
y_surv <- survival::Surv(data$time, data$event)
x <- data[, !(names(data) %in% c("time", "event", "id")), drop = FALSE]

x <- data[, !(names(data) %in% c("time", "event", "id")), drop = FALSE]

Comment on lines +327 to +329
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

[air] reported by reviewdog 🐶

Suggested change
x <- data[, !(names(data) %in% c("time", "event", "id")), drop = FALSE]
x <- data[, !(names(data) %in% c("time", "event", "id")), drop = FALSE]

# request linear predictor / risk score
y_hat <- try(predict_custom(x, NULL, fit, model, type = "lp"), silent = TRUE)
if (inherits(y_hat, "try-error")) {
Expand All @@ -322,7 +342,9 @@ survival_cindex <- function(data, fit, model) {
# Cox-like calibration slope (uses linear predictor)
survival_calib_slope <- function(data, fit, model) {
y_surv <- survival::Surv(data$time, data$event)

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

[air] reported by reviewdog 🐶

Suggested change

x <- data[, !(names(data) %in% c("time", "event", "id")), drop = FALSE]

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

[air] reported by reviewdog 🐶

Suggested change

y_hat <- try(predict_custom(x, NULL, fit, model, type = "lp"), silent = TRUE)
if (inherits(y_hat, "try-error")) {
return(NaN)
Expand Down
22 changes: 19 additions & 3 deletions R/model_generators.R
Original file line number Diff line number Diff line change
Expand Up @@ -25,13 +25,29 @@ default_models <- list(
nthreads <- ncores - 2

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

[air] reported by reviewdog 🐶

Suggested change

x <- d[, -1, drop = FALSE]
y <- d[, 1]
y <- as.factor(d[, 1])

#### new

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

[air] reported by reviewdog 🐶

Suggested change

ff <- NULL
invisible(
capture.output(
ff <- randomForest::tuneRF(x, y, trace = FALSE, plot = FALSE)
)
)

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

[air] reported by reviewdog 🐶

Suggested change

bestmtry <- data.frame(ff)
mtry_best <- bestmtry$mtry[which.min(bestmtry$OOBError)]

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

[air] reported by reviewdog 🐶

Suggested change

ranger::ranger(
x = x,
y = y,
mtry = mtry_best,
probability = TRUE,
num.threads = nthreads
num.trees = 300,
num.threads = nthreads
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

[air] reported by reviewdog 🐶

Suggested change
num.threads = nthreads
num.threads = nthreads

)

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

[air] reported by reviewdog 🐶

Suggested change

},
xgboost = function(d, nrounds = 100, params = list(objective = "binary:logistic", eval_metric = "logloss")) {
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

[air] reported by reviewdog 🐶

Suggested change
xgboost = function(d, nrounds = 100, params = list(objective = "binary:logistic", eval_metric = "logloss")) {
xgboost = function(
d,
nrounds = 100,
params = list(objective = "binary:logistic", eval_metric = "logloss")
) {

# expects first column y (0/1), remaining columns predictors
Expand Down Expand Up @@ -141,7 +157,7 @@ default_models <- list(

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

[air] reported by reviewdog 🐶

Suggested change

stopifnot(all(c("time", "event") %in% colnames(d)))
formula <- stats::as.formula("survival::Surv(time, event) ~ .")
ranger::ranger(formula, data = d, num.threads = nthreads)
ranger::ranger(formula, data = d, num.trees = 300, num.threads = nthreads)
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

[air] reported by reviewdog 🐶

Suggested change
ranger::ranger(formula, data = d, num.trees = 300, num.threads = nthreads)
ranger::ranger(formula, data = d, num.trees = 300, num.threads = nthreads)

},
xgboost = function(d, nrounds = 100, params = list(objective = "survival:cox", eval_metric = "cox-nloglik")) {
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

[air] reported by reviewdog 🐶

Suggested change
xgboost = function(d, nrounds = 100, params = list(objective = "survival:cox", eval_metric = "cox-nloglik")) {
xgboost = function(
d,
nrounds = 100,
params = list(objective = "survival:cox", eval_metric = "cox-nloglik")
) {

# XGBoost Cox objective: uses times as label but does not directly take a censoring vector.
Expand Down
2 changes: 1 addition & 1 deletion R/simulate_wrappers.R
Original file line number Diff line number Diff line change
Expand Up @@ -364,7 +364,7 @@ simulate_survival <- function(
)

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

[air] reported by reviewdog 🐶

Suggested change

# Tune the data-generating function
tune_param <- binary_tuning(
tune_param <- survival_tuning(
target_prevalence = 1 - censoring_rate,
target_performance = large_sample_cindex,
candidate_features = signal_parameters,
Expand Down
Loading