This function basically creates a new df
from data
for
each term in terms
, creating a range from minimum and maximum of the
predict(fit, newdata=df, type="terms")
. Terms are then
stacked to a tidy data frame.
get_terms(data, fit, terms, ...)
A tibble with 5 columns.
library(survival)
fit <- coxph(Surv(time, status) ~ pspline(karno) + pspline(age), data=veteran)
terms_df <- veteran %>% get_terms(fit, terms = c("karno", "age"))
head(terms_df)
#> # A tibble: 6 × 6
#> term x eff se ci_lower ci_upper
#> <chr> <dbl> <dbl> <dbl> <dbl> <dbl>
#> 1 karno 10 1.90 0.884 0.127 3.66
#> 2 karno 10.9 1.89 0.822 0.243 3.53
#> 3 karno 11.8 1.88 0.763 0.353 3.41
#> 4 karno 12.7 1.87 0.707 0.455 3.28
#> 5 karno 13.6 1.86 0.654 0.549 3.16
#> 6 karno 14.5 1.84 0.604 0.634 3.05
tail(terms_df)
#> # A tibble: 6 × 6
#> term x eff se ci_lower ci_upper
#> <chr> <dbl> <dbl> <dbl> <dbl> <dbl>
#> 1 age 78.6 -0.166 0.712 -1.59 1.26
#> 2 age 79.1 -0.192 0.751 -1.69 1.31
#> 3 age 79.6 -0.219 0.793 -1.80 1.37
#> 4 age 80.1 -0.247 0.837 -1.92 1.43
#> 5 age 80.5 -0.275 0.884 -2.04 1.49
#> 6 age 81 -0.303 0.933 -2.17 1.56