###############################################################
######################### Survival ##########################
###############################################################
library(timereg)
n <- 100
dfr <- data.frame(to = rexp(n,1),from=0,event=1)
fit <- survfit(Surv(from,to,event==1)~1,data=dfr)
times <- fit$time
dN <- fit$n.event
Y <- fit$n.risk
Y[1] <- n
dA <- matrix(dN/Y,nrow=1,ncol=length(dN))
# Function specification
F_fun_Survival <- function(x)-matrix(x,1,1)
JacobianListSurvival <- list(function(x)-matrix(1,1,1))
X0_Survival <- matrix(1,1,1)
V0_Survival <- matrix(0,1,1)
paramEst_survival <- pluginEstimate(
n,dA,F_fun_Survival,JacobianListSurvival,X0_Survival,V0_Survival
)
KM <- cumprod(1 - dA)
Greenwood <- KM^2 * cumsum(dA^2)
plot(
times,paramEst_survival$X,type="s",main="SDE plugin survival estimates",
ylab="",xlab="time"
)
lines(times,paramEst_survival$X + 1.96*sqrt(paramEst_survival$covariance[1,1,]),type="s")
lines(times,paramEst_survival$X - 1.96*sqrt(paramEst_survival$covariance[1,1,]),type="s")
lines(seq(0,10,length.out=100),exp(-seq(0,10,length.out=100)),col=2)
legend("topright",c("SDE plugin estimates","Exact"),lty=1,col=c(1,2),bty="n")
plot(
times,paramEst_survival$covariance,type="s",
main="SDE plugin variance vs Greenwood variance",ylab="",xlab="time"
)
lines(times,Greenwood,type="s",col=4,lty=1)
legend("topright",c("SDE plugin estimates","Greenwood"),lty=1,col=c(1,4),bty="n")
#################################################################################
############ Competing risks and Cumulative incidence(two states) ###############
#################################################################################
n <- 200
x1 <- rexp(n,1)
x2 <- rexp(n,1.3)
to.states <- ifelse(x1Run the code above in your browser using DataLab