The hardware and bandwidth for this mirror is donated by METANET, the Webhosting and Full Service-Cloud Provider.
If you wish to report a bug, or if you are interested in having us mirror your free-software or open-source project, please feel free to contact us at mirror[@]metanet.ch.
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.
s()
, te()
, ti()
), GLM splines
(ns()
, bs()
), and Cox
pspline()
You can install the released version of splineplot from CRAN:
install.packages("splineplot")
Or install the development version from GitHub:
# install.packages("devtools")
::install_github("jinseob2kim/splineplot") devtools
library(splineplot)
library(mgcv)
library(survival)
library(splines)
library(ggplot2)
# Generate sample data
set.seed(123)
<- 500
n <- rnorm(n, mean = 35, sd = 8)
x <- -0.06*(x - 35) + 0.0009*(x - 35)^3/(8^2)
lp <- rexp(n, rate = exp(lp))
time <- rbinom(n, 1, 0.8)
status <- rbinom(n, 1, plogis(lp))
binary_y
<- data.frame(x, time, status, binary_y) dat
# Fit GAM Cox model
<- gam(time ~ s(x),
fit_gam_cox 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 2 rows containing missing values or values outside the scale range
#> (`geom_rect()`).
#> Warning: Removed 39 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 49 rows containing missing values or values outside the scale range
#> (`geom_line()`).
# Fit logistic model with natural splines
<- glm(binary_y ~ ns(x, df = 4),
fit_glm 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 2 rows containing missing values or values outside the scale range
#> (`geom_rect()`).
#> Warning: Removed 22 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()`).
# Add a grouping variable
$group <- factor(sample(c("A", "B"), n, replace = TRUE))
dat
# Fit model with interaction
<- gam(time ~ s(x, by = group),
fit_interaction 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 2 rows containing missing values or values outside the scale range
#> (`geom_rect()`).
#> 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()`).
# Default: dotted lines
splineplot(fit_gam_cox, dat, ribbon_ci = FALSE)
# Alternative: ribbon/shaded area
splineplot(fit_gam_cox, dat, ribbon_ci = TRUE)
# 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)
#> Warning: Removed 2 rows containing missing values or values outside the scale range
#> (`geom_rect()`).
#> Warning: Removed 1 row containing missing values or values outside the scale range
#> (`geom_segment()`).
#> Removed 1 row containing missing values or values outside the scale range
#> (`geom_segment()`).
#> Removed 1 row containing missing values or values outside the scale range
#> (`geom_segment()`).
#> Removed 1 row containing missing values or values outside the scale range
#> (`geom_segment()`).
#> Removed 1 row containing missing values or values outside the scale range
#> (`geom_segment()`).
# 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
#> Warning: Removed 2 rows containing missing values or values outside the scale range
#> (`geom_rect()`).
#> Warning: Removed 1 row containing missing values or values outside the scale range
#> (`geom_segment()`).
#> Removed 1 row containing missing values or values outside the scale range
#> (`geom_segment()`).
#> Removed 1 row containing missing values or values outside the scale range
#> (`geom_segment()`).
#> Removed 1 row containing missing values or values outside the scale range
#> (`geom_segment()`).
#> Removed 1 row containing missing values or values outside the scale range
#> (`geom_segment()`).
Model Type | Model Function | Spline Types | Outcome |
---|---|---|---|
GAM | mgcv::gam() |
s() , te() ,
ti() |
HR, OR, RR, Effect |
GLM | stats::glm() |
ns() , bs() |
OR, RR, Effect |
Linear | stats::lm() |
ns() , bs() |
Effect |
Cox | survival::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.
Parameter | Description | Default |
---|---|---|
fit |
Fitted model object | Required |
data |
Data frame used for fitting | Required |
xvar |
Variable name for x-axis | Auto-detected |
by_var |
Interaction variable | Auto-detected |
refx |
Reference x value | Median of x |
xlim |
X-axis limits | Data range |
ylim |
Y-axis limits | Auto |
show_hist |
Show histogram | TRUE |
ribbon_ci |
Use ribbon CI style | FALSE |
log_scale |
Use log scale for y-axis | FALSE |
show_ref_point |
Show reference point marker | TRUE |
xlab |
X-axis label | Variable name |
ylab |
Y-axis label | Auto by model |
ylab_right |
Right y-axis label | โPercent of Populationโ |
If you use splineplot in your research, please cite:
citation("splineplot")
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.
Apache License 2.0 ยฉ Jinseob Kim / Zarathu.
Special thanks to the developers of mgcv, survival, and ggplot2 packages.
These binaries (installable software) and packages are in development.
They may not be fully stable and should be used with caution. We make no claims about them.