0
$\begingroup$

I am using survfit object from package survminer to draw a survival curve in ggplot. Now I am trying to understand how I can use that same object from method survfit and calculate the P-value up to a specific time (e.g. 365 days, instead of the whole 2500+ days curve).

On stackoverflow I posted the following very similar question R draw survival curve and calculate P-value at specific times!

The answer helped me to draw a vertical line at 365 for indicative purpose and calculate the z-score / z-test up at 365 days.

fit1 <- survfit(Surv(dat$diff_in_days, dat$survivalstat)~Schedule,data = dat)

#turn into df
df <- broom::tidy(fit1) 

fit_df <- df  %>% 
  #group by strata
  group_by(strata) %>% 
  #get day  of interest or day before it
  filter(time <= 365) %>% 
  arrange(time) %>% 
  # pull last date
  do(tail(.,1))

#calculate z score based on 2 sample test at that time point
z <- (fit_df$estimate[1]-fit_df$estimate[2]) / (sqrt( fit_df$std.error[1]^2+ fit_df$std.error[2]^2))
#get probability of z score
pz <- pnorm(abs(z))
#get p value
pvalue <- round(2 * (1-pz),2)

ggsurv <- ggsurvplot(
  fit1, 
  data = dat,

  pval = TRUE,
  pval.size = 4,
  pval.method = TRUE,
  pval.method.size = 3,
  log.rank.weights = "1",

  conf.int = TRUE,
  conf.int.style = "ribbon",
  conf.int.alpha = 0.2,

  xlab = "Time in days",

  tables.theme = theme_cleantable(),
  ggtheme = theme_bw()
)

ggsurv$plot <- ggsurv$plot + 
                geom_vline(aes(xintercept=365))+ 
                geom_text(aes(x = 500,y=.8,label = paste0("p = " ,pvalue) ))

print(ggsurv)

I am relatively new to R and especially statistics. How can I calculate the log-rank (external requirement) of the survival curve upto 365 days? instead of the z-score in above's code.

Aim is to see if there is a difference in survival between different treatment schedules. Some parts of the curve seem to deviate but later come together. I want to see if the difference at certain points is statistically significant instead of the entire curve. patients with x treatment at 1 year have a higher survival probability than patients with x treatment.

$\endgroup$

closed as off-topic by gung Nov 19 '18 at 2:15

  • This question does not appear to be about statistics within the scope defined in the help center.
If this question can be reworded to fit the rules in the help center, please edit the question.

  • $\begingroup$ Please add a reproducible example for people to work with. $\endgroup$ – gung Nov 19 '18 at 2:14
  • $\begingroup$ I'm voting to close this question as off-topic because it is about how to use R without a reproducible example. $\endgroup$ – gung Nov 19 '18 at 2:15
1
$\begingroup$

Found the solution in this question. how can I focus the log rank test in a selected period of time of follow up?

time_730 <- ifelse(time>=730, 730, time)
event_730 <- ifelse(time>=730, 0, event)

sdf <- survdiff(Surv(dat_365$diff_in_days, dat_365$survivalstat) ~ Schedule,data = dat_365)
p.val <- 1 - pchisq(sdf$chisq, length(sdf$n) - 1)

Cut the time at desired measure time and all events past time have not happened yet thus are made 0.

Adding csing custom ggplot lines and text gets the desired result.

geom_vline(aes(xintercept=730))+
geom_text(aes(x = 500,y=.8,label = paste0("p = " ,pvalue) ))
$\endgroup$

Not the answer you're looking for? Browse other questions tagged or ask your own question.