-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy path07-temporal.Rmd
154 lines (120 loc) · 3.75 KB
/
07-temporal.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
# Temporal and Spatial Effects
```{r, echo=FALSE, message=FALSE,warning=FALSE}
rm(list=ls())
library(dplyr)
library(flair)
library(lme4)
library(MCMCglmm)
library(scales)
library(squidSim)
library(knitr)
set.seed(25)
```
## Simple Temporal Effects
We might have measured a variable over the course of a certain time period (e.g. 20 years). We might expect that there is stochastic year-to-year variation, which we can simulate already. However we might also want to simulate patterns in that temporal data. We can treat the levels associated with a particular grouping factor (e.g. year) as both a factor and continuous.
To treat a grouping factor as continuous, we use `covariate=TRUE` in the parameter list. In this way we can simulate a linear effect of year:
```{r}
squid_data <- simulate_population(
data_structure= make_structure(structure = "year(20) + sex(2)/individual(50)",repeat_obs=20),
parameters=list(
year_cont = list(
group="year",
names= "year_cont",
covariate=TRUE,
beta=0.3
),
year = list(
vcov = 0.8
),
residual=list(
vcov = 1
)
)
)
```
note we have specified `group` in the parameter list. This enables us to link a set of parameters to the grouping factor in the data structure. This doesn't have to be specified and defaults to the name of the list item.
```{r,fig.width=6,fig.height=6}
data <- get_population_data(squid_data)
head(data)
plot(y ~ year_cont, data)
```
Here we can see there is within year variation, year to year variation, as well as a linear directional year effect.
```{r}
lmer(y ~ year_cont + (1|year), data)
```
In a similar way we can also simulate a quadratic effect of time.
```{r,fig.width=6,fig.height=6}
squid_data <- simulate_population(
data_structure = make_structure(structure = "year(20) + sex(2)/individual(50)",repeat_obs=20),
parameters=list(
year_cont = list(
group="year",
names= c("year_cont"),
covariate=TRUE,
beta=c(0.3)
),
interactions=list(
names= c("year_cont:year_cont"),
beta=c(-0.05)
),
year = list(
vcov = 1
),
residual=list(
vcov = 0.8
)
)
)
data <- get_population_data(squid_data)
plot(y~year_cont,data)
```
<br>
## Cyclical Temporal Effects
The `squidR` function in the {squid} R package uses the sinusoidal equation to implement cyclical temporal effects:
<div class="alert alert-info">
$$
y = A sin(B(x - C)) + D
$$
</div>
where A is the amplitude, $B/2\pi$ is the period $C/B$ is the horizontal shift and D is the vertical shift. We can visualise this
```{r}
time <- 1:20
amplitude <- 10 # |A| = the amplitude
period <- 10
h_shift <- 3
v_shift <- 5
B <- (2*pi) / abs(period) # 2pi/|B| = the period
cyclic_effect <- amplitude*sin(B*time - B^2*h_shift ) + v_shift
plot(cyclic_effect~time)
```
We can simulate this using the model part of the `simulate_population()`, adding the extra parameters for the cyclical effects into the year_cont part of the list.
```{r}
squid_data <- simulate_population(
data_structure= make_structure(structure = "year(20) + sex(2)/individual(50)",repeat_obs=1),
parameters=list(
year_cont = list(
group="year",
names= "linear_effect",
covariate=TRUE,
beta=0.3,
amplitude = 2, # |A| = the amplitude
period = 10,
h_shift = 3,
v_shift = 5
),
year = list(
vcov = 1.2
),
residual=list(
vcov = 1
)
),
model=" B =(2*pi) / abs(period);
cyclic_effect = amplitude*sin(B*I(linear_effect) - B^2*h_shift ) + v_shift;
y = linear_effect + cyclic_effect + year_effect + residual"
)
data <- get_population_data(squid_data)
plot(y~year,data)
```
## Temporal Autocorrelation {#temporalauto}
## Spatial Autocorrelation {#spatialauto}