This function fits a piecewise linear or parabolic model to melatonin profile data. It separately models the base segment and the ascending segment, ensuring a smooth transition at the point of interest (POI).
Arguments
- data
A tibble containing melatonin concentration data with
datetimevalues.- poi
A list containing:
x: The x-coordinate (decimal time) of the POI.y: The y-coordinate (melatonin concentration) of the POI.
- fit_type
Character. Type of fit for the ascending segment:
"linear"(default): Uses a linear model for both base and ascending segments."parabolic": Uses a parabolic fit for the ascending segment.
- threshold
Numeric. The threshold for constraining the base segment.
Value
A list containing:
residual: The sum of squared residuals from both fits.base_params: The estimated parameters for the base fit.ascending_params: The estimated parameters for the ascending fit.
Details
The base segment is always fit using a linear model.
The ascending segment is fit using a linear or parabolic model depending on
fit_type.If the ascending segment has at least 3 data points, it first fits a linear model and then refines it with a parabolic fit.
This function is used in inflection point detection.
Examples
data <- tibble::tibble(
datetime = as.POSIXct(c("2024-06-01 22:00", "2024-06-01 22:15", "2024-06-01 22:30")),
melatonin = c(0.5, 0.8, 1.2),
base = c(1, 1, 0) # Base segment indicators
)
poi <- list(x = posixct_to_decimal(data$datetime[2], data$datetime), y = data$melatonin[2])
fit(data, poi, fit_type = "linear", threshold = 2.3)
#> $residual
#> [1] 0.03125
#>
#> $base_params
#> [1] 0.2
#>
#> $ascending_params
#> [1] 1.6
#>