生存分析處理預(yù)測(cè)特定事件將要發(fā)生的時(shí)間。 它也被稱為故障時(shí)間分析或分析死亡時(shí)間。 例如,預(yù)測(cè)患有癌癥的人將存活的天數(shù)或預(yù)測(cè)機(jī)械系統(tǒng)將失敗的時(shí)間。
命名為survival的R語言包用于進(jìn)行生存分析。 此包包含函數(shù)Surv(),它將輸入數(shù)據(jù)作為R語言公式,并在選擇的變量中創(chuàng)建一個(gè)生存對(duì)象用于分析。 然后我們使用函數(shù)survfit()創(chuàng)建一個(gè)分析圖。
安裝軟件包
<pre class="result notranslate" style="margin: 15px 0px; padding: 10px 5px; position: relative; width: auto; max-width: 700px; box-sizing: border-box; display: block; line-height: 1.7; background: rgb(239, 239, 239); border-radius: 3px; font-size: 14px; font-family: Consolas, "Courier New", Courier, monospace; overflow-x: auto; border: 1px solid rgb(221, 221, 221); word-wrap: break-word !important; white-space: pre-wrap !important; color: rgb(0, 0, 0); font-style: normal; font-variant-ligatures: normal; font-variant-caps: normal; font-weight: normal; letter-spacing: normal; orphans: 2; text-align: start; text-indent: 0px; text-transform: none; widows: 2; word-spacing: 0px; -webkit-text-stroke-width: 0px; text-decoration-style: initial; text-decoration-color: initial;">install.packages("survival")
</pre>
語法
在R語言中創(chuàng)建生存分析的基本語法是 -
<pre class="result notranslate" style="margin: 15px 0px; padding: 10px 5px; position: relative; width: auto; max-width: 700px; box-sizing: border-box; display: block; line-height: 1.7; background: rgb(239, 239, 239); border-radius: 3px; font-size: 14px; font-family: Consolas, "Courier New", Courier, monospace; overflow-x: auto; border: 1px solid rgb(221, 221, 221); word-wrap: break-word !important; white-space: pre-wrap !important; color: rgb(0, 0, 0); font-style: normal; font-variant-ligatures: normal; font-variant-caps: normal; font-weight: normal; letter-spacing: normal; orphans: 2; text-align: start; text-indent: 0px; text-transform: none; widows: 2; word-spacing: 0px; -webkit-text-stroke-width: 0px; text-decoration-style: initial; text-decoration-color: initial;">Surv(time,event)
survfit(formula)
</pre>
以下是所使用的參數(shù)的描述 -
time是直到事件發(fā)生的跟蹤時(shí)間。
event指示預(yù)期事件的發(fā)生的狀態(tài)。
formula是預(yù)測(cè)變量之間的關(guān)系。
例
我們將考慮在上面安裝的生存包中存在的名為“pbc”的數(shù)據(jù)集。 它描述了關(guān)于受肝原發(fā)性膽汁性肝硬化(PBC)影響的人的生存數(shù)據(jù)點(diǎn)。 在數(shù)據(jù)集中存在的許多列中,我們主要關(guān)注字段“time”和“status”。 時(shí)間表示在接受肝移植或患者死亡的患者的登記和事件的較早之間的天數(shù)。
<pre class="prettyprint notranslate tryit" style="margin: 15px 0px; padding: 10px 5px; position: relative; width: auto; max-width: 700px; box-sizing: border-box; display: block; line-height: 1.7; background: rgb(239, 239, 239); border-radius: 3px; font-size: 14px; font-family: Consolas, "Courier New", Courier, monospace; overflow-x: auto; border: 1px solid rgb(221, 221, 221); word-wrap: break-word !important; white-space: pre-wrap !important; color: rgb(0, 0, 0); font-style: normal; font-variant-ligatures: normal; font-variant-caps: normal; font-weight: normal; letter-spacing: normal; orphans: 2; text-align: start; text-indent: 0px; text-transform: none; widows: 2; word-spacing: 0px; -webkit-text-stroke-width: 0px; text-decoration-style: initial; text-decoration-color: initial;"># Load the library.
library("survival")
Print first few rows.
print(head(pbc))
</pre>
當(dāng)我們執(zhí)行上面的代碼,它產(chǎn)生以下結(jié)果及圖表 -
<pre class="result notranslate" style="margin: 15px 0px; padding: 10px 5px; position: relative; width: auto; max-width: 700px; box-sizing: border-box; display: block; line-height: 1.7; background: rgb(239, 239, 239); border-radius: 3px; font-size: 14px; font-family: Consolas, "Courier New", Courier, monospace; overflow-x: auto; border: 1px solid rgb(221, 221, 221); word-wrap: break-word !important; white-space: pre-wrap !important; color: rgb(0, 0, 0); font-style: normal; font-variant-ligatures: normal; font-variant-caps: normal; font-weight: normal; letter-spacing: normal; orphans: 2; text-align: start; text-indent: 0px; text-transform: none; widows: 2; word-spacing: 0px; -webkit-text-stroke-width: 0px; text-decoration-style: initial; text-decoration-color: initial;"> id time status trt age sex ascites hepato spiders edema bili chol
1 1 400 2 1 58.76523 f 1 1 1 1.0 14.5 261
2 2 4500 0 1 56.44627 f 0 1 1 0.0 1.1 302
3 3 1012 2 1 70.07255 m 0 0 0 0.5 1.4 176
4 4 1925 2 1 54.74059 f 0 1 1 0.5 1.8 244
5 5 1504 1 2 38.10541 f 0 1 1 0.0 3.4 279
6 6 2503 2 2 66.25873 f 0 1 0 0.0 0.8 248
albumin copper alk.phos ast trig platelet protime stage
1 2.60 156 1718.0 137.95 172 190 12.2 4
2 4.14 54 7394.8 113.52 88 221 10.6 3
3 3.48 210 516.0 96.10 55 151 12.0 4
4 2.54 64 6121.8 60.63 92 183 10.3 4
5 3.53 143 671.0 113.15 72 136 10.9 3
6 3.98 50 944.0 93.00 63 NA 11.0 3
</pre>
從上述數(shù)據(jù),我們正在考慮分析的時(shí)間和狀態(tài)。
應(yīng)用Surv()和survfit()函數(shù)
現(xiàn)在我們繼續(xù)應(yīng)用Surv()函數(shù)到上面的數(shù)據(jù)集,并創(chuàng)建一個(gè)將顯示趨勢(shì)圖。
<pre class="prettyprint notranslate tryit" style="margin: 15px 0px; padding: 10px 5px; position: relative; width: auto; max-width: 700px; box-sizing: border-box; display: block; line-height: 1.7; background: rgb(239, 239, 239); border-radius: 3px; font-size: 14px; font-family: Consolas, "Courier New", Courier, monospace; overflow-x: auto; border: 1px solid rgb(221, 221, 221); word-wrap: break-word !important; white-space: pre-wrap !important; color: rgb(0, 0, 0); font-style: normal; font-variant-ligatures: normal; font-variant-caps: normal; font-weight: normal; letter-spacing: normal; orphans: 2; text-align: start; text-indent: 0px; text-transform: none; widows: 2; word-spacing: 0px; -webkit-text-stroke-width: 0px; text-decoration-style: initial; text-decoration-color: initial;"># Load the library.
library("survival")
Create the survival object.
survfit(Surv(pbc$time,pbc$status == 2)~1)
Give the chart file a name.
png(file = "survival.png")
Plot the graph.
plot(survfit(Surv(pbc$time,pbc$status == 2)~1))
Save the file.
dev.off()
</pre>
當(dāng)我們執(zhí)行上面的代碼,它產(chǎn)生以下結(jié)果及圖表 -
<pre class="result notranslate" style="margin: 15px 0px; padding: 10px 5px; position: relative; width: auto; max-width: 700px; box-sizing: border-box; display: block; line-height: 1.7; background: rgb(239, 239, 239); border-radius: 3px; font-size: 14px; font-family: Consolas, "Courier New", Courier, monospace; overflow-x: auto; border: 1px solid rgb(221, 221, 221); word-wrap: break-word !important; white-space: pre-wrap !important; color: rgb(0, 0, 0); font-style: normal; font-variant-ligatures: normal; font-variant-caps: normal; font-weight: normal; letter-spacing: normal; orphans: 2; text-align: start; text-indent: 0px; text-transform: none; widows: 2; word-spacing: 0px; -webkit-text-stroke-width: 0px; text-decoration-style: initial; text-decoration-color: initial;">Call: survfit(formula = Surv(pbc$time, pbc$status == 2) ~ 1)
n events median 0.95LCL 0.95UCL
418 161 3395 3090 3853
</pre>

上圖中的趨勢(shì)有助于我們預(yù)測(cè)在特定天數(shù)結(jié)束時(shí)的生存概率。