The goal of gamdiffs is to provide a set of convenience functions for the estimation and associated standard errors / confidence intervals for a range of Generalized Additive Models. The particular motivation is to provide tools for estimating the difference in outcome under certain counterfactual scenarios for models relevant to population public health.
Current implemented confidence interval estimates are
- delta method
- posterior sampling
- Efron bootstrap sampling
You can install the released version of gamdiffs from CRAN with:
install.packages("gamdiffs")
And the development version from GitHub with:
# install.packages("devtools")
devtools::install_github("sempwn/gamdiffs")
This basic example generates some data and fit a thin-plate spline to a
time variable (x
) with outcome y
which is Poisson-distributed. The
sum of the outcome between time 0 to 20 is then compared to the outcome
between time 21 to time 40 and the resulting expected difference with
associated confidence intervals is provided
library(gamdiffs)
library(mgcv)
#> Loading required package: nlme
#> This is mgcv 1.8-35. For overview type 'help("mgcv-package")'.
## basic usage
res <- gamdiffs:::create_random_data()
m <- mgcv::gam(y ~ s(x), data = res, family = poisson)
baseline_data <- dplyr::tibble(x = 0:20)
counter_data <- dplyr::tibble(x = 21:40)
test_diffs <- calc_sum_counterfactual_gam(m, baseline_data,
counter_data = counter_data,
ci = 0.95
)
The resulting expected sum difference between the baseline_data
and
counter_data
are
print(test_diffs)
#> $m
#> [1] -18
#>
#> $lc
#> [1] -38
#>
#> $uc
#> [1] 1.4
The default setting is to estimate the confidence interval from the delta method. The resulting confidence interval from sampling of the posterior (with improper priors) can be calculated as
post_diffs <- calc_sum_counterfactual_gam(m,
baseline_data,
use_post = TRUE,
nrep = 1000,
counter_data = counter_data,
ci = 0.95
)
print(post_diffs)
#> $m
#> [1] -18
#>
#> $lc
#> 2.5%
#> -39
#>
#> $uc
#> 97.5%
#> 2.2
Please note that the gamdiffs project is released with a Contributor Code of Conduct. By contributing to this project, you agree to abide by its terms.