plot.lgspline: Plot Method for lgspline Objects

View source: R/methods.R

plot.lgsplineR Documentation

Plot Method for lgspline Objects

Description

Wrapper for the internal lgspline plot function. Produces a 1D line plot (base R) or interactive 3D surface plot (plotly) depending on the number of predictors, with optional formula overlays per partition. When plotting a subset of variables via vars, non-plotted predictors are automatically set to zero (or a user-specified value via fixed_values).

Usage

## S3 method for class 'lgspline'
plot(
  x,
  show_formulas = FALSE,
  include_all_terms_in_formulas = FALSE,
  digits = 4,
  legend_pos = "topright",
  custom_response_lab = "y",
  custom_predictor_lab = NULL,
  custom_predictor_lab1 = NULL,
  custom_predictor_lab2 = NULL,
  custom_formula_lab = NULL,
  custom_title = "Fitted Function",
  text_size_formula = NULL,
  legend_args = list(),
  new_predictors = NULL,
  xlim = NULL,
  ylim = NULL,
  color_function = NULL,
  add = FALSE,
  vars = c(),
  legend_order = NULL,
  se.fit = FALSE,
  cv = 1,
  band_col = "grey80",
  band_border = NA,
  fixed_values = NULL,
  n_grid = 200,
  ...
)

Arguments

x

A fitted lgspline model object.

show_formulas

Logical; display partition-level polynomial formulas. Default FALSE.

include_all_terms_in_formulas

Logical; when show_formulas = TRUE and plotting only a subset of predictors via vars, include all fitted terms in the displayed formulas rather than only the terms involving the plotted predictor(s). Default FALSE retains the current marginal-only formula display.

digits

Integer; decimal places for formula coefficients. Default 4.

legend_pos

Character; legend position for 1D plots. Default "topright".

custom_response_lab

Character; response axis label. Default "y".

custom_predictor_lab

Character; predictor axis label (1D). Default NULL uses the column name.

custom_predictor_lab1

Character; first predictor axis label (2D). Default NULL.

custom_predictor_lab2

Character; second predictor axis label (2D). Default NULL.

custom_formula_lab

Character; fitted response label on the link scale. Default NULL.

custom_title

Character; plot title. Default "Fitted Function".

text_size_formula

Numeric; formula text size. Passed to cex (1D) or hover font size (2D). Default NULL (0.8 for 1D, 8 for 2D).

legend_args

List; additional arguments passed to legend() (1D only).

new_predictors

Matrix; optional predictor values for prediction. Default NULL. When vars is specified and new_predictors is NULL, a grid is automatically generated with non-plotted variables set to zero (or values from fixed_values).

xlim

Numeric vector; x-axis limits (1D only). Default NULL.

ylim

Numeric vector; y-axis limits (1D only). Default NULL.

color_function

Function; returns K+1 colors, one per partition. Default NULL uses grDevices::rainbow(K+1) for 1D and a Spectral palette for 2D.

add

Logical; add to an existing plot (1D only). Default FALSE.

vars

Numeric or character vector; predictor indices or names to plot. Default c() plots all.

legend_order

Numeric; re-ordered partition indices for the legend.

se.fit

Logical; if TRUE, plot pointwise confidence bands. Default FALSE.

cv

Numeric; critical value for confidence bands. Default 1.

band_col

Character; color for confidence band fill. Default "grey80".

band_border

Character or NA; border color for confidence band polygon. Default NA (no border).

fixed_values

Named list; fixed values for non-plotted predictors when vars is specified. Names should match predictor names. Default NULL sets non-plotted predictors to zero.

n_grid

Integer; number of grid points for automatic grid generation when vars is specified and new_predictors is NULL. Default 200.

...

Additional arguments passed to plot (1D) or plot_ly (2D).

Details

Partition boundaries are indicated by color changes. For 1D models, observation points can be overlaid. For 2D models, plotly is used.

When using vars to plot a subset of predictors, the non-plotted predictors are automatically set to zero. This can be overridden by passing a named list to fixed_values (e.g., fixed_values = list(Height = 75)). The automatic zeroing replaces the previous behavior where the user had to manually construct new_predictors with non-plotted variables set to fixed values.

When se.fit = TRUE, pointwise confidence bands are drawn around the fitted function. These are Wald-type intervals constructed on the link scale and back-transformed to the response scale, using cv as the critical value to multiply se.fit by (default 1 for actual se).

The function extracts predictor positions from linear expansion terms. If linear terms are excluded (e.g., via exclude_these_expansions), plotting will fail. As a workaround, constrain those terms to zero via constraint_vectors / constraint_values so they remain in the expansion but are zeroed out.

Value

For 1D models: invisibly returns NULL (base R plot drawn to device). For 2D models: returns a plotly object.

See Also

lgspline, plot, plot_ly

Examples


set.seed(1234)
t_data <- runif(1000, -10, 10)
y_data <- 2*sin(t_data) + -0.06*t_data^2 + rnorm(length(t_data))
model_fit <- lgspline(t_data, y_data, K = 9)

## Basic plot
plot(model_fit)

## Plot with confidence bands
plot(model_fit,
     se.fit = TRUE,
     cv = 1.96,
     custom_title = 'Fitted Function with 95% CI')

## Multi-predictor: automatically zeros non-plotted variables
# plot(model_fit_2d, vars = 'x1', se.fit = TRUE)


lgspline documentation built on May 8, 2026, 5:07 p.m.