MyNixOS website logo
Description

Visualization of Spline Effects in GAM and GLM Models.

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.

splineplot

R-CMD-check CRAN_Status_Badge CRAN_Download_Badge Codecov testcoverage GitHubissues GitHubstars Lifecycle:experimental

The splineplot package provides a unified interface for visualizing spline effects from GAM (Generalized Additive Models) and GLM (Generalized Linear Models) in R. It creates publication-ready plots with confidence intervals, supporting various model types including Linear, Logistic, Poisson, and Cox proportional hazards models.

Key Features

  • 📊 Unified visualization for spline effects across different model types
  • 🎨 Publication-ready plots with customizable aesthetics
  • 📈 Automatic detection of spline terms and interaction variables
  • 🔄 Support for multiple spline types: GAM smooths (s(), te(), ti()), GLM splines (ns(), bs()), and Cox pspline()
  • 📉 Flexible confidence intervals: dotted lines or ribbon style
  • 📊 Built-in histograms showing data distribution
  • 🎯 Reference point marking with automatic SE = 0 at reference value
  • 🔀 Interaction term support with by-variable visualization

Installation

You can install the released version of splineplot from CRAN:

install.packages("splineplot")

Or install the development version from GitHub:

# install.packages("devtools")
devtools::install_github("jinseob2kim/splineplot")

Basic Usage

library(splineplot)
library(mgcv)
library(survival)
library(splines)
library(ggplot2)

# Generate sample data
set.seed(123)
n <- 500
x <- rnorm(n, mean = 35, sd = 8)
lp <- -0.06*(x - 35) + 0.0009*(x - 35)^3/(8^2)
time <- rexp(n, rate = exp(lp))
status <- rbinom(n, 1, 0.8)
binary_y <- rbinom(n, 1, plogis(lp))

dat <- data.frame(x, time, status, binary_y)

GAM with Cox family

# Fit GAM Cox model
fit_gam_cox <- gam(time ~ s(x),
                   family = cox.ph(), weights = status, data = dat)

# Create spline plot
splineplot(fit_gam_cox, dat,
          ylim = c(0.2, 2.0),
          xlab = "Age (years)",
          ylab = "Hazard Ratio")
#> Using 'x' as x variable
#> Using refx = 35.17 (median of x)
#> Warning: Removed 67 rows containing missing values or values outside the scale range
#> (`geom_line()`).
#> Warning: Removed 56 rows containing missing values or values outside the scale range
#> (`geom_line()`).
#> Warning: Removed 62 rows containing missing values or values outside the scale range
#> (`geom_line()`).

Logistic regression with natural splines

# Fit logistic model with natural splines
fit_glm <- glm(binary_y ~ ns(x, df = 4),
               family = binomial(), data = dat)

# Create spline plot
splineplot(fit_glm, dat,
          ylim = c(0.2, 2.0),
          ylab = "Odds Ratio")
#> Using 'x' as x variable
#> Using refx = 35.17 (median of x)
#> Warning: Removed 37 rows containing missing values or values outside the scale range
#> (`geom_line()`).
#> Warning: Removed 85 rows containing missing values or values outside the scale range
#> (`geom_line()`).
#> Warning: Removed 59 rows containing missing values or values outside the scale range
#> (`geom_line()`).

GAM with interaction terms

# Add a grouping variable
dat$group <- factor(sample(c("A", "B"), n, replace = TRUE))

# Fit model with interaction
fit_interaction <- gam(time ~ s(x, by = group),
                      family = cox.ph(),
                      weights = status,
                      data = dat)

# Plot with interaction
splineplot(fit_interaction, dat,
          ylim = c(0.2, 2.0))
#> Using 'x' as x variable
#> Detected interaction with 'group'
#> Using refx = 35.17 (median of x)
#> Warning: No shared levels found between `names(values)` of the manual scale and the
#> data's fill values.
#> Warning: Removed 136 rows containing missing values or values outside the scale range
#> (`geom_line()`).
#> Warning: Removed 116 rows containing missing values or values outside the scale range
#> (`geom_line()`).
#> Warning: Removed 126 rows containing missing values or values outside the scale range
#> (`geom_line()`).

Advanced Features

Customizing confidence intervals

# Default: dotted lines
splineplot(fit_gam_cox, dat, ribbon_ci = FALSE)

# Alternative: ribbon/shaded area
splineplot(fit_gam_cox, dat, ribbon_ci = TRUE)

Log scale for odds/rate/hazard ratios

# Use log scale for y-axis
splineplot(fit_glm, dat, log_scale = TRUE)
#> Using 'x' as x variable
#> Using refx = 35.17 (median of x)

Custom reference values

# Set custom reference point (default is median)
splineplot(fit_gam_cox, dat,
          refx = 40,  # Reference at x = 40
          show_ref_point = TRUE)  # Show diamond marker
#> Using 'x' as x variable

Supported Model Types

Model TypeModel FunctionSpline TypesOutcome
GAMmgcv::gam()s(), te(), ti()HR, OR, RR, Effect
GLMstats::glm()ns(), bs()OR, RR, Effect
Linearstats::lm()ns(), bs()Effect
Coxsurvival::coxph()ns(), bs(), pspline()*HR

*Note: pspline() has limited support due to its internal structure. We recommend using ns() or bs() with Cox models for optimal results.

Function Parameters

ParameterDescriptionDefault
fitFitted model objectRequired
dataData frame used for fittingRequired
xvarVariable name for x-axisAuto-detected
by_varInteraction variableAuto-detected
refxReference x valueMedian of x
xlimX-axis limitsData range
ylimY-axis limitsAuto
show_histShow histogramTRUE
ribbon_ciUse ribbon CI styleFALSE
log_scaleUse log scale for y-axisFALSE
show_ref_pointShow reference point markerTRUE
xlabX-axis labelVariable name
ylabY-axis labelAuto by model
ylab_rightRight y-axis label“Percent of Population”

Citation

If you use splineplot in your research, please cite:

citation("splineplot")

Getting Help

Contributing

Contributions are welcome! Please feel free to submit a Pull Request. For major changes, please open an issue first to discuss what you would like to change.

License

Apache License 2.0 © Jinseob Kim / Zarathu

Acknowledgments

Special thanks to the developers of mgcv, survival, and ggplot2 packages.

Metadata

Version

0.3.0

License

Unknown

Platforms (80)

    Darwin
    FreeBSD
    Genode
    GHCJS
    Linux
    MMIXware
    NetBSD
    none
    OpenBSD
    Redox
    Solaris
    uefi
    WASI
    Windows
Show all
  • aarch64-darwin
  • aarch64-freebsd
  • aarch64-genode
  • aarch64-linux
  • aarch64-netbsd
  • aarch64-none
  • aarch64-uefi
  • aarch64-windows
  • aarch64_be-none
  • arc-linux
  • arm-none
  • armv5tel-linux
  • armv6l-linux
  • armv6l-netbsd
  • armv6l-none
  • armv7a-linux
  • armv7a-netbsd
  • armv7l-linux
  • armv7l-netbsd
  • avr-none
  • i686-cygwin
  • i686-freebsd
  • i686-genode
  • i686-linux
  • i686-netbsd
  • i686-none
  • i686-openbsd
  • i686-windows
  • javascript-ghcjs
  • loongarch64-linux
  • m68k-linux
  • m68k-netbsd
  • m68k-none
  • microblaze-linux
  • microblaze-none
  • microblazeel-linux
  • microblazeel-none
  • mips-linux
  • mips-none
  • mips64-linux
  • mips64-none
  • mips64el-linux
  • mipsel-linux
  • mipsel-netbsd
  • mmix-mmixware
  • msp430-none
  • or1k-none
  • powerpc-linux
  • powerpc-netbsd
  • powerpc-none
  • powerpc64-linux
  • powerpc64le-linux
  • powerpcle-none
  • riscv32-linux
  • riscv32-netbsd
  • riscv32-none
  • riscv64-linux
  • riscv64-netbsd
  • riscv64-none
  • rx-none
  • s390-linux
  • s390-none
  • s390x-linux
  • s390x-none
  • sh4-linux
  • vc4-none
  • wasm32-wasi
  • wasm64-wasi
  • x86_64-cygwin
  • x86_64-darwin
  • x86_64-freebsd
  • x86_64-genode
  • x86_64-linux
  • x86_64-netbsd
  • x86_64-none
  • x86_64-openbsd
  • x86_64-redox
  • x86_64-solaris
  • x86_64-uefi
  • x86_64-windows