forked from Jpomz/detecting-spectra-differences
/
NEON_estimate.R
184 lines (162 loc) · 5.32 KB
/
NEON_estimate.R
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
# Empirical example: AMD
library(tidyverse)
library(sizeSpectra) #bounded power law and MLE functions
source("custom_functions.R")
# compare estimates with empirical data
# data is from Pomeranz et al. 2018 Freshwater Biology
neon_dat <- read_csv("neon_data.csv")
dw_range = range(neon_dat$dw, na.rm = TRUE)
# not using the MLEbin method, as in Pomeranz et al. 2022
# just using the "normal" MLE method to compare
# NEON "Bins" individuals into 1 mm widths, so doesn't drastically alter body size estimates
(neon_result <- neon_dat %>%
# mutate(date = as.Date(collectDate)) %>%
group_by(mat.c, ID) %>%
nest() %>%
mutate(method_compare =
map(data,
compare_slopes,
rsp_var = "dw",
dw_range = dw_range)) %>%
ungroup() %>%
select(-data) %>%
unnest(cols = method_compare))
neon_result %>%
mutate(Model = factor(name,
levels =
c("MLE",
"ELBn",
"NAS"))) %>%
ggplot(aes(x = mat.c,
y = estimate,
color = Model))+
geom_point() +
stat_smooth(method = "lm",
se = FALSE)+
theme_bw() +
scale_color_manual(
values = c("#019AFF",
"#FF914A",
"#FF1984" )) +
labs(title = "Change in exponent across NEON",
x = "Mean annual air temp",
y = "Slope/exponent estimate")
ggsave("figures/NEON_plot.png")
neon_relationship <- neon_result %>%
group_by(name) %>%
nest() %>%
mutate(lm_mod =
map(data,
~lm(estimate ~ mat.c, data = .x))) %>%
mutate(tidied = map(lm_mod, broom::tidy)) %>%
unnest(tidied) %>%
filter(term == "mat.c") %>%
select(-data, -lm_mod)
neon_relationship %>%
mutate(abs_change = estimate *
(max(neon_dat$mat.c) - min(neon_dat$mat.c))) %>%
write_csv("results/NEON_estimates.csv")
neon_relationship %>%
mutate(Model = factor(name,
levels =
c("MLE",
"ELBn",
"NAS"))) %>%
ggplot(aes(x = estimate,
xmin = estimate - std.error,
xmax = estimate + std.error,
y = Model,
color = Model)) +
geom_pointrange()+
scale_color_manual(
values = c("#019AFF", "#FF914A", "#FF1984" )) +
theme_bw() +
labs(y = "Relationship estimate",
title = "Mean +/- Std. Error Beta across Neon")
ggsave("figures/neon_relationship.png")
# estimate by sample ------------------------------------------------------
# not sure where to go with this want to compare what the estimates are for each sample (i.e. surber sample), and see how that compares to the avergae across samples, and the estimate when combining all of the data for one estimate.
# need to re-export NEON data from project and include sample number
# estimate CSS parameter for each sample
# compare "total" estimate with individual sample estimates and mean_sample estimates
(neon_sample_result <- neon_dat %>%
group_by(mat.c, ID, sample) %>%
mutate(n = n()) %>%
filter(n >= 1000) %>% # filter out samples that have < 1000 individuals
nest() %>%
mutate(method_compare =
map(data,
compare_slopes,
rsp_var = "dw",
dw_range = dw_range)) %>%
ungroup() %>%
select(-data) %>%
unnest(cols = method_compare))
ggplot(neon_sample_result,
aes(x = mat.c,
y = estimate,
color = name))+
geom_point(position = position_jitter(widt = 0.2),
alpha = 0.5) +
stat_smooth(method = "lm",
se = FALSE)+
theme_bw() +
labs(title = "Change in exponent across NEON",
x = "Mean annual air temp",
y = "Slope/exponent estimate")
neon_sample_result %>%
group_by(name) %>%
nest() %>%
mutate(lm_mod =
map(data,
~lm(estimate ~ mat.c, data = .x))) %>%
mutate(tidied = map(lm_mod, broom::tidy)) %>%
unnest(tidied) %>%
filter(term == "mat.c") %>%
select(-data, -lm_mod)
ggplot(neon_sample_result,
aes(x = mat.c,
y = estimate))+
geom_point(position = position_jitter(widt = 0.2),
alpha = 0.5) +
stat_summary(aes(group = ID), fun.data = "mean_se", color = "red", size = 0.5) +
stat_smooth(method = "lm",
se = FALSE)+
theme_bw() +
geom_point(inherit.aes = FALSE,
data = neon_result,
aes(x = mat.c,
y = estimate),
color = "blue",
size = 1) +
facet_wrap(~name,
ncol = 1)
neon_sample_result %>%
filter(ID == 1) %>%
ggplot(
aes(x = mat.c,
y = estimate))+
geom_point(position = position_jitter(),
alpha = 0.5) +
stat_summary(aes(group = ID),
fun.data = "mean_se",
color = "red",
size = 0.5) +
stat_smooth(method = "lm",
se = FALSE)+
theme_bw() +
geom_pointrange(
inherit.aes = FALSE,
data = neon_result %>%
filter(ID == 1),
aes(x = mat.c,
y = estimate,
ymin = minCI,
ymax = maxCI),
color = "dodgerblue") +
facet_wrap(~name,
ncol = 1) +
labs(title = "NEON Sample Estimates",
subtitle = "Black = sample, red = mean sample, blue = combined",
y = "Slope/exponent estimate") +
NULL