KM survival time transfer function in Cox reg I have a binary risk factor that can change value over time.  I corrected the data using tmerge().  It turns out that the risk factor loses its effect over time.  So, introduced an identity time transfer into the coxph().  I have two questions: 1) How can I use the K-M time transfer function in the call to coxph() instead?  2) Once I introduced the tt function into coxph(), the resulting object had many more residuals (and linear predictor values) than I had in the original data after tmerge().  Here is an example to illustrate.  
library(asaur)
library(dplyr)
library(survival)
panc <- asaur::pancreatic2 %>% 
   mutate(stage.n = as.numeric(stage == "M"))
result <- coxph(Surv(pfs) ~ stage.n,data=panc)
length(residuals(result,type = 'deviance'))   # 41 
temp <- cox.zph(result.coxph,transform="km") # check PH assumption
print(temp)
         rho chisq      p
stageM -0.328  3.86 0.0496
plot(temp[1],main='transform=K-M(time)',xlab='K-M(time)') # effect diminishes over time

How would you change the code to use K-M survival time transfer instead? 
result.tt <- update(result.coxph, .~. + tt(stage.n), 
                    tt = function(x, t, ...) x * t) 

Why 741 residuals when there are only 41 subjects? 
length(residuals(result.tt,type = 'deviance'))  
    [1] 741

 A: In reverse order:
2) When you estimate a time-dependent coefficient by using tt(), there is no longer a single covariate value associated with each patient. The covariate values need to be determined for all cases still at risk at each event time, taking the time-dependence function into account. This increases the number of observations from 1 per patient to the sum of all patients still at risk over all event times.
In your example all cases have events. So when the first event happens you need to determine all 41 covariate values; at the second event, there are 40 to calculate, and so on. This could in principle give you up to 861 covariate values, but there are 4 ties in survival times in this data set decreasing the total number of observations.
To see the underlying data structures that are generated to do the Cox regression, examine the $x$ and $y$ values in your Cox model, for example:
result.3 <- coxph(Surv(pfs) ~ stage.n + tt(stage.n),tt=function(x, t, ...) x * t,data=panc,x=TRUE,y=TRUE)
xyCox <- cbind(result.3$x,result.3$y)
dim(xyCox)
[1] 741   4

and note that several observations are now associated with each patient up to that patient's event time, shown here for patient 1:
xyCox[grepl("^1\\.|^1$",rownames(xyCox)),]
    stage.n tt(stage.n) time status
1         1          48   48      1
1.1       1          43   43      0
1.2       1          42   42      0
1.3       1          36   36      0

Deviance residuals are calculated for each observation, not just for each event, so you end up with 741 residuals calculated.
1) In principle, you can use any function involving time in the tt() formalism. So if you want to use a K-M survival estimate as the function of time, you just have to write a function that returns the K-M estimate for each time. If you type cox.zph at the R prompt when its survival package is loaded, you can see how that function accomplishes this task; it calls survfitKM() on a null model of the survival data at hand. Note that with a null model the K-M estimate is strictly empirical without taking covariates into account.
It's not clear, however, that using this type of K-M estimate in a time-dependence function would make much sense. For this approach to be a reasonable model of reality, the processes in an individual leading to an event would somehow have to know the survival data for all members of the cohort that provide the empirical K-M estimate. It would seem to make more sense to define some reasonable continuous function of time that depends only on the particular individual at risk.
