# Log Rank Test in R-Survival Curve Comparison

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

**Test hypotheses:**

Ho: In terms of survivability, there is no difference between the two groups.

Hi: There is a survival differential between the two groups.

We can reject the null hypothesis and infer 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 0.05 (95% confidence level).

LSTM Network in R » Recurrent Neural network »

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

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

The Chi-Squared test statistic and related p-value are returned by the above function

## log-rank test in R.

Following libraries are required for the analysis.

library("survival") library("survminer") library("Rcpp")

The lung cancer data was utilized from the survival package.

Linear optimization using R » Optimal Solution »

data("lung") head(lung)

inst time status age sex ph.ecog ph.karno pat.karno meal.cal wt.loss 1 3 306 2 74 1 1 90 100 1175 NA 2 3 455 2 68 1 0 90 90 1225 15 3 3 1010 1 56 1 0 90 90 NA 15 4 5 210 2 57 1 1 90 60 1150 11 5 1 883 2 60 1 0 100 90 NA 0 6 12 1022 1 74 1 1 50 80 513 0

Type the following code to compute survival curves:

fit <- survfit(Surv(time, status) ~ sex, data = lung) print(fit)

Call: survfit(formula = Surv(time, status) ~ sex, data = lung) n events median 0.95LCL 0.95UCL sex=1 138 112 270 212 310 sex=2 90 53 426 348 550

The sort summary table can be accessed based on the below code

Linear Discriminant Analysis in R » LDA Prediction »

summary(fit)$table

records n.max n.start events *rmean *se(rmean) median 0.95LCL 0.95UCL sex=1 138 138 138 112 325.0663 22.59845 270 212 310 sex=2 90 90 90 53 458.2757 33.78530 426 348 550

## Visulization

The following code allows us to understand the survival curves in a better way.

ggsurvplot(fit, pval = TRUE, conf.int = TRUE, risk.table = TRUE, # Add risk table risk.table.col = "strata", # Change risk table color by groups linetype = "strata", # Change line type by groups surv.median.line = "hv", # Specify median survival ggtheme = theme_bw(), # Change ggplot2 theme palette = c("#E7B800", "#2E9FDF"))

The survival chance is 1.0 at time zero (or 100 percent of the participants are alive).

At time 250, the chances of survival for sex=1 are about 0.55 (or 55 percent) and 0.75 (or 75 percent) for sex=2.

The median survival time for sex=1 is 270 days and for sex=2 is 426 days, indicating that sex=2 has a better survival rate than sex=1.

The following code shows how to perform a log-rank test to determine if there is a difference in survival between sex who received different treatments:

Decision Trees in R » Classification & Regression »

### Perform log-rank test

surv_diff <- survdiff(Surv(time, status) ~ sex, data = lung) surv_diff

Call: survdiff(formula = Surv(time, status) ~ sex, data = lung) N Observed Expected (O-E)^2/E (O-E)^2/V sex=1 138 112 91.6 4.55 10.3 sex=2 90 53 73.4 5.68 10.3 Chisq= 10.3 on 1 degrees of freedom, p= 0.001

The Chi-Squared test statistic is 10.3 with 1 degree of freedom and the corresponding p-value is 0.001. Since this p-value is less than 0.05, we reject the null hypothesis.

In other words, we have sufficient evidence to say that there is a statistically significant difference in survival between the two groups.