In one particular situation predict.coxph gives an error message. Namely: stratified data, predict='expected', new data, se=TRUE. I think I found the error but I'll leave that to you to decide.
Thanks,
Chris
######## CODE
library(survival)
set.seed(20121221)
nn <- 10 # sample size in each group
lambda0 <- 0.1 # event rate in group 0
lambda1 <- 0.2 # event rate in group 1
t0 <- 10 # time to estimate expected numbers of events
# 'correct' number of events at time t0 = rate of events (lambda) times time (t0)
t0*lambda0
t0*lambda1
# generate uncensored data
T0 <- rexp(nn, rate=lambda0)
T1 <- rexp(nn, rate=lambda1)
thedata <- data.frame(Time=c(T0, T1), X=rep(0:1, e=nn), X2=rep(0:1, nn))
# 4 covariate combinations
(ndf <- data.frame(Time=t0, X=rep(0:1,e=2), X2=rep(0:1, 2)))
# model with 2 effects
mod <- coxph(Surv(Time) ~ X + X2, data=thedata)
summary(mod)
# coefficient of X2 should be 0
# coefficient of X should be log(lambda1/lambda0)
log(lambda1/lambda0)
predict(mod , newdata=ndf, type="expected", se.fit=TRUE)
# stratified model
mod2 <- coxph(Surv(Time) ~ strata(X) + X2, data=thedata)
predict(mod2, newdata=ndf, type="expected" ) # no se
predict(mod2, newdata=ndf, se.fit=TRUE) # type="lp"
predict(mod2, type="expected", se.fit=TRUE) # prediction at old data, which has different Time
try(predict(mod2, newdata=ndf, type="expected", se.fit=TRUE)) # error?
#Error in predict.coxph(mod2, newdata = ndf, type = "expected", se.fit = TRUE) :
# subscript out of bounds
#mypc <- getAnywhere(predict.coxph)
#mypc2 <- mypc$objs[[1]]
#debug(mypc2)
#mypc2(mod2, newdata=ndf, type="expected", se.fit=TRUE)
# The error appears to be an incorrect subscripting of xbar (and xbar2) in defining dt (and dt2) in one part of predict.coxph:
#
#if (ncol(y) == 2) {
# if (se.fit) {
# dt <- (chaz * newx[indx2, ]) - xbar[indx2, ] # *** SHOULD BE JUST xbar
# se[indx2] <- sqrt(varh + rowSums((dt %*% object$var) * dt)) * newrisk[indx2]
# }
#}
#else {
# j2 <- approx(afit$time, 1:afit.n, newy[indx2, 2], method = "constant", f = 0, yleft = 0, yright = afit.n)$y # chaz2 <- approx(-afit$time, afit$cumhaz, -newy[indx2, 2], method = "constant", rule = 2, f = 0)$y # chaz2 <- c(0, afit$cumhaz)[j2 + 1] # pred[indx2] <- (chaz2 - chaz) * newrisk[indx2] # if (se.fit) {
# varh2 <- c(0, cumsum(afit$varhaz))[j1 + 1]
# xbar2 <- rbind(0, afit$xbar)[j1 + 1, , drop = F]
# dt <- (chaz * newx[indx2, ]) - xbar[indx2, ] # *** SHOULD BE JUST xbar
# dt2 <- (chaz2 * newx[indx2, ]) - xbar2[indx2, ] # *** SHOULD BE JUST xbar2
#
# To be like other sections of the code, xbar (and xbar2) should not be subscripted.
sessionInfo()
maintainer('survival')
########################## OUTPUT
library(survival)
Loading required package: splines
set.seed(20121221)
nn <- 10 # sample size in each group
lambda0 <- 0.1 # event rate in group 0
lambda1 <- 0.2 # event rate in group 1
t0 <- 10 # time to estimate expected numbers of events
# 'correct' number of events at time t0 = rate of events (lambda)
times time (t0)
t0*lambda0
[1] 1
t0*lambda1
[1] 2
# generate uncensored data
T0 <- rexp(nn, rate=lambda0)
Error in predict.coxph(mod2, newdata = ndf, type = "expected", se.fit = TRUE) :
subscript out of bounds
#Error in predict.coxph(mod2, newdata = ndf, type = "expected", se.fit = TRUE) :
# subscript out of bounds
#mypc <- getAnywhere(predi .... [TRUNCATED]
R version 2.15.2 (2012-10-26)
Platform: x86_64-w64-mingw32/x64 (64-bit)
locale:
[1] LC_COLLATE=English_United States.1252 LC_CTYPE=English_United States.1252 LC_MONETARY=English_United States.1252
[4] LC_NUMERIC=C LC_TIME=English_United States.1252
attached base packages:
[1] splines stats graphics grDevices utils datasets methods base
other attached packages:
[1] survival_2.37-2
loaded via a namespace (and not attached):
[1] tools_2.15.2
maintainer('survival')
[1] "Terry Therneau <therneau.terry at mayo.edu>"
**********************************************************
Electronic Mail is not secure, may not be read every day, and should not be used for urgent or sensitive issues