# Packages required for this example
library( splines )
library( survival )
data( lung )
par( mfrow=c(1,2) )
# Plot the Kaplan-meier-estimator
plot( survfit( Surv( time, status==2 ) ~ 1, data=lung ) )
# Declare data as Lexis
lungL <- Lexis(exit = list(tfd=time),
exit.status = (status == 2) * 1,
data = lung)
summary(lungL)
# Split the follow-up every 10 days
sL <- splitLexis(lungL, "tfd", breaks=seq(0,1100,10))
summary(sL)
# Fit a Poisson model with a natural spline for the effect of time (left
# end points of intervals are used as covariate)
mp <- glm(cbind(lex.Xst == 1, lex.dur)
~ Ns(tfd,knots = c(0, 50, 100, 200, 400, 700)),
family = poisreg,
data = sL)
# mp is now a model for the rates along the time scale tfd
# prediction data frame for select time points on this time scale
nd <- data.frame(tfd = seq(5,995,10)) # *midpoints* of intervals
Lambda <- ci.cum ( mp, nd, intl=10 )
surv <- ci.surv( mp, nd, intl=10 )
# Put the estimated survival function on top of the KM-estimator
# recall the ci.surv return the survival at *start* of intervals
matshade(nd$tfd - 5, surv, col = "Red", alpha = 0.15)
# Extract and plot the fitted intensity function
lambda <- ci.pred(mp, nd) * 365.25 # mortality
matshade(nd$tfd, lambda, log = "y", ylim = c(0.2, 5), plot = TRUE,
xlab = "Time since diagnosis",
ylab = "Mortality per year")
# same thing works with gam from mgcv
library(mgcv)
mg <- gam(cbind(lex.Xst == 1, lex.dur) ~ s(tfd), family = poisreg, data=sL )
matshade(nd$tfd - 5, ci.surv(mg, nd, intl=10), plot=TRUE,
xlab = "Days since diagnosis", ylab="P(survival)")
matshade(nd$tfd , ci.pred(mg, nd) * 365.25, plot=TRUE, log="y",
xlab = "Days since diagnosis", ylab="Mortality per 1 py")
Run the code above in your browser using DataLab