| plot.lgspline | R Documentation |
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).
## 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,
...
)
x |
A fitted lgspline model object. |
show_formulas |
Logical; display partition-level polynomial formulas. Default FALSE. |
include_all_terms_in_formulas |
Logical; when |
digits |
Integer; decimal places for formula coefficients. Default 4. |
legend_pos |
Character; legend position for 1D plots. Default
|
custom_response_lab |
Character; response axis label. Default
|
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 |
text_size_formula |
Numeric; formula text size. Passed to |
legend_args |
List; additional arguments passed to |
new_predictors |
Matrix; optional predictor values for prediction.
Default NULL. When |
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 |
add |
Logical; add to an existing plot (1D only). Default FALSE. |
vars |
Numeric or character vector; predictor indices or names to
plot. Default |
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
|
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 |
n_grid |
Integer; number of grid points for automatic grid
generation when |
... |
Additional arguments passed to |
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.
For 1D models: invisibly returns NULL (base R plot drawn to device). For 2D models: returns a plotly object.
lgspline,
plot,
plot_ly
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)
Add the following code to your website.
For more information on customizing the embed code, read Embedding Snippets.