m.rodwell
m.rodwell

Reputation: 39

Survival Curves in R

I am new to survival curves and trying to graph them in R. I have the following columns in my excel file: Sample (Experimental or Control), Age (Old or Young), Sex (Male or Female), Bacteria status (either positive or negative) and days 0-20 (21 separate columns); see below for an example. My survival lasts 20 days and starts at day 0. Day 0 has the most and my samples die throughout and can end (n=0) before day 20, and sometimes there are a few remaining at the last day. I would then like x axis=day and y axis=survival probability and to compare experimental vs controls, sex, age and bacteria status using Log-Rank (Mantel-Cox) test.

Sample Sex Young Bacteria Status Day 0 Day 1 Day 2
Experimental 1 Male Young Positive 35 34 33
Control 1 Female Old Negative 35 35 35

Upvotes: 0

Views: 425

Answers (1)

Stoer
Stoer

Reputation: 51

Try out the "survival", "survminer", "condSURV" packages, "ggsurvfit" for plotting. You need to format your data correctly so make a new dataset. When someone dies you need to make a row with 3 columns minimum: Day, Survival, Group.

  • Day is the day of the experiment so a number between 1-21.
  • Survival is a binary number (0 or 1), with 1 meaning they died. Only use 1's when the experiment is ongoing. Only at the end of the experiment you make as many rows as there are survivors and put down 0's in the survival column meaning they are censored/survived/left the study.
  • Group is your experimental condition (i.e. presence of bacteria)

So if someone dies on day 1, you put in 1 for day, 1 for survival (they died), and "infected" or something for group, assuming they are infected. If 4 people day on day 10, thats 4 separate rows each with 10's in the day column, 1's in the survival column (they all died) and their respective groups in the group column i.e. infected or not infected.

Then just use the survival analysis code on the data, for example to compare the survival of the groups:

survdiff(Surv(Day, Survival) ~ Group, data = survival_data)

For a pairwise version its:

pairwise_survdiff(Surv(Day,Survival) ~ Group, data = survival_data)

Or to plot the survival curves:

survfit2(Surv(Day, Survival) ~ Group, data = survival_data)%>%
  ggsurvfit()

Upvotes: 0

Related Questions