Skip to contents

This function fits a linear or parabolic model to either the base or ascending segment of a melatonin profile relative to a specified point of interest (POI). It ensures that the fit adheres to predefined constraints.

Usage

fit_profile(
  x,
  y,
  poi,
  fit_type = "linear",
  region = "base",
  base_tangent = NULL,
  ascending_linear_tangent = NULL,
  base_id = NULL,
  weight_base = TRUE,
  threshold = threshold
)

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.

fit_type

Character. "linear" (default) or "parabolic" for the type of fit.

region

Character. "base" (default) or "ascending" to specify which part of the profile to fit.

base_tangent

Numeric. Estimated slope of the base segment (used if fit_type = "parabolic").

ascending_linear_tangent

Numeric. Estimated slope of the ascending segment (used if fit_type = "parabolic").

base_id

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

weight_base

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

threshold

Numeric. Upper boundary for the base fit. Default is threshold.

Value

A list containing:

  • residual: The mean squared residual error.

  • params: The fitted model parameters.

Details

  • Base segment fitting (region = "base"): Uses a linear fit constrained within slope bounds.

  • Ascending segment fitting (region = "ascending"):

    • If fit_type = "linear", a linear fit is applied with no slope constraints.

    • If fit_type = "parabolic", a quadratic fit is optimized while enforcing monotonicity.

  • The function penalizes fits that do not meet slope constraints.

  • Weighted residuals prioritize ascending points when computing the fit error.

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)
fit_profile(x, y, poi, fit_type = "linear", region = "ascending", weight_base = TRUE)
#> $residual
#> [1] NaN
#> 
#> $params
#> [1] 1.15
#>