Type: | Package |
Title: | Fit a Cosinor Model Using a Generalized Mixed Modeling Framework |
Version: | 0.2.1 |
Description: | Allows users to fit a cosinor model using the 'glmmTMB' framework. This extends on existing cosinor modeling packages, including 'cosinor' and 'circacompare', by including a wide range of available link functions and the capability to fit mixed models. The cosinor model is described by Cornelissen (2014) <doi:10.1186/1742-4682-11-16>. |
License: | GPL (≥ 3) |
URL: | https://github.com/ropensci/GLMMcosinor, https://docs.ropensci.org/GLMMcosinor/ |
BugReports: | https://github.com/ropensci/GLMMcosinor/issues |
Imports: | assertthat, cowplot, ggforce, ggplot2, glmmTMB, lme4, rlang, scales, stats |
Suggests: | cosinor, covr, DHARMa, dplyr, DT, flextable, ftExtra, knitr, rmarkdown, testthat (≥ 3.0.0), vdiffr, withr |
VignetteBuilder: | knitr |
Config/testthat/edition: | 3 |
Encoding: | UTF-8 |
LazyData: | true |
RoxygenNote: | 7.3.2 |
Depends: | R (≥ 4.1.0) |
Language: | en-US |
NeedsCompilation: | no |
Packaged: | 2024-10-31 23:04:18 UTC; parsonrr |
Author: | Rex Parsons |
Maintainer: | Rex Parsons <rex.parsons94@gmail.com> |
Repository: | CRAN |
Date/Publication: | 2024-11-01 00:20:02 UTC |
GLMMcosinor: A package for fitting generalized linear mixed-effects models with cosinor terms
Description
To learn more about how to use GLMMcosinor, see the vignettes at the website or using the following code:
browseVignettes(package = "GLMMcosinor")
This package provides functions for fitting generalized linear mixed-effects models with cosinor terms. Cosinor terms can be used to model periodic variations in time series data, and are particularly useful in chronobiology and sleep research. The package uses the flexible glmmTMB framework that allows users to specify a wide range of data families, and provides tools for visualization.
Author(s)
Maintainer: Rex Parsons rex.parsons94@gmail.com (ORCID)
Authors:
Other contributors:
Prasad Chunduri (ORCID) [contributor, funder]
Margaret Doyle [contributor]
Michael Sachs (ORCID) [reviewer]
Joaquin Cavieres (ORCID) [reviewer]
See Also
Useful links:
Report bugs at https://github.com/ropensci/GLMMcosinor/issues
Used to specify a cosinor component in the model formula.
Description
Checks the validity of user inputs before creating an updated formula
and associated modifications to the data.frame
.
Usage
amp_acro(time_col, n_components = 1, group, period, ...)
Arguments
time_col |
A |
n_components |
The Number of cosinor components in the model. |
group |
A vector of the names for the group factors (column names
within the |
period |
A |
... |
Extra arguments for use within |
Value
A data.frame
and formula
appropriate for use by
data_processor()
.
Examples
# Single component cosinor model
cglmm(
vit_d ~ amp_acro(time_col = time, group = "X", period = 12),
data = vitamind
)
# 2-component cosinor model with simulated data
sim_data <- simulate_cosinor(
n = 500,
mesor = 5,
amp = c(2, 1),
acro = c(1, 1.5),
beta.mesor = 2,
beta.amp = c(2, 1),
beta.acro = c(1, 1.5),
family = "gaussian",
period = c(12, 6),
n_components = 2,
beta.group = TRUE,
)
cglmm(
Y ~ group + amp_acro(times,
n_components = 2,
group = "group",
period = c(12, 6)
),
data = sim_data,
family = gaussian
)
Plot a cosinor model
Description
Given a cglmm model fit, generate a plot of the data with the fitted values. Optionally allows for plotting by covariates
Usage
## S3 method for class 'cglmm'
autoplot(
object,
ci_level = 0.95,
x_str,
type = "response",
xlims,
pred.length.out,
points_per_min_cycle_length = 20,
superimpose.data = FALSE,
data_opacity = 0.3,
predict.ribbon = TRUE,
ranef_plot = NULL,
cov_list = NULL,
quietly = TRUE,
...
)
Arguments
object |
An |
ci_level |
The level for calculated confidence intervals. Defaults to
|
x_str |
A |
type |
A |
xlims |
A vector of length two containing the limits for the x-axis. |
pred.length.out |
An integer value that specifies the number of
predicted data points. The larger the value, the more smooth the fitted line
will appear. If missing, uses |
points_per_min_cycle_length |
Used to determine the number of samples
to create plot if |
superimpose.data |
A |
data_opacity |
A number between 0 and 1 inclusive that controls the
opacity of the superimposed data. (Used as the |
predict.ribbon |
A |
ranef_plot |
Specify the random effects variables that you wish to plot. If not specified, only the fixed effects will be visualized. |
cov_list |
Specify the levels of the covariates that you wish to plot as
a list. For example, if you have two covariates: var1, and var 2, you could
fix the level to be plotted as such |
quietly |
A |
... |
Additional, ignored arguments. |
Value
Returns a ggplot
object.
Examples
# A simple model
model <- cglmm(
vit_d ~ X + amp_acro(time, group = "X", period = 12),
data = vitamind
)
autoplot(model, x_str = "X")
# Plotting a model with various covariates
test_data <- vitamind[vitamind$X == 1, ]
test_data$var1 <- sample(c("a", "b", "c"), size = nrow(test_data), replace = TRUE)
test_data$var2 <- rnorm(n = nrow(test_data))
object <- cglmm(
vit_d ~ amp_acro(time, period = 12) + var1 + var2,
data = test_data
)
autoplot(object,
cov_list = list(
var1 = "a",
var2 = 1
),
superimpose.data = TRUE
)
Fit cosinor model with {glmmTMB}
Description
Given an outcome and time variable, fit the cosinor model with optional covariate effects.
Usage
cglmm(
formula,
data,
family = stats::gaussian(),
quietly = TRUE,
dispformula = ~1,
ziformula = ~0,
...
)
Arguments
formula |
A |
data |
A |
family |
A |
quietly |
A |
dispformula |
A one-sided (i.e., no response variable) |
ziformula |
A one-sided (i.e., no response variable) |
... |
Optional additional arguments passed to |
Value
Returns a fitted cosinor model as a cglmm
object.
References
Tong, YL. Parameter Estimation in Studying Circadian Rhythms, Biometrics (1976). 32(1):85–94.
Examples
# Single component cosinor model
cglmm(
vit_d ~ amp_acro(time_col = time, group = "X", period = 12),
data = vitamind
)
# 2-component cosinor model with simulated data
sim_data <- simulate_cosinor(
n = 500,
mesor = 5,
amp = c(2, 1),
acro = c(1, 1.5),
beta.mesor = 2,
beta.amp = c(2, 1),
beta.acro = c(1, 1.5),
family = "gaussian",
period = c(12, 6),
n_components = 2,
beta.group = TRUE,
)
cglmm(
Y ~ group + amp_acro(times,
n_components = 2,
group = "group",
period = c(12, 6)
),
data = sim_data,
family = gaussian
)
cosinor_mixed dataset for cosinor modeling examples.
Description
Simulated data set to illustrate a mixed cosinor model. The Y
column
contains a simulated outcome variable that varies over the time variable
(times
). The subject
column is a grouping variable that can be
used as a random effect. The rhythm has a period of 24 hours. Data was
simulated using simulate_cosinor
.
Usage
cosinor_mixed
Format
A data.frame
with 3 variables: Y
, times
, and
subject
.
Fit the cosinor GLMM model using the output from
update_formula_and_data()
and a new formula
Description
Fit the cosinor GLMM model using the output from
update_formula_and_data()
and a new formula
Usage
fit_model_and_process(obj, formula, ...)
Arguments
obj |
Output from |
formula |
A (optionally) new formula to use when fitting the cosinor model (maybe with random effects) or other covariates found in the data. |
... |
Optional additional arguments passed to |
Value
Returns a fitted cosinor model as a cglmm
object.
Examples
# Use vitamind data but add a "patient" identifier used as a random effect
vitamind2 <- vitamind
vitamind2$patient <- sample(
LETTERS[1:5],
size = nrow(vitamind2), replace = TRUE
)
# Use update_formula_and_data() to perform wrangling steps of cglmm()
# without yet fitting the model
data_and_formula <- update_formula_and_data(
data = vitamind2,
formula = vit_d ~ X + amp_acro(time,
group = "X",
period = 12
)
)
# print formula from above
data_and_formula$newformula
# fit model while adding random effect to cosinor model formula.
mod <- fit_model_and_process(
obj = data_and_formula,
formula = update.formula(
data_and_formula$newformula, . ~ . + (1 | patient)
)
)
mod
mod$fit # printing the `glmmTMB` model within shows Std.Dev. of random effect
Generates a polar plot with elliptical confidence intervals
Description
Generates a polar plot with elliptical confidence intervals
Usage
polar_plot(
x,
ci_level = 0.95,
n_breaks = 5,
component_index = NULL,
grid_angle_segments = 8,
radial_units = c("radians", "degrees", "period"),
clockwise = FALSE,
text_size = 3,
text_opacity = 0.5,
fill_colors,
ellipse_opacity = 0.3,
circle_linetype = "dotted",
start = c("right", "left", "top", "bottom"),
view = c("full", "zoom", "zoom_origin"),
overlay_parameter_info = FALSE,
quietly = TRUE,
show_component_labels = TRUE,
xlims,
ylims,
...
)
Arguments
x |
An object of class |
ci_level |
The level for calculated confidence ellipses. Defaults to 0.95. |
n_breaks |
The number of concentric circles that will be plotted using
the |
component_index |
A number that corresponds to a particular component
from the |
grid_angle_segments |
An |
radial_units |
A
|
clockwise |
A |
text_size |
A number controlling the font size of the text labels. Defaults to 3. |
text_opacity |
A |
fill_colors |
A |
ellipse_opacity |
A |
circle_linetype |
A |
start |
A |
view |
A
|
overlay_parameter_info |
A |
quietly |
Analogous to verbose, this |
show_component_labels |
Logical argument, TRUE by default. When TRUE, the polar plots have labels corresponding to their components. |
xlims |
A vector of length two containing the limits for the x-axis. |
ylims |
A vector of length two containing the limits for the y-axis. |
... |
Additional, ignored arguments. |
Value
Returns a ggplot
object.
Examples
data(vitamind)
model <- cglmm(
vit_d ~ X + amp_acro(time, group = "X", period = 12),
data = vitamind
)
polar_plot(model, radial_units = "period")
Generates a polar plot with elliptical confidence intervals
Description
Generates a polar plot with elliptical confidence intervals
Usage
## S3 method for class 'cglmm'
polar_plot(
x,
ci_level = 0.95,
n_breaks = 5,
component_index = NULL,
grid_angle_segments = 8,
radial_units = c("radians", "degrees", "period"),
clockwise = FALSE,
text_size = 3.5,
text_opacity = 1,
fill_colors,
ellipse_opacity = 0.3,
circle_linetype = "dotted",
start = c("right", "left", "top", "bottom"),
view = c("full", "zoom", "zoom_origin"),
overlay_parameter_info = FALSE,
quietly = TRUE,
show_component_labels = TRUE,
xlims,
ylims,
...
)
Arguments
x |
An object of class |
ci_level |
The level for calculated confidence ellipses. Defaults to 0.95. |
n_breaks |
The number of concentric circles that will be plotted using
the |
component_index |
A number that corresponds to a particular component
from the |
grid_angle_segments |
An |
radial_units |
A
|
clockwise |
A |
text_size |
A number controlling the font size of the text labels. Defaults to 3. |
text_opacity |
A |
fill_colors |
A |
ellipse_opacity |
A |
circle_linetype |
A |
start |
A |
view |
A
|
overlay_parameter_info |
A |
quietly |
Analogous to verbose, this |
show_component_labels |
Logical argument, TRUE by default. When TRUE, the polar plots have labels corresponding to their components. |
xlims |
A vector of length two containing the limits for the x-axis. |
ylims |
A vector of length two containing the limits for the y-axis. |
... |
Additional, ignored arguments. |
Value
Returns a ggplot
object.
Examples
model <- cglmm(
vit_d ~ X + amp_acro(time, group = "X", period = 12),
data = vitamind
)
polar_plot(model, radial_units = "period")
Predict from a cosinor model
Description
Given a time variable and optional covariates, generate predicted values from a cosinor fit. Default prediction is the mean value, optionally can predict at a given month
Usage
## S3 method for class 'cglmm'
predict(object, newdata, ...)
Arguments
object |
An object of class |
newdata |
Optional new data. |
... |
other arguments passed to |
Value
Returns predicted values from the cosinor model.
Examples
fit <- cglmm(vit_d ~ X + amp_acro(time,
group = "X",
n_components = 1,
period = 12
), data = vitamind)
predict(fit)
Print a brief summary of the cglmm
model.
Description
Print a brief summary of the cglmm
model.
Usage
## S3 method for class 'cglmm'
print(x, digits = getOption("digits"), ...)
Arguments
x |
A |
digits |
Controls the number of digits displayed in the summary output. |
... |
Additional, ignored arguments. |
Value
print(x)
returns x
invisibly.
Examples
# Single component cosinor model
cglmm(
vit_d ~ amp_acro(time_col = time, group = "X", period = 12),
data = vitamind
)
Print test of model
Description
Print test of model
Usage
## S3 method for class 'cglmmSubTest'
print(x, ...)
Arguments
x |
A |
... |
Additional, ignored arguments. |
Value
print(x)
returns x
invisibly.
Examples
data_2_component <- simulate_cosinor(
n = 10000,
mesor = 5,
amp = c(2, 5),
acro = c(0, pi),
beta.mesor = 4,
beta.amp = c(3, 4),
beta.acro = c(0, pi / 2),
family = "gaussian",
n_components = 2,
period = c(10, 12),
beta.group = TRUE
)
mod_2_component <- cglmm(
Y ~ group + amp_acro(times,
n_components = 2, group = "group",
period = c(10, 12)
),
data = data_2_component
)
test_output <- test_cosinor_levels(
mod_2_component,
param = "amp",
x_str = "group"
)
print(test_output$global.test)
Print the summary of a cosinor model
Description
Print the summary of a cosinor model
Usage
## S3 method for class 'cglmmSummary'
print(x, digits = getOption("digits"), ...)
Arguments
x |
An object of class |
digits |
Controls the number of digits displayed in the summary output |
... |
Currently unused |
Value
print
returns x
invisibly.
Examples
fit <- cglmm(vit_d ~ X + amp_acro(time,
group = "X",
n_components = 1,
period = 12
), data = vitamind)
summary(fit)
Print results of test of cosinor model
Description
Print results of test of cosinor model
Usage
## S3 method for class 'cglmmTest'
print(x, ...)
Arguments
x |
A |
... |
Arguments passed to |
Value
print(x)
returns x
invisibly.
Examples
data_2_component <- simulate_cosinor(
n = 10000,
mesor = 5,
amp = c(2, 5),
acro = c(0, pi),
beta.mesor = 4,
beta.amp = c(3, 4),
beta.acro = c(0, pi / 2),
family = "gaussian",
n_components = 2,
period = c(10, 12),
beta.group = TRUE
)
mod_2_component <- cglmm(
Y ~ group + amp_acro(times,
n_components = 2, group = "group",
period = c(10, 12)
),
data = data_2_component
)
test_cosinor_levels(
mod_2_component,
param = "amp",
x_str = "group"
)
Objects exported from other packages
Description
These objects are imported from other packages. Follow the links below to see their documentation.
Extract residual standard deviation or dispersion parameter
Description
see ?glmmTMB::sigma
for more details.
Usage
## S3 method for class 'cglmm'
sigma(object, ...)
Arguments
object |
An object of class |
... |
(ignored; for method compatibility) |
Value
a numeric
.
Examples
testdata_poisson <- simulate_cosinor(100,
n_period = 2,
mesor = 7,
amp = c(0.1, 0.5),
acro = c(1, 1),
beta.mesor = 4.4,
beta.amp = c(0.1, 0.46),
beta.acro = c(0.5, -1.5),
family = "poisson",
period = c(12, 6),
n_components = 2,
beta.group = TRUE
)
mod <- cosinor_model <- cglmm(
Y ~ group + amp_acro(times,
period = c(12, 6),
n_components = 2,
group = "group"
),
data = testdata_poisson,
family = glmmTMB::nbinom1()
)
sigma(mod)
Simulate data from a cosinor model
Description
This function simulates data from a cosinor model with a single covariate, where the time scale is month, and optionally allows for single covariate effects on the mean, amplitude, and acrophase.
Usage
simulate_cosinor(
n,
mesor,
amp,
acro,
period = 24,
n_components,
beta.group = FALSE,
beta.mesor,
beta.amp,
beta.acro,
n_period = 1,
family = c("gaussian", "poisson", "binomial", "gamma"),
...
)
Arguments
n |
The sample size. An |
mesor |
A |
amp |
A |
acro |
A |
period |
The period of the rhythm data (for |
n_components |
The number of components in the model.
This must match the length of the inputs for |
beta.group |
A |
beta.mesor |
A |
beta.amp |
A |
beta.acro |
A |
n_period |
A |
family |
A |
... |
Extra arguments, including |
Value
Returns simulated data in a data.frame
.
Examples
simulate_cosinor(
n = 100,
mesor = 1,
amp = 1,
acro = 1,
period = 24,
family = "gaussian"
)
Summarize a cosinor model Given a time variable and optional covariates, generate inference a cosinor fit. Gives estimates, confidence intervals, and tests for the raw parameters, and for the mean, amplitude, and acrophase parameters. If the model includes covariates, the function returns the estimates of the mean, amplitude, and acrophase for the group with covariates equal to 1 and equal to 0. This may not be the desired result for continuous covariates.
Description
Summarize a cosinor model Given a time variable and optional covariates, generate inference a cosinor fit. Gives estimates, confidence intervals, and tests for the raw parameters, and for the mean, amplitude, and acrophase parameters. If the model includes covariates, the function returns the estimates of the mean, amplitude, and acrophase for the group with covariates equal to 1 and equal to 0. This may not be the desired result for continuous covariates.
Usage
## S3 method for class 'cglmm'
summary(object, ci_level = 0.95, ...)
Arguments
object |
An object of class |
ci_level |
The level for calculated confidence intervals. Defaults to 0.95. |
... |
Currently unused |
Value
Returns a summary of the cglmm
model as
a cglmmSummary
object.
Examples
fit <- cglmm(vit_d ~ X + amp_acro(time,
group = "X",
n_components = 1,
period = 12
), data = vitamind)
summary(fit)
Test for differences in a cosinor model between components.
Description
Given a time variable and optional covariates, generate inference a cosinor fit. For the covariate named (or vector of covariates), this function performs a Wald test comparing the group with covariates equal to 1 to the group with covariates equal to 0. This may not be the desired result for continuous covariates.
Usage
test_cosinor_components(
x,
x_str = NULL,
param = "amp",
comparison_A = 1,
comparison_B = 2,
level_index = 0,
ci_level = 0.95
)
Arguments
x |
An |
x_str |
A |
param |
A |
comparison_A |
An |
comparison_B |
An |
level_index |
An |
ci_level |
The level for calculated confidence intervals. Defaults to
|
Value
Returns a test_cosinor
object.
Examples
data_2_component <- simulate_cosinor(
n = 10000,
mesor = 5,
amp = c(2, 5),
acro = c(0, pi),
beta.mesor = 4,
beta.amp = c(3, 4),
beta.acro = c(0, pi / 2),
family = "gaussian",
n_components = 2,
period = c(10, 12),
beta.group = TRUE
)
mod_2_component <- cglmm(
Y ~ group + amp_acro(times,
n_components = 2, group = "group",
period = c(10, 12)
),
data = data_2_component
)
test_cosinor_components(mod_2_component, param = "amp", x_str = "group")
Test for differences in a cosinor model between levels of the grouping variable.
Description
Given a time variable and optional covariates, generate inference a cosinor fit. For the covariate named (or vector of covariates), this function performs a Wald test comparing the group with covariates equal to 1 to the group with covariates equal to 0. This may not be the desired result for continuous covariates.
Usage
test_cosinor_levels(
x,
x_str,
param = "amp",
comparison_A,
comparison_B,
component_index = 1,
ci_level = 0.95
)
Arguments
x |
An |
x_str |
A |
param |
A |
comparison_A |
An |
comparison_B |
An |
component_index |
An |
ci_level |
The level for calculated confidence intervals. Defaults to
|
Value
Returns a test_cosinor
object.
Examples
data_2_component <- simulate_cosinor(
n = 10000,
mesor = 5,
amp = c(2, 5),
acro = c(0, pi),
beta.mesor = 4,
beta.amp = c(3, 4),
beta.acro = c(0, pi / 2),
family = "gaussian",
n_components = 2,
period = c(10, 12),
beta.group = TRUE
)
mod_2_component <- cglmm(
Y ~ group + amp_acro(times,
n_components = 2, group = "group",
period = c(10, 12)
),
data = data_2_component
)
test_cosinor_levels(mod_2_component, param = "amp", x_str = "group")
Update data and formula for fitting cglmm model
Description
Update data and formula for fitting cglmm model
Usage
update_formula_and_data(
data,
formula,
family = "gaussian",
quietly = TRUE,
dispformula = ~1,
ziformula = ~0
)
Arguments
data |
input data for fitting cglmm model. |
formula |
model formula, specified by user including |
family |
the model family. |
quietly |
controls whether messages from amp_acro are displayed. TRUE by default |
dispformula |
The formula specifying the dispersion model |
ziformula |
The formula specifying the zero-inflation model |
Value
Returns a list
.
Examples
# Use vitamind data but add a "patient" identifier used as a random effect
vitamind2 <- vitamind
vitamind2$patient <- sample(
LETTERS[1:5],
size = nrow(vitamind2), replace = TRUE
)
# Use update_formula_and_data() to perform wrangling steps of cglmm()
# without yet fitting the model
data_and_formula <- update_formula_and_data(
data = vitamind2,
formula = vit_d ~ X + amp_acro(time,
group = "X",
period = 12
)
)
# print formula from above
data_and_formula$newformula
# fit model while adding random effect to cosinor model formula.
mod <- fit_model_and_process(
obj = data_and_formula,
formula = update.formula(
data_and_formula$newformula, . ~ . + (1 | patient)
)
)
mod
mod$fit # printing the `glmmTMB` model within shows Std.Dev. of random effect
Vitamin D dataset for cosinor modeling examples.
Description
Simulated data set to illustrate the cosinor model. The vit_d
column
contains the blood vitamin D levels which vary over time (time
).
The rhythm of the vitamind D fluctuations follows a cosine function and can
be modeled with a cosinor model. The X
column is a binary covariate
representing two groups of patients and is associated with the
characteristics of the rhythm. The rhythm has a period of about 12 hours.
Usage
vitamind
Format
A data.frame
with 3 variables: vit_d
, time
, and
X
.