-
Notifications
You must be signed in to change notification settings - Fork 0
/
EvergladesForecasting.qmd
176 lines (143 loc) · 5.35 KB
/
EvergladesForecasting.qmd
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
---
title: "Everglades Forcasting"
---
```{r}
library(dplyr)
library(fable)
library(feasts)
library(ggh4x)
library(ggplot2)
library(mvgam)
library(tidyr)
library(tsibble)
library(urca)
library(wader)
```
```{r}
download_observations(".")
```
## Data cleaning/manipulation questions
* How do we want to filter and complete time-series?
* Currently checking to see if at least 10 years of observations and then filling all missing data between min and max observed year with zeros
* Probably want some logic for this in wader eventually
* How to handle crossing scales
* Wader currently takes colony counts for colony level
* For sub region level it aggregates colony counts if no region count exists, but uses region counts if available
* region and use region counts
* region only returns wcas & enp
## Colony time-series
```{r}
max_counts <- tibble(max_counts())
max_counts <- max_counts |>
filter(species %in% c("gbhe", "greg", "rosp", "sneg", "wost", "whib")) |>
group_by(colony) |>
filter(n_distinct(year) > 10) |>
complete(year = full_seq(year, 1), species, fill = list(count = 0)) |>
ungroup()
ggplot(max_counts, aes(x = year, y = count, color = species)) +
geom_point() +
geom_line() +
facet_wrap(~colony, scales = "free")
#ggsave("results/colony-timeseries.png", height = 26, width = 26)
```
## Regions
```{r}
region_counts <- tibble(max_counts(level = "subregion"))
region_counts <- region_counts |>
filter(species %in% c("gbhe", "greg", "rosp", "sneg", "wost", "whib")) |>
group_by(region) |>
filter(n_distinct(year) > 10) |>
complete(year = full_seq(year, 1), species, fill = list(count = 0)) |>
ungroup()
ggplot(region_counts, aes(x = year, y = count, color = species)) +
geom_point() +
geom_line() +
facet_grid2(vars(species), vars(region), scales = "free", independent = "y")
```
```{r}
ggsave("results/region-timeseries.png", height = 26, width = 26)
```
### Load covariates
```{r}
water <- load_datafile("Water/eden_covariates.csv")
region_water <- filter(water, region %in% c(unique(region_counts$region))) |>
filter(year < 2024) # 2024 data for birds not yet available
count_env_data <- region_counts |>
filter(year >= 1991) |> # No water data prior to 1991
full_join(region_water, by = c("year", "region")) |>
as_tsibble(key = c(species, region), index = year)
```
### Models
#### Separate models by region
```{r}
#region_counts <- as_tsibble(region_counts, key = c(species, region), index = year)
models <- model(count_env_data,
arima = ARIMA(count),
arima_exog = ARIMA(count ~ breed_season_depth + dry_days + pre_recession + post_recession),
tslm = TSLM(count ~ breed_season_depth + dry_days + pre_recession + post_recession + trend()))
glance(models)
models_aug <- augment(models)
autoplot(models_aug, count) +
autolayer(models_aug, .fitted, linetype = 2) +
facet_grid(vars(species), vars(region), scales = "free")
ggplot(mapping = aes(x = year, y = count)) +
geom_line(data = count_env_data) +
geom_line(data = models_aug, mapping = aes(y = .fitted, color = `.model`)) +
facet_grid2(vars(species), vars(region), scales = "free_y", independent = "y")
ggsave("results/region-models.png", height = 12, width = 12)
```
#### Single model with regions as replicates
## Everglades
```{r}
everglades_counts <- tibble(max_counts(level = "all"))
everglades_counts <- everglades_counts |>
filter(species %in% c("gbhe", "greg", "rosp", "sneg", "wost", "whib")) |>
complete(year = full_seq(year, 1), species, fill = list(count = 0)) |>
ungroup()
ggplot(everglades_counts, aes(x = year, y = count, color = species)) +
geom_point() +
geom_line()
#ggsave("results/everglades-timeseries.png", height = 26, width = 26)
```
## Everglades wide forecasts
### ARIMA
```{r}
everglades_counts <- as_tsibble(everglades_counts, key = species, index = year)
models <- model(everglades_counts, ARIMA(count))
glance(models)
models_aug <- augment(models)
autoplot(models_aug, count) +
autolayer(models_aug, .fitted, color = 'black', linetype = 2) +
facet_wrap(~species, scales = "free")
```
### With covariates
#### Load covariates
```{r}
water <- load_datafile("Water/eden_covariates.csv")
everglades_water <- filter(water, region == "all") |>
filter(year < 2024) # 2024 data for birds not yet available
count_env_data <- everglades_counts |>
filter(year >= 1991) |> # No water data prior to 1991
full_join(everglades_water, by = "year") |>
as_tsibble(key = species, index = year)
```
#### TSLM
```{r}
tslm_model <- model(count_env_data, TSLM(count ~ breed_season_depth + breed_season_depth^2 + pre_recession + post_recession + recession + trend()))
glance(tslm_model)
tslm_model_aug = augment(tslm_model)
autoplot(tslm_model_aug, count) +
autolayer(tslm_model_aug, .fitted, color = 'black', linetype = 2) +
facet_wrap(~species, scales = "free") +
theme(legend.position="none")
```
#### Exog ARIMA
```{r}
arima_exog_model <- model(count_env_data, ARIMA(count ~ breed_season_depth + breed_season_depth^2 + pre_recession + post_recession + recession))
glance(arima_exog_model)
arima_exog_model_aug = augment(arima_exog_model)
autoplot(arima_exog_model_aug, count) +
autolayer(arima_exog_model_aug, .fitted, color = 'black', linetype = 2) +
facet_wrap(~species, scales = "free") +
theme(legend.position="none")
```