-
Notifications
You must be signed in to change notification settings - Fork 8
/
Copy path.Rhistory
88 lines (88 loc) · 4.11 KB
/
.Rhistory
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
84
85
86
87
88
library(survival) # this is the cornerstone command for survival analysis in R
library(ggplot2) # newer package that does nice plots
g <- read.csv("data/simulated-HF-mort-data.csv")
head(g)
gender <- as.factor(g[,"gender"]) # R calls categorical variables factors
fu_time <- g[,"fu_time"] # continuous variable (numeric)
death <- g[,"death"] # binary variable (numeric)
## plot KM
km_fit <- survfit(Surv(fu_time, death) ~ 1)
plot(km_fit)
##
summary(km_fit, times = c(1:7,30,60,90*(1:10)))
## now lets fit it with gender
km_gender_fit <- survfit(Surv(fu_time, death) ~ gender)
plot(km_gender_fit)
## run log rank test
survdiff(Surv(fu_time, death) ~ gender, rho=0)
#now check for 65 plus age
age_65plus <- ifelse(g[,"age"]>=65,1,0) # dichotomise age
table(age_65plus, exclude = NULL) # inspect the numbers - always a good idea
survdiff(Surv(fu_time, death) ~ age_65plus, rho=0)
library(survminer)
g <- read.csv("data/simulated-HF-mort-data.csv")
cox <- coxph(Surv(fu_time, death) ~ ethnicgroup, data = g) # take variables straight from g
library(survival)
cox <- coxph(Surv(fu_time, death) ~ ethnicgroup, data = g) # take variables straight from g
summary(cox)
ethnicgroup <- factor(g[,"ethnicgroup"]) # can also use “as.factor” rather than “factor”
fu_time <- g[,"fu_time"]
death <- g[,"death"]
cox <- coxph(Surv(fu_time, death) ~ ethnicgroup)
summary(cox)
library(survminer)
library(survival)
g <- read.csv("data/simulated-HF-mort-data.csv")
head(g)
cox <- coxph(Surv(fu_time, death) ~ ethnicgroup, data = g) # take variables straight from g
ethnicgroup <- factor(g[,"ethnicgroup"]) # can also use “as.factor” rather than “factor”
fu_time <- g[,"fu_time"]
death <- g[,"death"]
## week 3
cox <- coxph(Surv(fu_time, death) ~ age + gender + copd + prior_dnas + ethnicgroup)
names(g)
summary(g$age)
## week 3
age <- g[,"age"]
gender <- g[,"gender"]
copd <- g[, "copd"]
prior_dnas <- g[,"prior_dnas"]
cox <- coxph(Surv(fu_time, death) ~ age + gender + copd + prior_dnas + ethnicgroup)
summary(cox)
summary(prior_dnas)
table(prior_dnas)
quintile <- g[,"quintile"]
cox <- coxph(Surv(fu_time, death) ~ age + gender + copd + quintile + ethnicgroup)
summary(cox)
cox <- coxph(Surv(fu_time, death) ~ age + gender + copd + quintile + ethnicgroup)
warnings()
quintile <- factor(g[,"quintile"])
cox <- coxph(Surv(fu_time, death) ~ age + gender + copd + quintile + ethnicgroup)
summary(cox)
fit <- coxph(Surv(fu_time, death) ~ gender) # fit the desired model
temp <- cox.zph(fit)# apply the cox.zph function to the desired model
print(temp) # display the results
plot(temp) # plot the curves
ggcoxzph(fit)
ggcoxzph(temp)
res.cox <- coxph(Surv(fu_time, death) ~ age)
ggcoxdiagnostics(res.cox, type = "dfbeta",
linear.predictions = FALSE, ggtheme = theme_bw())
ggcoxdiagnostics(res.cox, type = "deviance",
linear.predictions = FALSE, ggtheme = theme_bw())
## Another issue is whether any continuous variables that you assume to have a linear relation with the outcome
## actually do have a linear relation. If you fit age as a single term in the model, then that’s what you’re assuming.
## The martingale residual is used to test this assumption:
ggcoxfunctional(Surv(fu_time, death) ~ age + log(age) + sqrt(age))
## Another issue is whether any continuous variables that you assume to have a linear relation with the outcome
## actually do have a linear relation. If you fit age as a single term in the model, then that’s what you’re assuming.
## The martingale residual is used to test this assumption:
fit2 <- Surv(fu_time, death) ~ age + log(age) + sqrt(age)
ggcoxfunctional(fit2)
## Let’s go through how to include this interaction term and test whether it’s statistically significant.
## For mathematical reasons, you can’t just include the follow-up time itself as part of the interaction but
## instead need to transform it. The easiest way to do this in R is via the “tt” function (short for “time transform”):
fit <- coxph(Surv(fu_time, death) ~ gender + tt(gender)) # "tt" is the time-transform function
summary(fit)
ggcoxfunctional(fit, data = g) # note we must specify original dataframe
ggcoxfunctional(fit2, data = g) # note we must specify original dataframe