Skip to content

Commit ad95d2e

Browse files
Merge pull request #232 from canmod/tv-stitch
* more trajectory methods * parameterized trajectory * optimized spec * forecaster version of a simulator * check what functions are used * expose print out of parameterization info * sim offset and much more robust sim offset/sim bounds * insert log linear (experimental) * insert basic transformations of trajectories * expose objective function * more consistently use best last parameter * wrap more optimizers * should we really reset best? * current$data_frame() should give best.last.par * make my own term column -- keep an eye on this * only back transform if there are more than zero coefficientss * check if opt_attempted * potentiallly widespread effects of bind_rows fix (keep an eye on this) * two new engine functions -- getting last item in a vector and checking if a matrix is finite
2 parents 6ffc1dc + f3bbd74 commit ad95d2e

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

81 files changed

+3043
-849
lines changed

DESCRIPTION

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
Package: macpan2
22
Title: Fast and Flexible Compartmental Modelling
3-
Version: 1.15.3
3+
Version: 1.16.0
44
Authors@R: c(
55
person("Steve Walker", email="swalk@mcmaster.ca", role=c("cre", "aut")),
66
person("Weiguang Guan", role="aut"),
@@ -37,6 +37,7 @@ Suggests:
3737
visNetwork,
3838
kableExtra,
3939
broom.mixed,
40+
DEoptim,
4041
outbreaks,
4142
tmbstan,
4243
numDeriv,

Makefile

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -53,6 +53,7 @@ quick-test-all:
5353
make run-vignette-code
5454
make run-tests
5555
make run-examples
56+
make run-starter-readme-code
5657

5758
quick-test:
5859
make quick-doc-install

NAMESPACE

Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -38,8 +38,10 @@ S3method(length,StructuredVector)
3838
S3method(macpan2::TMBPar,ParArg)
3939
S3method(macpan2::TMBPar,character)
4040
S3method(macpan2::TMBPar,list)
41+
S3method(mp_default,TMBCalibrator)
4142
S3method(mp_default,TMBModelSpec)
4243
S3method(mp_default,TMBSimulator)
44+
S3method(mp_default_list,TMBCalibrator)
4345
S3method(mp_default_list,TMBModelSpec)
4446
S3method(mp_default_list,TMBSimulator)
4547
S3method(mp_dynamic_simulator,DynamicModel)
@@ -53,16 +55,23 @@ S3method(mp_expand,TMBModelSpec)
5355
S3method(mp_extract,DynamicModel)
5456
S3method(mp_extract,Ledger)
5557
S3method(mp_extract,ModelDefRun)
58+
S3method(mp_final,TMBCalibrator)
5659
S3method(mp_final,TMBSimulator)
5760
S3method(mp_final_list,TMBSimulator)
5861
S3method(mp_fit,character)
5962
S3method(mp_fit,numeric)
63+
S3method(mp_functions_used,ExprList)
64+
S3method(mp_functions_used,TMBCalibrator)
65+
S3method(mp_functions_used,TMBModelSpec)
66+
S3method(mp_functions_used,TMBSimulator)
6067
S3method(mp_hazard,TMBModelSpec)
6168
S3method(mp_index,character)
6269
S3method(mp_index,data.frame)
6370
S3method(mp_index,numeric)
71+
S3method(mp_initial,TMBCalibrator)
6472
S3method(mp_initial,TMBModelSpec)
6573
S3method(mp_initial,TMBSimulator)
74+
S3method(mp_initial_list,TMBCalibrator)
6675
S3method(mp_initial_list,TMBModelSpec)
6776
S3method(mp_initial_list,TMBSimulator)
6877
S3method(mp_labels,Index)
@@ -71,7 +80,11 @@ S3method(mp_nofit,character)
7180
S3method(mp_nofit,numeric)
7281
S3method(mp_optimize,TMBCalibrator)
7382
S3method(mp_optimize,TMBSimulator)
83+
S3method(mp_optimized_spec,TMBCalibrator)
84+
S3method(mp_optimized_spec,TMBSimulator)
7485
S3method(mp_optimizer_output,TMBCalibrator)
86+
S3method(mp_parameterization,TMBCalibrator)
87+
S3method(mp_parameterization,TMBSimulator)
7588
S3method(mp_reduce,TMBModelSpec)
7689
S3method(mp_reference,Index)
7790
S3method(mp_reference,Ledger)
@@ -94,13 +107,18 @@ S3method(mp_tmb_coef,TMBCalibrator)
94107
S3method(mp_tmb_coef,TMBSimulator)
95108
S3method(mp_tmb_fixef_cov,TMBCalibrator)
96109
S3method(mp_tmb_fixef_cov,TMBSimulator)
110+
S3method(mp_tmb_objective,TMBCalibrator)
111+
S3method(mp_tmb_objective,TMBSimulator)
97112
S3method(mp_tmb_test,TMBModelSpec)
98113
S3method(mp_tmbstan_coef,TMBCalibrator)
99114
S3method(mp_tmbstan_coef,TMBSimulator)
100115
S3method(mp_trajectory,TMBCalibrator)
101116
S3method(mp_trajectory,TMBSimulator)
102117
S3method(mp_trajectory_ensemble,TMBCalibrator)
103118
S3method(mp_trajectory_ensemble,TMBSimulator)
119+
S3method(mp_trajectory_par,TMBCalibrator)
120+
S3method(mp_trajectory_par,TMBSimulator)
121+
S3method(mp_trajectory_replicate,TMBCalibrator)
104122
S3method(mp_trajectory_replicate,TMBSimulator)
105123
S3method(mp_trajectory_sd,TMBCalibrator)
106124
S3method(mp_trajectory_sd,TMBSimulator)
@@ -180,6 +198,8 @@ S3method(to_string,default)
180198
S3method(to_string,formula)
181199
S3method(to_values,data.frame)
182200
S3method(to_values,numeric)
201+
S3method(updated_param_vector,data.frame)
202+
S3method(updated_param_vector,list)
183203
export(BinaryOperator)
184204
export(CSVReader)
185205
export(JSONReader)
@@ -223,6 +243,9 @@ export(mp_final_list)
223243
export(mp_fit)
224244
export(mp_flow_frame)
225245
export(mp_flow_vars)
246+
export(mp_forecaster)
247+
export(mp_functions_used)
248+
export(mp_generates_randomness)
226249
export(mp_group)
227250
export(mp_hazard)
228251
export(mp_identity)
@@ -247,8 +270,10 @@ export(mp_neg_bin)
247270
export(mp_nofit)
248271
export(mp_normal)
249272
export(mp_optimize)
273+
export(mp_optimized_spec)
250274
export(mp_optimizer_output)
251275
export(mp_par)
276+
export(mp_parameterization)
252277
export(mp_per_capita_flow)
253278
export(mp_per_capita_inflow)
254279
export(mp_per_capita_outflow)
@@ -268,11 +293,13 @@ export(mp_set_numbers)
268293
export(mp_setdiff)
269294
export(mp_show_models)
270295
export(mp_sim_bounds)
296+
export(mp_sim_offset)
271297
export(mp_simulator)
272298
export(mp_slices)
273299
export(mp_sqrt)
274300
export(mp_square)
275301
export(mp_state_dependence_frame)
302+
export(mp_state_flow_vars)
276303
export(mp_state_vars)
277304
export(mp_structured_vector)
278305
export(mp_subset)
@@ -286,14 +313,18 @@ export(mp_tmb_entire_library)
286313
export(mp_tmb_expr_list)
287314
export(mp_tmb_fixef_cov)
288315
export(mp_tmb_insert)
316+
export(mp_tmb_insert_log_linear)
289317
export(mp_tmb_insert_reports)
318+
export(mp_tmb_insert_trans)
290319
export(mp_tmb_library)
291320
export(mp_tmb_model_spec)
321+
export(mp_tmb_objective)
292322
export(mp_tmb_update)
293323
export(mp_tmbstan_coef)
294324
export(mp_traj)
295325
export(mp_trajectory)
296326
export(mp_trajectory_ensemble)
327+
export(mp_trajectory_par)
297328
export(mp_trajectory_replicate)
298329
export(mp_trajectory_sd)
299330
export(mp_trajectory_sim)
@@ -340,6 +371,7 @@ importFrom(oor,return_object)
340371
importFrom(stats,as.formula)
341372
importFrom(stats,make.link)
342373
importFrom(stats,na.omit)
374+
importFrom(stats,plogis)
343375
importFrom(stats,qlogis)
344376
importFrom(stats,qnorm)
345377
importFrom(stats,quantile)

R/cal_utils.R

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
get_last_best_par = function(ad_fun) {
2+
best_par = ad_fun$env$last.par.best
3+
ranef_indices = ad_fun$env$random
4+
fixef_indices = setdiff(seq_along(best_par), ranef_indices)
5+
best_par[fixef_indices] |> rep_name("params")
6+
}

R/calibration.R

Lines changed: 81 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -17,14 +17,6 @@ TMBOptimizer = function(simulator) {
1717
force(arg_mappings)
1818
force(opt_func)
1919
force(opt_method_nm)
20-
## TODO: add package dependencies to assert using assert_dependencies
21-
22-
get_last_best_par = function(ad_fun) {
23-
best_par = ad_fun$env$last.par.best
24-
ranef_indices = ad_fun$env$random
25-
fixef_indices = setdiff(seq_along(best_par), ranef_indices)
26-
best_par[fixef_indices]
27-
}
2820

2921
self[[opt_method_nm]] = function() {
3022

@@ -63,8 +55,7 @@ TMBOptimizer = function(simulator) {
6355
## invalidate the now out-of-date sdreport
6456
self$simulator$cache$sdreport$invalidate()
6557

66-
#ad_fun$fn(opt_obj$par) ## probably this should be last.par.best
67-
ad_fun$fn(get_last_best_par(ad_fun))
58+
self$simulator$objective(get_last_best_par(ad_fun))
6859

6960
opt_obj
7061
}
@@ -78,6 +69,32 @@ TMBOptimizer = function(simulator) {
7869
"nlminb", stats::nlminb
7970
, par = "start", fn = "objective", gr = "gradient", he = "hessian"
8071
)
72+
wrap(
73+
"DEoptim", DEoptim::DEoptim
74+
, fn = "fn"
75+
)
76+
wrap(
77+
"optimize", stats::optimize
78+
, fn = "f"
79+
)
80+
wrap(
81+
"optimise", stats::optimize
82+
, fn = "f"
83+
)
84+
85+
self$extract_best = list(
86+
nlminb = \(obj) obj$par
87+
, optim = \(obj) obj$par
88+
, optimize = \(obj) obj$minimum
89+
, optimise = \(obj) obj$minimum
90+
, DEoptim = \(obj) obj$optim$bestmem
91+
)
92+
self$reset_best = function(opt_obj, optimizer) {
93+
## this is useful to make sure that the names
94+
## of the parameter vector do not get mangled
95+
## by the optimizers
96+
self$simulator$objective(self$extract_best[[optimizer]](opt_obj))
97+
}
8198

8299
return_object(self, "TMBOptimizer")
83100
}
@@ -94,7 +111,8 @@ TMBCurrentParams = function(simulator) { ## TMBSimulator
94111
}
95112
self$params_vector = function() {
96113
if (self$n_params() == 0L) return(numeric())
97-
self$simulator$ad_fun()$env$parList()$params
114+
get_last_best_par(self$simulator$ad_fun())
115+
# $env$parList()$params
98116
}
99117
self$random_vector = function() {
100118
if (self$n_random() == 0L) return(numeric())
@@ -107,6 +125,58 @@ TMBCurrentParams = function(simulator) { ## TMBSimulator
107125
self$random_frame = function() {
108126
self$simulator$tmb_model$random$data_frame(current = self$random_vector())
109127
}
128+
129+
## update a matrix_list, which is a list of numeric matrices (or numeric
130+
## vectors, which will be treated as n-by-1 matrices) so that the current
131+
## fitted values of parameters are used to replace associated values in the
132+
## matrix_list
133+
self$update_matrix_list = function(matrix_list) {
134+
current_frame = rbind(self$params_frame(), self$random_frame())
135+
keepers = current_frame$mat %in% names(matrix_list)
136+
current_frame = current_frame[keepers, , drop = FALSE]
137+
if (nrow(current_frame) == 0L) return(matrix_list)
138+
for (i in seq_len(nrow(current_frame))) {
139+
mat = current_frame[i, "mat"]
140+
mat = mat[]
141+
142+
row = current_frame[i, "row"]
143+
nr = nrow(matrix_list[[mat]])
144+
if (is.null(nr)) nr = length(matrix_list[[mat]])
145+
if (row > nr) {
146+
stop(
147+
sprintf(
148+
"Matrix, %s, only has %s rows, but attempting to update row %s"
149+
, mat, nr, row + 1L
150+
)
151+
)
152+
}
153+
154+
col = current_frame[i, "col"]
155+
nc = ncol(matrix_list[[mat]])
156+
if (is.null(nc)) nc = 1L
157+
if (col > nc) {
158+
stop(
159+
sprintf(
160+
"Matrix, %s, only has %s columns, but attempting to update column %s"
161+
, mat, nc, col + 1L
162+
)
163+
)
164+
}
165+
166+
val = current_frame[i, "current"]
167+
if (is.matrix(matrix_list[[mat]])) {
168+
matrix_list[[mat]][row + 1L, col + 1L] = val
169+
} else {
170+
matrix_list[[mat]][row + 1L] = val
171+
}
172+
} # endfor
173+
return(matrix_list)
174+
}
175+
176+
self$update_params_vector = function(matrix_list) {
177+
178+
}
179+
110180
return_object(self, "OptimizedParams")
111181
}
112182

R/calibrator_arg_constructors.R

Lines changed: 13 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -5,15 +5,15 @@
55
#' Define the prior distributions for parameters and random effects to be
66
#' passed to `par` argument of the \code{\link{mp_tmb_calibrator}} function.
77
#'
8-
#' @param param Named list of distributional specifications for the
8+
#' @param params Named list of distributional specifications for the
99
#' fixed effects.
1010
#' @param random Named list of distributional specifications for the random
1111
#' effects.
1212
#' @concept create-model-calibrator-args
1313
#' @export
14-
mp_par = function(param, random) {
14+
mp_par = function(params, random) {
1515
arg = list()
16-
arg$param = param
16+
arg$params = params
1717
arg$random = random
1818
structure(arg, class = "ParArg")
1919
}
@@ -75,6 +75,14 @@ mp_rbf = function(tv, dimension, initial_weights, seed, prior_sd = 1, fit_prior_
7575
}
7676

7777

78+
mp_piecewise = function(tv, data) {
79+
arg = list()
80+
arg$tv = tv
81+
arg$data = data
82+
structure(arg, class = "PiecewiseArg")
83+
}
84+
85+
7886
## construct objects to pass to the traj argument of mp_tmb_calibrator
7987

8088
#' Trajectory Specification
@@ -90,8 +98,8 @@ mp_rbf = function(tv, dimension, initial_weights, seed, prior_sd = 1, fit_prior_
9098
#' @concept create-model-calibrator-args
9199
#' @export
92100
mp_traj = function(
93-
likelihood = list()
94-
, condensation = list()
101+
likelihood = empty_named_list()
102+
, condensation = empty_named_list()
95103
) {
96104
arg = list()
97105
arg$likelihood = likelihood

0 commit comments

Comments
 (0)