In this article, we share with you what the log-rank test in R is and how to perform the log-rank test by the
survdiff() function. It is one of the methods to work with survival analysis. Let’s go into detail now.
What does the log-rank test do in R?
Before you learn what the log-rank test does, we will introduce you to the
survival analysis previously. Survival analysis is the collection of statistical methods which is used to analyze the duration or the survival of time until an event happens. For example, analyze the duration until the patient dies or relapses the disease. And the log-rank test, which we are caring of, is one of the methods to perform the survival analysis in R. It is a test that is used to compare two or more survival curves. In theory, to perform the log-rank test, you have to perform complicated calculations. But in R, you can use the
survdiff() function, which is built in the ‘survival’ package to perform the log-rank test. Let’s take a look at the
survival:: survdiff(formula, data, subset, na.action, rho, timefix)
- formula: The formula expression, which is used for other models.
- data: The data is used with the formula of the function.
- subset: The expression that indicates which subset of rows should be used appropriately.
- na.action: The function to filter the missing values.
- rho: The default is 0. The scalar parameter.
- timefix: The default is TRUE.
After learning what the log-rank test in R is, and the syntax of the function to perform, you will learn how to use it in the next title below.
How to perform the log-rank test in R?
To perform the log-rank test, you can use the
survdiff() function, which is built into the
To use the
survdiff() function, you have to install the ‘survival’ package first by the following command.
Prepare the dataset
In this example, I will use the dataset named ‘lung’, which is also built in the ‘survival’ package.
# Load the library library("survival") library("dplyr") df <- lung %>% slice(10:40)
Perform the log-rank test by the
You can use the
survdiff() function to perform the log-rank test with the created data frame.
Look at the example below.
library("survival") library("dplyr") df <- lung %>% slice(10:40) log_rank_test <- survdiff(Surv(time, status) ~ sex, data = df) log_rank_test
Call: survdiff(formula = Surv(time, status) ~ sex, data = df) N Observed Expected (O-E)^2/E (O-E)^2/V sex=1 21 21 17.5 0.716 1.82 sex=2 10 9 12.5 0.998 1.82 Chisq= 1.8 on 1 degrees of freedom, p= 0.2
The Chi-Squared test statistic is 1.8 on 1 degree of the freedom with the corresponding p-value is 0.2
You can also learn the R language with the series of R tutorials here.
We have introduced you to the log-rank test in R, which is one of the methods of
survival analysis. You can perform the log-rank test by the
survdiff() function in the ‘survival’ package. If you have any questions about this tutorial, leave your comment below. Thanks!
Maybe you are interested:
Name of the university: PTIT