---
# System prepended metadata

title: 存活分析 (Survival analysis)
tags: [基礎統計, R教學]

---

# 存活分析 (Survival analysis) 
###### tags: `R教學` `基礎統計`


練習檔案：[survival analysis.csv](https://drive.google.com/file/d/1jBDiXEY53HUpWkiC1d6l8_G7B80uzw92/view?usp=sharing)

參考資料：
1. [Survival Analysis in R](https://www.emilyzabor.com/tutorials/survival_analysis_in_r_tutorial.html)
2. [Survival Analysis in R For Beginners](https://www.datacamp.com/community/tutorials/survival-analysis-R)

```r=
rm(list=ls())
setwd(choose.dir())

library("survival")
library("multcomp")

dat <- read.csv("survival analysis.csv", head=T)
dat$trt <- as.factor(dat$trt)

dat$SurvObj <- with(dat, Surv(day , status == 0))

Sur.dat <- survfit(SurvObj ~ trt, data = dat)
summary(aml.surv.dat <- survreg(SurvObj ~ trt + as.factor(rep), data = dat))
summary(aml.surv.glht.dat <- glht(aml.surv.dat, linfct = mcp(trt = "Tukey")))
letter.dat <- cld(aml.surv.glht.dat,
                  alpha   = 0.05,
                  Letters = letters,
                  decreasing = TRUE)

tiff(filename = "survival.dat.tif",
     width = 350*3, height = 280*3, pointsize = 20)
plot(Sur.dat
     ,col=1:5
     ,lty=1:5
     ,lwd=5 ,las=1 ,xlab="day", ylab="survival rate", ylim=c(0,1), xlim=c(0,5.2))
legend(0,0,legend=paste0(levels(dat$trt), "(", letter.dat$mcletters$Letters, ")"), 
       col=1:5, lty=1:5, cex=1.5, yjust=0, lwd=2)
dev.off()
```
![](https://i.imgur.com/qw54ME7.jpg)
