Skip to content

Data Science Tutorials

For Data Science Learners

  • How to add Axes to Plot in R R
  • How to create Sankey plot in R
    How to create a Sankey plot in R? R
  • Using describeBy() in R: A Comprehensive Guide R
  • Extract patterns in R
    Extract patterns in R? R
  • Type II Error in R
    Type II Error in R R
  • Sort Data in R With Examples
    Sort Data in R With Examples R
  • How to remove files and folders in R
    How to remove files and folders in R R
  • Crosstab calculation in R
    Crosstab calculation in R R
Survival Plot in R

How to Perform a Log Rank Test in R

Posted on May 30May 30 By Admin No Comments on How to Perform a Log Rank Test in R

 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.

R

Post navigation

Previous Post: How to compare variances in R
Next Post: Interactive 3d plot in R-Quick Guide

Related Posts

  • Cumulative Sum calculation in R
    Cumulative Sum calculation in R R
  • Creating a Histogram of Two Variables in R R
  • Augmented Dickey-Fuller Test in R
    Augmented Dickey-Fuller Test in R R
  • Jarque-Bera Test in R
    Jarque-Bera Test in R With Examples R
  • Find the Maximum Value by Group in R
    Find the Maximum Value by Group in R R
  • Ogive Graph in R
    Ogive Graph in R R

Leave a Reply Cancel reply

Your email address will not be published. Required fields are marked *

  • Best Prompt Engineering Books
  • Understanding Machine Learning and Data Science
  • Best Git Books
  • Top 5 Books to Learn Data Engineering
  • Mastering R Programming for Data Science: Tips and Tricks
  • About Us
  • Contact
  • Disclaimer
  • Privacy Policy

https://www.r-bloggers.com

  • YouTube
  • Twitter
  • Facebook
  • Course
  • Excel
  • Machine Learning
  • Opensesame
  • R
  • Statistics

Check your inbox or spam folder to confirm your subscription.

  • Error attempt to apply non function in r
    Error attempt to apply non function in r R
  • Hypothesis Testing in R Programming
    Hypothesis Testing in R Programming R
  • Normal distribution in R
    Normal Distribution in R R
  • Group By Sum in R
    Group By Sum in R R
  • Convert Multiple Columns to Numeric in R
    Convert Multiple Columns to Numeric in R R
  • How to Scale Only Numeric Columns in R
    How to Scale Only Numeric Columns in R R
  • Data Science Challenges in R Programming Language
    Data Science Challenges in R Programming Language Machine Learning
  • How to Count Distinct Values in R
    How to Count Distinct Values in R R

Privacy Policy

Copyright © 2025 Data Science Tutorials.

Powered by PressBook News WordPress theme