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, ...)

Arguments

data

A data frame containing variables used to fit the model. Only first row will be used.

fit

A fitted object of class gam.

terms

A character vector (can be length one). Specifies the terms for which partial effects will be returned

...

Further arguments passed to seq_range.

Value

A tibble with 5 columns.

Examples

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.162     3.63
#> 2 karno  10.9  1.89 0.822    0.276     3.50
#> 3 karno  11.8  1.88 0.763    0.383     3.38
#> 4 karno  12.7  1.87 0.707    0.483     3.25
#> 5 karno  13.6  1.86 0.654    0.575     3.14
#> 6 karno  14.5  1.84 0.604    0.658     3.03
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.56     1.23
#> 2 age    79.1 -0.192 0.751    -1.66     1.28
#> 3 age    79.6 -0.219 0.793    -1.77     1.33
#> 4 age    80.1 -0.247 0.837    -1.89     1.39
#> 5 age    80.5 -0.275 0.884    -2.01     1.46
#> 6 age    81   -0.303 0.933    -2.13     1.52