MyNixOS website logo
Description

Tools for Estimating and Predicting the Cosinor Model.

A set of simple functions that transforms longitudinal data to estimate the cosinor linear model as described in Tong (1976). Methods are given to summarize the mean, amplitude and acrophase, to predict the mean annual outcome value, and to test the coefficients.

Installation

This is an R package for estimation and prediction of the cosinor model for periodic data. It allows for covariate effects and provides tools for inference on the mean shift, amplitude, and acrophase. Check out the shiny app that illustrates the model here:

https://sachsmc.shinyapps.io/cosinor-shinyapp/

The package is on CRAN and can be installed as follows

install.packages("cosinor")

To install from github, you must first have the devtools package installed. Then run this command to install the latest version:

remotes::install_github("sachsmc/cosinor")

Model details

For outcome $Y$, time variable $t$, and fixed period $D$ it is of interest to fit the periodic model

$$ Y(t) = \alpha + \beta_1 * \cos{2 * \pi * t / D - \beta_2} + \varepsilon $$

where $\alpha$ is the intercept, $\beta_1$ is the amplitude, and $\beta_2$ is the acrophase (also called phase-shift). The $\varepsilon$ is an error term with mean 0.

This model transforms so that it can be fit using a simple linear model. Let $r = \cos{2 * \pi * t / D}$ and let $s = \sin{2 * \pi * t / D}$. Then we have

$$ Y(t) = \alpha + \gamma_1 * r + \gamma_2 * s + \varepsilon. $$

The original coefficients can be recovered as follows:

$$ \beta_1 = \sqrt{\gamma_1^2 + \gamma_2^2} $$

and

$$ \beta_2 = tan^{-1}(\gamma_2 / \gamma_1). $$

In the package, $(\alpha, \gamma_1, \gamma_2)$ is estimated using lm, and inference on the transformed coefficients is obtained using the delta method.

Example usage

Load the package into your library:

library(cosinor)

The package comes with an example dataset called vitamind to illustrate the usage. Fit a basic cosinor model with covariates:

fit <- cosinor.lm(Y ~ time(time) + X + amp.acro(X), data = vitamind, period = 12)

The time variable is indicated by the time function in the formula. By default, the period length is 12. Covariates can be included directly in the formula to allow for mean shifts. To allow for differences in amplitude and acrophase by covariate values, include the covariate wrapped in the amp.acro function in the formula. Let’s summarize the model.

summary(fit)
## Raw model coefficients:
##             estimate standard.error lower.CI upper.CI p.value
## (Intercept)  29.6898         0.4654  28.7776  30.6020  0.0000
## X             1.9019         0.8041   0.3258   3.4779  0.0180
## rrr           0.9308         0.6357  -0.3151   2.1767  0.1431
## sss           6.2010         0.6805   4.8673   7.5347  0.0000
## X:rrr         5.5795         1.1386   3.3479   7.8111  0.0000
## X:sss        -1.3825         1.1364  -3.6097   0.8447  0.2237
## 
## ***********************
## 
## Transformed coefficients:
##             estimate standard.error lower.CI upper.CI p.value
## (Intercept)  29.6898         0.4654  28.7776  30.6020   0.000
## [X = 1]       1.9019         0.8041   0.3258   3.4779   0.018
## amp           6.2705         0.6799   4.9378   7.6031   0.000
## [X = 1]:amp   8.0995         0.9095   6.3170   9.8820   0.000
## acr           1.4218         0.1015   1.2229   1.6207   0.000
## [X = 1]:acr   0.6372         0.1167   0.4084   0.8659   0.000

This prints the raw coefficients, and the transformed coefficients. The transformed coefficients display the amplitude and acrophase for the covariate = 1 group and the covariate = 0 group. Beware when using continuous covariates!

Testing is easy to do, tell the function which covariate to test, and whether to test the amplitude or acrophase. The global test is useful when you have multiple covariates in the model.

test_cosinor(fit, "X", param = "amp")
## Global test: 
## Statistic: 
## [1] 2.59
## 
## 
##  P-value: 
## [1] 0.1072
## 
##  Individual tests: 
## Statistic: 
## [1] 1.61
## 
## 
##  P-value: 
## [1] 0.1072
## 
##  Estimate and confidence interval[1] "1.83 (-0.4 to 4.05)"

The predict function allows you to estimate the mean value of your outcome for individuals. This is useful for adjusting for the seasonal effects on some measurement. Let’s compare the raw values to the seasonally adjusted values of the vitamin D dataset.

summary(vitamind$Y)
##    Min. 1st Qu.  Median    Mean 3rd Qu.    Max. 
##   13.27   25.59   29.79   30.09   34.85   51.30
summary(predict(fit))
##    Min. 1st Qu.  Median    Mean 3rd Qu.    Max. 
##   16.10   25.98   29.26   29.69   33.59   46.48

Plotting the fitted curves is also easy to do. Currently only ggplot2 is supported.

library(ggplot2)
ggplot.cosinor.lm(fit, x_str = "X")

Metadata

Version

1.2.3

License

Unknown

Platforms (75)

    Darwin
    FreeBSD
    Genode
    GHCJS
    Linux
    MMIXware
    NetBSD
    none
    OpenBSD
    Redox
    Solaris
    WASI
    Windows
Show all
  • aarch64-darwin
  • aarch64-genode
  • aarch64-linux
  • aarch64-netbsd
  • aarch64-none
  • aarch64_be-none
  • arm-none
  • armv5tel-linux
  • armv6l-linux
  • armv6l-netbsd
  • armv6l-none
  • armv7a-darwin
  • armv7a-linux
  • armv7a-netbsd
  • armv7l-linux
  • armv7l-netbsd
  • avr-none
  • i686-cygwin
  • i686-darwin
  • i686-freebsd
  • i686-genode
  • i686-linux
  • i686-netbsd
  • i686-none
  • i686-openbsd
  • i686-windows
  • javascript-ghcjs
  • loongarch64-linux
  • m68k-linux
  • m68k-netbsd
  • m68k-none
  • microblaze-linux
  • microblaze-none
  • microblazeel-linux
  • microblazeel-none
  • mips-linux
  • mips-none
  • mips64-linux
  • mips64-none
  • mips64el-linux
  • mipsel-linux
  • mipsel-netbsd
  • mmix-mmixware
  • msp430-none
  • or1k-none
  • powerpc-netbsd
  • powerpc-none
  • powerpc64-linux
  • powerpc64le-linux
  • powerpcle-none
  • riscv32-linux
  • riscv32-netbsd
  • riscv32-none
  • riscv64-linux
  • riscv64-netbsd
  • riscv64-none
  • rx-none
  • s390-linux
  • s390-none
  • s390x-linux
  • s390x-none
  • vc4-none
  • wasm32-wasi
  • wasm64-wasi
  • x86_64-cygwin
  • x86_64-darwin
  • x86_64-freebsd
  • x86_64-genode
  • x86_64-linux
  • x86_64-netbsd
  • x86_64-none
  • x86_64-openbsd
  • x86_64-redox
  • x86_64-solaris
  • x86_64-windows