-
Notifications
You must be signed in to change notification settings - Fork 0
/
analysis-dendro.Rmd
253 lines (203 loc) · 7.2 KB
/
analysis-dendro.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
```{r}
source("lib-dendro.R")
library(Rmisc) # for summarySE()
library(ggplot2)
library(dplyr)
library(patchwork) # to stack plots
DATA_DIR = 'Miedes-last'
ENVIRONMENT_DIR = 'Prec'
```
```{r}
#ts_start<-"2022-03-12 00:00:00" #from March 12 (2 days after installation)
#ts_end<-"2023-12-28 12:45:00" # last timestamp of downloaded data
```
```{r}
list_files <- list.files(file.path(".",DATA_DIR), pattern="*.csv$", full.names=TRUE)
db<-read.all.processed(list_files)
str(db)
```
Cargamos datos de los árboles para sacar y separar por las 3 categorías de dendrómetros: Pino Decaído (PD), Pino No Decaído (P) y Quercus (Q)
```{r}
TreeList<-read.table("TreeList.txt",header=T)
db <- merge(db,TreeList[,c(1,4,6)], by = "series")
#aggregate(value ~ class , db, mean)
```
# Un poco de estadística descriptiva
```{r}
db_ND <- db[db$class == "ND",]
summary(db_ND)
sd(db_ND$value, na.rm = T)
db_D <- db[db$class == "D",]
summary(db_D)
sd(db_D$value, na.rm = T)
db_Q <- db[db$class != "D" & db$class != "ND",]
summary(db_Q)
sd(db_Q$value, na.rm = T)
```
Pintamos un cleveland dotplots para ver mejor dispersión y outliers:
```{r}
# hacer antes un agregado horario o algo así para que sean menos datos a pintar.
# probar tb con ggplot
# Pintar de color diferente cada dendro y separar las 3 especies
# si acaso luego pintar todas juntas pero distinguiendo también cada dendro.
dotchart(db$value,
groups = factor(db$class),
ylab = "Class", xlab = "Value",
main = "Cleveland dotplot", pch = db$class)
```
Nos quedamos solamente con el año 2023 para el análisis
```{r}
dim(db)
ts_start <- "2023-01-01 00:00:00" #from March 19 (2 days after installation)
ts_end <- "2023-09-13 09:00:00" # last timestamp of downloaded data
#db2023<-db[which(db$ts>=ts_start & db$ts<=ts_end),]
db2023 <- reset.initial.values(db, ts_start, ts_end)
dim(db2023)
```
```{r}
db2023.class <- db2023 %>%
group_by(class, ts) %>%
summarise(mean_value = mean(value, na.rm = TRUE))
```
```{r}
# Pintar cada dendrómetro
plot_cat_full_year<-
ggplot(data = db2023.class, aes(x=ts, y=mean_value, color=class))+
geom_line( )+
labs(x=expression(''),
y=expression(Delta*" D (um)"))+
theme_bw() +
geom_hline(yintercept=0,lty=2,linewidth=0.2)+
#facet_grid(class~.,scales = "free_y")+
scale_x_datetime(date_breaks = "1 month", date_labels="%b %Y") +
theme(axis.text.x = element_text(angle = 30, hjust=1))
plot_cat_full_year
ggsave('mean_full_year-23.png', width = 15, height = 10)
```
Calculamos Error estándar e intervalo de confianza para pintarlo junto con los datos:
```{r}
db2023.SE <- summarySE(db2023, measurevar = "value", groupvars = c("class", "ts"), na.rm = TRUE)
head(db2023.SE)
tail(db2023.SE)
```
```{r}
plot_cat_full_year<-
ggplot(data = db2023.SE, aes(x=ts, y=value, color=class))+
geom_ribbon(aes(ymin=value-se, ymax=value+se, fill=class), alpha=0.2, show.legend = FALSE, linetype = 0) +
geom_line( )+
labs(x=expression(''),
y=expression(Delta*" D (um)"))+
theme_bw() +
geom_hline(yintercept=0,lty=2,linewidth=0.2)+
#facet_grid(class~.,scales = "free_y")+
scale_x_datetime(date_breaks = "1 month", date_labels="%b %Y") +
theme(axis.text.x = element_text(angle = 30, hjust=1))
plot_cat_full_year
ggsave('mean+SE_full_year-23.png', width = 15, height = 10)
```
# DATOS AMBIENTALES DE CLIMA
Ahora vamos a añadir datos ambientales de clima: temperatura y humedad relativa.
Cargamos los datos en variable `env.db` y filtramos solamente para datos de 2023:
```{r}
sensor.id = 94231938;
filename = file.path(ENVIRONMENT_DIR, paste0("data_", sensor.id, "_2023_09_13_0.csv"))
env.db <- read.env.data(filename)
# filter dates
env.db<-env.db[which(env.db$ts>=ts_start & env.db$ts<=ts_end),]
```
Generamos y sobreponemos las gráficas de temperatura y de humedad relativa:
```{r}
plot_temp_and_humidity <-
ggplot(data = env.db, aes(x=ts, y=top.temp)) +
geom_line(aes(colour = top.temp)) +
theme_bw() +
scale_colour_gradient(low = "light blue", high = "red") +
scale_x_datetime(date_breaks = "1 month", date_labels="%b %Y") +
theme(axis.text.x = element_text(angle = 30, hjust=1)) +
scale_y_continuous("Temperature (ºC)", sec.axis = sec_axis( ~ . * 100, name = "Humidity (mV)" ))+
geom_line(aes(x = ts, y = humidity / 100, linetype = "Humidity (mV)"), col="blue", show.legend = TRUE) +
scale_linetype_manual(NULL, values = 1) +
labs(x=expression(''), colour = "Temperature (ºC)")
plot_temp_and_humidity
```
Y la juntamos con las gráficas de dendro, apiladas una debajo de la otra:
```{r}
plot_cat_full_year / plot_temp_and_humidity
ggsave('mean+SE+temp+hum_full_year-23.png', width = 16, height = 10)
```
# Análisis Quercus dos años: 2022 y 2023
```{r}
db.qi.2y <- db[db$class == "Quercus",]
ts_start <- "2022-03-19 00:00:00" #from March 19 (2 days after installation)
ts_end <- "2023-12-12 00:00:00" # last timestamp of downloaded data
db.qi.2y <-db.qi.2y[which(db.qi.2y$ts>=ts_start & db.qi.2y$ts<=ts_end),]
```
```{r}
mean.qi.2y <- db.qi.2y %>%
group_by(ts) %>%
summarise(mean_value = mean(value, na.rm = TRUE))
```
```{r}
ggplot(data = mean.qi.2y, aes(x=ts, y=mean_value))+
geom_line( )+
labs(x=expression(''),
y=expression(Delta*" D (um)"))+
theme_bw() +
geom_hline(yintercept=0,lty=2,linewidth=0.2)+
#facet_grid(class~.,scales = "free_y")+
scale_x_datetime(date_breaks = "1 month", date_labels="%b %Y") +
theme(axis.text.x = element_text(angle = 30, hjust=1))
```
```{r}
qi2y.summ <- summarySE(data=db.qi.2y, measurevar = "value", groupvars = c("ts", "class"))
qi2y.summ
```
```{r}
plot_qi_2_year<-
ggplot(data = qi2y.summ, aes(x=ts, y=value))+
geom_ribbon(aes(ymin=value-se, ymax=value+se), fill="brown", alpha=0.2, show.legend = FALSE, linetype = 0) +
geom_line( aes (linetype = "Quercus Ilex"), col="brown", show.legend = TRUE) +
scale_linetype_manual(NULL, values = 1) +
labs(x=expression(''),
y=expression(Delta*" D (um)"))+
theme_bw() +
geom_vline(xintercept = as.numeric(as.POSIXct("2023-01-01 00:00:00")), lty=2,linewidth=0.2) +
#facet_grid(class~.,scales = "free_y")+
scale_x_datetime(date_breaks = "1 month", date_labels="%b %Y") +
theme(axis.text.x = element_text(angle = 30, hjust=1))
plot_qi_2_year
ggsave('mean+SE_Qi-22&23.png', width = 15, height = 10)
```
# Anális septiembre 2023
Nos quedamos solamente con el mes de sept-23 para el 2º análisis
```{r}
dim(db2023)
ts_start <- "2023-09-01 00:00:00" #from March 19 (2 days after installation)
ts_end <- "2023-09-13 00:00:00" # last timestamp of downloaded data
dbsept<-db[which(db$ts>=ts_start & db$ts<=ts_end),]
dim(dbsept)
```
Calculamos medias para cada categoría
```{r}
dbsept.class <- dbsept %>%
group_by(class, ts) %>%
summarise(mean_value = mean(value, na.rm = FALSE))
# Print the result
print(dbsept.class)
dim(dbsept.class)
```
Y pintamos:
```{r}
plot_cat_sept_23<-
ggplot(data = dbsept.class, aes(x=ts, y=mean_value, color=class))+
geom_line( )+
labs(x=expression('Date'),
y=expression(Delta*" D (um)"))+
theme_bw() +
geom_hline(yintercept=0,lty=2,linewidth=0.2)+
#facet_grid(rows = vars(series), scales = "free_y")+
scale_x_datetime(date_breaks = "1 day")+
theme(axis.text.x = element_text(angle = 30, hjust=1))
plot_cat_sept_23
ggsave('mean_sept-23.png', width = 15, height = 10)
```