Skip to content
New issue

Have a question about this project? # for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “#”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? # to your account

anova.coxph crashes with tt-function calling nsk() #288

Open
alexploner opened this issue Dec 5, 2024 · 0 comments
Open

anova.coxph crashes with tt-function calling nsk() #288

alexploner opened this issue Dec 5, 2024 · 0 comments

Comments

@alexploner
Copy link

Time-transformations with the tt-special in coxph crash anova.coxph when the transformation function calls survival::nsk.

## Example from vignette("timedep", package = "survival"), Section 4.2
library(survival)
fit1 <- coxph(Surv(time, status) ~ trt + prior + karno + tt(karno), data=veteran,
               tt = function(x, t, ...) x* nsk(t, knots=c(5, 100, 200, 400),
               Boundary.knots = FALSE))
anova(fit1)
## Error: variable 'tt(karno)' was fitted with type "numeric" but type "nmatrix.3" was supplied

For a time-varying factor variable, the error message differs:

veteran2 <- transform(veteran, trt = factor(trt))
fit2 <- coxph(Surv(time, status) ~ trt + tt(trt) + prior + karno , data=veteran2,
               tt = function(x, t, ...) as.numeric(x == "2")* nsk(t, knots=c(5, 100, 200, 400),
               Boundary.knots = FALSE))
anova(fit2)
## Error in x[ol] - k.pivot : non-numeric argument to binary operator

In both cases, traceback() indicates that the error is generated in ns() called by nsk().

Both models work if the time transformation is something simple:

fit3 <- coxph(Surv(time, status) ~ trt + prior + karno + tt(karno), data=veteran,
               tt = function(x, t, ...) x*log(t + 20))
anova(fit3)
fit4 <- coxph(Surv(time, status) ~ trt + tt(trt) + prior + karno , data=veteran2,
               tt = function(x, t, ...) as.numeric(x == "2")* log(t + 20))
anova(fit4)

Session:

sessionInfo() 
R version 4.4.0 (2024-04-24 ucrt)
Platform: x86_64-w64-mingw32/x64
Running under: Windows 10 x64 (build 19045)

Matrix products: default


locale:
[1] LC_COLLATE=Swedish_Sweden.utf8  LC_CTYPE=Swedish_Sweden.utf8    LC_MONETARY=Swedish_Sweden.utf8
[4] LC_NUMERIC=C                    LC_TIME=Swedish_Sweden.utf8    

time zone: Europe/Stockholm
tzcode source: internal

attached base packages:
[1] stats     graphics  grDevices utils     datasets  methods   base     

other attached packages:
[1] survival_3.7-0

loaded via a namespace (and not attached):
[1] compiler_4.4.0 Matrix_1.7-0   tools_4.4.0    splines_4.4.0  grid_4.4.0     lattice_0.22-6
# for free to join this conversation on GitHub. Already have an account? # to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

1 participant