-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathtemp_normalized_mann_kendall.R
More file actions
336 lines (250 loc) · 13.3 KB
/
Copy pathtemp_normalized_mann_kendall.R
File metadata and controls
336 lines (250 loc) · 13.3 KB
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
#Mann Kendall Tests for select long term SMPs-this is normalized by water temperature
# Farshad Ebrahimi, 6/5/2023
library(plyr)
library(dplyr)
library(odbc)
library(ggplot2)
library(stats)
library(zoo)
library(hydroTSM)
library(tsibble)
library(fable)
library(forecast)
library(lubridate)
library(fpp2)
library(EnvStats)
library(readxl)
library(DBI)
library(tidyverse)
library(formattable)
library(htmltools)
library(webshot)
options(scipen = 999)
# DB PG14
con <- dbConnect(odbc::odbc(), dsn = "mars14_datav2", MaxLongVarcharSize = 8190)
# Residuals and model stats from Brian
infil_temp_models <- dbGetQuery(con, "SELECT * FROM metrics.tbl_infil_temp_models where model_type = 'linear'")
# Get OW_UID-System_ID
ow <- dbGetQuery(con, "SELECT *,admin.fun_smp_to_system(smp_id) as system_id FROM fieldwork.tbl_ow")
#get the design metrics to provide context
design_metrics <- dbGetQuery(con, "SELECT * FROM external.tbl_systembdv")
#system categories
System_Categories <- read_excel("\\\\pwdoows\\OOWS\\Watershed Sciences\\GSI Monitoring\\06 Special Projects\\52 Long-Term GSI Performance Trends\\04 Analysis\\System Categories\\System Categories_Long-Term Trend Analysis.xlsx",
sheet = "System Variables")
#retain categories
System_Categories_trend <- System_Categories %>%
select(system_id = `System ID`, categories = `SC Category for Trend Analysis`)
#get the rain event time and date
smp_radarcell <- dbGetQuery(con, "SELECT * FROM data.tbl_radar_event") %>%
select(-eventdatastart_edt)
metric_residual <- infil_temp_models %>%
inner_join(smp_radarcell, by = c("radar_event_uid")) %>%
select(ow_uid, radar_event_uid, infiltration_inhr, eventdatastart_edt, residual) %>%
na.omit() %>%
distinct()
# mann-kendall test
metric_residual["p_value"] <- NA
metric_residual["slope_estimate"] <- NA
metric_residual["tau"] <- NA
metric_residual["intercept_estimate"] <- NA
output <- metric_residual[0,]
owid_unique <- metric_residual %>%
select(ow_uid) %>%
distinct()
for (i in 1:nrow(owid_unique)) {
metric_df <- metric_residual %>%
filter(ow_uid == owid_unique[i, ])
kendall <- kendallTrendTest(residual ~ eventdatastart_edt , data = metric_df)
metric_df["p_value"] <- kendall$p.value
metric_df["intercept_estimate"] <- kendall$estimate[3]
metric_df["slope_estimate"] <- kendall$estimate[2]
metric_df["tau"] <- kendall$estimate[1]
output <- rbind(output, metric_df)
}
output <- output %>%
select(ow_uid, p_value, slope_estimate, intercept_estimate, tau) %>%
distinct()
### Write to db
#dbWriteTable(con, SQL("data.tbl_linear_norm_mk"), output)
#summarize results-significant positive trends
kendal_summary_sig_pos <- output %>%
filter(p_value < 0.05 & slope_estimate > 0) %>%
nrow()
#summarize results-significant negative trends
kendal_summary_sig_neg <- output %>%
filter(p_value < 0.05 & slope_estimate < 0) %>%
nrow()
#add the categories
kendal_categories <- output %>%
inner_join(ow, by = "ow_uid") %>%
inner_join(System_Categories_trend, by = "system_id") %>%
mutate(trend = case_when(p_value < 0.05 & slope_estimate > 0 ~ "sig_positive",
p_value < 0.05 & slope_estimate < 0 ~ "sig_negative",
p_value > 0.05 & slope_estimate > 0 ~ "insig_positive",
p_value > 0.05 & slope_estimate < 0 ~ "insig_negative"))
# Calculate the count of each trend within each category
counts <- table(kendal_categories$categories, kendal_categories$trend)
# Convert the counts into a dataframe
counts_df <- as.data.frame.matrix(counts)
# Reshape the dataframe for plotting
counts_df <- as.data.frame(counts_df)
counts_df$category <- row.names(counts_df)
counts_df <- tidyr::gather(counts_df, key = "trend", value = "count", -category)
# Create the stacked bar chart
ggplot(counts_df, aes(x = category, y = count, fill = trend)) +
geom_bar(stat = "identity") +
labs(x = "Category", y = "Count", fill = "Trend")+
scale_y_continuous(breaks = seq(0,15, by = 1))+
theme(panel.grid.major.y = element_line(size = 1))+
ggtitle("Breakdown of the Trends based on SC categories")
# Creating plots for the memo
#add the categories
kendal_categories <- output %>%
inner_join(ow, by = "ow_uid") %>%
inner_join(System_Categories_trend, by = "system_id") %>%
mutate(trend = case_when(p_value < 0.05 & slope_estimate > 0 ~ "Significant_Positive",
p_value < 0.05 & slope_estimate < 0 ~ "Significant_Negative",
p_value > 0.05 & slope_estimate > 0 ~ "Insignificant_Positive",
p_value > 0.05 & slope_estimate < 0 ~ "Insignificant_Negative")) %>%
filter(categories != "Excluded")
#TRIM AND WRITE TO csv
temp_normalized_mk <- kendal_categories %>%
select(smp_id, ow_uid, slope_estimate_inhrsec = slope_estimate, intercept_estimate, tau, p_value, sc_category = categories)
write.csv(x = temp_normalized_mk, file = paste0("\\\\pwdoows\\oows\\Watershed Sciences\\GSI Monitoring\\06 Special Projects\\52 Long-Term GSI Performance Trends\\04 Analysis\\temp_normalized_mk_",Sys.Date(),".csv"), row.names = FALSE)
# Calculate the count of each trend within each category
counts <- table(kendal_categories$categories, kendal_categories$trend)
# Convert the counts into a dataframe
counts_df <- as.data.frame.matrix(counts)
# Reshape the dataframe for plotting
counts_df <- as.data.frame(counts_df)
counts_df$category <- row.names(counts_df)
counts_df <- tidyr::gather(counts_df, key = "trend", value = "count", -category)
# Create the stacked bar chart wit SC categories
sc_barchart <- ggplot(counts_df, aes(x = category, y = count, fill = trend)) +
geom_bar(stat = "identity") +
labs(x = "Category", y = "Count", fill = "Trend")+
scale_y_continuous(breaks = seq(0,15, by = 1))+
theme(panel.grid.major.y = element_line(size = 1), panel.grid.minor.y = element_blank(), text = element_text(size = 20)) +
ggtitle("Breakdown of the Infiltration Rate Residual (in/hr) Mann-Kendall Trends based on Short-circuiting Categories")
# Create Table outputs and scatter plots
mk_table <- kendal_categories %>%
select(smp_id, system_id, ow_uid, slope_estimate, p_value, categories)
mk_table$p_value <- round_any(mk_table$p_value, 0.001)
mk_table$slope_estimate <- round_any( mk_table$slope_estimate*3.15E+7 , 0.001)
mk_table_plot_df <- mk_table %>%
mutate(trend = case_when(p_value < 0.05 ~ "Statistically Significant",
p_value > 0.05 ~ "Statistically Insignificant")) %>%
arrange(slope_estimate)
# get the mean infiltration rates
ow_uid_rates <- infil_temp_models %>%
select(ow_uid, infiltration_inhr) %>%
na.omit()
mean_rates <- ow_uid_rates %>%
group_by(ow_uid) %>%
summarise_at(vars(infiltration_inhr), list(ave_rate = mean))
mk_table_plot_df <- mk_table_plot_df %>%
inner_join(mean_rates, by = "ow_uid")
mk_table_plot_df$ave_rate <- round_any(mk_table_plot_df$ave_rate, 0.001)
mk_table_plot_df_norm <- mk_table_plot_df %>%
mutate(norm = slope_estimate/ave_rate)
#### Scattter Plot
theilsen_plot <- ggplot(mk_table_plot_df, aes(x=factor(smp_id, level=mk_table_plot_df$smp_id), y=slope_estimate, fill = trend)) +
geom_point(size=5, shape=23) +
scale_y_continuous(breaks = seq(-1,1, by = 0.1)) +
geom_hline(yintercept=0, linetype="dashed",
color = "red", size=2)+
theme(text = element_text(size = 20), axis.text.x = element_text(angle = 90, vjust = 0.5, hjust=1)) +
labs(x = "SMP ID", y = "Slope of the Theil-Sen Line Estimate", fill = "Trend")+
ggtitle("Slope of Theil-Sen Estimators (In/hr.Year)")
ggplot(mk_table_plot_df, aes(x=factor(smp_id, level=mk_table_plot_df$smp_id), y=slope_estimate)) +
geom_point(aes(color = categories, shape = trend), size = 5) +
scale_color_manual(values=c('springgreen4','springgreen2', 'brown2', 'red4', 'tan2'))+
scale_y_continuous(breaks = seq(-1,1, by = 0.1)) +
geom_hline(yintercept=0, linetype="dashed",
color = "red", size=2)+
theme(text = element_text(size = 20), axis.text.x = element_text(angle = 90, vjust = 0.5, hjust=1)) +
labs(x = "SMP ID", y = "Slope of the Theil-Sen Line Estimate", shape = "Trend", color = "SC Category")+
ggtitle("Slope of Theil-Sen Estimators (In/hr.Year)")
### Create the table output with formattable package
table_df <- mk_table_plot_df_norm %>%
select(smp_id, categories, slope_estimate, ave_rate ,norm, p_value) %>%
arrange(categories, norm)
names(table_df) <- c("SMP ID","SC Category", "Theil-Sen Slope (in/hr.yr)","Average Observed Rate (in/hr)", "Normalized Slope (1/yr)", "P-value")
costum_format <- formatter(.tag = "span", style = function(x) style(
display = "block",
`font-weight` = "bold",
padding = "0 4px",
`border-radius` = "4px",
`background-color` = csscolor(gradient(abs(as.numeric(x)),
customGreen0, customGreen)),
# color = ifelse(x <= 0, "red","black")))
color = ifelse(x <= 0, "black","black")))
costum_format_pval <- formatter(.tag = "span", style = function(x) style(
display = "block",
`font-weight` = "bold",
padding = "0 4px",
`border-radius` = "4px",
color = ifelse(x <= 0.05, "black","black")))
customGreen = "red"
customGreen0 = "white"
table_df$`Normalized Slope (1/yr)` <- round(table_df$`Normalized Slope (1/yr)`, 3)
table_df <- table_df %>% dplyr::arrange(`Normalized Slope (1/yr)`)
# Add asterisk for drainage well SMP's
smp_ids <- c("9-1-1", "20-4-1", "1029-1-1*", "171-2-1",
"8-1-1", "10-1-1", "250-1-1",
"18-1-1", "366-2-2", "1024-1-1*",
"88-1-1", "187-3-3", "20-1-1",
"20-8-1", "14-1-2", "9-2-1",
"231-2-1", "1025-1-1*", "326-1-1",
"1-3-1", "1-1-1", "179-5-1",
"211-1-2", "1006-1-1")
table_df$`SMP ID` <- smp_ids
memo_table <- formattable(table_df,
align =c("c","c","c"),
list("SMP ID" = costum_format_pval,
"SC Category" = costum_format_pval,
"Theil-Sen Slope (in/hr.yr)" = costum_format_pval,
"Average Observed Rate (in/hr)" = costum_format_pval,
"Normalized Slope (1/yr)" = costum_format,
"P-value" = costum_format_pval))
# thank you: https://stackoverflow.com/questions/38833219/command-for-exporting-saving-table-made-with-formattable-package-in-r
export_formattable <- function(f, file, width = "100%", height = NULL,
background = "white", delay = 0.2) {
w <- as.htmlwidget(f, width = width, height = height)
path <- html_print(w, background = background, viewer = NULL)
url <- paste0("file:///", gsub("\\\\", "/", normalizePath(path)))
webshot(url,
file = file,
selector = ".formattable_widget",
delay = delay)
}
export_formattable(memo_table, "\\\\pwdoows\\OOWS\\Watershed Sciences\\GSI Monitoring\\06 Special Projects\\52 Long-Term GSI Performance Trends\\05 Deliverables\\02 Final Deliverables\\03 Plots\\Theil-Sen_ESL.png")
#### Bar chart reflecting inflow/outflow pathways
# Calculate the count of each trend within each category
System_Categories_flow <- System_Categories[,c(1,9,10)]
names(System_Categories_flow) <- c("system_id","inflow","outflow")
inflow_df <- mk_table_plot_df_norm %>%
inner_join(System_Categories_flow, by = "system_id") %>%
mutate(inflow = case_when(inflow == "Subsurface" ~ "Subsurface Only",
inflow != "Subsurface" ~ "With Surface")) %>%
mutate(outflow = case_when(outflow == "Sewer" ~ "Sewer",
outflow != "Sewer" ~ "Non-Sewer")) %>%
mutate(trend = case_when(p_value < 0.05 & slope_estimate > 0 ~ "Significant_Positive",
p_value < 0.05 & slope_estimate < 0 ~ "Significant_Negative",
p_value > 0.05 & slope_estimate > 0 ~ "Insignificant_Positive",
p_value > 0.05 & slope_estimate < 0 ~ "Insignificant_Negative")) %>%
mutate(flow = paste(inflow, outflow, sep = ", "))
counts <- table(inflow_df$flow, inflow_df$trend)
# Convert the counts into a dataframe
counts_df <- as.data.frame.matrix(counts)
# Reshape the dataframe for plotting
counts_df <- as.data.frame(counts_df)
counts_df$category <- row.names(counts_df)
counts_df <- tidyr::gather(counts_df, key = "trend", value = "count", -category)
# Create the stacked bar chart wit SC categories
sc_barchart <- ggplot(counts_df, aes(x = category, y = count, fill = trend)) +
geom_bar(stat = "identity") +
labs(x = "Category", y = "Count", fill = "Trend")+
scale_y_continuous(breaks = seq(0,15, by = 1))+
theme(panel.grid.major.y = element_line(size = 1), panel.grid.minor.y = element_blank(), text = element_text(size = 20, face = "bold")) +
ggtitle("Breakdown of MK Trends by The Inflow/Outflow Pathways")