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.
The package can be installed from CRAN:
install.packages("cosinor")
To install from github, you must first have the devtools
package installed. Then run this command to install the latest version:
devtools::install_github("cosinor", "sachsmc")
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)
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")
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) summary(predict(fit))
Plotting the fitted curves is also easy to do. Currently only ggplot2
is supported.
library(ggplot2) ggplot.cosinor.lm(fit, x_str = "X")
The package comes with an interactive web app that can be used to analyze your own data. If you have your data loaded in R as a data.frame
, run the command
cosinor_analyzer(vitamind)
to run the Shiny application.
Add the following code to your website.
For more information on customizing the embed code, read Embedding Snippets.