Residual Analysis Using R Activity
Residual Analysis Using R Activity
Data on the recurrence times to infection, at the point of insertion of the catheter, for kidney
patients using portable dialysis equipment. Catheters may be removed for reasons other than
infection, in which case the observation is censored. Each patient has exactly 2 observations.
(We will ignore this issue that data lacks independence for the time being)
Format
patient: id
time: time
status: event status
age: in years
sex: 1=male, 2=female
disease: disease type (0=GN, 1=AN, 2=PKD, 3=Other)
frail: frailty estimate from original paper
>data("kidney")
>head(kidney)
2. Create the cox proportional hazard model for age, sex and disease
Martingale Residuals
>martingaleres = resid(kfit0,type="martingale")
Cox Snell
>coxsnellres=kidney$status-resid(kfit0,type="martingale")
Create the Kaplan-Meier test on the Cox Snell residuals using the original censored values.
Then, plot the -log(S(t)) vs time.
>mysurv = Surv(coxsnellres,kidney$status)
>plot(-log(KMfit$surv) ~ KMfit$time)
If the data is in a straight line with slope = 1, y-intercept = 0, the model is a good fit.
>index = 1:nrow(kidney)
>plot(martingaleres~index)
If very negative, outlier. If close to 1 unusual.
Analyze for outliers. Any values far away or the absolute value is greater than 2 within
reason.
>dfbetares = resid(kfit0,type="dfbetas")
>plot(dfbetares[,4]~index,type = "h")
>martingaleres = resid(kfitnull,type="martingale")
>plot(martingaleres~kidney$age)
>lw1=loess(martingaleres ~ kidney$age)
Need to put the variable age in ascending order. Otherwise, the loess line creates data art.
>j = order(kidney$age)
>lines(kidney$age[j],lw1$fitted[j])
Create a new variate called fakevariate using the rnorm() function to add a random number
with mean 35 and sd 18 from a normal distribution. Run several times to see
>fakevariate = rnorm(length(kidney$age),35,18)
>plot(martingaleres~fakevariate)
>lw1=loess(martingaleres ~ fakevariate)
>j = order(fakevariate)
>lines(fakevariate[j],lw1$fitted[j])
The cox.zph() function returns a table with test statistics and p-values to help assess
>schoenfeldres = cox.zph(kfit0)
>schoenfeldres
>plot(schoenfeldres)