# NOT RUN {
library("timereg")
library("survival")
data(diabetes)
# Marginal Cox model with treat as covariate
margph <- coxph(Surv(time,status)~treat,data=diabetes)
### Clayton-Oakes, from timereg
fitco1<-two.stage(margph,data=diabetes,theta=1.0,detail=0,Nit=40,clusters=diabetes$id)
summary(fitco1)
### Plackett model
fitp <- survival.twostage(margph,data=diabetes,theta=3.0,Nit=40,
clusters=diabetes$id,var.link=1,model="plackett")
summary(fitp)
### Clayton-Oakes
fitco2 <- survival.twostage(margph,data=diabetes,theta=0.0,detail=0,
clusters=diabetes$id,var.link=1,model="clayton.oakes")
summary(fitco2)
fitco3 <- survival.twostage(margph,data=diabetes,theta=1.0,detail=0,
clusters=diabetes$id,var.link=0,model="clayton.oakes")
summary(fitco3)
### without covariates using Aalen for marginals
marg <- aalen(Surv(time,status)~+1,data=diabetes,n.sim=0,max.clust=NULL,robust=0)
fitpa <- survival.twostage(marg,data=diabetes,theta=1.0,detail=0,Nit=40,
clusters=diabetes$id,score.method="optimize")
summary(fitpa)
fitcoa <- survival.twostage(marg,data=diabetes,theta=1.0,detail=0,Nit=40,clusters=diabetes$id,
var.link=1,model="clayton.oakes")
summary(fitcoa)
### Piecewise constant cross hazards ratio modelling
########################################################
d <- subset(simClaytonOakes(2000,2,0.5,0,stoptime=2,left=0),!truncated)
udp <- piecewise.twostage(c(0,0.5,2),data=d,score.method="optimize",
id="cluster",timevar="time",
status="status",model="clayton.oakes",silent=0)
summary(udp)
# }
# NOT RUN {
## Reduce Ex.Timings
### Same model using the strata option, a bit slower
########################################################
## makes the survival pieces for different areas in the plane
##ud1=surv.boxarea(c(0,0),c(0.5,0.5),data=d,id="cluster",timevar="time",status="status")
##ud2=surv.boxarea(c(0,0.5),c(0.5,2),data=d,id="cluster",timevar="time",status="status")
##ud3=surv.boxarea(c(0.5,0),c(2,0.5),data=d,id="cluster",timevar="time",status="status")
##ud4=surv.boxarea(c(0.5,0.5),c(2,2),data=d,id="cluster",timevar="time",status="status")
## everything done in one call
ud <- piecewise.data(c(0,0.5,2),data=d,timevar="time",status="status",id="cluster")
ud$strata <- factor(ud$strata);
ud$intstrata <- factor(ud$intstrata)
## makes strata specific id variable to identify pairs within strata
## se's computed based on the id variable across strata "cluster"
ud$idstrata <- ud$id+(as.numeric(ud$strata)-1)*2000
marg2 <- aalen(Surv(boxtime,status)~-1+factor(num):factor(intstrata),
data=ud,n.sim=0,robust=0)
tdes <- model.matrix(~-1+factor(strata),data=ud)
fitp2 <- survival.twostage(marg2,data=ud,se.clusters=ud$cluster,clusters=ud$idstrata,
score.method="fisher.scoring",model="clayton.oakes",
theta.des=tdes,step=0.5)
summary(fitp2)
### now fitting the model with symmetry, i.e. strata 2 and 3 same effect
ud$stratas <- ud$strata;
ud$stratas[ud$strata=="0.5-2,0-0.5"] <- "0-0.5,0.5-2"
tdes2 <- model.matrix(~-1+factor(stratas),data=ud)
fitp3 <- survival.twostage(marg2,data=ud,clusters=ud$idstrata,se.cluster=ud$cluster,
score.method="fisher.scoring",model="clayton.oakes",
theta.des=tdes2,step=0.5)
summary(fitp3)
### same model using strata option, a bit slower
fitp4 <- survival.twostage(marg2,data=ud,clusters=ud$cluster,se.cluster=ud$cluster,
score.method="fisher.scoring",model="clayton.oakes",
theta.des=tdes2,step=0.5,strata=ud$strata)
summary(fitp4)
# }
# NOT RUN {
### structured random effects model additive gamma ACE
### simulate structured two-stage additive gamma ACE model
data <- simClaytonOakes.twin.ace(2000,2,1,0,3)
out <- twin.polygen.design(data,id="cluster")
pardes <- out$pardes
des.rv <- out$des.rv
aa <- aalen(Surv(time,status)~+1,data=data,robust=0)
ts <- survival.twostage(aa,data=data,clusters=data$cluster,detail=0,
theta=c(2,1)/10,var.link=0,step=0.5,
random.design=des.rv,theta.des=pardes)
summary(ts)
### case control sampling of data, call via pairs
data2 <- fast.reshape(data,id="cluster")
ncase <- 400; ncont <- 100
controls <- which(data2$status2==0)
cases <- which(data2$status2==1)
controls<-sort(sample(controls,min(ncont,length(controls))))
cases <- sort( sample(cases, min(ncase,length(cases))))
clustco <- data2$cluster[controls]
clustca <- data2$cluster[cases]
ss <- data$cluster %in% c(clustco,clustca)
datacc <- data[ss,]
mm <- familycluster.index(datacc$cluster)
pairs <- mm$pairs
head(pairs)
## second column of pairs represent probands
kinship <- rep(1,nrow(pairs))
kinship[datacc$zyg[pairs[,1]]=="DZ"] <- 0.5
dout <- make.pairwise.design(pairs,kinship,type="ace")
## additive model specified via formula-list
cr.models <- list(Surv(time,status)~+1)
tscce <- survival.twostage(NULL,data=datacc,clusters=datacc$cluster,
detail=0,theta=c(2,1)/10,var.link=0,step=1.0,
pairs=pairs,
random.design=dout$random.design,theta.des=dout$theta.des,
pairs.rvs=dout$ant.rvs,
case.control=1, marginal.status=datacc$status,
cr.models=cr.models)
summary(tscce)
### see also pairwise*.r demos under inst for frailty, competing risks and
### case control sampling
# }
Run the code above in your browser using DataLab