# NOT RUN {
library(survival)
library(rms)
library(prodlim)
set.seed(10)
#### Survival settings ####
#### ATE with Cox model ####
## generate data
n <- 100
dtS <- sampleData(n, outcome="survival")
dtS$time <- round(dtS$time,1)
dtS$X1 <- factor(rbinom(n, prob = c(0.3,0.4) , size = 2), labels = paste0("T",0:2))
## estimate the Cox model
fit <- cph(formula = Surv(time,event)~ X1+X2,data=dtS,y=TRUE,x=TRUE)
## compute the ATE at times 5, 6, 7, and 8 using X1 as the treatment variable
# }
# NOT RUN {
## only point estimate (argument se = FALSE)
ateFit1a <- ate(fit, data = dtS, treatment = "X1", times = 5:8,
se = FALSE)
## standard error / confidence intervals computed using the influence function
## (argument se = TRUE and B = 0)
ateFit1b <- ate(fit, data = dtS, treatment = "X1", times = 5:8,
se = TRUE, B = 0)
## same as before with in addition the confidence bands for the ATE
## (argument band = TRUE)
ateFit1c <- ate(fit, data = dtS, treatment = "X1", times = 5:8,
se = TRUE, band = TRUE, B = 0)
## standard error / confidence intervals computed using 100 boostrap samples
## (argument se = TRUE and B = 100)
ateFit1d <- ate(fit, data = dtS, treatment = "X1",
times = 5:8, se = TRUE, B = 100)
## NOTE: for real applications 100 bootstrap samples is not enougth
## same but using 2 cpus for generating and analyzing the boostrap samples
## (parallel computation, argument mc.cores = 2)
ateFit1e <- ate(fit, data = dtS, treatment = "X1",
times = 5:8, se = TRUE, B = 100, mc.cores = 2)
# }
# NOT RUN {
#### Survival settings without censoring ####
#### ATE with glm ####
## generate data
n <- 100
dtB <- sampleData(n, outcome="binary")
dtB[, X2 := as.numeric(X2)]
## estimate a logistic regression model
fit <- glm(formula = Y ~ X1+X2, data=dtB, family = "binomial")
## compute the ATE using X1 as the treatment variable
## only point estimate (argument se = FALSE)
ateFit1a <- ate(fit, data = dtB, treatment = "X1", se = FALSE)
# }
# NOT RUN {
## standard error / confidence intervals computed using the influence function
ateFit1b <- ate(fit, data = dtB, treatment = "X1",
times = 5, ## just for having a nice output not used in computations
se = TRUE, B = 0)
## standard error / confidence intervals computed using 100 boostrap samples
ateFit1d <- ate(fit, data = dtB, treatment = "X1",
times = 5, se = TRUE, B = 100)
## using the lava package
ateLava <- estimate(fit, function(p, data){
a <- p["(Intercept)"] ; b <- p["X11"] ; c <- p["X2"] ;
R.X11 <- expit(a + b + c * data[["X2"]])
R.X10 <- expit(a + c * data[["X2"]])
list(risk0=R.X10,risk1=R.X11,riskdiff=R.X11-R.X10)},
average=TRUE)
ateLava
ateFit1b$meanRisk
# }
# NOT RUN {
#### Competing risks settings ####
#### ATE with cause specific Cox regression ####
# }
# NOT RUN {
## generate data
n <- 500
set.seed(10)
dt <- sampleData(n, outcome="competing.risks")
dt$time <- round(dt$time,1)
dt$X1 <- factor(rbinom(n, prob = c(0.2,0.3) , size = 2), labels = paste0("T",0:2))
## estimate cause specific Cox model
fitCR <- CSC(Hist(time,event)~ X1+X8,data=dt,cause=1)
## compute the ATE at times 10, 15, 20 using X1 as the treatment variable
ateFit2a <- ate(fitCR, data = dt, treatment = "X1", times = c(10,15,20),
cause = 1, se = FALSE)
## standard error / confidence intervals computed using the influence function
## (argument se = TRUE and B = 0)
ateFit2b <- ate(fitCR, data = dt, treatment = "X1", times = c(10,15,20),
cause = 1, se = TRUE, B = 0)
## same as before with in addition the confidence bands for the ATE
## (argument band = TRUE)
ateFit2c <- ate(fitCR, data = dt, treatment = "X1", times = c(10,15,20),
cause = 1, se = TRUE, band = TRUE, B = 0)
## standard error / confidence intervals computed using 100 boostrap samples
## (argument se = TRUE and B = 100)
ateFit2d <- ate(fitCR, data = dt, treatment = "X1", times = c(10,15,20),
cause = 1, se = TRUE, B = 100)
## NOTE: for real applications 100 bootstrap samples is not enougth
## same but using 2 cpus for generating and analyzing the boostrap samples
## (parallel computation, argument mc.cores = 2)
ateFit2e <- ate(fitCR, data = dt, treatment = "X1", times = c(10,15,20),
cause = 1, se = TRUE, B = 100, mc.cores = 2)
# }
# NOT RUN {
#### time-dependent covariates ###
# }
# NOT RUN {
library(survival)
fit <- coxph(Surv(time, status) ~ celltype+karno + age + trt, veteran)
vet2 <- survSplit(Surv(time, status) ~., veteran,
cut=c(60, 120), episode ="timegroup")
fitTD <- coxph(Surv(tstart, time, status) ~ celltype+karno + age + trt,
data= vet2,x=1)
set.seed(16)
resVet <- ate(fitTD,formula=Hist(entry=tstart,time=time,event=status)~1,
data = vet2, treatment = "celltype", contrasts = NULL,
times=5,verbose=1,
landmark = c(0,30,60,90), cause = 1, B = 10, se = 1,
band = FALSE, mc.cores=1)
resVet
# }
# NOT RUN {
# }
# NOT RUN {
set.seed(137)
d=sampleDataTD(127)
library(survival)
d[,status:=1*(event==1)]
d[,X3:=as.factor(X3)]
## ignore competing risks
cox1TD <- coxph(Surv(start,time, status,type="counting") ~ X3+X5+X6+X8,
data=d, x = TRUE)
resTD1 <- ate(cox1TD,formula=Hist(entry=start,time=time,event=status)~1,
data = d, treatment = "X3", contrasts = NULL,
times=.5,verbose=1,
landmark = c(0,0.5,1), B = 20, se = 1,
band = FALSE, mc.cores=1)
resTD1
## account for competing risks
cscTD <- CSC(Hist(time=time, event=event,entry=start) ~ X3+X5+X6+X8, data=d)
set.seed(16)
resTD <- ate(cscTD,formula=Hist(entry=start,time=time,event=event)~1,
data = d, treatment = "X3", contrasts = NULL,
times=.5,verbose=1,
landmark = c(0,0.5,1), cause = 1, B = 20, se = 1,
band = FALSE, mc.cores=1)
resTD
# }
Run the code above in your browser using DataLab