-
Notifications
You must be signed in to change notification settings - Fork 1
/
states_define.R
281 lines (252 loc) · 6.5 KB
/
states_define.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
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
#' Define a Markov Model State
#'
#' Define the values characterising a Markov Model state for
#' 1 cycle.
#'
#' As with [define_parameters()], state values are
#' defined sequentially. Later state definition can thus
#' only refer to values defined earlier.
#'
#' For the `modify` function, existing values are
#' modified, no new values can be added. Values order
#' matters since only values defined earlier can be
#' referenced in later expressions.
#'
#' @param ... Name-value pairs of expressions defining state
#' values.
#' @param starting_values Optional starting values defined
#' with [define_starting_values()].
#' @param .OBJECT An object of class `state`.
#' @param x Used to work around non-standard evaluation.
#'
#' @return An object of class `state` (actually a named
#' list of quosures).
#' @export
#'
#' @example inst/examples/example_define_state.R
#'
define_state <- function(..., starting_values = define_starting_values()) {
.dots <- quos(...)
deprecated_x_cycle(.dots)
define_state_(list(.dots = .dots, starting_values = starting_values))
}
#' @export
#' @rdname define_state
define_state_ <- function(x) {
.dots <- x$.dots
check_names(names(.dots))
starting_values <- check_starting_values(
x = x$starting_values,
ref = names(.dots)
)
structure(list(
.dots = .dots,
starting_values = starting_values),
class = c("state", "list"))
}
#' @export
#' @rdname define_state
modify.state <- function(.OBJECT, ...) {
.dots <- quos(...)
modify_(.OBJECT = .OBJECT, .dots = .dots)
}
#' @export
modify_.state <- function(.OBJECT, .dots) {
check_names(names(.dots))
if ("starting_values" %in% names(.dots)) {
starting_values <- check_starting_values(
x = eval_tidy(.dots$starting_values),
ref = names(.OBJECT$.dots)
)
.OBJECT <- utils::modifyList(.OBJECT, list(starting_values = starting_values))
.dots <- utils::modifyList(.dots, list(starting_values = NULL))
}
if (!length(.dots)){
return(.OBJECT)
}
if (! all(names(.dots) %in% names(.OBJECT$.dots))) {
stop(sprintf(
"The following state values are not defined: %s.",
names(.dots)[names(.dots) %in% names(.OBJECT$.dots)]
))
}
utils::modifyList(.OBJECT$.dots, .dots)
}
#' Define Markov Model State List
#'
#' Define the states of a Markov model by combining
#' `state` objects.
#'
#' State names have to correspond to those specified through
#' [define_transition()].
#'
#' All states should have the same value names.
#'
#' The `modify` function can modify existing states or
#' add new ones.
#'
#' @param ... Name-value pairs of expressions defining model
#' states.
#' @param .OBJECT An `uneval_states` object.
#' @param .dots List of states, only used by
#' `define_state_list_` to avoid using `...`.
#'
#' @return An object of class `uneval_state_list` (a
#' list of `state` objects).
#'
#' @examples
#' \dontrun{
#' s1 <- define_state(cost = 1, util = 1)
#' s2 <- define_state(cost = 3, util = .4)
#'
#' states_mod <- define_state_list(
#' healthy = s1,
#' sick = s2
#' )
#'
#' states_mod
#'
#' s1_bis <- define_state(cost = 0, util = 1)
#' s3 <- define_state(cost = 10, util = .1)
#'
#' modify(
#' states_mod,
#' healthy = s1_bis,
#' sicker = s3
#' )
#' }
#'
#' @keywords internal
define_state_list <- function(...) {
.dots <- list(...)
define_state_list_(.dots)
}
#' @rdname define_state_list
define_state_list_ <- function(.dots) {
# states <- lapply(.dots, function(x){
# structure(
# x$.dots,
# class = class(x)
# )
# })
states <- .dots
state_names <- names(states)
if (is.null(state_names)) {
if (!identical(Sys.getenv("TESTTHAT"), "true"))
message("No named state -> generating names.")
state_names <- LETTERS[seq_along(states)]
names(states) <- state_names
}
if (any(state_names == "")) {
warning("Not all states are named -> generating names.")
state_names <- LETTERS[seq_along(states)]
names(states) <- state_names
}
if (any(duplicated(names(states)))) {
stop("Some state names are duplicated.")
}
if (! all(unlist(lapply(states,
function(x) "state" %in% class(x))))) {
.x <- names(states)[! unlist(lapply(
states,
function(x) "state" %in% class(x)))]
stop(sprintf(
"Incorrect state object%s: %s",
plur(length(.x)),
paste(.x, collapse = ", ")
))
}
check_states(states)
structure(
states,
class = c("uneval_state_list", class(states))
)
}
#' @rdname define_state_list
#' @export
modify.uneval_state_list <- function(.OBJECT, ...) {
.dots <- list(...)
modify_(.OBJECT = .OBJECT, .dots = .dots)
}
#' @export
modify_.uneval_state_list <- function(.OBJECT, .dots) {
res <- utils::modifyList(.OBJECT, .dots)
check_states(res)
res
}
#' Check Model States for Consistency
#'
#' For internal use.
#'
#' All states should have the same value names.
#'
#' @param x An object of class `uneval_states`.
#'
#' @return `NULL`
#'
#' @keywords internal
check_states <- function(x){
if (! list_all_same(lapply(x, function(y) length(y$.dots)))) {
stop("Number of state values differ between states.")
}
if (! list_all_same(lapply(x, function(y) sort(names(y$.dots))))) {
stop("State value names differ between states.")
}
NULL
}
#' Return Number of State
#'
#' For internal use.
#'
#' Work with both `uneval_states` and
#' `eval_states`.
#'
#' @param x An object containing states.
#'
#' @return An integer: number of states.
#'
#' @keywords internal
get_state_number <- function(x){
# !mod!
# rename get_state_count
length(get_state_names(x))
}
#' Return Names of State Values
#'
#' @param x An object containing states.
#'
#' @return A character vector of state value names.
#'
#' @keywords internal
get_state_value_names <- function(x){
UseMethod("get_state_value_names")
}
#' @export
get_state_value_names.uneval_state_list <- function(x) {
names(x[[1]][[1]])
}
#' @export
get_state_value_names.state <- function(x){
names(x[[1]])
}
#' Get State Names
#'
#' Retrieve state names from an object containing states.
#'
#' @param x An object containing states.
#' @param ... Additional arguments passed to methods.
#'
#' @return A character vector of state names.
#'
#' @keywords internal
get_state_names <- function(x, ...){
UseMethod("get_state_names")
}
#' @export
get_state_names.eval_state_list <- function(x, ...){
names(x$.dots)
}
#' @export
get_state_names.default <- function(x, ...){
names(x)
}