-
Notifications
You must be signed in to change notification settings - Fork 5
/
ensemble_summary.R
168 lines (147 loc) · 6.15 KB
/
ensemble_summary.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
#' Computes summary timeseries in HS object
#'
#' Allows easy computation of summaries across runoff datasets with user defined
#' functions. The functions provided are run either individually for, or across,
#' each runoff/downscaled/discharge timeseries in an HS* object.
#'
#' Applicable functions take a vector of numeric values as input, and return
#' a single numerical value.
#'
#' @param HS \code{HS} object, or a list of data frames with column \code{Date}.
#' @param summarise_over_timeseries Apply function(s) column-wise (to
#' each timeseries separately, \code{TRUE}), or row-wise for each date in
#' timeseries(\code{FALSE}, default).
#' @param aggregate_monthly Return results as averages for the 12 months of the
#' year? Defaults to \code{FALSE}.
#' @param funs Functions to evaluate. By default, computes \code{min, mean,
#' median and max}.
#' @param drop Drop existing timeseries in \code{runoff_ts}, \code{discharge_ts},
#' or not.
#' @param ... Additional arguments passed to \code{funs}.
#' @param verbose Indicate progress, or not. Defaults to \code{FALSE}.
#'
#' @return Returns the input \code{HS} object, or a list, where
#' runoff/downscaled/discharge is replaced with the computed summaries.
#'
#' @export
ensemble_summary <- function(HS,
summarise_over_timeseries = FALSE,
aggregate_monthly = FALSE,
funs=c("min","mean","median","max"),
drop=FALSE,
...,
verbose = FALSE) {
UseMethod("ensemble_summary")
}
#' @export
ensemble_summary.list <- function(HS,
summarise_over_timeseries = FALSE,
aggregate_monthly = FALSE,
funs=c("min","mean","median","max"),
drop = FALSE,
...,
verbose = FALSE) {
Date <- NULL
Month <- NULL
Pred <- NULL
Value <- NULL
Stat <- NULL
Prediction <- NULL
total <- length(HS)
if (verbose) pb <- txtProgressBar(min = 0, max = total, style = 3)
for (seg in seq_along(HS)) {
data <- HS[[seg]]
if (summarise_over_timeseries) {
if (aggregate_monthly) {
data <- data %>%
tibble::as_tibble(.name_repair = "minimal") %>%
dplyr::mutate(Month = lubridate::month(Date)) %>%
dplyr::group_by(Month) %>%
dplyr::select(-Date) %>%
dplyr::summarise_all(.funs=funs, na.rm=TRUE) %>%
tidyr::gather(Pred, Value,-Month) %>%
dplyr::mutate(Stat = stringr::word(Pred, -1, sep="_"),
Prediction = stringr::str_replace(Pred,
paste0("_", Stat), "")) %>%
dplyr::select(Month, Prediction, Stat, Value)
} else {
data <- data %>%
tibble::as_tibble(.name_repair = "minimal") %>%
dplyr::select(-Date) %>%
dplyr::summarise_all(.funs=funs, ...) %>%
tidyr::gather(Pred, Value) %>%
dplyr::mutate(Stat = stringr::word(Pred, -1, sep="_"),
Prediction = stringr::str_replace(Pred,
paste0("_", Stat), "")) %>%
dplyr::select(Prediction, Stat, Value)
}
} else {
data <- data %>%
tidyr::gather(Prediction, Value, -Date)
if (aggregate_monthly) {
data <- data %>%
tibble::as_tibble(.name_repair = "minimal") %>%
dplyr::mutate(Month = lubridate::month(Date)) %>%
dplyr::group_by(Month) %>%
dplyr::select(-Date,-Prediction) %>%
dplyr::summarise_all(.funs=funs)
} else {
data <- data %>%
tibble::as_tibble(.name_repair = "minimal") %>%
dplyr::group_by(Date) %>%
dplyr::select(-Prediction) %>%
dplyr::summarise_all(.funs=funs)
}
if (!drop) {
data <- dplyr::left_join(HS[[seg]], data, by="Date")
}
}
HS[[seg]] <- data
if(verbose) setTxtProgressBar(pb, seg)
}
if(verbose) close(pb)
return(HS)
}
#' @export
ensemble_summary.HS <- function(HS,
summarise_over_timeseries = FALSE,
aggregate_monthly = FALSE,
funs=c("min","mean","median","max"),
drop = FALSE,
...,
verbose = FALSE) {
runoff <- hasName(HS, "runoff_ts")
discharge <- hasName(HS, "discharge_ts")
if (runoff) {
data <- HS$runoff_ts
data <- ensemble_summary(data,
summarise_over_timeseries,
aggregate_monthly,
funs,
drop = drop,
...,
verbose = verbose)
if(summarise_over_timeseries) {
HS$runoff_summary <- data
} else {
HS$runoff_ts <- data
}
}
if (discharge) {
data <- HS$discharge_ts
data <- ensemble_summary(data,
summarise_over_timeseries,
aggregate_monthly,
funs,
drop = drop,
...)
if(summarise_over_timeseries) {
HS$discharge_summary <- data
} else {
HS$discharge_ts <- data
}
}
HS <- reorder_cols(HS)
HS <- assign_class(HS, "HS")
return(HS)
}