Type: | Package |
Title: | Visualization of Spline Effects in GAM and GLM Models |
Version: | 0.1.1 |
Maintainer: | Jinseob Kim <jinseob2kim@gmail.com> |
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 interaction terms and provides hazard ratio plots with histograms for survival analysis. Wood (2017, ISBN:9781498728331) provides comprehensive methodology for generalized additive models. |
License: | Apache License 2.0 |
Encoding: | UTF-8 |
Imports: | ggplot2 (≥ 3.0.0), stats |
Suggests: | mgcv, survival, splines, testthat (≥ 3.0.0), knitr, rmarkdown, covr, pkgdown |
RoxygenNote: | 7.3.2 |
VignetteBuilder: | knitr |
URL: | https://github.com/jinseob2kim/splineplot, https://jinseob2kim.github.io/splineplot/ |
BugReports: | https://github.com/jinseob2kim/splineplot/issues |
Config/testthat/edition: | 3 |
NeedsCompilation: | no |
Packaged: | 2025-09-23 15:09:22 UTC; js |
Author: | Jinseob Kim |
Repository: | CRAN |
Date/Publication: | 2025-09-30 08:30:14 UTC |
Extract Spline Data
Description
Extract predictions and confidence intervals from fitted models
Usage
extract_spline_data(
fit,
data,
xvar,
refx,
model_info,
term_index = 1,
log_scale = FALSE,
ci_level = 0.95
)
Arguments
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 |
Value
Data frame with predictions
Examples
# 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)
Spline Plot for GAM and GLM Models
Description
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().
Usage
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"
)
Arguments
fit |
A fitted model object (gam, glm, lm, coxph) |
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") |
Value
A ggplot2 object
Examples
# 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)
}