Learn R Programming

LaMa (version 2.1.2)

cosinor: Trigonometric basis expansion

Description

Builds a design matrix of sin/cos pairs for use in models with periodic predictors. Can be used directly or inside formulas passed to make_matrices (where expansion is handled automatically).

Usage

cosinor(x, period = 24)

Value

A numeric matrix with 2 * length(period) columns named

sin(2*pi*x/period) / cos(2*pi*x/period).

Arguments

x

Numeric vector of the periodic variable.

period

Numeric vector of period lengths, e.g. 24 for a daily cycle with hourly data or c(24, 12) for a daily + semi-daily cycle.

Details

The resulting columns form the basis for linear predictors of the form $$ \eta_t = \beta_0 + \sum_k \Bigl( \beta_{1k} \sin\!\Bigl(\tfrac{2 \pi x_t}{\text{period}_k}\Bigr) + \beta_{2k} \cos\!\Bigl(\tfrac{2 \pi x_t}{\text{period}_k}\Bigr) \Bigr). $$

Examples

Run this code
cosinor(1:24, period = 24)
cosinor(1:24, period = c(24, 12, 6))

## In model formulas (expand_cosinor handles the expansion):
form <- ~ x + temp * cosinor(hour, c(24, 12))
data <- data.frame(x = runif(24), temp = rnorm(24, 20), hour = 1:24)
modmat <- make_matrices(form, data = data)

Run the code above in your browser using DataLab