-
Notifications
You must be signed in to change notification settings - Fork 21
/
check.R
166 lines (160 loc) · 4.4 KB
/
check.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
#' Check Quantiles Required are Present
#'
#' @param posterior A `data.frame` containing quantiles identified using
#' the `q5` naming scheme. Default: No default.
#'
#' @param req_probs A numeric vector of required probabilities. Default:
#' c(0.5, 0.95, 0.2, 0.8).
#'
#' @return NULL
#'
#' @family check
check_quantiles <- function(posterior, req_probs = c(0.5, 0.95, 0.2, 0.8)) {
if (any(req_probs <= 0) || any(req_probs >= 1)) {
stop("Please provide probabilities as numbers between 0 and 1.")
}
cols <- colnames(posterior)
if (sum(cols %in% paste0("q", req_probs * 100)) != length(req_probs)) {
stop(
"Following quantiles must be present (set with probs): ",
toString(req_probs)
)
}
return(invisible(NULL))
}
#' Check Report and Reference Dates are present
#'
#' @param obs An observation `data.frame` containing \code{report_date} and
#' \code{reference_date} columns.
#'
#' @return Returns the input `data.frame` with dates converted to date format
#' if not already.
#'
#' @importFrom data.table as.data.table copy
#' @family check
check_dates <- function(obs) {
obs <- data.table::as.data.table(obs)
obs <- data.table::copy(obs)
if (is.null(obs$reference_date) && is.null(obs$report_date)) {
stop(
"Both reference_date and report_date must be present in order to use this
function"
)
} else if (is.null(obs$reference_date)) {
stop("reference_date must be present")
} else if (is.null(obs$report_date)) {
stop("report_date must be present")
}
obs[, report_date := as.IDate(report_date)]
obs[, reference_date := as.IDate(reference_date)]
return(obs[])
}
#' Check Observations for reserved grouping variables
#'
#' @param obs An observation `data.frame` that does not contain `.group`,
#' `.old_group`, or `.new_group` as these are reserved variables.
#'
#' @return NULL
#'
#' @family check
check_group <- function(obs) {
if (!is.null(obs$.group)) {
stop(
".group is a reserved variable and must not be present in the input
data"
)
} else if (!is.null(obs$.new_group)) {
stop(
".new_group is a reserved variable and must not be present in the input
data"
)
} else if (!is.null(obs$.old_group)) {
stop(
".old_group is a reserved variable and must not be present in the input
data"
)
}
return(invisible(NULL))
}
#' Check by variables are present in the data
#'
#' @param obs An observation `data.frame`.
#'
#' @param by A character vector of variables to group by.
#'
#' @return NULL
#'
#' @family check
check_by <- function(obs, by = NULL) {
if (length(by) > 0) {
if (!is.character(by)) {
stop("`by` must be a character vector")
}
if (!all(by %in% colnames(obs))) {
stop(
"`by` must be a subset of the columns in `obs`. \n",
toString(by[!(by %in% colnames(obs))]),
" are not present in `obs`"
)
}
}
return(invisible(NULL))
}
#' Add a reserved grouping variable if missing
#'
#' @param x A data.table
#'
#' @return A data table with a `.group` variable
#' @family check
add_group <- function(x) {
if (is.null(x[[".group"]])) {
x <- x[, .group := 1]
}
return(x[])
}
#' Check a model module contains the required components
#'
#' @param module A model module. For example [enw_expectation()].
#'
#' @return NULL
#'
#' @family check
check_module <- function(module) {
if (!"data" %in% names(module)) {
stop(
"Must contain a list component specifying the data requirements for
further modelling as a list"
)
}
if (!is.list(module[["data"]])) {
stop(
"data must be a list of required data"
)
}
return(invisible(NULL))
}
#' Check that model modules have compatible specifications
#'
#' @param modules A list of model modules.
#'
#' @return NULL
#'
#' @family check
check_modules_compatible <- function(modules) {
if (
modules[[4]]$data$model_miss &&
!modules[[6]]$data$likelihood_aggregation
) {
warning(
"Incompatible model specification: A missingness model has ",
"been specified but likelihood aggregation is specified as ",
"by snapshot. Switching to likelihood aggregation by group.",
" This has no effect on the nowcast but limits the ",
"number of threads per chain to the number of groups. To ",
"silence this warning, set the `likelihood_aggregation` ",
"argument in `enw_fit_opts` to 'groups'.",
immediate. = TRUE
)
}
return(invisible(NULL))
}