-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathCox_Regression_python_R.qmd
83 lines (53 loc) · 1.28 KB
/
Cox_Regression_python_R.qmd
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
---
title: sample code for implement cox regression using python and R
format: gfm
warning: false
message: false
---
```{r}
#| include: false
library(reticulate)
path <- Sys.which("python")
path <- gsub("\\", "//", path, fixed = TRUE)
use_python(path)
```
# Using R
```{r}
# if (!require(gtsummary)) {
# chooseCRANmirror(ind = 1, graphics = FALSE)
# install.packages("gtsummary")
# library(gtsummary)
# }
# if (!require(stargazer)) {
# chooseCRANmirror(ind = 1, graphics = FALSE)
# install.packages("stargazer")
# library(stargazer)
# }
library(survival)
lung |> names()
lung$status |> table()
dat <- na.omit(lung)
Y <- with(dat, Surv(time, status == 2))
names(dat)
Model <- coxph(Y ~ ph.ecog + ph.karno, data = dat)
Model |> summary()
# Model |> tbl_regression()
# stargazer(Model)
```
***
***
# Using Python
```{python}
import numpy as np
import pandas as pd
from lifelines import CoxPHFitter
cph = CoxPHFitter()
dat_py = r.dat
dat_py.head()
dat2 = dat_py[['time', 'status', 'ph.ecog', 'ph.karno']]
dat2.head()
dat2['status'] = dat2['status'].replace([1, 2], [0, 1])
dat2.head()
model_cardio_cox_ph = cph.fit(df = dat2, duration_col = 'time', event_col = 'status')
model_cardio_cox_ph.print_summary() # access the individual results using cph.summary
```