Skip to contents

Using: ‘simulate_cosinor.R’

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:

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:

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)

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"
)
#> 
#>  Conditional Model 
#> Raw model coefficients:
#>                      estimate standard.error     lower.CI upper.CI    p.value
#> (Intercept)       0.984587770    0.095015435  0.798360940  1.17081 < 2.22e-16
#> group1           -0.705439529    0.134372116 -0.968804037 -0.44208 1.5219e-07
#> group0:main_rrr1  0.488457156    0.134793717  0.224266326  0.75265 0.00029038
#> group1:main_rrr1  0.272687084    0.134793717  0.008496255  0.53688 0.04307361
#> group0:main_sss1  1.985573122    0.133581268  1.723758648  2.24739 < 2.22e-16
#> group1:main_sss1 -0.030481932    0.133581268 -0.292296406  0.23133 0.81949841
#>                     
#> (Intercept)      ***
#> group1           ***
#> group0:main_rrr1 ***
#> group1:main_rrr1 *  
#> group0:main_sss1 ***
#> group1:main_sss1    
#> ---
#> Signif. codes:  0 '***' 0.001 '**' 0.01 '*' 0.05 '.' 0.1 ' ' 1
#> 
#> Transformed coefficients:
#>                   estimate standard.error    lower.CI upper.CI    p.value    
#> (Intercept)     0.98458777     0.09501543  0.79836094  1.17081 < 2.22e-16 ***
#> [group=1]      -0.70543953     0.13437212 -0.96880404 -0.44208 1.5219e-07 ***
#> [group=0]:amp1  2.04477163     0.13566828  1.77886669  2.31068 < 2.22e-16 ***
#> [group=1]:amp1  0.27438548     0.18924438 -0.09652669  0.64530    0.14709    
#> [group=0]:acr1  1.32958297     0.06686639  1.19852725  1.46064 < 2.22e-16 ***
#> [group=1]:acr1 -0.11132141     0.68356351 -1.45108127  1.22844    0.87063    
#> ---
#> Signif. codes:  0 '***' 0.001 '**' 0.01 '*' 0.05 '.' 0.1 ' ' 1

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:

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

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)
#> 
#>  Conditional Model 
#> Raw model coefficients:
#>                      estimate standard.error     lower.CI upper.CI    p.value
#> (Intercept)       6.998161137    0.003503253  6.991294886  7.00503 < 2.22e-16
#> group1           -2.602134468    0.019693184 -2.640732400 -2.56354 < 2.22e-16
#> group0:main_rrr1  0.051974331    0.004440518  0.043271076  0.06068 < 2.22e-16
#> group1:main_rrr1  1.107684957    0.021131916  1.066267162  1.14910 < 2.22e-16
#> group0:main_sss1  0.082793763    0.004493713  0.073986247  0.09160 < 2.22e-16
#> group1:main_sss1  1.691737881    0.026948093  1.638920590  1.74456 < 2.22e-16
#> group0:main_rrr2  0.023025259    0.003983879  0.015216999  0.03083 7.4875e-09
#> group1:main_rrr2  0.051503432    0.014997274  0.022109316  0.08090 0.00059434
#> group0:main_sss2  0.397066000    0.004778517  0.387700279  0.40643 < 2.22e-16
#> group1:main_sss2 -1.017051643    0.017191418 -1.050746203 -0.98336 < 2.22e-16
#> group0:main_rrr3  0.496125614    0.004261273  0.487773673  0.50448 < 2.22e-16
#> group1:main_rrr3  0.210943152    0.009332789  0.192651221  0.22924 < 2.22e-16
#> group0:main_sss3  0.047833276    0.004000656  0.039992134  0.05567 < 2.22e-16
#> group1:main_sss3 -0.339274425    0.011341959 -0.361504256 -0.31704 < 2.22e-16
#>                     
#> (Intercept)      ***
#> group1           ***
#> group0:main_rrr1 ***
#> group1:main_rrr1 ***
#> group0:main_sss1 ***
#> group1:main_sss1 ***
#> group0:main_rrr2 ***
#> group1:main_rrr2 ***
#> group0:main_sss2 ***
#> group1:main_sss2 ***
#> group0:main_rrr3 ***
#> group1:main_rrr3 ***
#> group0:main_sss3 ***
#> group1:main_sss3 ***
#> ---
#> Signif. codes:  0 '***' 0.001 '**' 0.01 '*' 0.05 '.' 0.1 ' ' 1
#> 
#> Transformed coefficients:
#>                    estimate standard.error     lower.CI upper.CI    p.value    
#> (Intercept)     6.998161137    0.003503253  6.991294886  7.00503 < 2.22e-16 ***
#> [group=1]      -2.602134468    0.019693184 -2.640732400 -2.56354 < 2.22e-16 ***
#> [group=0]:amp1  0.097755503    0.003779042  0.090348716  0.10516 < 2.22e-16 ***
#> [group=1]:amp1  2.022113504    0.030229827  1.962864132  2.08136 < 2.22e-16 ***
#> [group=0]:amp2  0.397733040    0.006967418  0.384077152  0.41139 < 2.22e-16 ***
#> [group=1]:amp2  1.018354873    0.017872328  0.983325753  1.05338 < 2.22e-16 ***
#> [group=0]:amp3  0.498426170    0.005572804  0.487503675  0.50935 < 2.22e-16 ***
#> [group=1]:amp3  0.399504879    0.011573116  0.376821990  0.42219 < 2.22e-16 ***
#> [group=0]:acr1  1.010217189    0.038377501  0.934998669  1.08544 < 2.22e-16 ***
#> [group=1]:acr1  0.991081058    0.014003507  0.963634689  1.01853 < 2.22e-16 ***
#> [group=0]:acr2  1.512872702    0.013817098  1.485791688  1.53995 < 2.22e-16 ***
#> [group=1]:acr2 -1.520199609    0.015485420 -1.550550475 -1.48985 < 2.22e-16 ***
#> [group=0]:acr3  0.096116553    0.012191338  0.072221971  0.12001 3.1705e-15 ***
#> [group=1]:acr3 -1.014539038    0.026883502 -1.067229733 -0.96185 < 2.22e-16 ***
#> ---
#> Signif. codes:  0 '***' 0.001 '**' 0.01 '*' 0.05 '.' 0.1 ' ' 1
autoplot(object,
  superimpose.data = TRUE,
  x_str = "group",
  predict.ribbon = FALSE
)

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)
#> 
#>  Conditional Model 
#> Raw model coefficients:
#>                     estimate standard.error    lower.CI upper.CI    p.value    
#> (Intercept)       0.97559852     0.02876704  0.91921615  1.03198 < 2.22e-16 ***
#> group1            0.97474309     0.04068227  0.89500730  1.05448 < 2.22e-16 ***
#> group0:main_rrr1 -0.53942373     0.04085878 -0.61950548 -0.45934 < 2.22e-16 ***
#> group1:main_rrr1  0.03777641     0.04070734 -0.04200852  0.11756    0.35341    
#> group0:main_sss1  0.01593054     0.04074583 -0.06392983  0.09579    0.69582    
#> group1:main_sss1  0.19955800     0.04072400  0.11974042  0.27938 9.5712e-07 ***
#> group0:main_rrr2 -0.02400799     0.04092524 -0.10421999  0.05620    0.55745    
#> group1:main_rrr2 -0.20151834     0.04004145 -0.27999814 -0.12304 4.8351e-07 ***
#> group0:main_sss2  0.51936763     0.04070750  0.43958240  0.59915 < 2.22e-16 ***
#> group1:main_sss2  0.04478294     0.04139754 -0.03635475  0.12592    0.27935    
#> group0:main_rrr3 -0.46466252     0.04074769 -0.54452653 -0.38480 < 2.22e-16 ***
#> group1:main_rrr3  0.03116401     0.04099674 -0.04918813  0.11152    0.44716    
#> group0:main_sss3  0.04388676     0.04102651 -0.03652372  0.12430    0.28475    
#> group1:main_sss3  0.22562060     0.04042518  0.14638870  0.30485 2.3888e-08 ***
#> ---
#> Signif. codes:  0 '***' 0.001 '**' 0.01 '*' 0.05 '.' 0.1 ' ' 1
#> 
#> Transformed coefficients:
#>                  estimate standard.error   lower.CI upper.CI    p.value    
#> (Intercept)    0.97559852     0.02876704 0.91921615  1.03198 < 2.22e-16 ***
#> [group=1]      0.97474309     0.04068227 0.89500730  1.05448 < 2.22e-16 ***
#> [group=0]:amp1 0.53965891     0.04089387 0.45950840  0.61981 < 2.22e-16 ***
#> [group=1]:amp1 0.20310207     0.05731338 0.09076992  0.31543 0.00039454 ***
#> [group=0]:amp2 0.51992222     0.05782894 0.40657958  0.63326 < 2.22e-16 ***
#> [group=1]:amp2 0.20643438     0.05732742 0.09407471  0.31879 0.00031703 ***
#> [group=0]:amp3 0.46673044     0.06019577 0.34874890  0.58471 8.9364e-15 ***
#> [group=1]:amp3 0.22776271     0.05713526 0.11577966  0.33975 6.7090e-05 ***
#> [group=0]:acr1 3.11206871     0.07556850 2.96395718  3.26018 < 2.22e-16 ***
#> [group=1]:acr1 1.38370970     0.28250411 0.83001182  1.93741 9.6809e-07 ***
#> [group=0]:acr2 1.61698886     0.11070308 1.40001481  1.83396 < 2.22e-16 ***
#> [group=1]:acr2 2.92291859     0.28155027 2.37109021  3.47475 < 2.22e-16 ***
#> [group=0]:acr3 3.04742333     0.12545683 2.80153247  3.29331 < 2.22e-16 ***
#> [group=1]:acr3 1.43353910     0.25290791 0.93784870  1.92923 1.4428e-08 ***
#> ---
#> Signif. codes:  0 '***' 0.001 '**' 0.01 '*' 0.05 '.' 0.1 ' ' 1
autoplot(object,
  superimpose.data = TRUE,
  x_str = "group",
  predict.ribbon = FALSE
)