Skip to contents

This function fits a sine curve of the form \(y = \text{amp} \cdot \sin\left(\frac{2\pi}{\text{period}} \cdot (x + \text{phase})\right) + \text{offset}\) to time series data using Levenberg-Marquardt optimization from `minpack.lm::nls.lm`.

Usage

fit_sine_curve(
  x,
  y,
  parStart = list(amp = 3, phase = 0, offset = 0, period = 24)
)

Arguments

x

Numeric vector of time values (e.g., hours or days).

y

Numeric vector of response values.

parStart

Named list with starting values for parameters: `amp`, `phase`, `offset`, and `period`.

Value

A named list with:

amp

Amplitude (peak distance from baseline).

phase

Phase shift (horizontal time shift/ scanner rotation) in same units as `tt`.

offset

Vertical shift (baseline).

peak

Time position of the peak / scanner rotation position, relative to phase and period.

period

Distance between peaks

tss

Total sum of squares.

rss

Residual sum of squares.

R2

Coefficient of determination (pseudo R²).

residual_se

Residual standard error.

df_residual

Degrees of freedom of the residual.

predicted

Fitted values.

residuals

Model residuals.