data-simulations

Oliver Jayasinghe and Rex Parsons

library(GLMMcosinor)

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:

Note that the parameter controls extra arguments such as standard deviation, and the shape parameter for a Gamma distribution:

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:

Similarly, the standard deviation (sd) for the Gaussian distribution or the alpha parameter for the Gamma distribution are specified as:

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)       1.0865944      0.0950086  0.9003810  1.27281 < 2.22e-16 ***
#> group1           -0.7946815      0.1343624 -1.0580271 -0.53134 3.3296e-09 ***
#> group0:main_rrr1  0.7522135      0.1368724  0.4839485  1.02048 3.8910e-08 ***
#> group1:main_rrr1  0.5610164      0.1368724  0.2927514  0.82928 4.1525e-05 ***
#> group0:main_sss1  1.9251734      0.1319165  1.6666218  2.18373 < 2.22e-16 ***
#> group1:main_sss1  0.0746534      0.1319165 -0.1838983  0.33321    0.57145    
#> ---
#> Signif. codes:  0 '***' 0.001 '**' 0.01 '*' 0.05 '.' 0.1 ' ' 1
#> 
#> Transformed coefficients:
#>                   estimate standard.error    lower.CI upper.CI    p.value    
#> (Intercept)     1.08659445     0.09500860  0.90038102  1.27281 < 2.22e-16 ***
#> [group=1]      -0.79468153     0.13436245 -1.05802709 -0.53134 3.3296e-09 ***
#> [group=0]:amp1  2.06691022     0.13145486  1.80926342  2.32456 < 2.22e-16 ***
#> [group=1]:amp1  0.56596156     0.13636687  0.29868739  0.83324 3.3206e-05 ***
#> [group=0]:acr1  1.19831105     0.06643533  1.06810019  1.32852 < 2.22e-16 ***
#> [group=1]:acr1  0.13229096     0.23400716 -0.32635465  0.59094    0.57185    
#> ---
#> 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:

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.999943543    0.003300256  6.993475160  7.00641 < 2.22e-16
#> group1           -2.622160500    0.019201770 -2.659795278 -2.58453 < 2.22e-16
#> group0:main_rrr1  0.065595557    0.004553901  0.056670075  0.07452 < 2.22e-16
#> group1:main_rrr1  1.098445169    0.022502801  1.054340488  1.14255 < 2.22e-16
#> group0:main_sss1  0.080898789    0.004101585  0.072859830  0.08894 < 2.22e-16
#> group1:main_sss1  1.698295390    0.024894825  1.649502431  1.74709 < 2.22e-16
#> group0:main_rrr2  0.026415376    0.004185885  0.018211193  0.03462 2.7798e-10
#> group1:main_rrr2  0.078394891    0.014019529  0.050917120  0.10587 2.2468e-08
#> group0:main_sss2  0.403254450    0.004370425  0.394688574  0.41182 < 2.22e-16
#> group1:main_sss2 -1.009604580    0.018327031 -1.045524901 -0.97368 < 2.22e-16
#> group0:main_rrr3  0.497014113    0.004101392  0.488975531  0.50505 < 2.22e-16
#> group1:main_rrr3  0.211901463    0.010292832  0.191727883  0.23208 < 2.22e-16
#> group0:main_sss3  0.048874221    0.004163714  0.040713493  0.05703 < 2.22e-16
#> group1:main_sss3 -0.343877716    0.011393586 -0.366208734 -0.32155 < 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.999943543    0.003300256  6.993475160  7.00641 < 2.22e-16 ***
#> [group=1]      -2.622160500    0.019201770 -2.659795278 -2.58453 < 2.22e-16 ***
#> [group=0]:amp1  0.104150810    0.003649067  0.096998769  0.11130 < 2.22e-16 ***
#> [group=1]:amp1  2.022569905    0.030061050  1.963651330  2.08149 < 2.22e-16 ***
#> [group=0]:amp2  0.404118700    0.004374963  0.395543930  0.41269 < 2.22e-16 ***
#> [group=1]:amp2  1.012643652    0.018396602  0.976586974  1.04870 < 2.22e-16 ***
#> [group=0]:amp3  0.499411372    0.004102339  0.491370934  0.50745 < 2.22e-16 ***
#> [group=1]:amp3  0.403923401    0.010565090  0.383216205  0.42463 < 2.22e-16 ***
#> [group=0]:acr1  0.889483603    0.047277154  0.796822084  0.98215 < 2.22e-16 ***
#> [group=1]:acr1  0.996679059    0.007374355  0.982225590  1.01113 < 2.22e-16 ***
#> [group=0]:acr2  1.505384302    0.010346321  1.485105886  1.52566 < 2.22e-16 ***
#> [group=1]:acr2 -1.493302719    0.013754208 -1.520260471 -1.46634 < 2.22e-16 ***
#> [group=0]:acr3  0.098020542    0.008335374  0.081683509  0.11436 < 2.22e-16 ***
#> [group=1]:acr3 -1.018541682    0.027583428 -1.072604206 -0.96448 < 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)       1.0102091281   0.0288447469  0.9536744630  1.06674 < 2.22e-16
#> group1            0.9878703264   0.0407890439  0.9079252695  1.06782 < 2.22e-16
#> group0:main_rrr1 -0.5296653789   0.0411593553 -0.6103362328 -0.44899 < 2.22e-16
#> group1:main_rrr1  0.0004277321   0.0411595151 -0.0802434350  0.08110 0.99170849
#> group0:main_sss1  0.0032780893   0.0406838495 -0.0764607904  0.08302 0.93578018
#> group1:main_sss1  0.1442815786   0.0406916588  0.0645273929  0.22404 0.00039153
#> group0:main_rrr2 -0.0591539421   0.0420614459 -0.1415928611  0.02328 0.15961438
#> group1:main_rrr2 -0.1454165415   0.0412950000 -0.2263532543 -0.06448 0.00042926
#> group0:main_sss2  0.4790938816   0.0398600314  0.4009696557  0.55722 < 2.22e-16
#> group1:main_sss2 -0.0190177273   0.0405345883 -0.0984640604  0.06043 0.63894611
#> group0:main_rrr3 -0.5889549583   0.0416266354 -0.6705416644 -0.50737 < 2.22e-16
#> group1:main_rrr3 -0.0380672440   0.0416352765 -0.1196708864  0.04354 0.36055783
#> group0:main_sss3 -0.0299417816   0.0404293580 -0.1091818672  0.04930 0.45893902
#> group1:main_sss3  0.1899860505   0.0403948567  0.1108135863  0.26916 2.5609e-06
#>                     
#> (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)     1.01020913     0.02884475  0.95367446  1.06674 < 2.22e-16 ***
#> [group=1]       0.98787033     0.04078904  0.90792527  1.06782 < 2.22e-16 ***
#> [group=0]:amp1  0.52967552     0.04115404  0.44901508  0.61034 < 2.22e-16 ***
#> [group=1]:amp1  0.14428221     0.04069425  0.06452294  0.22404 0.00039184 ***
#> [group=0]:amp2  0.48273195     0.03978704  0.40475078  0.56071 < 2.22e-16 ***
#> [group=1]:amp2  0.14665485     0.04128183  0.06574394  0.22757 0.00038155 ***
#> [group=0]:amp3  0.58971557     0.04150517  0.50836694  0.67106 < 2.22e-16 ***
#> [group=1]:amp3  0.19376226     0.04098973  0.11342386  0.27410 2.2776e-06 ***
#> [group=0]:acr1  3.13540375     0.07681915  2.98484098  3.28597 < 2.22e-16 ***
#> [group=1]:acr1  1.56783177     0.28525311  1.00874594  2.12692 3.8788e-08 ***
#> [group=0]:acr2  1.69364503     0.08727513  1.52258891  1.86470 < 2.22e-16 ***
#> [group=1]:acr2 -3.01154966     0.27648591 -3.55345208 -2.46965 < 2.22e-16 ***
#> [group=0]:acr3 -3.09079755     0.06876883 -3.22558199 -2.95601 < 2.22e-16 ***
#> [group=1]:acr3  1.76854631     0.21185629  1.35331560  2.18378 < 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
)