2

I am trying to manually calculate the scaled Schoenfeld residuals in a Cox model. Please see the code below. sch2 is the calculation in the cox.zph function using Schoenfeld residuals. sch1 is R's result using the residuals function and type="scaledsch". However, these two output are different...

> res.cox <- coxph(Surv(time, status) ~ age + sex + wt.loss, data =  lung)
> 
> sch1 <- residuals(res.cox,"scaledsch")
> 
> sresid <- residuals(res.cox, "schoenfeld")
> 
> varnames <- names(res.cox$coefficients)
> nvar <- length(varnames)
> ndead <- length(sresid)/nvar
> 
> sch2 <- sresid %*% res.cox$var*ndead
> 
> head(sch1)
         [,1]      [,2]        [,3]
5  0.03835823  2.812213 -0.02767246
11 0.15355137 -1.703988 -0.06138393
11 0.25305878 -1.543679  0.02613398
12 0.15583315 -1.626676  0.05505592
13 0.18551303 -1.611875  0.05555588
13 0.02926177 -1.802935 -0.00284424
> head(sch2)
          [,1]      [,2]        [,3]
5  0.018270011  3.333245 -0.02843205
11 0.133463157 -1.182956 -0.06214352
11 0.232970565 -1.022647  0.02537439
12 0.135744939 -1.105644  0.05429633
13 0.165424817 -1.090843  0.05479629
13 0.009173552 -1.281903 -0.00360383

UPDATE: I have to add the coefficient estimates from the Cox model in sch2. See the answer below.

Statisfun
  • 707
  • 1
    If you have figured this out, please add an answer to this question and accept it. It's OK on this site to answer your own question, and a formal answer will make it easier for someone else with a similar question in the future to find the answer. – EdM Oct 25 '20 at 19:37
  • I am having a similar problem of manual computation of Schoenfeld residuals in R. https://math.stackexchange.com/questions/4177480/how-to-calculate-schoenfeld-residuals-in-r – John Majimboni Jun 19 '21 at 18:30

1 Answers1

1

I have to add the coefficient estimates from the Cox model in sch2:

sch2 <- sresid %*% res.cox$var*ndead + rep(res.cox$coefficients, each = nrow(sresid))

This would give the same thing as sch1.

Statisfun
  • 707