How to Perform a Log Rank Test in R, The most frequent technique to compare survival curves between two groups is to use a log-rank test.

The following hypotheses are used in this test

H0: There is no difference in survival between the two groups.

HA: There is a difference in survival between the two groups.

Artificial Intelligence Examples-Quick View – Data Science Tutorials

We can reject the null hypothesis and conclude that there is enough evidence to claim there is a difference in survival between the two groups if the p-value of the test is less than some significance level (e.g. =0.05).

In R, we may use the survdiff() function from the survival package to do a log-rank test, which has the following syntax.

Best Books to Learn R Programming – Data Science Tutorials

## How to Perform a Log Rank Test in R

survdiff(Surv(time, status) ~ predictors, data)

The Chi-Squared test statistic and related p-value are returned by this function.

This function is used to execute a log-rank test in R, as seen in the example below.

### Example: Log Rank Test in R

We’ll use the ovarian dataset from the survival package for this example. The following information about 26 patients may be found in this dataset:

After being diagnosed with ovarian cancer, how long do you live (in months)?

Whether or whether the time spent surviving was censored

(rx = 1 or rx = 2) Type of treatment received

The following code demonstrates how to inspect the dataset’s first six rows.

Principal Component Analysis in R »

library(survival)

Let’s view the first six rows of the dataset

head(ovarian)

futime fustat age resid.ds rx ecog.ps 1 59 1 72.3315 2 1 1 2 115 1 74.4932 2 1 1 3 156 1 66.4658 2 1 2 4 421 0 53.3644 2 2 1 5 431 1 50.3397 2 1 1 6 448 0 56.4301 1 1 2

The following code demonstrates how to use a log-rank test to see if patients who got various treatments had different survival rates.

make a log-rank test

survdiff(Surv(futime, fustat) ~ rx, data=ovarian)

Call: survdiff(formula = Surv(futime, fustat) ~ rx, data = ovarian) N Observed Expected (O-E)^2/E (O-E)^2/V rx=1 13 7 5.23 0.596 1.06 rx=2 13 5 6.77 0.461 1.06 Chisq= 1.1 on 1 degrees of freedom, p= 0.3

With one degree of freedom, the Chi-Squared test statistic is 1.1, and the associated p-value is 0.3. We cannot reject the null hypothesis because the p-value is not smaller than 0.05.

To put it another way, we don’t have enough evidence to establish that the two treatments have a statistically significant difference in survival.

Best Books on Data Science with Python – Data Science Tutorials

The survival curves for each group can alternatively be plotted using the following syntax.

For each therapy group, draw a survival curve.

plot(survfit(Surv(futime, fustat) ~ rx, data = ovarian), xlab = "Time", ylab = "Survival probability")

In R, a plot of survival curves.

Although the survival curves change somewhat, the difference is not statistically significant, according to the log-rank test.