Search code examples
rfunctionsurvival-analysisp-valuecox-regression

Why is this code yielding erroneous P-values?


I am trying to calculate P-values associated with point estimates obtained from a Cox PH model with time-varying coefficients. The function that I have written does not provide the correct P-values. I will illustrate this by making use of the NCCTG Lung Cancer Data from the survival package.

# Setup
require(survival)

# Effect of Karnofsky score, linear
fit <- coxph(Surv(time/365.25, status == 2) ~ ph.karno + tt(ph.karno), 
             lung, tt=function(x, t, ...) {x*t})

The function:

# Same function but now with a P-value in the output
calculate.timeDependentHazard.P <- function(model,time) {
  index.1 <- which(names(model$coef)=="ph.karno")
  index.2 <- which(names(model$coef)=="tt(ph.karno)")

  coef <- model$coef[c(index.1,index.2)]
  var <- rbind(c(model$var[index.1,index.1],model$var[index.1,index.2]),
               c(model$var[index.2,index.1],model$var[index.2,index.2]))

  var.at.time <- t(c(1,time)) %*% var %*% c(1,time)

  hazard.at.time <- t(c(1,time)) %*% coef

  lower.95 <- hazard.at.time - 1.96*sqrt(var.at.time)
  upper.95 <- hazard.at.time + 1.96*sqrt(var.at.time)

  z.at.time <- hazard.at.time/(sqrt(var.at.time))

  p.value <- pnorm(-abs(z.at.time))

  results <- c(exp(c(hazard.at.time,lower.95,upper.95)),p.value)
  names(results) <- c("hazard ratio","95% lower","95% upper","P.value")

  options(scipen = 999)

  results

}

# Point estimates after 1.05*365.25 = 383.5 days of follow-up
calculate.timeDependentHazard.P(fit,1.05)

The output:

> calculate.timeDependentHazard.P(fit,1.05)
hazard ratio    95% lower    95% upper      P.value 
  0.98913256   0.97654719   1.00188013   0.04721342

Apparently, the P-value should be >.05 but somehow it is not. The P-values calculated via this approach seem to be too low. Anyone who can discover the flaw?


Solution

  • It seems like you want a two sided alternative so multiply pnorm(-abs(z.at.time)) by two. I.e., do 2*pnorm(-abs(z.at.time)).