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

add casebase #7

Merged
merged 3 commits into from
Mar 2, 2021
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
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@

.DS_Store
.Rproj.user
.Rhistory
75 changes: 43 additions & 32 deletions survival-analysis/README.Rmd
Original file line number Diff line number Diff line change
@@ -1,14 +1,16 @@

---
always_allow_html: true
---

```{r, load_libs, message = FALSE, warning = FALSE, echo = FALSE, results = 'hide'}
library(prodlim)
library(survival)
library(flexsurv)
library(casebase)
library(rpart)
library(pec)
library(ipred)
library(randomForestSRC)
library(ranger)
library(mboost)
library(tidymodels)
library(cli)
Expand Down Expand Up @@ -105,6 +107,7 @@ fitw <-
dist = "weibull")
times <-
summary(fitw, ovarian[1:2,], type = "mean") %>%
map(~ setNames(.x, c("est", "lcl", "ucl"))) %>%
bind_rows() %>%
select(.pred_time = est) %>%
as_tibble()
Expand Down Expand Up @@ -195,6 +198,42 @@ summary(flxs_mod, test_pred, type = "survival", t = .times, tidy = TRUE)

The `flexsurvspline()` function has the same interface.

## `casebase::`[`fitSmoothHazard`](https://rdrr.io/cran/casebase/man/fitSmoothHazard.html)

The main fitting function is `fitSmoothHazard`:

```{r, cb_fit}
library(casebase)
cb_mod <-
fitSmoothHazard(status ~ time + (X1 + X2) ^ 2,
data = train_dat)
```

Note that `time` is modeled explicitly and with flexibility:

```{r, cb_splines}
library(splines)
cb_sp_mod <-
fitSmoothHazard(status ~ bs(time) + (X1 + X2) ^ 2,
data = train_dat)
```

And we can also do penalized estimation:

```{r, cb_glment}
cb_glmnet_mod <-
fitSmoothHazard(status ~ time + (X1 + X2) ^ 2,
data = train_dat, family = "glmnet")
```

Predictions for the cumulative incidence or survival probabilities are obtained using `absoluteRisk`:

```{r, cb_pred}
absoluteRisk(cb_mod, newdata = test_pred, time = .times, type = "CI")
absoluteRisk(cb_mod, newdata = test_pred, time = .times, type = "survival")
```

(Note that the argument `type` is only available in the development version.)

## Cox PH Models

Expand Down Expand Up @@ -389,34 +428,6 @@ although these values do not bracket the observed times:
summary(train_dat$time)
```

### `ranger:::`[`ranger`](https://rdrr.io/cran/ranger/man/ranger.html)

```{r, ranger_fit}
library(ranger)
ranger_mod <- ranger(Surv(time, status) ~ X1 + X2, data = train_dat, num.trees = 1000)
```

The `predict` function generates an object with class `"ranger.prediction"`.

```{r, ranger_pred}
ranger_pred = predict(ranger_mod, test_pred)
str(ranger_pred)
```

The `unique.death.times` slot has the unique death times:

```{r, }
all(sort(unique(train_dat$time)) == ranger_pred$unique.death.times)
```

The `survival` slot contains a matrix of predicted survival curves, with a row for each test case and a column for each death time. The `chf` slot is the same for the cumulative hazard function:

```{r, ranger_surv}
ranger_pred$survival[, 1:5]
ranger_pred$chf[, 1:5]
```


# Model Prediction Scorecard

In most cases, the open circle means that this type of prediction is facilitated _indirectly_. For example, using "tricks" related to `survfit()`, methods in the `pec` package, or other means.
Expand All @@ -431,15 +442,15 @@ pred_types <-
~Package, ~Function, ~`pred event time`, ~`survival probs`, ~`linear pred`,
"`survival`", "`survreg`", yep, nope, yep,
"`flexsurv`", "`flexsurvreg`", yep, yep, yep,
"`casebase`", "`fitSmoothHazard`", nope, yep, yep,
"`survival`", "`coxph`", yep, yep, yep,
"`glmnet`", "`glmnet`", kinda, kinda, yep,
"`mboost`", "`*boost`", kinda, kinda, yep,
"`rpart`", "`rpart`", yep, kinda, nope,
"`ipred`", "`bagging`", yep, kinda, nope,
"`party`", "`ctree`", yep, kinda, nope,
"`party`", "`cforest`", yep, kinda, nope,
"`randomForestSRC`", "`rfsrc`", yep, kinda, nope,
"`ranger`", "`ranger`", kinda, yep, nope
"`randomForestSRC`", "`rfsrc`", yep, kinda, nope
)

pred_types %>%
Expand Down
Loading