predict.lgspline: Predict Method for lgspline Objects

View source: R/methods.R

predict.lgsplineR Documentation

Predict Method for lgspline Objects

Description

Generates predictions, derivatives, and basis expansions from a fitted lgspline model. Wrapper for the internal predict closure stored in the object.

Usage

## S3 method for class 'lgspline'
predict(
  object,
  newdata = NULL,
  parallel = FALSE,
  cl = NULL,
  chunk_size = NULL,
  num_chunks = NULL,
  rem_chunks = NULL,
  B_predict = NULL,
  take_first_derivatives = FALSE,
  take_second_derivatives = FALSE,
  expansions_only = FALSE,
  new_predictors = NULL,
  ...
)

Arguments

object

A fitted lgspline model object.

newdata

Matrix or data.frame; new predictor values. Default NULL.

parallel

Logical; use parallel processing (experimental). Default FALSE.

cl

Cluster object for parallel processing. Default NULL.

chunk_size

Integer; chunk size for parallel processing. Default NULL.

num_chunks

Integer; number of chunks. Default NULL.

rem_chunks

Integer; remainder chunks. Default NULL.

B_predict

List; per-partition coefficient list for prediction, e.g. from generate_posterior. Default NULL uses object$B.

take_first_derivatives

Logical; compute first derivatives. Default FALSE.

take_second_derivatives

Logical; compute second derivatives. Default FALSE.

expansions_only

Logical; return basis expansion matrix only. Default FALSE.

new_predictors

Matrix or data.frame; overrides newdata.

...

Additional arguments passed to the internal predict method.

Details

new_predictors takes priority over newdata when both are supplied. When both are NULL, the training data is used.

Fitted values are also accessible directly as model_fit$ytilde or via model_fit$predict().

The parallel processing feature is experimental.

Additional arguments passed through ... include se.fit and cv for pointwise interval summaries.

Predictor input should use the original predictor columns. Named extra columns are dropped when they can be identified as irrelevant to the fitted expansions.

Value

A numeric vector of predictions, or a list when derivatives or interval summaries are requested:

preds

Numeric vector of predictions when derivatives are requested.

fit

Numeric vector of predictions when se.fit = TRUE and no derivatives are requested.

first_deriv

Numeric vector or named list of first derivatives (if requested).

second_deriv

Numeric vector or named list of second derivatives (if requested).

se.fit

Pointwise standard errors on the link scale (if requested).

lower

Pointwise lower interval bound (if requested).

upper

Pointwise upper interval bound (if requested).

cv

Critical value returned when se.fit = TRUE without derivative requests.

If expansions_only = TRUE, returns a list of basis expansions.

See Also

lgspline, plot.lgspline

Examples


set.seed(1234)
t <- runif(1000, -10, 10)
y <- 2*sin(t) + -0.06*t^2 + rnorm(length(t))
model_fit <- lgspline(t, y)

newdata <- matrix(sort(rnorm(10000)), ncol = 1)
preds <- predict(model_fit, newdata)

deriv1_res <- predict(model_fit, newdata, take_first_derivatives = TRUE)
deriv2_res <- predict(model_fit, newdata, take_second_derivatives = TRUE)

oldpar <- par(no.readonly = TRUE)
layout(matrix(c(1,1,2,2,3,3), byrow = TRUE, ncol = 2))

plot(newdata[,1], preds, main = 'Fitted Function',
     xlab = 't', ylab = "f(t)", type = 'l')
plot(newdata[,1], deriv1_res$first_deriv, main = 'First Derivative',
     xlab = 't', ylab = "f'(t)", type = 'l')
plot(newdata[,1], deriv2_res$second_deriv, main = 'Second Derivative',
     xlab = 't', ylab = "f''(t)", type = 'l')

par(oldpar)


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