| Title: | Tidy Tools for Joinpoint Regression Models |
|---|---|
| Description: | Provides tools to fit joinpoint regression models with a log-linear specification by levels of one or two categorical variable(s) using the grid-search method. It includes functions to estimate the Annual Percent Change (APC) and the Average Annual Percent Change (AAPC), along with their 95% confidence intervals, and to generate formatted summary tables and plots of results. |
| Authors: | Tamara Ricardo [aut, cre] (ORCID: <https://orcid.org/0000-0002-0921-2611>) |
| Maintainer: | Tamara Ricardo <[email protected]> |
| License: | MIT + file LICENSE |
| Version: | 2.0.0 |
| Built: | 2026-10-08 16:49:12 UTC |
| Source: | https://github.com/datos-ine/joinpointr |
Computes the Bayesian Information Criterion (BIC),
the penalized BIC (BIC3), and the weighted BIC (WBIC) for
models of class model_jp.
bic_jp(models)bic_jp(models)
models |
Models of class |
The Bayesian Information Criterion (BIC) for k joinpoints is estimated as:
where MSE is the mean squared error of the fitted model,
param(k) is the number of parameters in the model, and n is the
number of observations.
The number of parameters for the standard BIC are calculated as:
For BIC3, the penalty parameter is defined as:
The weighted Bayesian Information Criterion (WBIC) combines BIC and BIC3 using a weighted penalty term based on the data:
where wt is the calculated weighted penalty.
A tibble containing the values for the BIC, BIC3, WBIC and the
weights used to estimate the WBIC.
# Load packages library(dplyr) # Create a reduced dataset data <- hiv_data |> filter(admin == "ARG") # Fit the joinpoint models mods <- model_jp_grid(data = data, rate = hiv_rate, time = year, group = "sex") # Check the BIC, BIC3, WBIC values for a single model bic_jp(mods[1])# Load packages library(dplyr) # Create a reduced dataset data <- hiv_data |> filter(admin == "ARG") # Fit the joinpoint models mods <- model_jp_grid(data = data, rate = hiv_rate, time = year, group = "sex") # Check the BIC, BIC3, WBIC values for a single model bic_jp(mods[1])
Calculates the annual percent change (APC) and the average annual percent change (AAPC) for joinpoint regression models.
get_summary( models, stats = c("both", "apc", "aapc"), hide = c("none", "ci", "sig"), level.ci = 0.95, as.ft = FALSE, dec = c(".", ",") ) get_apc(models, stats = "apc", ...) get_aapc(models, stats = "aapc", ...)get_summary( models, stats = c("both", "apc", "aapc"), hide = c("none", "ci", "sig"), level.ci = 0.95, as.ft = FALSE, dec = c(".", ",") ) get_apc(models, stats = "apc", ...) get_aapc(models, stats = "aapc", ...)
models |
A model or a list of models of class |
stats |
Character. Summary statistics to display in the table.
One of |
hide |
Character. Which elements to hide: the confidence interval ( |
level.ci |
Numeric. Confidence level used to calculate confidence
intervals. Must be between 0 and 1. Defaults to |
as.ft |
Logical. Whether to return the summary as a |
dec |
Character. Decimal separator to use, either a point ( |
... |
Additional arguments passed to |
For each segment, the Annual Percent Change (APC) is calculated from the
estimated slope of the log-linear model as:
Confidence intervals for the APC are calculated using the Student's
distribution and the standard error of the estimated slope.
The resulting confidence limits are then back-transformed to the APC scale:
The standard errors used for the segment-specific slopes are obtained from the variance-covariance matrix of the fitted joinpoint model.
For each fitted model, the Annual Average Percent Change (AAPC) is calculated as the exponential transformation of the weighted average of the segment-specific slopes on the log scale:
where is the estimated slope for segment ,
and is the length of segment divided by the total
length of the selected time interval. Thus, the weights sum to one.
For models without joinpoints, the AAPC is equivalent to the APC.
The confidence interval for the AAPC is calculated on the log scale.
The variance of the weighted slope is obtained from the
variance-covariance matrix of the segment-specific slopes. The confidence
limits are calculated using the Student's distribution and the
residual degrees of freedom of the fitted model, and are then
back-transformed to the AAPC scale:
The confidence intervals described above are based on the variance-covariance matrix and residual degrees of freedom of the fitted joinpoint model and may therefore differ from confidence intervals reported by other joinpoint regression software using different inferential procedures.
For get_summary(), a tibble or a
flextable containing the APC and/or AAPC for each model,
along with their confidence intervals and significance stars according to the
values of stats and hide.
For get_apc(), a tibble or a
flextable containing the APC for each model, along
with its confidence intervals and significance stars according to the
value of hide.
For get_aapc(), a tibble or a
flextable containing the AAPC for each model, along
with its confidence intervals and significance stars according to the
value of hide.
# Create an example dataset data <- hiv_data |> dplyr::filter(admin == "ARG") # Fit the joinpoint models mods <- model_jp_grid(data = data, rate = hiv_rate, time = year, group = "sex") # Obtain the model summary get_summary(models = mods) # Same output calling summary(mods) summary(mods, as.ft = TRUE) # Obtain the APC with 95% CI get_apc(models = mods) # Obtain the AAPC with 95% CI get_aapc(models = mods)# Create an example dataset data <- hiv_data |> dplyr::filter(admin == "ARG") # Fit the joinpoint models mods <- model_jp_grid(data = data, rate = hiv_rate, time = year, group = "sex") # Obtain the model summary get_summary(models = mods) # Same output calling summary(mods) summary(mods, as.ft = TRUE) # Obtain the APC with 95% CI get_apc(models = mods) # Obtain the AAPC with 95% CI get_aapc(models = mods)
Creates a ggplot2 visualization of joinpoint regression models,
showing observed values, fitted regression lines, and optionally the
estimated joinpoints and Average Annual Percent Change (AAPC).
gg_jpoint( models, geom = c("linepoint", "line", "area"), facets = c("wrap", "grid", "grid2"), color.by = NULL, jp = TRUE, aapc = FALSE, time.var = c("year", "yearmon"), ... ) gg_jpoint_area(models, ...) gg_jpoint_line(models, ...)gg_jpoint( models, geom = c("linepoint", "line", "area"), facets = c("wrap", "grid", "grid2"), color.by = NULL, jp = TRUE, aapc = FALSE, time.var = c("year", "yearmon"), ... ) gg_jpoint_area(models, ...) gg_jpoint_line(models, ...)
models |
A model or a list of models of class |
geom |
Character. Determines how the model results are displayed.
|
facets |
Character. Determines the facet layout. |
color.by |
Character. Determines whether the data should be colored using
the grouping variable ( |
jp |
Logical. Whether to display the estimated joinpoint(s) as
vertical lines. Defaults to |
aapc |
Logical. Whether to display a label with the Average Annual
Percent Change (AAPC) and its statistical significance. Defaults to
|
time.var |
Character. Determines the class of the time variable,
among |
... |
Additional arguments passed to |
Available colorblind-friendly palettes can be checked using
plot_cbpal().
A ggplot2 object showing observed values, fitted joinpoint
regression lines, and optionally the estimated joinpoints and AAPC.
# Create an example dataset data <- hiv_data |> dplyr::filter(admin == "Catamarca") # Fit models mods <- model_jp_grid(data = data, rate = hiv_rate, time = year, group = "sex") # Plot results with AAPC gg_jpoint(models = mods, aapc = TRUE)# Create an example dataset data <- hiv_data |> dplyr::filter(admin == "Catamarca") # Fit models mods <- model_jp_grid(data = data, rate = hiv_rate, time = year, group = "sex") # Plot results with AAPC gg_jpoint(models = mods, aapc = TRUE)
HIV Incidence Rates in Argentina by Sex and Jurisdiction
hiv_datahiv_data
A data frame with 4 variables:
Administrative division or jurisdiction name.
Calendar year of observation.
Biological sex (male, female, or both sexes).
Incidence rate per 100,000 inhabitants.
A dataset containing HIV incidence rates for males, females, and both sexes combined, covering the national level as well as individual jurisdictions.
Fits log-linear joinpoint regression models using the grid-search method, assuming constant variance and uncorrelated errors. The best-fit model is selected according to the Bayesian Information Criterion (BIC).
model_jp( data, rate, time, group = NULL, jp = 2, min.dist = 2, method = "bic", silent = FALSE ) model_jp_grid(data, ...) ## S3 method for class 'model_jp' update(object, ...)model_jp( data, rate, time, group = NULL, jp = 2, min.dist = 2, method = "bic", silent = FALSE ) model_jp_grid(data, ...) ## S3 method for class 'model_jp' update(object, ...)
data |
A |
rate |
Character string specifying the variable with the rates. |
time |
Character string specifying the variable with the time points. |
group |
Character vector specifying the name(s) of the variable(s)
used to group the data. A maximum of two grouping variables is allowed.
Defaults to |
jp |
Integer specifying the maximum number of joinpoints to test.
Must be between 0 and 7 (see Details). Defaults to |
min.dist |
Integer specifying the minimum number of time points
required between consecutive joinpoints, as well as between each endpoint
of the time series and the nearest joinpoint. Defaults to |
method |
Character string specifying the method used to calculate the BIC.
Options are |
silent |
Logical. Whether to display a message showing the model name,
number of joinpoints detected and BIC of the best fit model. Defaults
to |
... |
Additional parameters passed to |
object |
Model or list of models to update. |
The maximum recommended number of joinpoints is determined by the number of time points in the series, following the criteria described by Kim et al. (2000):
0–6 time points: 0 joinpoints.
7–11 time points: 1 joinpoint.
12–16 time points: 2 joinpoints.
17–21 time points: 3 joinpoints.
22–26 time points: 4 joinpoints.
27–31 time points: 5 joinpoints.
32–36 time points: 6 joinpoints.
37+ time points: 7 joinpoints.
When grouping variables are specified, all groups must contain the same number of time points.
A named list with one element per group, where each element contains:
fit: An lm object corresponding to the selected
joinpoint model.
joinpoints: Numeric vector with the estimated joinpoint
positions of the selected model.
time: Vector of time points used to fit the model.
log_rate: Vector of log-transformed rates used as the response
variable.
BIC: BIC value of the selected model based on the specified
method.
model_sel: A tibble containing the number and positions
of joinpoints, SSE, BIC, BIC3, Weights and WBIC for all evaluated candidate
models.
Kim, H. J., Fay, M. P., Feuer, E. J., & Midthune, D. N. (2000). Permutation tests for joinpoint regression with applications to cancer rates. Statistics in Medicine, 19(3), 335–351. https://doi.org/10.1002/(sici)1097-0258(20000215)19:3%253C335::aid-sim336%253E3.0.co;2-z
# Create a reduced dataset data <- hiv_data |> dplyr::filter(admin == "ARG") # Fit the joinpoint models mods <- model_jp(data = data, rate = hiv_rate, time = year, group = "sex") # Update model using the BIC3 mods_bic3 <- update(mods, method = "bic3")# Create a reduced dataset data <- hiv_data |> dplyr::filter(admin == "ARG") # Fit the joinpoint models mods <- model_jp(data = data, rate = hiv_rate, time = year, group = "sex") # Update model using the BIC3 mods_bic3 <- update(mods, method = "bic3")
Plots a list of available colorblind-friendly palettes. Allows filtering by palette type, series, or name.
plot_cbpal(type = c("all", "cat", "seq", "div"), series = NULL, name = NULL)plot_cbpal(type = c("all", "cat", "seq", "div"), series = NULL, name = NULL)
type |
Character. Selects palettes based on type: |
series |
Character vector. Selects palettes based on their series name.
Defaults to |
name |
Character vector. Selects one or more specific palettes by name.
Defaults to |
Available palette series:
brewer, carto, cols4all, gmt, hcl,
kovesi, matplotlib, met, meteo, misc,
ocean, parks, powerbi, scico, seaborn,
stevens, tableau, tol.
A ggplot object showing the selected palettes.
# Display all available palettes plot_cbpal() # Display specific palettes by name plot_cbpal(name = c("viridis", "tokyo", "algae")) # Display a specific palette series plot_cbpal(series = "seaborn")# Display all available palettes plot_cbpal() # Display specific palettes by name plot_cbpal(name = c("viridis", "tokyo", "algae")) # Display a specific palette series plot_cbpal(series = "seaborn")
Scales for applying colorblind-friendly palettes to ggplot2 aesthetics.
scale_cbpal( palette = NULL, reverse = FALSE, discrete = TRUE, aesthetics = c("fill", "color"), ... ) scale_cbpal_fill(aesthetics = "fill", ...) scale_cbpal_colour(aesthetics = "color", ...) scale_cbpal_color(aesthetics = "color", ...)scale_cbpal( palette = NULL, reverse = FALSE, discrete = TRUE, aesthetics = c("fill", "color"), ... ) scale_cbpal_fill(aesthetics = "fill", ...) scale_cbpal_colour(aesthetics = "color", ...) scale_cbpal_color(aesthetics = "color", ...)
palette |
Character. String specifying the palette name. Defaults to
|
reverse |
Logical. If |
discrete |
Logical. Should the scale be discrete? |
aesthetics |
Character vector of aesthetics to apply the scale to. |
... |
Additional arguments passed to |
A ggplot2 scale.
library(ggplot2) data(iris) # Use a discrete variable for color iris |> ggplot(aes(x = Sepal.Length, y = Sepal.Width, color = Species)) + geom_point() + scale_cbpal_color() # Use a continuous variable for color iris |> ggplot(aes(x = Sepal.Length, y = Sepal.Width, color = Petal.Length)) + geom_point() + scale_cbpal_color(discrete = FALSE) # Use a discrete variable for fill iris |> ggplot(aes(x = Sepal.Length, fill = Species)) + geom_bar() + scale_cbpal_fill()library(ggplot2) data(iris) # Use a discrete variable for color iris |> ggplot(aes(x = Sepal.Length, y = Sepal.Width, color = Species)) + geom_point() + scale_cbpal_color() # Use a continuous variable for color iris |> ggplot(aes(x = Sepal.Length, y = Sepal.Width, color = Petal.Length)) + geom_point() + scale_cbpal_color(discrete = FALSE) # Use a discrete variable for fill iris |> ggplot(aes(x = Sepal.Length, fill = Species)) + geom_bar() + scale_cbpal_fill()
Use the shortcut summary()
## S3 method for class 'model_jp' summary(object, ...)## S3 method for class 'model_jp' summary(object, ...)
object |
Model or list of models to update. |
... |
Additional arguments passed to |