在ggsurvplot中包含weibull fit

时间:2017-04-11 13:28:09

标签: r ggplot2 survival-analysis

我想将weibull曲线拟合到某些事件数据,然后在survminer :: ggsurvplot绘制的生存图中包含拟合的weibull曲线。怎么想? 这是一个可以解决的例子:

模拟威布尔数据的函数:

# N = sample size    
# lambda = scale parameter in h0()
# rho = shape parameter in h0()
# beta = fixed effect parameter
# rateC = rate parameter of the exponential distribution of C

simulWeib <- function(N, lambda, rho, beta, rateC)
{
  # covariate --> N Bernoulli trials
  x <- sample(x=c(0, 1), size=N, replace=TRUE, prob=c(0.5, 0.5))

  # Weibull latent event times
  v <- runif(n=N)
  Tlat <- (- log(v) / (lambda * exp(x * beta)))^(1 / rho)

  # censoring times
  C <- rexp(n=N, rate=rateC)

  # follow-up times and event indicators
  time <- pmin(Tlat, C)
  status <- as.numeric(Tlat <= C)

  # data set
  data.frame(id=1:N,
             time=time,
             status=status,
             x=x)
}

生成数据

set.seed(1234)
betaHat <- rep(NA, 1e3)
for(k in 1:1e3)
{
  dat <- simulWeib(N=100, lambda=0.01, rho=1, beta=-0.6, rateC=0.001)
  fit <- coxph(Surv(time, status) ~ x, data=dat)
  betaHat[k] <- fit$coef
}

#Estimate a survival function
survfit(Surv(as.numeric(time), x)~1, data=dat) -> out0

#plot    

library(survminer)
ggsurvplot(out0, data = dat, risk.table = TRUE)

gg1 <- ggsurvplot(
  out0,                     # survfit object with calculated statistics.
  data = dat,  # data used to fit survival curves. 
  risk.table = TRUE,       # show risk table.
  pval = TRUE,             # show p-value of log-rank test.
  conf.int = TRUE,         # show confidence intervals for 
  # point estimaes of survival curves.
  xlim = c(0,2000),        # present narrower X axis, but not affect
  # survival estimates.
  break.time.by = 500,     # break X axis in time intervals by 500.
  ggtheme = theme_minimal(), # customize plot and risk table with a theme.
  risk.table.y.text.col = T, # colour risk table text annotations.
  risk.table.y.text = FALSE,
  surv.median.line = "hv",
  color = "darkgreen",
  conf.int.fill = "lightblue",
  title = "Survival probability",# show bars instead of names in text annotations
  # in legend of risk table
)
gg1

enter image description here

2 个答案:

答案 0 :(得分:1)

据我所知,目前不可能使用ggsurvplot

我创建了一个请求此功能的问题:https://github.com/kassambara/survminer/issues/276

您可以使用ggplot2绘制weibull模型的幸存者曲线,如下所示:

library("survival")
wbmod <- survreg(Surv(time, status) ~ x, data = dat)
s <- seq(.01, .99, by = .01)
t_0 <- predict(wbmod, newdata = data.frame(x = 0), 
                 type = "quantile", p = s)
t_1 <- predict(wbmod, newdata = data.frame(x = 1), 
                type = "quantile", p = s)

smod <- data.frame(time = c(t_0, t_1), 
                   surv = rep(1 - s, times = 2), 
                   strata = rep(c(0, 1), each = length(s)),
                   upper = NA, lower = NA)

head(surv_summary(cm))

library("ggplot2")
ggplot() + 
  geom_line(data = smod, aes(x = time, y = surv, color = factor(strata))) +
  theme_classic()

enter image description here 但据我所知,你不能使用幸存者(还):

library("survminer")

# wrong:
ggsurvplot(smod) 

# does not work:
gg1$plot + geom_line(data = smod, aes(x = time, y = surv, color = factor(strata)))

答案 1 :(得分:0)

以下对我有用。可能要归功于Heidi填写功能请求。 希望有人觉得这很有用。

{
  "root": true,
  "parser": "@typescript-eslint/parser",
  "parserOptions": {
    "createDefaultProgram": true
  },
  "plugins": ["@typescript-eslint"],
  "extends": [
    "plugin:@typescript-eslint/recommended",
    "prettier",
    "prettier/@typescript-eslint"
  ],
  "rules": {
    "@typescript-eslint/array-type": ["error", { "default": "array-simple" }],
    "@typescript-eslint/explicit-member-accessibility": ["off"],
    "@typescript-eslint/no-non-null-assertion": ["off"],
    "@typescript-eslint/no-use-before-define": ["off"],
    "@typescript-eslint/no-parameter-properties": ["off"],
    "@typescript-eslint/no-unused-vars": [
      "error",
      { "argsIgnorePattern": "^_", "varsIgnorePattern": "^_" }
    ],
    "@typescript-eslint/ban-ts-ignore": ["off"],
    "@typescript-eslint/no-empty-function": ["off"],
    "no-return-await": "error",
    "require-await": "error",
    "no-async-promise-executor": "error"
  },
  "overrides": [
    {
      "files": ["*.js"],
      "rules": {
        "@typescript-eslint/explicit-function-return-type": ["off"]
      }
    },
    {
      "files": ["tools/node_*.js"],
      "rules": {
        "@typescript-eslint/no-var-requires": ["off"]
      }
    }
  ]
}

enter image description here