-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathSpatial_Sim_Tables_Figures.Rmd
executable file
·195 lines (146 loc) · 13.8 KB
/
Spatial_Sim_Tables_Figures.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
---
title: "Summary of Spatial Validation Simulations"
author: "Daniel J Hocking"
date: '`r format(Sys.Date(), "%B %d, %Y")`'
output: html_document
---
```{r, echo=FALSE, results='hide', warning=FALSE, message=FALSE}
library(ggplot2)
library(dplyr)
df_sims <- read.csv(file = "Output/Sim_Spatial/Sim_Results.csv", header = TRUE, stringsAsFactors = FALSE)
load(file = "Output/Sim_Spatial/Sim_Spatial_Conditions.RData")
```
```{r, echo=FALSE, results='hide', warning=FALSE, message=FALSE}
theme_bw_journal <- function (base_family = "") {
theme_grey(base_family = base_family) %+replace%
theme(
axis.text = element_text(size = rel(0.8)), axis.ticks = element_line(colour = "black"),
legend.key = element_rect(colour = "grey80"), panel.background = element_rect(fill = "white",
colour = NA), panel.border = element_rect(fill = NA,
colour = "grey50"), panel.grid.major = element_line(colour = "grey90",
size = 0.2), panel.grid.minor = element_line(colour = "grey98",
size = 0.5), strip.background = element_rect(
fill = "grey80",
colour = "grey50", size = 0.2
)
)
}
theme_set(theme_bw_journal())
```
```{r, echo=FALSE, results='hide', warning=FALSE, message=FALSE}
df_sim_summary <- df_sims %>%
dplyr::group_by(spatial, sp_mod, theta, SD_ou) %>%
dplyr::select(-sim) %>%
dplyr::filter(converge == TRUE) %>%
dplyr::summarise_each(funs(mean(., na.rm = T)))
as.data.frame(df_sim_summary %>% dplyr::filter(sp_mod == 1))
as.data.frame(df_sim_summary %>% dplyr::filter(sp_mod == 0))
# ggplot(df_sim_summary, aes(SD_ou, max_N, colour = factor(theta), group = factor(theta))) + geom_smooth() #stat_smooth(aes(fill = factor(theta)))
df_converged <- df_sims %>%
dplyr::group_by(spatial, sp_mod, theta, SD_ou) %>%
dplyr::filter(converge == TRUE) %>%
dplyr::filter(theta_hat < 15,
mean_N_hat < 1000,
rmse < 100,
max_N < 1000) # no way converged
df_spatial <- df_converged %>%
dplyr::filter(sp_mod == TRUE)
theta_string <- noquote(paste(theta_vec, sep = ", ", collapse = ", "))
sd_string <- noquote(paste(SD_vec, sep = ", ", collapse = ", "))
```
I ran `r max(df_sims$sim)` simulations of the spatial model for the White River in Vermont with `r nrow(family)` nodes (~330 stream reaches) for 1 year (no temporal or spatiotemporal components). For each iteration, I varied the two parameters that defind the Ornstein-Uhlenbeck process and therefore the latent spatial correlation structure in the watershed. We used all combinations of $/theta$ (`r theta_string`) and $/sigma_{ou}$ (`r sd_string`). For each combination in each iteration, I fit the model including with and without spatial correlation.
```{r, echo=FALSE, warning=FALSE, message=FALSE, fig.height=10, fig.width=10}
# consider separating this out and just summarizing for the maximum number of sites or n_sites = 100 to more clearly isolate the year effects
# Make the plots
y_N <- ggplot(df_converged, aes(theta, mean_N_hat, group = interaction(theta, sp_mod))) + geom_boxplot(aes(fill = factor(sp_mod)), outlier.colour = "NA", width = 0.33) + scale_fill_manual(values=c("#F8766D", "#619CFF"), name = "Spatial", labels = c("FALSE", "TRUE")) + theme(legend.position = "right") + ylab("Mean density") + stat_summary(fun.y=mean, colour="darkred", geom="point", shape=18, size=3, aes(theta, mean_N, group = interaction(theta))) + geom_text(x = 5, y = 65, label = "b") + coord_cartesian(ylim = c(0, 85)) # + ylim(c(0,500)) #+ geom_hline(yintercept = mean(df_converged$mean_N, na.rm = T), linetype = 2, size = 1, colour = "red")
y_N_se <- ggplot(df_converged, aes(theta, N_se, group = interaction(theta, sp_mod))) + geom_boxplot(aes(fill = factor(sp_mod)), outlier.colour = "NA", width = 0.33) + scale_fill_manual(values=c("#F8766D", "#619CFF")) + theme(legend.position = "none") + ylab("SE of mean density") + geom_text(x = 5, y = 1.3, label = "c") + coord_cartesian(ylim = c(0, 3))
y_gamma <- ggplot(df_converged, aes(theta, coef_hat, group = interaction(theta, sp_mod))) + geom_boxplot(aes(fill = factor(sp_mod)), outlier.colour = "NA", width = 0.33) + scale_fill_manual(values=c("#F8766D", "#619CFF")) + geom_hline(yintercept = df_converged$coef_true, linetype = 2, size = 0.6, colour = "darkred") + theme(legend.position = "none") + ylab(expression(paste("Fixed effect (", gamma, ")"))) + geom_text(x = 5, y = 0.72, label = "e") + coord_cartesian(ylim = c(0.38, 0.62))
y_RMSE <- ggplot(df_converged, aes(theta, rmse, group = interaction(theta, sp_mod))) + geom_boxplot(aes(fill = factor(sp_mod)), outlier.colour = "NA", width = 0.33) + scale_fill_manual(values=c("#F8766D", "#619CFF")) + theme(legend.position = "none") + ylab("RMSE") + coord_cartesian(ylim = c(0, 80)) # + geom_text(x = 0, y = 55, label = "d")
y_theta <- ggplot(df_spatial, aes(theta, theta_hat, group = theta)) + geom_boxplot(aes(fill = "#619CFF"), outlier.colour = "NA", width = 0.33, width = 0.33) + scale_fill_manual(values=c("#619CFF")) + theme(legend.position="none") + stat_summary(fun.y=mean, colour="darkred", geom="point", shape=18, size=3, aes(theta, theta, group = theta)) + ylab(expression(paste("Spatial decorrelation (", theta[epsilon], ")"))) + xlab(expression(theta)) + geom_text(x = 5, y = 10.5, label = "a") + coord_cartesian(ylim = c(0, 7)) #+ geom_text(data = means, aes(label = weight, y = weight + 0.08))
# geom_hline(yintercept = df_spatial$theta, linetype = 2, size = 1, colour = "red") + xlab("")
y_theta_sd <- ggplot(df_spatial, aes(theta, theta_hat*(SD_ou_hat), group = theta)) + geom_boxplot(aes(fill = "#619CFF"), outlier.colour = "NA", width = 0.33, width = 0.33) + scale_fill_manual(values=c("#619CFF")) + theme(legend.position="none") + stat_summary(fun.y=mean, colour="darkred", geom="point", shape=18, size=3, aes(theta, theta*SD_ou, group = theta)) + ylab(expression(paste("Spatial component (", theta[epsilon]*sigma[epsilon], ")"))) + xlab(expression(theta[epsilon])) + geom_text(x = 5, y = 10.5, label = "a") + coord_cartesian(ylim = c(0, 30))
# df_spatial$ratio <- df_spatial$theta/df_spatial$SD_ou
# y_ratio <- ggplot(df_spatial, aes(ratio, theta_hat/(SD_ou_hat), group = ratio)) + geom_boxplot(aes(fill = "#619CFF"), outlier.colour = "NA", width = 0.33, width = 0.33) + scale_fill_manual(values=c("#619CFF")) + theme(legend.position="none") + stat_summary(fun.y=mean, colour="darkred", geom="point", shape=18, size=3, aes(ratio, ratio, group = ratio)) + ylab(expression(paste("Spatial ratio (", theta[epsilon]/sigma[epsilon], ")"))) + xlab(expression(theta)) + geom_text(x = 5, y = 10.5, label = "a") + coord_cartesian(ylim = c(0, 100)) + geom_abline(aes(intercept = 0, slope = 1))
#
# y_ratio <- ggplot(df_spatial, aes(ratio, theta_hat/(SD_ou_hat), group = ratio)) + geom_point() + theme(legend.position="none") + stat_summary(fun.y=mean, colour="darkred", geom="point", shape=18, size=3, aes(theta, ratio, group = ratio)) + ylab(expression(paste("Spatial ratio (", theta[epsilon]/sigma[epsilon], ")"))) + xlab(expression(theta)) + geom_text(x = 5, y = 10.5, label = "a") + coord_cartesian(ylim = c(0, 30))
# Save the legend
get_legend<-function(myggplot){
tmp <- ggplot_gtable(ggplot_build(myggplot))
leg <- which(sapply(tmp$grobs, function(x) x$name) == "guide-box")
legend <- tmp$grobs[[leg]]
return(legend)
}
legend <- get_legend(y_N)
# Remove the legend from the box plot
y_N <- y_N + theme(legend.position="none")
# Create a blank plot
# library(cowplot)
# blank_plot <- ggplot()+geom_blank(aes(1,1)) +
# cowplot::theme_nothing()
# Create grid of plots
library(gridExtra)
# grid.arrange(y_theta + xlab(""), legend,
# y_N + xlab(""), y_N_se + xlab(""),
# y_RMSE + xlab(expression(paste("True value of ", theta))), y_gamma + xlab(expression(paste("True value of ", theta))),
# ncol = 2,
# nrow = 3,
# widths = c(2.7, 2.7),
# heights = c(rep(3, times=3)))
y_grid <- arrangeGrob(y_theta_sd + xlab(""), legend,
y_N + xlab(""), y_N_se + xlab(""),
y_RMSE + xlab(expression(paste("True spatial decorrelation (", theta[epsilon], ")"))), y_gamma + xlab(expression(paste("True spatial decorrelation (", theta[epsilon], ")"))),
ncol = 2,
nrow = 3,
widths = c(2.7, 2.7),
heights = c(rep(3, times=3)))
grid::grid.draw(y_grid)
ggsave(file = "Output/Sim_Spatial/theta_grid_plot.png", y_grid, dpi = 300)
ggsave(file = "Output/Sim_Spatial/theta_grid_plot.pdf", y_grid, dpi = 300)
#grid.arrange(blank_plot, legend, y_N, y_N_se, y_RMSE, y_theta, y_rhot, y_sigmat, y_thet_st, y_rho_st, ncol = 2, nrow = 5, widths = c(2.7, 2.7), heights = c(0.2, rep(2.5, times=4))) # y_gamma,
```
Varying sigma
```{r, echo=FALSE, results='hide', warning=FALSE, message=FALSE, fig.height=10, fig.width=10}
# consider separating this out and just summarizing for the maximum number of years or n_years = 20 to more clearly isolate the site effects
# Make the plots
s_N <- ggplot(df_converged, aes(SD_ou, mean_N_hat, group = interaction(SD_ou, sp_mod))) + geom_boxplot(aes(fill = factor(sp_mod)), outlier.colour = "NA", width = 0.13) + scale_fill_manual(values=c("#F8766D", "#619CFF"), name = "Spatial", labels = c("FALSE", "TRUE")) + theme(legend.position = "right") + ylab("Mean density") + stat_summary(fun.y=mean, colour="darkred", geom="point", shape=18, size=3, aes(SD_ou, mean_N, group = interaction(SD_ou))) + coord_cartesian(ylim = c(0, 70)) # + geom_text(x = .2, y = 40, label = "b", size = 10) # + geom_hline(yintercept = mean(df_converged$mean_N, na.rm = T), linetype = 2, size = 1, colour = "red")
s_N_se <- ggplot(df_converged, aes(SD_ou, N_se, group = interaction(SD_ou, sp_mod))) + geom_boxplot(aes(fill = factor(sp_mod)), outlier.colour = "NA", width = 0.13) + scale_fill_manual(values=c("#F8766D", "#619CFF")) + theme(legend.position = "none") + ylab("SE of mean density") + coord_cartesian(ylim = c(0, 2.6)) # + geom_text(x = .2, y = 0.35, label = "c", size = 10)
s_gamma <- ggplot(df_converged, aes(SD_ou, coef_hat, group = interaction(SD_ou, sp_mod))) + geom_boxplot(aes(fill = factor(sp_mod)), outlier.colour = "NA", width = 0.13) + scale_fill_manual(values=c("#F8766D", "#619CFF")) + geom_hline(yintercept = df_converged$coef_true, linetype = 2, size = 0.6, colour = "darkred") + theme(legend.position = "none") + ylab(expression(paste("Fixed effect (", gamma, ")"))) + coord_cartesian(ylim = c(0.3, 0.7)) # + geom_text(x = .2, y = 0.61, label = "e", size = 10)
s_RMSE <- ggplot(df_converged, aes(SD_ou, rmse, group = interaction(SD_ou, sp_mod))) + geom_boxplot(aes(fill = factor(sp_mod)), outlier.colour = "NA", width = 0.13) + scale_fill_manual(values=c("#F8766D", "#619CFF")) + theme(legend.position = "none") + ylab("RMSE") + coord_cartesian(ylim = c(0, 65)) # + geom_text(x = .2, y = 28, label = "d", size = 10)
s_theta <- ggplot(df_spatial, aes(SD_ou, theta_hat, group = SD_ou)) + geom_boxplot(aes(fill = "#619CFF"), outlier.colour = "NA", width = 0.13) + scale_fill_manual(values=c("#619CFF")) + theme(legend.position="none") + stat_summary(fun.y=mean, colour="darkred", geom="point", shape=18, size=3, aes(SD_ou, SD_ou, group = SD_ou)) + ylab(expression(paste("Spatial decorrelation (", theta[epsilon], ")"))) + coord_cartesian(ylim = c(0, 6)) #+ geom_text(x = .2, y = 70, label = "b", size = 10)
s_sd <- ggplot(df_spatial, aes(SD_ou, SD_ou_hat, group = SD_ou)) + geom_boxplot(aes(fill = "#619CFF"), outlier.colour = "NA", width = 0.13) + scale_fill_manual(values=c("#619CFF")) + theme(legend.position="none") + xlab("") + stat_summary(fun.y=mean, colour="darkred", geom="point", shape=18, size=3, aes(SD_ou, SD_ou, group = SD_ou)) + ylab(expression(paste("Spatial variation (", sigma[epsilon], ")"))) + coord_cartesian(ylim = c(0, 1.5)) # + geom_text(x = .2, y = 0.8, label = "a", size = 10) # + geom_hline(yintercept = df_spatial$theta, linetype = 2, size = 1, colour = "red")
s_theta_sd <- ggplot(df_spatial, aes(SD_ou, theta_hat/(SD_ou_hat), group = SD_ou)) + geom_boxplot(aes(fill = "#619CFF"), outlier.colour = "NA", width = 0.13, width = 0.13) + scale_fill_manual(values=c("#619CFF")) + theme(legend.position="none") + stat_summary(fun.y=mean, colour="darkred", geom="point", shape=18, size=3, aes(SD_ou, theta/SD_ou, group = SD_ou)) + ylab(expression(paste("Spatial ratio (", theta[epsilon]/sigma[epsilon], ")"))) + xlab(expression(sigma[epsilon])) + geom_text(x = 5, y = 10.5, label = "a") + coord_cartesian(ylim = c(0, 175))
# Save the legend
get_legend<-function(myggplot){
tmp <- ggplot_gtable(ggplot_build(myggplot))
leg <- which(sapply(tmp$grobs, function(x) x$name) == "guide-box")
legend <- tmp$grobs[[leg]]
return(legend)
}
legend <- get_legend(s_N)
# Remove the legend from the box plot
s_N <- s_N + theme(legend.position="none")
# Create a blank plot
# library(cowplot)
# blank_plot <- ggplot()+geom_blank(aes(1,1)) +
# cowplot::theme_nothing()
# Create grid of plots
library(gridExtra)
# grid.arrange(s_sd + xlab(""), legend,
# s_N + xlab(""), s_N_se + xlab(""),
# s_RMSE + xlab(expression(paste("True value of ", sigma))), s_gamma + xlab(expression(paste("True value of ", sigma))),
# ncol = 2,
# nrow = 3,
# widths = c(2.7, 2.7),
# heights = c(rep(3, times=3)))
s_grid <- arrangeGrob(s_theta_sd + xlab(""), legend,
s_N + xlab(""), s_N_se + xlab(""),
s_RMSE + xlab(expression(paste("True asymptotic spatial variation (", sigma[epsilon], ")"))), s_gamma + xlab(expression(paste("True asymptotic spatial variation (", sigma[epsilon], ")"))),
ncol = 2,
nrow = 3,
widths = c(2.7, 2.7),
heights = c(rep(3, times=3)))
grid::grid.draw(s_grid)
ggsave(file = "Output/Sim_Spatial/sigma_grid_plot.png", s_grid, dpi = 300)
ggsave(file = "Output/Sim_Spatial/sigma_grid_plot.pdf", s_grid, dpi = 300)
#grid.arrange(blank_plot, legend, y_N, y_N_se, y_RMSE, y_theta, y_rhot, y_sigmat, y_thet_st, y_rho_st, ncol = 2, nrow = 5, widths = c(2.7, 2.7), heights = c(0.2, rep(2.5, times=4))) # y_gamma,
```