data(lung.saemix)
saemix.data<-saemixData(name.data=lung.saemix,header=TRUE,name.group=c("id"),
name.predictors=c("time","status","cens"),name.response=c("status"),
name.covariates=c("age", "sex", "ph.ecog", "ph.karno", "pat.karno", "wt.loss","meal.cal"),
units=list(x="days",y="",covariates=c("yr","","-","%","%","cal","pounds")))
weibulltte.model<-function(psi,id,xidep) {
T<-xidep[,1]
y<-xidep[,2] # events (1=event, 0=no event)
cens<-which(xidep[,3]==1) # censoring times (subject specific)
init <- which(T==0)
lambda <- psi[id,1] # Parameters of the Weibull model
beta <- psi[id,2]
Nj <- length(T)
ind <- setdiff(1:Nj, append(init,cens)) # indices of events
hazard <- (beta/lambda)*(T/lambda)^(beta-1) # H'
H <- (T/lambda)^beta # H
logpdf <- rep(0,Nj) # ln(l(T=0))=0
logpdf[cens] <- -H[cens] + H[cens-1] # ln(l(T=censoring time))
logpdf[ind] <- -H[ind] + H[ind-1] + log(hazard[ind]) # ln(l(T=event time))
return(logpdf)
}
saemix.model<-saemixModel(model=weibulltte.model,description="time model",modeltype="likelihood",
psi0=matrix(c(1,2),ncol=2,byrow=TRUE,dimnames=list(NULL, c("lambda","beta"))),
transform.par=c(1,1),covariance.model=matrix(c(1,0,0,0),ncol=2, byrow=TRUE))
saemix.options<-list(seed=632545,save=FALSE,save.graphs=FALSE, displayProgress=FALSE)
# \donttest{
tte.fit<-saemix(saemix.model,saemix.data,saemix.options)
# }
# The fit from saemix using the above Weibull model may be compared
# to the non-parametric KM estimate
if (FALSE) {
library(survival)
lung.surv<-lung.saemix[lung.saemix$time>0,]
lung.surv$status<-lung.surv$status+1
Surv(lung.surv$time, lung.surv$status) # 1=censored, 2=dead
f1 <- survfit(Surv(time, status) ~ 1, data = lung.surv)
xtim<-seq(0,max(lung.saemix$time), length.out=200)
estpar<-tte.fit@results@fixed.effects
ypred<-exp(-(xtim/estpar[1])^(estpar[2]))
plot(f1, xlab = "Days", ylab = "Overall survival probability")
lines(xtim,ypred, col="red",lwd=2)
}
Run the code above in your browser using DataLab