| Title: | Fit Segmented Regression Models |
|---|---|
| Description: | Estimates piecewise linear spline models for assessing temporal trends in cancer incidence and mortality rates. Provides tools for identifying and reporting knot locations, annual percent changes (APCs), and average annual percent changes (AAPCs), facilitating the analysis and communication of changes in cancer rates over time. |
| Authors: | Matthew T. Warkentin [aut, cre, cph] (ORCID: <https://orcid.org/0000-0001-8730-3511>), John Hutchinson [ctb], Yibing Ruan [ctb] |
| Maintainer: | Matthew T. Warkentin <[email protected]> |
| License: | MIT + file LICENSE |
| Version: | 0.1.0.9000 |
| Built: | 2026-09-26 09:58:20 UTC |
| Source: | https://github.com/mattwarkentin/cantrends |
Empiricial Quantile Confidence Intervals
empirical_quantile_ci(x, reps = 10000, conf.level = 0.95)empirical_quantile_ci(x, reps = 10000, conf.level = 0.95)
x |
A |
reps |
Number of replicates. |
conf.level |
The confidence level to use for the confidence interval. Must be strictly greater than 0 and less than 1. Defaults to 0.95 which corresponds to a 95 percent confidence interval. |
x but with empirical quantile confidence intervals instead of
parametric confidence intervals (i.e., for APC and AAPC).
Extract Information from Segmented Regression Objects
extract_fits(x, ...) extract_best_fit(x, metric = NULL, nknots = NULL, ...) extract_best_model(x, metric = NULL, nknots = NULL, ...) extract_best_metrics(x, metric = NULL, nknots = NULL, ...) extract_best_predictions(x, metric = NULL, nknots = NULL, ...) extract_best_apc(x, metric = NULL, nknots = NULL, ...) extract_best_aapc(x, metric = NULL, nknots = NULL, ...)extract_fits(x, ...) extract_best_fit(x, metric = NULL, nknots = NULL, ...) extract_best_model(x, metric = NULL, nknots = NULL, ...) extract_best_metrics(x, metric = NULL, nknots = NULL, ...) extract_best_predictions(x, metric = NULL, nknots = NULL, ...) extract_best_apc(x, metric = NULL, nknots = NULL, ...) extract_best_aapc(x, metric = NULL, nknots = NULL, ...)
x |
An object with class |
... |
Not currently used. |
metric |
Which metric to use for selecting the optimal model fit?
Default is |
nknots |
Number of knots used in model fit. Default is |
The following objects are returned by the extraction functions:
extract_fits(): a tibble of statistics for model fits
extract_best_fit(): a one-row tibble of statistics for a specific
model fit
extract_best_model*(): a model object with class "cantrends_spline_fit"
extract_best_metrics(): a named-list of model fit metrics
extract_best_predictions(): a tibble with the variables used in the
model formula and predictions based on the fitted model
extract_best_apc(): a tibble with data for the Annual Percent Change
(APC) for the chosen model
extract_best_aapc(): a tibble with data for the Avergae Annual
Percent Change (AAPC) for the chosen model
df <- read.delim(system.file("example.txt", package = "cantrends"), header = FALSE) res <- segmented_reg(V3 ~ V2, data = df) extract_best_fit(res) extract_best_model(res) extract_best_metrics(res) extract_best_predictions(res) extract_best_apc(res) extract_best_aapc(res)df <- read.delim(system.file("example.txt", package = "cantrends"), header = FALSE) res <- segmented_reg(V3 ~ V2, data = df) extract_best_fit(res) extract_best_model(res) extract_best_metrics(res) extract_best_predictions(res) extract_best_apc(res) extract_best_aapc(res)
Define rules for selecting knot locations for segmented regression.
knot_opts( min_knots = 1L, max_knots = NULL, min_obs_end = 2L, min_obs_between = 2L, pts_between = 0L, force = FALSE ) ## S3 method for class 'cantrends_knot_opts' print(x, ...) make_knot_sets(x, opts) make_point_set(x, opts)knot_opts( min_knots = 1L, max_knots = NULL, min_obs_end = 2L, min_obs_between = 2L, pts_between = 0L, force = FALSE ) ## S3 method for class 'cantrends_knot_opts' print(x, ...) make_knot_sets(x, opts) make_point_set(x, opts)
min_knots |
Minimum number of knots to evaluate. Default is |
max_knots |
Maximum number of knots to evaluate. If |
min_obs_end |
Minimum number of points from either end of the observed
data range to allow a knot location. Default is |
min_obs_between |
Minimum number of data points between knot
locations. Default is |
pts_between |
Number of new data points to place between adjacent
observed data points. Default is |
force |
Force the knot locations to be generated despite guidelines
around the mininum number of observaions recommended for analysis.
Default is |
x |
Observed time points. |
... |
Not currently used. |
opts |
A |
The minimum number of observations required to run the segmented regression
analysis is estimated as (2 * min_obs_end) + (max_knots - 1) * min_obs_between + max_knots.
knot_opts() returns a list with class "cantrends_knot_opts".
make_knot_sets() returns a two-item list with (1) a list of vectors that
specify the knot locations to evaluate in grid search and (2) the updated
opts object. make_point_set() returns the vector of points used to
generate the knot sets'.
knot_opts()knot_opts()
segmented_reg() fits a linear spline model for the outcome (e.g., rates)
against a time variables (e.g., years). Grid search is performed to find
the best model (i.e., optimal knot locations) according to the metric
criterion. Information about the best model can be extracted from the
returned object using the extract_best_*() set of functions.
segmented_reg( formula, data, events = NULL, opts = knot_opts(), metric = "bic3", conf_level = 0.95, progress = rlang::is_interactive(), knots, ... ) ## S3 method for class 'cantrends_segmented_reg' print(x, ...)segmented_reg( formula, data, events = NULL, opts = knot_opts(), metric = "bic3", conf_level = 0.95, progress = rlang::is_interactive(), knots, ... ) ## S3 method for class 'cantrends_segmented_reg' print(x, ...)
formula |
Model formula (e.g., |
data |
Data frame or |
events |
Vector with the number of events (e.g., deaths) that correspond
to the rate data in the |
opts |
A |
metric |
Metric to use for model selection. One of |
conf_level |
The confidence level to use for the confidence interval. Must be strictly greater than 0 and less than 1. Defaults to 0.95 which corresponds to a 95 percent confidence interval. |
progress |
Whether to show progress bars. By default, progress bars are
enabled in interactive sessions (i.e., if |
knots |
A numeric vector of knot locations or a |
... |
Not currently used. |
x |
A |
lspline::lspline() is used to compute the basis for a piecewise linear
spline to estimate coefficients in the segmented regression model.
If mirai::daemons() has been used to set persistent background processes,
this function will fit segmented regression models in parallel using all
available processes.
A named-list of class "cantrends_segmented_reg".
df <- read.delim(system.file("example.txt", package = "cantrends"), header = FALSE) res <- segmented_reg(V3 ~ V2, data = df)df <- read.delim(system.file("example.txt", package = "cantrends"), header = FALSE) res <- segmented_reg(V3 ~ V2, data = df)