adjust_span {doblin} | R Documentation |
Recursively adjust the LOESS span to ensure a stable fit
Description
This function attempts to fit a LOESS (Locally Estimated Scatterplot Smoothing) curve to the provided data using a span parameter that controls the degree of smoothing. If the initial fit produces NA values in key components of the model object, the function recursively increases the span until a stable fit is achieved or the span exceeds 1.
Usage
adjust_span(x, y, span)
Arguments
x |
A numeric vector representing the predictor variable (e.g., time or position). |
y |
A numeric vector representing the response variable to be smoothed. Must be non-negative due to log-transformation. |
span |
A numeric value specifying the smoothing parameter for the LOESS fit. Defaults to a low value (e.g., 0.2) and is increased recursively if the fit fails. |
Details
This approach avoids requiring the user to manually tune the span, instead adapting to the data distribution and scale. The fit is performed on the log-transformed y values to reduce skewness and avoid numerical instability.
Value
A fitted loess object if a valid fit is achieved.
Examples
set.seed(123)
x <- seq(0, 10, length.out = 100)
y <- abs(sin(x)) + rnorm(100, sd = 0.1)
# Fit using recursive span adjustment
fit <- adjust_span(x, y, span = 0.2)