Giter Site home page Giter Site logo

Comments (6)

mattwarkentin avatar mattwarkentin commented on August 28, 2024 2

I see you using the summary.flexsurvreg method for predictions (both here and in the package source code). You may wish to use predict.flexsurvreg which I've implemented to be very tidy-friendly.

flexsurv_probs <- function(object, new_data, time, type = "survival") {
type <- rlang::arg_match(type, c("survival", "hazard"))
res <- summary(object, newdata = new_data, type = type, t = time, ci = FALSE)
res <- unname(res)
col_name <- rlang::sym(paste0(".pred_", type))
res <- purrr::map(res, ~ dplyr::select(.x, time, est))
res <- purrr::map(res, ~ setNames(.x, c(".time", col_name)))
tibble::tibble(.pred = res)
}

from censored.

topepo avatar topepo commented on August 28, 2024 1

Here's some code to get probability estimates from the survival package (verified against flexsurv):

library(survival)
library(flexsurv) # <- test against this

l_norm <- survreg(Surv(futime, fustat) ~ ecog.ps + rx, ovarian, dist = "lognormal")
l_norm_2 <- flexsurvreg(Surv(futime, fustat) ~ ecog.ps + rx, data = ovarian, dist = "lnorm")

para_survival <- function(object, time, location, scale = object$scale, ...) {
  distr <- object$dist
  tibble::tibble(
    .time = time, 
    .prob_survival = 1 - psurvreg(time, location, distribution = distr, scale, ...)
  )
}

para_hazard <- function(object, time, location, scale = object$scale, ...) {
  distr <- object$dist
  prob <- 
    dsurvreg(time, location, scale, distribution = distr, ...) /
    (1 - psurvreg(time, location, distribution = distr, scale, ...))
  tibble::tibble(
    .time = time, 
    .prob_hazard = prob
  )
}

surv_via_survival <- para_survival(l_norm, time = 1:300, predict(l_norm, head(ovarian, 1), type = "lp"))
surv_via_flexsurv <- summary(l_norm_2, newdata = head(ovarian, 1), type = "survival", t = 1:300)[[1]]
all.equal(surv_via_flexsurv$est, surv_via_survival$.prob_survival, tol = .0001)

haz_via_survival <- para_hazard(l_norm, time = 1:300, predict(l_norm, head(ovarian, 1), type = "lp"))
haz_via_flexsurv <- summary(l_norm_2, newdata = head(ovarian, 1), type = "hazard", t = 1:300)[[1]]
all.equal(haz_via_flexsurv$est, haz_via_survival$.prob_hazard, tol = .0001)

I'll put this in a PR soon.

from censored.

hfrick avatar hfrick commented on August 28, 2024

thanks @mattwarkentin ! 🙌

from censored.

topepo avatar topepo commented on August 28, 2024

That's great. Would you like to do a PR for that?

(We ask them for this a while back I think)

from censored.

mattwarkentin avatar mattwarkentin commented on August 28, 2024

I'm not sure what you mean. A PR for this package or a PR for {flexsurv}?

The predict.flexsurvreg method has been available in the dev version since 2020-06-10 and on CRAN since flexsurv version 2.0 (2021-02-22), I believe.

from censored.

github-actions avatar github-actions commented on August 28, 2024

This issue has been automatically locked. If you believe you have found a related problem, please file a new issue (with a reprex: https://reprex.tidyverse.org) and link to this issue.

from censored.

Related Issues (20)

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.