# Анализ выживаемости

```{r}
library(survival)
library(KMsurv)
```

```{r}
data(tongue)
attach(tongue)
(surv <- Surv(time[type==1], delta[type == 1]))
detach(tongue)
```

```{r}
data(psych)
attach(psych)
(surv.temp <- Surv(age, age + time, death))
detach(psych)
```

```{r}
data(tongue)
attach(tongue)
surv <- Surv(time[type==1], delta[type == 1])
fit <- survfit(surv ~ 1)
#fit <- survfit(surv ~ 1, conf.type = "plain")
#fit <- survfit(surv ~ 1, conf.type = "log-log")
summary(fit)
detach(tongue)
```

```{r}
plot(survfit(surv ~ 1, conf.int = 0.9), main = "90% confidence interval")
plot(survfit(surv ~ 1, conf.int = 0.95), main = "95% confidence interval")
plot(survfit(surv ~ 1, conf.int = 0.99), main = "99% confidence interval")
```

## Функция риска

```{r}
data(tongue)
attach(tongue)
surv <- Surv(time[type==1], delta[type == 1])
fit <- survfit(surv ~ 1)
smr <- summary(fit)

# First one
Lambda.1 <- -log(smr$surv)
xs <- c(smr$time, 200)
ys.1 <- c(Lambda.1, tail(Lambda.1, 1))
plot(xs, ys.1, type = "s")

# Second one
summands <- smr$n.event / smr$n.risk
Lambda.2 <- cumsum(summands)
ys.2 <- c(Lambda.2, tail(Lambda.2, 1))
points(xs, ys.2, type = "s", lty = 2)
detach(tongue)
```

## Модель Кокса

```{r}
data(burn)
attach(burn)
surv <- Surv(T1, D1)
fit.1 <- coxph(surv ~ Z1)
fit.2 <- coxph(surv ~ Z1 + as.factor(Z11))
fit.1
fit.2
detach(burn)
```

