Skip to contents

This function fits a linear model to a given segment of a melatonin profile. It estimates the slope of the best-fit line while enforcing slope constraints and edge boundary conditions if specified.

Usage

fit_linear(
  x,
  y,
  poi,
  base_id = NULL,
  slope_bounds = NULL,
  edge_bounds = NULL,
  weight_base = TRUE
)

Arguments

x

Numeric vector of x-coordinates (decimal time).

y

Numeric vector of y-coordinates (melatonin concentration).

poi

List containing:

  • x: The x-coordinate of the Point of Interest (POI).

  • y: The y-coordinate of the POI.

base_id

Numeric vector. Indicator variable where 1 represents base points and 0 represents ascending points.

slope_bounds

Numeric vector of length 2 specifying lower and upper bounds for the slope. Default is NULL (no constraints).

edge_bounds

List containing:

  • left: Numeric vector [min, max] range for left boundary (base segment).

  • right: Numeric vector [min, max] range for right boundary (ascending segment). If NULL, no edge constraints are applied.

weight_base

Logical. If TRUE, applies lower weights to base points in residual computation.

Value

A list containing:

  • params: A numeric vector with the estimated slope.

Details

  • The function applies weights to prioritize ascending points over base points.

  • Slope constraints ensure that the estimated slope remains within predefined bounds.

  • If edge constraints are provided, the function ensures that the fitted line does not exceed specified y-limits at the segment edges.

Examples

x <- c(0, 1, 2, 3, 4)
y <- c(0.5, 1.0, 2.0, 3.5, 5.0)
poi <- list(x = 2, y = 2.0)
base_id <- c(1, 1, 0, 0, 0)
fit_linear(x, y, poi, base_id = base_id, slope_bounds = c(-0.2, 0.2), weight_base = TRUE)
#> $params
#> [1] 0.2
#>