<p><div><div><div><div data-show-faces="false" data-height="21" data-width="100" data-layout="button_count" data-send="true" data-href="https://www.r-bloggers.com/survival-analysis-part-i/"></div></div><div></div></div><div></div></div>
<div>
(This article was first published on <strong><a href="https://datascienceplus.com/survival-analysis-part-i/"> R Programming – DataScience+</a></strong>, and kindly contributed to <a rel="nofollow" href="https://www.r-bloggers.com/">R-bloggers)</a> </div>
<p>In this article I am going to talk about the non-parametric techniques used for survival analysis. To comprehend this article effectively, you’ll need basic understanding of probability, statistics and R. If you have any questions regarding the concept or the code, feel free to comment, I’ll be more than happy to get back to you.</p>
<p>Survival analysis is a set of methods to analyze the ‘time to occurrence’ of an event. The response is often referred to as a failure time, survival time, or event time. These methods are widely used in clinical experiments to analyze the ‘time to death’, but nowadays these methods are being used to predict the ‘when’ and ‘why’ of customer churn or employee turnover as well.</p>
<p>The dependent variables for the analysis are generally two functions:</p>
<p><b>Survival Function</b>: It is the probability that an individual survives beyond a specific time T. It has the following properties:</p>
<ul>
<li>It is non-increasing</li>
<li>At time t = 0, S(t) = 1. In other words, the probability of surviving past time 0 is 1</li>
<li>At time t = ∞, S(t) = S(∞) = 0. As time goes to infinity, the survival curve goes to 0</li>
</ul>
<p>In theory, the survival function is smooth. In practice, events are observed on a discrete time scale</p>
<p><b>Hazard Function</b>: the probability of failure in an infinitesimally small time period given that the individual has survived up till the present time. The cumulative hazard describes the accumulated risk up to the present time.</p>
<p>To estimate these two functions, three types of analysis are generally performed:</p>
<ul>
<li>Non-parametric</li>
<li>Semi-parametric</li>
<li>Parametric</li>
</ul>
<h3>Censoring of Data</h3>
<p>The observed data in this type of analysis is generally consisted of a binary event variable (event occurred =1) and a ‘time to the event’ variable. In some observations the time to event variable can be blank if the participants drop out before the study ends or the participants are event free at the end of the observation period. Those types of observations are called censored.</p>
<p>Censoring can be of two types:</p>
<p><b>Right Censoring</b>: where the time of event is unknown when the subject is removed from the study or the time of event is after the end of the study. There can be three types of Right Censoring:</p>
<ul>
<li>Fixed Type I Censoring: occurs when a study is designed to end after C years of follow up. In this case everyone who doesn’t have an event observed during the course of the study is censored at C years</li>
<li>Random Type I Censoring: the study is designed to end after C years, but censored subjects don’t have the same censoring type</li>
<li>Type II Censoring: a study ends when there is a pre-specified number of events</li>
</ul>
<p><b>Left Censoring</b>: where the subject’s survival time is incomplete on the left side of the follow up period e.g. the exact time of exposure is unknown</p>
<p>The same observation can be both left and right censored, termed as interval censoring.</p>
<p>Only one condition of censoring is that it must be independent of the event being looked at. Otherwise the estimates of the survival distribution can be seriously biased. This is called non-informative censoring.</p>
<h3>Why not Linear Regression?</h3>
<p>Survival times are typically positive numbers, ordinary linear regression may not be the best choice unless these times are first transformed in a way that removes this restriction</p>
<p>OLS can’t handle censoring of observations effectively</p>
<p>Difference between Logistic Regression and Survival Analysis:<br>
<a rel="nofollow" href="https://i2.wp.com/datascienceplus.com/wp-content/uploads/2017/09/1.png?ssl=1" target="_blank"><img sizes="(max-width: 490px) 100vw, 490px" src="https://i1.wp.com/datascienceplus.com/wp-content/uploads/2017/09/1-490x124.png?w=450&ssl=1" alt="" class="wp-image-8381" data-recalc-dims="1"></a></p>
<h2>Non-parametric Method (Kaplan Meier Estimate)</h2>
<p>The Kaplan-Meier survival curve is defined as the probability of surviving in a given length of time while considering time in many small intervals. </p>
<p>There are three assumptions used in this analysis:</p>
<ul>
<li>At any time records which are censored have the same survival prospects as those who continue to be followed</li>
<li>Survival probabilities are the same for subjects recruited early and late in the study</li>
<li>Event happens at the time specified</li>
</ul>
<p>The Kaplan-Meier estimate is also called as “product limit estimate”. It involves computing of probabilities of occurrence of an event at a certain point of time. These successive probabilities are multiplied by any earlier computed probabilities to get the final estimate.</p>
<p>For each time interval, survival probability is calculated as the number of subjects surviving divided by the number of patients at risk. Subjects who have died, dropped out, or move out are not counted as “at risk” i.e., subjects who are lost are considered “censored” and are not counted in the denominator. Total probability of survival till that time interval is calculated by multiplying all the probabilities of survival at all time intervals preceding that time.</p>
<h3>Comparing multiple Survival Curves</h3>
<p>Curves for two different groups of subjects can be compared. We can look for gaps in these curves in a horizontal or vertical direction. A vertical gap means that at a specific time point, one group had a greater fraction of subjects surviving. A horizontal gap means that it took longer for one group to experience a certain fraction of deaths.</p>
<p>The two survival curves can be compared statistically by testing the null hypothesis i.e. there is no difference regarding survival among two interventions. This null hypothesis is statistically tested by another test known as log-rank test.</p>
<h3>Log-Rank Test</h3>
<p>The log-rank test is the most commonly-used statistical test for comparing the survival distributions of two or more groups.</p>
<p>The null hypothesis is that the hazard rates of all populations are equal at all times less than the maximum observed time and the alternative hypothesis is that at least two of the hazard rates are different at some time less than the observed maximum time. </p>
<p>The test statistic is:<br>
<a rel="nofollow" href="https://i0.wp.com/datascienceplus.com/wp-content/uploads/2017/09/2.png?ssl=1" target="_blank"><img alt="" src="https://i0.wp.com/datascienceplus.com/wp-content/uploads/2017/09/2.png?resize=238%2C52&ssl=1" class="wp-image-8382" data-recalc-dims="1"></a><br>
where the O1 and O2 are the total numbers of observed events in groups 1 and 2, respectively, and E1 and E2 the total numbers of expected events.</p>
<p>The total expected number of events for a group is the sum of the expected number of events at the time of each event. The expected number of events at the time of an event can be calculated as the risk for death at that time multiplied by the number alive in the group. Under the null hypothesis, the risk of death (number of deaths/number alive) can be calculated from the combined data for both groups.</p>
<p>The test statistic is compared with a χ2 distribution with 1 degree of freedom.</p>
<p>It has the considerable advantage that it does not require us to know anything about the shape of the survival curve or the distribution of survival times.</p>
<p>The log-rank test is based on the same assumptions as the Kaplan Meier survival curve – namely, that censoring is unrelated to prognosis, the survival probabilities are the same for subjects recruited early and late in the study, and the events happened at the times specified.</p>
<p>The log-rank test is most likely to detect a difference between groups when the risk of an event is consistently greater for one group than another. It is unlikely to detect a difference when survival curves cross.</p>
<p>These tests are very useful in assessing whether a covariate affects survival. However, they do not allow us to say how survival is affected. Ideally, we’d like to be able to say how much more at risk on group is than another. This can be done using Cox’s proportional hazards model, a semi-parametric model to investigate the functional relationship between the covariates and survival.</p>
<h3>Pointwise Confidence Intervals of Survival</h3>
<p>A pointwise confidence interval for the survival probability at a specific time t is represented by two confidence limits which have been constructed so that the probability that the true survival probability lies between them is 1− α. These limits are constructed for a single time point. Several of them cannot be used together to form a confidence band such that the entire survival function lies within the band. When these are plotted with the survival curve, these limits must be interpreted on an individual, point by point, basis.</p>
<p>Three difference confidence intervals are available. All three confidence intervals perform about the same in large samples. The linear (Greenwood) interval is the most commonly used. However, the log-transformed and the arcsine-square intervals behave better in small to moderate samples, so they are recommended.</p>
<h2>Kaplan Meier Estimate in R</h2>
<p>To create a survival object:</p>
<pre>Surv(time, time2, event)</pre>
<p>where, <i>time</i> is for right censored data<br>
<i>time2</i> is ending time of the interval for interval censored or counting process data only<br>
<i>type</i> is a character string specifying the type of censoring. </p>
<p>When the type argument is missing the code assumes a type based on the following rules:</p>
<ul>
<li>If there are two unnamed arguments, they will match time and event in that order. If there are three unnamed arguments they match time, time2 and event</li>
<li>If the event variable is a factor then type ‘mstate’ is assumed. Otherwise type ‘right’ if there is no time2 argument, and type ‘counting’ if there is</li>
</ul>
<p>To create survival curves:</p>
<pre>survfit( formula)</pre>
<p>where, formula has a Surv object as the response on the left of the ~ operator and for a single survival curve the right hand side should be ~ 1.</p>
<p>For eg.</p>
<pre>model&lt;- survfit(Surv(time,event) ~ 1)</pre>
<p>For multiple survival curves, the right hand side can be the variable to differentiate survival functions </p>
<p>For eg.</p>
<pre>model&lt;- survfit(Surv(time,event) ~ group)</pre>
<p>To plot the survival curve:</p>
<pre>Plot(survfit( formula))</pre>
<p>To understand if there is a statistical difference between two survival curves:</p>
<pre>survdiff(survival ~ group, rho=0)</pre>
<p>where <i>rho</i> a scalar parameter that controls the type of test. With rho = 0 this is the log-rank or Mantel-Haenszel test, and with rho = 1 it is equivalent to the Peto &amp; Peto modification of the Gehan-Wilcoxon test.</p>
<p>To give greater weight to the first part of the survival curves, use rho larger than 0. To give weight to the later part of the survival curves, use rho smaller than 0.</p>
<p>For a detailed example of the above functions, please have a look at the <a rel="nofollow" href="https://www.kaggle.com/subhasree/employee-attrition-using-survival-analysis" target="_blank">this link</a>.</p>
<p>In a future article, I’ll discuss semi-parametric i.e cox proportional hazard model and parametric models for survival analysis.</p>
<p><b>References:</b></p>
<ul>
<a rel="nofollow" href="https://www.ncbi.nlm.nih.gov/pmc/articles/PMC1065034/" target="_blank">Statistics review 12: Survival analysis</a><br>
<a rel="nofollow" href="https://rpubs.com/daspringate/survival" target="_blank">Survival analysis by David Springate</a><br>
<a rel="nofollow" href="http://www.stats.ox.ac.uk/~mlunn/lecturenotes1.pdf" target="_blank">Lecture notes on Survival Analysis by stats.ox.ac.uk</a><br>
<a rel="nofollow" href="https://www.openintro.org/download.php?file=survival_analysis_in_R&referrer=/stat/surv.php" target="_blank">Survival Analysis in R by David M Diez</a><br>
<a rel="nofollow" href="https://cran.r-project.org/web/packages/survival/vignettes/timedep.pdf" target="_blank">Using Time Dependent Covariates and Time Dependent Coefficients in the Cox Model</a></ul>
<ul></ul>
<p><strong>
<p>Related Post</p>
<p>
<ol>
<li><a rel="nofollow" href="https://datascienceplus.com/linear-regression-from-scratch-in-python/" title="Permanent Link toLinear Regression from Scratch in Python" target="_blank">Linear Regression from Scratch in Python</a></li>
<li><a rel="nofollow" href="https://datascienceplus.com/generalized-additive-models/" title="Permanent Link toGeneralized Additive Models" target="_blank">Generalized Additive Models</a></li>
<li><a rel="nofollow" href="https://datascienceplus.com/second-step-with-non-linear-regression-adding-predictors/" title="Permanent Link toSecond step with non-linear regression: adding predictors" target="_blank">Second step with non-linear regression: adding predictors</a></li>
<li><a rel="nofollow" href="https://datascienceplus.com/weather-forecast-with-regression-models-part-4/" title="Permanent Link toWeather forecast with regression models – part 4" target="_blank">Weather forecast with regression models – part 4</a></li>
<li><a rel="nofollow" href="https://datascienceplus.com/weather-forecast-with-regression-models-part-3/" title="Permanent Link toWeather forecast with regression models – part 3" target="_blank">Weather forecast with regression models – part 3</a></li>
</ol></strong>
	<p>
<div>
<div>To <strong>leave a comment</strong> for the author, please follow the link and comment on their blog: <strong><a href="https://datascienceplus.com/survival-analysis-part-i/"> R Programming – DataScience+</a></strong>.</div>
<hr>
<a rel="nofollow" href="https://www.r-bloggers.com/">R-bloggers.com</a> offers <strong><a rel="nofollow" href="https://feedburner.google.com/fb/a/mailverify?uri=RBloggers">daily e-mail updates</a></strong> about <a rel="nofollow" href="https://www.r-project.org/" title="The R Project for Statistical Computing">R</a> news and <a rel="nofollow" href="https://www.r-bloggers.com/search/tutorial" title="R tutorials">tutorials</a> on topics such as: <a rel="nofollow" href="https://www.r-bloggers.com/search/data%20science" title="Data science">Data science</a>, <a rel="nofollow" href="https://www.r-bloggers.com/search/Big%20Data" title="Big Data">Big Data, <a rel="nofollow" href="https://www.r-users.com/" title="R jobs">R jobs</a>, visualization (<a rel="nofollow" href="https://www.r-bloggers.com/search/ggplot2" title="ggplot and ggplot2 tutorials">ggplot2</a>, <a rel="nofollow" href="https://www.r-bloggers.com/search/boxplot" title="Boxplots using lattice and ggplot2 tutorials">Boxplots</a>, <a rel="nofollow" href="https://www.r-bloggers.com/search/map" title="Maps and gis">maps</a>, <a rel="nofollow" href="https://www.r-bloggers.com/search/animation" title="Animation in R">animation</a>), programming (<a rel="nofollow" href="https://www.r-bloggers.com/search/RStudio" title="RStudio IDE for R">RStudio</a>, <a rel="nofollow" href="https://www.r-bloggers.com/search/sweave" title="Sweave and literate programming">Sweave</a>, <a rel="nofollow" href="https://www.r-bloggers.com/search/LaTeX" title="LaTeX in R">LaTeX</a>, <a rel="nofollow" href="https://www.r-bloggers.com/search/SQL" title="SQL and databases">SQL</a>, <a rel="nofollow" href="https://www.r-bloggers.com/search/eclipse" title="Eclipse IDE for R">Eclipse</a>, <a rel="nofollow" href="https://www.r-bloggers.com/search/git" title="git and github, Version Control System">git</a>, <a rel="nofollow" href="https://www.r-bloggers.com/search/hadoop" title="Large data in R using Hadoop">hadoop</a>, <a rel="nofollow" href="https://www.r-bloggers.com/search/Web+Scraping" title="Web Scraping of google, facebook, yahoo, twitter and more using R">Web Scraping</a>) statistics (<a rel="nofollow" href="https://www.r-bloggers.com/search/regression" title="Regressions and ANOVA analysis tutorials">regression</a>, <a rel="nofollow" href="https://www.r-bloggers.com/search/PCA" title="principal component analysis tutorial">PCA</a>, <a rel="nofollow" href="https://www.r-bloggers.com/search/time+series" title="Time series">time series</a>, <a rel="nofollow" href="https://www.r-bloggers.com/search/trading" title="finance trading">trading</a>) and more...
<img width="1" alt="" src="http://feeds.feedburner.com/~r/RBloggers/~4/MW_Q3QoaJ48" height="1"></a></div>