-
Notifications
You must be signed in to change notification settings - Fork 0
/
data-simulations.Rmd
247 lines (197 loc) · 6.86 KB
/
data-simulations.Rmd
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
---
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
)
```