# Survival Analysis in R Programming – Steps To Perform Analysis

**1. Objective**

In this R tutorial, we will discuss Survival Analysis in R. First we will see the meaning of R Survival Analysis. Along with this, we will also cover the syntax and usage R survival analysis in detail. At last, we will look at the functions of R Survival Analysis.

So, let’s start Survival Analysis in R tutorial.

**2. What is Survival Analysis in R?**

In R, survival analysis particularly deals with predicting the time when a specific event is going to occur. It is also known as the analysis of time to death.

**For example**:

To Predict the number of days a person in the last stage will survive.

We use the R package to carry out this analysis.

In the R survival package, a function named surv() takes the input data as an R formula. It creates a survival object among the chosen variables for analysis. Thus, after this survfit() is being used to create a plot for the analysis.

**What is R Survival Analysis?**

- Model time to event (esp. failure)and is used in medicine, biology, actuary, finance, engineering, sociology, etc.
- It is able to account for censoring.
- We can also compare between 2+ groups.
- It is able to access relationship between covariates and survival time.

**i. Install Package**

install.packages(“survival”)

**Syntax**

surv(time,event) survfit(formula)

**Description of the parameters used** −

- time is the follow-up time until the event occurs.
- the event indicates the status of occurrence of the expected event.
- the formula is the relationship between the predictor variables.

Graphical Analysis is also an important part of R.You can follow this below-mentioned link to learn this:

Introduction to Graphical Analysis

**3. Survival Analysis in R**

Let us see the various steps to perform R programming survival analysis:

- Install survival Package: survival >library (survival)
- Create a survival subject: Surv
- Kaplan – Meier Estimator: survfit
- Mantel-Haenzel Test: survdiff
- Cox Model: coxph

Learn more about R- R Introduction

**i. Creating the survival object**

Survival object in R is created by Surv function:

**Usage**

>Surv (time, time2, event, type=c

(‘right’, ‘left’, ‘interval’,

(‘right’, ‘left’, ‘interval’,

‘counting’, ‘interval2’), origin=0)

**ii. Kaplan-Meier Estimator**

- Also known as product-limit estimator
- It is like the censoring version of an empirical survival function
- It generates a stair-step curve
- Variance is estimated by Greenwood’s formula
- It does not account for effect of another covariate

**iii. Kaplan-Meier Estimator (Cont.)**

**It is Computed by the function**: survfit

**Usage**

>survfit (formula, ...)

**iv. Mantel-Haenzel Test**

- It is also known as a log-rank test.
- It is generated from a sequence of 2×2 tables.
- Conditional independence.
- It is efficient in comparing groups differed by categorical variables, but not continuous ones.

**v. Mantel-Haenzel Test (Cont.)**

**Computed by the function**: survdiff

**Usage**

>survdiff (formula, data, subset, na.action, rho=0)

**vi. Cox Model**

- It is also known as proportional hazard model.
- Here the assumption is quite strong.

**vii. Cox Model (Cont.)**

**Computed by the function**: coxph

**Usage**:

>coxph (formula, data=, weights, subset, na. action, init, control, method=c ("efron","breslow","exact"), singular. ok=TRUE, robust=FALSE, model=FALSE, x=FALSE, y=TRUE, ...)

**viii. Cox Model (Cont.)**

**For Baseline**

>pbc.null<-data.frame(age=rep(0,1), edema=rep(0,1),bili=rep(1,1),albumin =rep(1,1),protime=rep(1,1)) =rep(1,1),protime=rep(1,1)) >plot(survfit(cfit,newdata=pbc.null), lwd=2,ylim=c(.99,1),main='baseline survivor‘,xlab ='Days',ylab= 'Survival',conf.int=T)

**ix. Cox Model (Cont.)**

**For mean covariates**

>plot(survfit(cfit),lwd=2,main= 'fitted survival function at mean covariates‘, xlab='Days', mean covariates‘, xlab='Days', ylab='Survival')

**x. Diagnostic of Cox Model**

- Cox model is amazing, but the assumption is strong.
- Schoenfeld residuals etc,

So, this was all in Survival Analysis in R. Hope you like our explanation.

**4. Conclusion – R Survival Analysis**

Hence, we have studied R survival analysis in detail. Moreover, we have also learned its syntax and usages. Along with this, the most important thing we have studied is its functions which help you to understand its real-life applications. Still, if you have any query regarding Survival analysis in R, ask in the comment tab.

Good work