-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathanalysis-correlations-amplitude.Rmd
369 lines (302 loc) · 9.32 KB
/
analysis-correlations-amplitude.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
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
---
title: "analysis-correlations-amplitude"
author: "Abel Serrano Juste"
date: "`r Sys.Date()`"
output: html_document
---
# INITIALIZATION
SET GLOBAL VARS
```{r}
PATH = dirname(rstudioapi::getSourceEditorContext()$path)
print(PATH)
setwd(PATH)
```
Import libraries
```{r}
require(visreg)
library(ggnewscale)
library(tidyverse)
library(glue)
```
Load amplitude data set for each site
```{r}
ampl.df <- read.csv(file.path('cooked-data', 'amplitudeAndClimate-Miedes.csv'))
summary(ampl.df)
```
Convert categorical variables to R "factor"
```{r}
ampl.df <- ampl.df %>% mutate_at (vars(site, class, series), \(x) {as.factor(x)}) %>% mutate(date = as.Date(date))
str(ampl.df)
```
Create one different dataframe for every class
```{r}
amplitude.Qi <- ampl.df[ampl.df$class == "Quercus",]
amplitude.P_ND <- ampl.df[ampl.df$class == "ND",]
amplitude.P_D <- ampl.df[ampl.df$class == "D",]
```
## VISUALIZATION
Plot amplitudes:
```{r}
# Define color scales for each class
green_colors <- c("lightgreen", "green", "darkgreen", "darkolivegreen", "olivedrab", "springgreen")
blue_colors <- c("blue", "cyan", "aquamarine", "cadetblue", "dodgerblue", "darkblue")
orange_colors <- c("orange", "orangered", "tomato", "darkorange", "coral", "tan")
ggplot() +
ggtitle("Amplitudes painted by color scale according to its group") +
geom_line(data = amplitude.P_D, aes(x = date, y = ampl, col = series)) +
scale_color_manual(values = orange_colors, name = "Declining trees") +
new_scale_color() +
geom_line(data = amplitude.P_ND, aes(x = date, y = ampl, col = series), ) +
scale_color_manual(values = blue_colors, name = "Non-Declining trees") +
new_scale_color() +
geom_line(data = amplitude.Qi, aes(x = date, y = ampl, col = series)) +
scale_color_manual(values = green_colors, name = "Quercus Ilex") +
labs(x=expression(''),
y=expression(Delta*" D (um)")) +
scale_x_date(date_breaks = "1 month", date_labels="%b %Y") +
theme(axis.text.x = element_text(angle = 30, hjust=1))
```
```{r}
boxplot(ampl ~ class, data = ampl.df)
```
```{r}
data <- ampl.df %>% filter(series == first(unique(amplitude.P_D$series)) | series == first(unique(amplitude.Qi$series)) | series == first(unique(amplitude.P_ND$series)))
ggplot() +
ggtitle("Amplitudes painted by color scale according to its group") +
geom_line(data = data, aes(x = date, y = ampl, col = series)) +
scale_x_date(date_breaks = "1 month", date_labels="%b %Y")
```
Plot histograms:
```{r}
for (dendro.no in unique(ampl.df$series)) {
dat = ampl.df %>% filter(series == dendro.no)
hist(log(dat$ampl))
}
```
# Analysis
## ANOVA to explore significant difference between means by class
Analysis ANOVA to see if there's significant difference between the average amplitude of each class
```{r}
aov.model <- aov(ampl ~ class + series, data = ampl.df)
summary(aov.model)
```
```{r}
plot(aov.model$residuals)
```
```{r}
TukeyHSD(aov.model)
```
```{r}
plot(TukeyHSD(aov.model))
```
## Correlations
### Aggregate data
With all daily amplitudes, calculate mean of each one by class
```{r}
amplitude.all = ampl.df %>% group_by(date) %>% summarise(mean = mean(ampl), .groups = "drop")
amplitude.all
amplitude.df = ampl.df %>% group_by(date, class) %>% summarise(mean = mean(ampl))
amplitude.df
amplitude.Qi <- amplitude.df[amplitude.df$class == "Quercus",]
amplitude.P_ND <- amplitude.df[amplitude.df$class == "ND",]
amplitude.P_D <- amplitude.df[amplitude.df$class == "D",]
```
### Amplitude ~ climate
Here we show a plot with VWC vs amplitude per class
```{r}
ggplot(data = amplitude.df, mapping = aes(x = date, y = mean, col = class)) +
ggtitle(glue('Soil moisture vs daily stem amplitude difference for {PLACE}')) +
geom_line() +
scale_y_continuous("Amplitude (um)", breaks=seq(0,100,10), sec.axis = dup_axis(name = "Volumetric Water Content (%)" ))+
geom_line(data = clim.daily, mapping = aes(x = date, y = range.vwc * 100, linetype = "Max - Min Volumetric Water Content (%)"), col="blue")
```
Following, we will explore correlation within microclimate variables (Soil Moisture and temperature) and daily seasonal amplitude.
## Amplitude ~ Temperature
Explore correlation within temperature and daily stem amplitude difference (Not significant)
```{r}
cor.test(
#specify the two variables to correlate
amplitude.P_D$mean, clim.daily$interquartil.temp,
# correlation methods (pearson, spearman, kendall)
method = c("pearson"),
# set confidence interval
conf.level = 0.95)
```
```{r}
find_Max_CCF(amplitude.P_D$mean, clim.daily$interquartil.temp)
```
## Amplitude ~ Soil Moisture
Explore cross-correlation within Soil Moisture and daily seasonal amplitude
### for all trees and classes:
```{r}
ccf (amplitude.all$mean, clim.daily$max.vwc,
#indicate what to do with "NA".
na.action = na.pass,
#indicate if you want the plot
plot = "TRUE")
sort_CCF_values(amplitude.all$mean, clim.daily$max.vwc)
```
### for Quercus Ilex:
Plot VWC and daily amplitude for Qi:
```{r}
plot(clim.daily$max.vwc, amplitude.Qi$mean)
```
```{r}
plot(log10(clim.daily$max.vwc), log10(amplitude.Qi$mean))
```
Test correlation:
```{r}
cor.test(
#specify the two variables to correlate
clim.daily$max.vwc, amplitude.Qi$mean,
# correlation methods (pearson, spearman, kendall)
method = c("pearson"),
# set confidence interval
conf.level = 0.95)
```
```{r}
hist(log10(amplitude.Qi$mean+1))
```
```{r}
hist(log10(clim.daily$max.vwc+1))
```
Standard correlations using different methods:
```{r}
cor.test(amplitude.Qi$mean, clim.daily$max.vwc,
# correlation methods (pearson, spearman, kendall)
method = c("pearson"),
# set confidence interval
conf.level = 0.95)
```
```{r}
cor.test(amplitude.Qi$mean, clim.daily$max.vwc,
# correlation methods (pearson, spearman, kendall)
method = c("spearman"),
# set confidence interval
conf.level = 0.95)
```
```{r}
cor.test(amplitude.Qi$mean, clim.daily$max.vwc,
# correlation methods (pearson, spearman, kendall)
method = c("kendall"),
# set confidence interval
conf.level = 0.95)
```
Cross-correlations:
```{r}
ccf (amplitude.Qi$mean, clim.daily$max.vwc,
#indicate what to do with "NA".
na.action = na.pass,
#indicate if you want the plot
plot = "TRUE")
ccf (log10(amplitude.Qi$mean), log10(clim.daily$max.vwc),
#indicate what to do with "NA".
na.action = na.pass,
#indicate if you want the plot
plot = "TRUE")
```
```{r}
find_Max_CCF(amplitude.Qi$mean, clim.daily$max.vwc)
sort_CCF_values(amplitude.Qi$mean, clim.daily$max.vwc)
```
### For P_ND: amplitude ~ VWC
Exploring plots VWC ~ daily amplitude:
```{r}
plot(clim.daily$max.vwc, amplitude.P_ND$mean)
# plot(lm(log10(clim.daily$mean.vwc) ~ log10(amplitude.P_ND$mean)))
```
```{r}
plot(log10(clim.daily$max.vwc), log10(amplitude.P_ND$mean))
# plot(lm(clim.daily$mean.vwc ~ amplitude.P_ND$mean))
```
```{r}
join.df <- full_join(clim.daily, amplitude.P_ND, by="date")
ggplot(data = join.df, aes(log10(max.vwc), log10(mean))) +
# black points graph
geom_point() +
# add correlation with errors and blue color
stat_smooth(method = 'lm',
method.args = list(start= c(a = 1,b=1)),
se=T, color = "blue") +
# theme
theme_classic() +
# add labels
labs( x = "Max Soil Moisture per day - Log",
y = "amplitude variations (um) - Log")
```
Exploring correlations:
```{r}
cor.test(amplitude.P_ND$mean, clim.daily$max.vwc,
# correlation methods (pearson, spearman, kendall)
method = c("pearson"),
# set confidence interval
conf.level = 0.95)
```
```{r}
cor.test(amplitude.P_ND$mean, clim.daily$max.vwc,
# correlation methods (pearson, spearman, kendall)
method = c("spearman"),
# set confidence interval
conf.level = 0.95)
```
```{r}
cor.test(amplitude.P_ND$mean, clim.daily$max.vwc,
# correlation methods (pearson, spearman, kendall)
method = c("kendall"),
# set confidence interval
conf.level = 0.95)
```
```{r}
ccf (amplitude.P_ND$mean, clim.daily$max.vwc,
#indicate what to do with "NA".
na.action = na.pass,
#indicate if you want the plot
plot = "TRUE")
ccf (log10(amplitude.P_ND$mean), log10(clim.daily$max.vwc),
#indicate what to do with "NA".
na.action = na.pass,
#indicate if you want the plot
plot = "TRUE")
```
```{r}
sort_CCF_values(amplitude.P_ND$mean, clim.daily$max.vwc)
sort_CCF_values(log10(amplitude.P_ND$mean), log10(clim.daily$max.vwc))
```
## With P_D
```{r}
cor.test(log10(amplitude.P_D$mean), log10(clim.daily$max.vwc),
# correlation methods (pearson, spearman, kendall)
method = c("pearson"),
# set confidence interval
conf.level = 0.95)
```
```{r}
cor.test(amplitude.P_D$mean, clim.daily$max.vwc,
# correlation methods (pearson, spearman, kendall)
method = c("spearman"),
# set confidence interval
conf.level = 0.95)
```
```{r}
cor.test(amplitude.P_D$mean, clim.daily$max.vwc,
# correlation methods (pearson, spearman, kendall)
method = c("kendall"),
# set confidence interval
conf.level = 0.95)
```
```{r}
ccf (amplitude.P_D$mean, clim.daily$max.vwc,
#indicate what to do with "NA".
na.action = na.pass,
#indicate if you want the plot
plot = "TRUE")
ccf (log10(amplitude.P_D$mean), log10(clim.daily$max.vwc),
#indicate what to do with "NA".
na.action = na.pass,
#indicate if you want the plot
plot = "TRUE")
```
```{r}
sort_CCF_values(amplitude.P_D$mean, clim.daily$max.vwc)
sort_CCF_values(log10(amplitude.P_D$mean), log10(clim.daily$max.vwc))
```