# 1. Duration Model require(survival) Surv(time, event) Surv(time, time2, event, type=, origin=0) with(aml, Surv(time, status)) with(heart, Surv(start,stop,event)) # fit a Kaplan-Meier and plot it fit <- survfit(Surv(time, status) ~ x, data=aml) summary(fit) plot(fit, lty=2:3, col=2:3) legend(80,0.8, c("Maintained", "Nonmaintained"), lty=2:3, col=2:3) # plot only the second curve plot(fit[2]) # fit a cox proportional hazards model and plot the predicted survival curve fit2 <- coxph( Surv(futime,fustat)~resid.ds+rx+ecog.ps,data=ovarian) fit2 plot(survfit( fit2)) # 2. Function smoothing (loess) # example 1 require(MASS) attach(GAGurine) plot(Age,GAG,type="p",main="loess") lines(loess.smooth(Age,GAG),col=2) # example 2 period <- 120 x <- 1:120 y <- sin(2*pi*x/period) + runif(length(x),-1,1) plot(x,y, main="Sine Curve + 'Uniform' Noise") mtext("showing loess smoothing (local regression smoothing)") # Apply loess smoothing using the default span value of 0.75: y.loess <- loess(y ~ x, span=0.75, degree=2, data.frame(x=x, y=y)) y.predict <- predict(y.loess, data.frame(x=x)) lines(x,y.predict,col="red") # Degrees of smoothing plot(x,y, main="Sine Curve + 'Uniform' Noise") mtext("showing loess smoothing (local regression smoothing)") s <- c(0.1,0.5,1,1.5,2) c <- c("red","blue","black","violet","yellow") for(i in 1:length(s)){ y.loess <- loess(y ~ x, span=s[i], data.frame(x=x, y=y)) lines(x,predict(y.loess, data.frame(x=x)),col=c[i]) } # 3. Quantile Smoothing Splines (rqss) require(SemiPar) require(quantreg) data(lidar) attach(lidar) taus <- c(.05,.25,.5,.75,.95) par(mfrow = c(1,2)) plot(range,logratio,cex = .5) for(i in 1:length(taus)){ plot(rqss(logratio ~ qss(range,lambda = 10),tau = taus[i]),col="red") } plot(range,logratio,cex = .5) for(i in 1:length(taus)){ plot(rqss(logratio ~ qss(range,lambda = 10,constraint = "D"),tau = taus[i]),col="red") }