Â 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.