# KeepNotes blog

Stay hungry, Stay Foolish.

0%

``````library(survival)

l.coxph <- coxph(Surv(time, status) ~ sex, data=lung)
>summary(l.coxph)
Call:
coxph(formula = Surv(time, status) ~ sex, data = lung)

n= 228, number of events= 165

coef exp(coef) se(coef)      z Pr(>|z|)
sex -0.5310    0.5880   0.1672 -3.176  0.00149 **
---
Signif. codes:  0 ‘***’ 0.001 ‘**’ 0.01 ‘*’ 0.05 ‘.’ 0.1 ‘ ’ 1

exp(coef) exp(-coef) lower .95 upper .95
sex     0.588      1.701    0.4237     0.816

Concordance= 0.579  (se = 0.021 )
Likelihood ratio test= 10.63  on 1 df,   p=0.001
Wald test            = 10.09  on 1 df,   p=0.001
Score (logrank) test = 10.33  on 1 df,   p=0.001``````

HR小于1，说明female的风险低于male（根据sex值的大小来，second group relative to the first group）；而exp(-coef)则是反之，是first group relative to the second，其值大于1，同样说明male风险高于female

``````results <- rownames_to_column(lung, var = "id") %>%
select(c("id", "time", "status", "sex")) %>%
bind_cols(tibble(lp = predict(l.coxph, type="lp"),
risk = predict(l.coxph, type="risk"),
expected = predict(l.coxph, type="expected"),
terms = predict(l.coxph, type="terms")))
id time status sex         lp      risk  expected        sex
1   1  306      2   1  0.2096146 1.2332026 0.8280030  0.2096146
2   2  455      2   1  0.2096146 1.2332026 1.3880051  0.2096146
3   3 1010      1   1  0.2096146 1.2332026 3.5060567  0.2096146
4   4  210      2   1  0.2096146 1.2332026 0.5185439  0.2096146
5   5  883      2   1  0.2096146 1.2332026 3.5060567  0.2096146
6   6 1022      1   1  0.2096146 1.2332026 3.5060567  0.2096146
7   7  310      2   2 -0.3214090 0.7251266 0.4999485 -0.3214090
8   8  361      2   2 -0.3214090 0.7251266 0.6107055 -0.3214090
9   9  218      2   1  0.2096146 1.2332026 0.5367625  0.2096146
10 10  166      2   1  0.2096146 1.2332026 0.3309150  0.2096146``````

``````> (lung[1,"sex"] - l.coxph\$means) %*% coef(l.coxph)["sex"]
[,1]
[1,] 0.2096146``````

``````> exp((lung[1,"sex"] - l.coxph\$means) %*% coef(l.coxph)["sex"])
[,1]
[1,] 1.233203``````

``````options(na.action=na.exclude)
l.coxph <- coxph(Surv(time, status) ~ sex+ph.ecog, data=lung)
results <- rownames_to_column(lung, var = "id") %>%
select(c("id", "time", "status", "sex", "ph.ecog")) %>%
bind_cols(tibble(lp = predict(l.coxph, type="lp"),
risk = predict(l.coxph, type="risk"),
expected = predict(l.coxph, type="expected"),
terms = predict(l.coxph, type="terms")))``````

``````> table(results\$sex, results\$ph.ecog)

0  1  2  3
1 36 71 29  1
2 27 42 21  0``````

``````> length(table(results\$lp))
[1] 7``````

``````> length(table(results\$terms))
[1] 6``````