nl_knots: Automatic knot / degrees-of-freedom selection for spline...

View source: R/nl_knots.R

nl_knotsR Documentation

Automatic knot / degrees-of-freedom selection for spline models

Description

Performs a grid search over candidate degrees of freedom (df) for a natural cubic spline ("ns") or B-spline ("bs") model and selects the best value by an information criterion (AIC or BIC). A diagnostic plot of the criterion against df is returned.

This function is called internally by nl_fit when df = "auto", but it can also be called directly for exploration before fitting the final model.

Usage

nl_knots(
  data,
  y,
  x,
  time = NULL,
  cluster = NULL,
  nested = FALSE,
  controls = NULL,
  method = c("ns", "bs"),
  df_range = 2:10,
  criterion = c("AIC", "BIC"),
  family = stats::gaussian(),
  plot = TRUE,
  ...
)

Arguments

data

A data frame.

y

Outcome variable name (string).

x

Focal predictor name (string).

time

Optional time variable name.

cluster

Optional character vector of cluster variable names.

nested

Logical; nested clustering. Default FALSE.

controls

Optional character vector of control variable names.

method

Either "ns" (default) or "bs".

df_range

Integer vector of candidate df values. Default 2:10.

criterion

Either "AIC" (default) or "BIC".

family

A family object. Default stats::gaussian().

plot

Logical; if TRUE, prints a diagnostic plot of criterion vs df. Default TRUE.

...

Additional arguments passed to the underlying fitter.

Value

A list with:

best_df

The df value with the lowest criterion value.

search_table

Data frame with columns df and criterion.

criterion

The criterion used ("AIC" or "BIC").

plot

A ggplot object (if plot = TRUE).

See Also

nl_fit, nl_compare


MultiSpline documentation built on April 16, 2026, 9:06 a.m.