| Title: | Visualization of Spline Effects in GAM and GLM Models |
|---|---|
| Description: | Creates 'ggplot2'-based visualizations of smooth effects from GAM (Generalized Additive Models) fitted with 'mgcv' and spline effects from GLM (Generalized Linear Models). Supports survey-weighted models ('svyglm', 'svycoxph') from the 'survey' package, interaction terms, and provides hazard ratio plots with histograms for survival analysis. Wood (2017, ISBN:9781498728331) provides comprehensive methodology for generalized additive models. |
| Authors: | Jinseob Kim [aut, cre] (ORCID: <https://orcid.org/0000-0002-9403-605X>), Zarathu [cph, fnd] |
| Maintainer: | Jinseob Kim <[email protected]> |
| License: | Apache License 2.0 |
| Version: | 0.3.0 |
| Built: | 2026-05-12 09:36:16 UTC |
| Source: | https://github.com/jinseob2kim/splineplot |
Extract predictions and confidence intervals from fitted models
extract_spline_data( fit, data, xvar, refx, model_info, term_index = 1, log_scale = FALSE, ci_level = 0.95 )extract_spline_data( fit, data, xvar, refx, model_info, term_index = 1, log_scale = FALSE, ci_level = 0.95 )
fit |
Fitted model object |
data |
Data frame |
xvar |
Variable name |
refx |
Reference value |
model_info |
Model information list |
term_index |
Which smooth term to use (for multiple s() terms) |
log_scale |
Whether to use log scale |
ci_level |
Confidence level |
Data frame with predictions
# Create sample data set.seed(123) n <- 100 x <- rnorm(n, mean = 50, sd = 10) y <- rbinom(n, 1, plogis(-0.05*(x - 50))) dat <- data.frame(x = x, y = y) # Fit GLM with splines library(splines) fit <- glm(y ~ ns(x, df = 4), family = binomial(), data = dat) # Extract spline data model_info <- list(type = "glm", family = "binomial", ylabel = "Odds Ratio") df <- extract_spline_data(fit, dat, "x", refx = 50, model_info, log_scale = FALSE, ci_level = 0.95) head(df)# Create sample data set.seed(123) n <- 100 x <- rnorm(n, mean = 50, sd = 10) y <- rbinom(n, 1, plogis(-0.05*(x - 50))) dat <- data.frame(x = x, y = y) # Fit GLM with splines library(splines) fit <- glm(y ~ ns(x, df = 4), family = binomial(), data = dat) # Extract spline data model_info <- list(type = "glm", family = "binomial", ylabel = "Odds Ratio") df <- extract_spline_data(fit, dat, "x", refx = 50, model_info, log_scale = FALSE, ci_level = 0.95) head(df)
Create ggplot2 visualizations of smooth or spline effects from GAM and GLM models. Supports Linear, Logistic, Poisson, and Cox models with interaction terms. Handles GAM smooth terms (s(), te(), ti()), GLM splines (ns(), bs()), and Cox pspline().
splineplot( fit, data, xvar = NULL, by_var = NULL, refx = NULL, term_index = 1, bins = 12, xlim = NULL, ylim = NULL, show_hist = NULL, log_scale = FALSE, ci_level = 0.95, show_ref_point = TRUE, colors = NULL, ribbon_ci = FALSE, xlab = NULL, ylab = NULL, ylab_right = "Percent of Population" )splineplot( fit, data, xvar = NULL, by_var = NULL, refx = NULL, term_index = 1, bins = 12, xlim = NULL, ylim = NULL, show_hist = NULL, log_scale = FALSE, ci_level = 0.95, show_ref_point = TRUE, colors = NULL, ribbon_ci = FALSE, xlab = NULL, ylab = NULL, ylab_right = "Percent of Population" )
fit |
A fitted model object (gam, glm, lm, coxph, svyglm, svycoxph) |
data |
The data frame used to fit the model |
xvar |
Character string specifying the variable name for x-axis (default: first spline term) |
by_var |
Character string specifying the interaction variable (default: auto-detect from model) |
refx |
Reference value for the x variable (default: median) |
term_index |
For GAM with multiple smooth terms, which term to plot (default: 1) |
bins |
Number of bins for histogram (default: 12) |
xlim |
X-axis limits (default: range of x variable) |
ylim |
Y-axis limits (default: auto-determined, e.g., c(0.25, 2.0) for HR/OR/RR) |
show_hist |
Logical, whether to show histogram (default: TRUE) |
log_scale |
Logical, whether to use log scale for OR/RR/HR (default: FALSE) |
ci_level |
Confidence interval level (default: 0.95) |
show_ref_point |
Logical, whether to show reference point marker (default: TRUE) |
colors |
Named vector of colors for by_var levels |
ribbon_ci |
Logical, whether to use ribbon style for CI (default: FALSE, uses dotted lines) |
xlab |
Custom x-axis label (default: xvar name) |
ylab |
Custom y-axis label (default: auto-determined based on model type) |
ylab_right |
Custom right y-axis label for histogram (default: "Percent of Population") |
A ggplot2 object
# Create sample data set.seed(123) n <- 200 x <- rnorm(n, mean = 50, sd = 10) lp <- -0.05*(x - 50) + 0.001*(x - 50)^2 y <- rbinom(n, 1, plogis(lp)) dat <- data.frame(x = x, y = y) # GLM with natural splines library(splines) fit_glm <- glm(y ~ ns(x, df = 4), family = binomial(), data = dat) p <- splineplot(fit_glm, dat) # GAM example (requires mgcv) if (requireNamespace("mgcv", quietly = TRUE)) { fit_gam <- mgcv::gam(y ~ s(x), family = binomial(), data = dat) p2 <- splineplot(fit_gam, dat) } # Cox model example (requires survival) if (requireNamespace("survival", quietly = TRUE)) { time <- rexp(n, rate = exp(lp/2)) status <- rbinom(n, 1, 0.8) dat$time <- time dat$status <- status fit_cox <- survival::coxph(survival::Surv(time, status) ~ ns(x, df = 4), data = dat) p3 <- splineplot(fit_cox, dat) }# Create sample data set.seed(123) n <- 200 x <- rnorm(n, mean = 50, sd = 10) lp <- -0.05*(x - 50) + 0.001*(x - 50)^2 y <- rbinom(n, 1, plogis(lp)) dat <- data.frame(x = x, y = y) # GLM with natural splines library(splines) fit_glm <- glm(y ~ ns(x, df = 4), family = binomial(), data = dat) p <- splineplot(fit_glm, dat) # GAM example (requires mgcv) if (requireNamespace("mgcv", quietly = TRUE)) { fit_gam <- mgcv::gam(y ~ s(x), family = binomial(), data = dat) p2 <- splineplot(fit_gam, dat) } # Cox model example (requires survival) if (requireNamespace("survival", quietly = TRUE)) { time <- rexp(n, rate = exp(lp/2)) status <- rbinom(n, 1, 0.8) dat$time <- time dat$status <- status fit_cox <- survival::coxph(survival::Surv(time, status) ~ ns(x, df = 4), data = dat) p3 <- splineplot(fit_cox, dat) }