--- title: "Simulating data" author: "Oliver Jayasinghe and Rex Parsons" output: rmarkdown::html_vignette vignette: > %\VignetteIndexEntry{data-simulations} %\VignetteEngine{knitr::rmarkdown} %\VignetteEncoding{UTF-8} --- ```{r, include = FALSE} knitr::opts_chunk$set( collapse = TRUE, comment = "#>" ) ``` ```{r setup} library(GLMMcosinor) ``` ## Simulating rhythmic data `simulate_cosinor` allows users to simulate circadian data from Gaussian, Gamma, Binomial, or Poisson distributions. It also supports generation of multi-component data, as well as simulation of grouped data with two levels. ### Understanding the inputs for a simple model `n` specifies the number of datapoints. `mesor`, `amp`, and `acro` represent the parameters that will be used to simulate the dataset. Note that `acro` should be expressed in units of radians. `period` determines the period of the dataset `n_components` corresponds to the number of components in the simulated dataset. Details about how to specify a multi-component model are included later in this vignette The `family` argument determines the distribution that the data is simulated from. Currently, `simulate_cosinor` supports simulations from Gaussian, Gamma, Binomial, and Poisson distributions: - `family = 'gaussian'` - `family = 'gamma'` - `family = 'binomial'` - `family = 'poisson'` Note that the `…` parameter controls extra arguments such as standard deviation, and the shape parameter for a Gamma distribution: - `sd` controls the standard deviation when sampling from a normal distribution. `sd` is set to 1 by default - `alpha` controls the shape parameter for the Gamma distribution. `alpha` is set to 1 by default `n_period` is the number of periods that are simulated. By default, the maximum period supplied defines the upper limit of the time vector used in the simulation. Thus, increasing `n_period` increases the number of cycles that are simulated. Consider the following example of a single-component Poisson data-set with no grouping variable: ```{r, message=F, warning=F, eval=F} testdata <- simulate_cosinor( n = 200, mesor = 1, amp = 2, acro = 1.2, period = 12, n_period = 3, family = "poisson" ) testdata ``` Now, let's fit a `cglmm()` model to this simulated dataset to see how it matches with our original parameters: ```{r, message=F, warning=F, eval=F} object <- cglmm( Y ~ amp_acro(times, n_components = 1, period = 12 ), data = testdata, family = poisson() ) summary(object) autoplot(object, superimpose.data = TRUE) ``` ### Simulating grouped cosinor data The `simulate_cosinor()` function can simulate grouped data from two levels with their own parameters when `beta.group = TRUE`. The reference group is specified by the parameters `mesor`, `amp`, `acro`. The treatment group is specified in the same manner, but with the `beta` prefix. For example: - `beta.mesor` - `beta.amp` - `beta.acro` Similarly, the standard deviation (`sd)` for the Gaussian distribution or the `alpha` parameter for the Gamma distribution are specified as: - `beta.sd` (1 by default) - `beta.alpha` (1 by default) ```{r, message=F, warning=F, eval=F} simulate_cosinor( n = 100, mesor = 1, amp = 2, acro = 1.2, period = 12, beta.group = TRUE, beta.mesor = 0.4, beta.amp = 0.5, beta.acro = 0.2, n_period = 3, n_components = 1, family = "poisson" ) ``` ```{r, message=F, warning=F, echo=F} testdata <- simulate_cosinor( n = 100, mesor = 1, amp = 2, acro = 1.2, period = 12, beta.group = TRUE, beta.mesor = 0.4, beta.amp = 0.5, beta.acro = 0.2, n_components = 1, n_period = 3, family = "gaussian" ) DT::datatable(testdata) object <- cglmm( Y ~ group + amp_acro(times, n_components = 1, period = 12, group = "group" ), data = testdata, family = gaussian() ) summary(object) autoplot(object, superimpose.data = TRUE) ``` ### Simulating multi-component cosinor data To simulate multi-component data with the number of components corresponding to `n_components` , specify a vector of values for the parameter inputs, `amp`, `acro`, and `period`. Since only one `mesor` is provided for a multi-component cosinor curve, the `mesor` argument in `simualte_cosinor` will only accept single-element inputs. For example: ```{r, message=F, warning=F, eval=F} testdata <- simulate_cosinor( n = 200, mesor = 1, amp = c(0.2, 1), acro = c(1.2, 2), period = c(12, 6), n_components = 2, n_period = 2, family = "poisson" ) testdata object <- cglmm( Y ~ amp_acro(times, n_components = 2, period = c(12, 6) ), data = testdata, family = poisson() ) summary(object) autoplot(object, superimpose.data = TRUE) ``` In this example: - `mesor = 1` suggests that the intercept of the overall cosinor curve (accounting for all components) is 1 - `amp=c(0.2,1)` indicates that the amplitude of the first component is 0.2, and the second is 1 - `acro = c(1.2, 2)` indicates that the acrophase of the first component is 1.2 radians, and the second is 2 radians - `period = c(12, 6)` indicates that the period of the first component is 12 units, and the second is 6 units. - `n_components = 2`, because we are specifying a two-component dataset To simulate a dataset with more than two components, specify more elements in the vector inputs for these parameters. Ensure that the number of inputs for each parameter corresponds to the number of components specified in `n_components`. For example, if `n_components = 3`, then `amp`, `acro`, `period` must all have 3 elements corresponding to each of the three components. The following are examples of a multi-component cosinor dataset with multiple groups. The first one is from a Poisson distribution, and the second is from a Gamma distribution ```{r, message=F, warning=F, fig.show = "hold"} testdata <- simulate_cosinor(100, mesor = 7, amp = c(0.1, 0.4, 0.5), acro = c(1, 1.5, 0.1), beta.mesor = 4.4, beta.amp = c(2, 1, 0.4), beta.acro = c(1, -1.5, -1), family = "poisson", period = c(12, 6, 8), n_period = 2, n_components = 3 ) object <- cglmm(Y ~ group + amp_acro(times, n_components = 3, period = c(12, 6, 8), group = "group" ), data = testdata, family = poisson()) summary(object) autoplot(object, superimpose.data = TRUE, x_str = "group", predict.ribbon = FALSE ) ``` ```{r, message=F, warning=F, fig.show = "hold"} testdata <- simulate_cosinor(500, mesor = 1, amp = c(0.5, 0.5, 0.5), acro = c(pi, pi / 2, pi), alpha = 2, beta.mesor = 2, beta.amp = c(0.2, 0.2, 0.2), beta.acro = c(pi / 2, pi, pi / 2), beta.alpha = 3, family = "gamma", period = c(12, 6, 8), n_period = 2, n_components = 3 ) object <- cglmm(Y ~ group + amp_acro(times, n_components = 3, period = c(12, 6, 8), group = "group" ), data = testdata, family = Gamma(link = "log")) summary(object) autoplot(object, superimpose.data = TRUE, x_str = "group", predict.ribbon = FALSE ) ```