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:
::install_github("cosinor", "sachsmc") devtools
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:
<- cosinor.lm(Y ~ time(time) + X + amp.acro(X), data = vitamind, period = 12) fit
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")
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.