R語言 生存分析

生存分析處理預(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>

,使用R生存分析

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

最后編輯于
?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請(qǐng)聯(lián)系作者
【社區(qū)內(nèi)容提示】社區(qū)部分內(nèi)容疑似由AI輔助生成,瀏覽時(shí)請(qǐng)結(jié)合常識(shí)與多方信息審慎甄別。
平臺(tái)聲明:文章內(nèi)容(如有圖片或視頻亦包括在內(nèi))由作者上傳并發(fā)布,文章內(nèi)容僅代表作者本人觀點(diǎn),簡(jiǎn)書系信息發(fā)布平臺(tái),僅提供信息存儲(chǔ)服務(wù)。

相關(guān)閱讀更多精彩內(nèi)容

  • Spring Cloud為開發(fā)人員提供了快速構(gòu)建分布式系統(tǒng)中一些常見模式的工具(例如配置管理,服務(wù)發(fā)現(xiàn),斷路器,智...
    卡卡羅2017閱讀 136,564評(píng)論 19 139
  • 有朋友跟我私下抱怨:自從開通微信,加了一堆朋友后,現(xiàn)在搞得她每天要花一兩個(gè)小時(shí)看朋友圈。一開始覺得好玩,現(xiàn)在覺得好...
    看車侃車閱讀 453評(píng)論 1 0
  • 對(duì)于帶參數(shù)的方法,方法體內(nèi)不但可以使用類中定義的屬性,還可以使用參數(shù)列表中的參數(shù)。 定義 形式參數(shù)就是函數(shù)(方法)...
    _Dansh閱讀 1,888評(píng)論 0 0
  • 我期待的是閱讀 114評(píng)論 0 0
  • 拋出幾個(gè)問題為什么有時(shí)候茶葉喝起來澀澀的?為什么有些茶葉喝起來有很鮮爽的感覺?為什么人們常說喝茶能夠抗癌,預(yù)防衰老...
    Stark醬閱讀 864評(píng)論 0 2

友情鏈接更多精彩內(nèi)容