| 1 |
skip_if_not_installed("modeltests") |
| 2 |
library(modeltests) |
| 3 |
|
| 4 |
skip_if_not_installed("survival") |
| 5 |
suppressPackageStartupMessages(library(survival)) |
| 6 |
|
| 7 |
fit <- coxph(Surv(time, status) ~ age + sex, lung) |
| 8 |
fit2 <- coxph(Surv(time, status) ~ age + sex, lung, robust = TRUE) |
| 9 |
fit3 <- coxph(Surv(time, status) ~ age + sex + frailty(inst), lung) |
| 10 |
|
| 11 |
bladder1 <- bladder[bladder$enum < 5, ] |
| 12 |
fit4 <- coxph( |
| 13 |
Surv(stop, event) ~ (rx + size + number) * strata(enum) + cluster(id), |
| 14 |
bladder1 |
| 15 |
) |
| 16 |
|
| 17 |
# this model does not have summary(x)$used.robust |
| 18 |
fit5 <- coxph(Surv(time, status) ~ age + pspline(nodes), data = colon) |
| 19 |
|
| 20 |
colon$super_long_variable_name_that_would_be_truncated <- colon$nodes |
| 21 |
|
| 22 |
# see #1153 |
| 23 |
fit6 <- coxph( |
| 24 |
Surv(time, status) ~ |
| 25 |
age + |
| 26 |
pspline(super_long_variable_name_that_would_be_truncated), |
| 27 |
data = colon |
| 28 |
) |
| 29 |
|
| 30 |
test_that("coxph tidier arguments", { |
| 31 |
check_arguments(tidy.coxph) |
| 32 |
check_arguments(glance.coxph) |
| 33 |
check_arguments(augment.coxph) |
| 34 |
}) |
| 35 |
|
| 36 |
test_that("tidy.coxph", { |
| 37 |
td <- tidy(fit) |
| 38 |
td2 <- tidy(fit, exponentiate = TRUE) |
| 39 |
td3 <- tidy(fit2) |
| 40 |
td4 <- tidy(fit3) |
| 41 |
td5 <- tidy(fit3, exponentiate = TRUE) |
| 42 |
td6 <- tidy(fit3, conf.int = TRUE) |
| 43 |
td7 <- tidy(fit4) |
| 44 |
td8 <- tidy(fit4, exponentiate = TRUE) |
| 45 |
td9 <- tidy(fit4, conf.int = TRUE) |
| 46 |
td10 <- tidy(fit5) |
| 47 |
td11 <- tidy(fit5, conf.int = TRUE) |
| 48 |
td12 <- tidy(fit6, maxlabel = 1000) |
| 49 |
|
| 50 |
check_tidy_output(td) |
| 51 |
check_tidy_output(td2) |
| 52 |
check_tidy_output(td3) |
| 53 |
check_tidy_output(td4) |
| 54 |
check_tidy_output(td5) |
| 55 |
check_tidy_output(td6) |
| 56 |
check_tidy_output(td7) |
| 57 |
check_tidy_output(td8) |
| 58 |
check_tidy_output(td9) |
| 59 |
check_tidy_output(td10) |
| 60 |
check_tidy_output(td11) |
| 61 |
check_tidy_output(td12) |
| 62 |
|
| 63 |
expect_equal( |
| 64 |
td12$term[2], |
| 65 |
"pspline(super_long_variable_name_that_would_be_truncated), linear" |
| 66 |
) |
| 67 |
}) |
| 68 |
|
| 69 |
test_that("glance.coxph", { |
| 70 |
gl <- glance(fit) |
| 71 |
gl2 <- glance(fit2) |
| 72 |
gl3 <- glance(fit3) |
| 73 |
gl4 <- glance(fit4) |
| 74 |
|
| 75 |
check_glance_outputs(gl, gl2, gl3, gl4, strict = FALSE) |
| 76 |
}) |
| 77 |
|
| 78 |
test_that("augment.coxph", { |
| 79 |
check_augment_function( |
| 80 |
aug = augment.coxph, |
| 81 |
model = fit, |
| 82 |
data = lung, |
| 83 |
newdata = lung |
| 84 |
) |
| 85 |
|
| 86 |
check_augment_function( |
| 87 |
aug = augment.coxph, |
| 88 |
model = fit2, |
| 89 |
data = lung, |
| 90 |
newdata = lung |
| 91 |
) |
| 92 |
}) |