Skip to content

Commit 0a16d3d

Browse files
fix #350
1 parent b62b02c commit 0a16d3d

10 files changed

Lines changed: 68 additions & 21 deletions

File tree

DESCRIPTION

Lines changed: 1 addition & 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: 3.2.2
3+
Version: 3.3.0
44
Authors@R: c(
55
person("Steve Walker", email="swalk@mcmaster.ca", role=c("cre", "aut")),
66
person("Weiguang Guan", role="aut"),

R/fit_methods.R

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -113,7 +113,7 @@ mp_tmb_coef.TMBSimulator = function(model, back_transform = TRUE, ...) {
113113
}
114114
tab = bind_rows(tab)
115115
}
116-
tab
116+
rm_no_info_coef_cols(tab)
117117
}
118118

119119
#' @export

R/formula_list_generators.R

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1157,4 +1157,8 @@ to_change_component.formula = function(x) Formula(x)
11571157
mp_reduce = function(model) UseMethod("mp_reduce")
11581158

11591159
#' @export
1160-
mp_reduce.TMBModelSpec = function(model) model$expand()
1160+
mp_reduce.TMBModelSpec = function(model) {
1161+
msg =
1162+
warning("mp_reduce() is deprecated. Please use mp_expand() instead.")
1163+
model$expand()
1164+
}

R/lists.R

Lines changed: 44 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -93,22 +93,55 @@ melt_default_matrix_list = function(x, zeros_are_blank = TRUE, simplify_as_scala
9393
|> lapply(melt_matrix, zeros_are_blank)
9494
|> bind_rows(.id = "matrix")
9595
)
96-
if (simplify_as_scalars) {
97-
rm_rs = all(f$row == "")
98-
rm_cs = all(f$col == "")
99-
if (rm_rs) f$row = NULL
100-
if (rm_cs) f$col = NULL
101-
if (rm_rs & rm_cs) {
102-
nms = colnames(f)
103-
mat_col = nms == "matrix"
104-
if (any(mat_col)) names(f)[mat_col] = "quantity"
105-
}
106-
}
96+
if (simplify_as_scalars) f = rm_no_info_traj_cols(f)
10797

10898
rownames(f) = NULL
10999
f
110100
}
111101

102+
rm_no_info_traj_cols = function(x
103+
, matrix_col_name = "quantity"
104+
, collapse_traj = getOption("macpan2_collapse_traj")
105+
) {
106+
107+
if (!collapse_traj) return(x)
108+
109+
## check if time is zero-info too? harder to know what
110+
## the implied value should be. argument for developers
111+
## to control context dependence of this implied value?
112+
113+
urs = unique(x$row)
114+
ucs = unique(x$col)
115+
rm_rs = identical(urs, "") | identical(as.integer(urs), 0L)
116+
rm_cs = identical(ucs, "") | identical(as.integer(ucs), 0L)
117+
if (rm_rs) x$row = NULL
118+
if (rm_cs) x$col = NULL
119+
if (rm_rs & rm_cs) {
120+
nms = colnames(x)
121+
mat_col = nms == "matrix"
122+
if (any(mat_col)) names(x)[mat_col] = matrix_col_name
123+
}
124+
return(x)
125+
}
126+
127+
rm_no_info_coef_cols = function(x
128+
, matrix_col_name = "par"
129+
, collapse_coef = getOption("macpan2_collapse_coef")
130+
) {
131+
if (!collapse_coef) return(x)
132+
x = rm_no_info_traj_cols(x)
133+
x$term = NULL
134+
if (nrow(x) == 1L) {
135+
if (x$type == "fixed") x$type = NULL
136+
return(x)
137+
}
138+
cols_to_keep = vapply(x
139+
, \(col) is.numeric(col) | (length(unique(col)) > 1L)
140+
, logical(1L)
141+
)
142+
return(x[ , cols_to_keep, drop = FALSE])
143+
}
144+
112145
melt_list_of_char_vecs = function(x) {
113146
n = vapply(x, length, integer(1L))
114147
list(

R/mp_tmb_calibrator.R

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -495,7 +495,9 @@ TMBCalDataStruc = function(data, time) {
495495
, "time_step", "timeStep", "TimeStep"
496496
)
497497
, matrix = c(
498-
"matrix", "Matrix", "mat", "Mat", "variable", "var", "Variable", "Var"
498+
"matrix", "Matrix", "mat", "Mat"
499+
, "variable", "var", "Variable", "Var"
500+
, "quantity", "Quantity"
499501
)
500502
, row = c("row", "Row")
501503
, col = c("col", "Col", "column", "Column")

R/tmb_model.R

Lines changed: 8 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -549,7 +549,8 @@ mp_trajectory.TMBSimulator = function(model, include_initial = FALSE) {
549549
macro = getOption("macpan2_traj_tmb_macro") |> as.character()
550550
if (length(macro) > 1L) macro = macro[[1L]]
551551
if (length(macro) < 1L) macro = "simulate"
552-
model[[macro]](.phases = phases) |> reset_rownames()
552+
traj = model[[macro]](.phases = phases) |> reset_rownames()
553+
rm_no_info_traj_cols(traj, "matrix")
553554
}
554555

555556
#' @export
@@ -597,7 +598,8 @@ trajectory_par_util = function(simulator
597598
) {
598599
phases = trajectory_phases_util(include_initial, include_final)
599600
vector = trajectory_vec_util(simulator, parameter_updates, value_column_name)
600-
simulator$simulate(vector, .phases = phases)
601+
traj = simulator$simulate(vector, .phases = phases)
602+
rm_no_info_traj_cols(traj, "matrix")
601603
}
602604

603605
trajectory_rep_util = function(n, simulator
@@ -764,7 +766,7 @@ mp_trajectory_sd.TMBSimulator = function(model
764766
vars = intersect(c("value", "conf.low", "conf.high"), names(r))
765767
r = backtrans(r, vars, "matrix", "sd", "value")
766768
}
767-
r
769+
rm_no_info_traj_cols(r, "matrix")
768770
}
769771

770772
#' @export
@@ -780,7 +782,7 @@ mp_trajectory_sd.TMBCalibrator = function(model
780782
, back_transform
781783
)
782784
traj$time = model$time_steps_obj$internal_to_external(traj$time)
783-
return(traj)
785+
rm_no_info_traj_cols(traj, "matrix")
784786
}
785787

786788
#' @export
@@ -790,7 +792,7 @@ mp_trajectory_ensemble.TMBSimulator = function(model
790792
) {
791793
best_pars = get_last_best_par(model$ad_fun())
792794
traj = model$report_ensemble(best_pars, .n = n, .probs = probs)
793-
return(traj)
795+
rm_no_info_traj_cols(traj, "matrix")
794796
}
795797

796798
#' @export
@@ -876,7 +878,7 @@ mp_trajectory_sim.TMBSimulator = function(model
876878
, n
877879
, probs = c(0.025, 0.25, 0.5, 0.75, 0.975)
878880
) {
879-
r = model$simulate()
881+
r = model$simulate() |> rm_no_info_traj_cols("matrix")
880882
r = r[, names(r) != "value", drop = FALSE]
881883
rr = (n
882884
|> replicate(model$simulate_values())

R/zzz.R

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,8 @@
1010
options(
1111
macpan2_dll = "macpan2"
1212
, macpan2_verbose = FALSE
13+
, macpan2_collapse_traj = FALSE
14+
, macpan2_collapse_coef = FALSE
1315
, macpan2_default_loss = c("clamped_poisson", "poisson", "sum_of_squares", "neg_bin")
1416
, macpan2_tmb_type = NULL
1517
, macpan2_tmb_check = TRUE
-27.2 KB
Loading

vignettes/options.Rmd

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,8 @@ knitr::opts_chunk$set(
2323
## Potentially Useful
2424

2525
* `macpan2_verbose = FALSE` : Should the [TMB](https://github.com/kaskr/adcomp) computational engine used by `macpan2` be verbose or not (default). In particular, the `silent` argument of `TMB::MakeADFun` gets `!getOption("macpan2_verbose")`.
26+
* `macpan2_collapse_traj = FALSE` : Should `row` and `col` columns in data frames returned by `mp_trajectory_*()` functions be removed if they provide no information (e.g., if all variables in the trajectory are scalars).
27+
* `macpan2_collapse_coef = FALSE` : Should `row` and `col` columns in data frames returned by `mp_*_coef()` functions be removed if they provide no information (e.g., if all variables in the trajectory are scalars).
2628
* `macpan2_session_name = "default"` and `macpan2_log_dir = tools::R_user_dir("macpan2")` : Where to put log files generated by the engine. By default, the path to log files will be `{macpan2_log_dir}/.macpan2/{macpan2_session_name}/log.txt`. If you want to get access to the log file you should set the value of `macpan2_log_dir`. If you want to set `macpan2_log_dir` to the working directory at the time the model is created, you should set `macpan2_log_dir = ""`.
2729
* `macpan2_tol_hazard_div = 1e-8` : Used by the `mp_hazard()` function to generate an expression that calls the `proportions()` engine function. This option becomes the third tolerance argument to `proportions()`.
2830
* `macpan2_tol_singular_cov = `1e-6` : Used to determine if estimated

vignettes/quickstart.Rmd

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -111,6 +111,8 @@ The simulation results are output as a data frame with the following columns:
111111
- `row`, `col`: Placeholders for variable components in more complicated structured models (not covered in this article, but useful for example when S and I in different age groups or geographic locations are tracked separately).
112112
- `value`: The simulated value for a particular state and time step.
113113

114+
(Note that the `row` and `col` placeholders can be completely omitted by using `option(macpan2_collapse_traj = TRUE)` near the start of your script that calls `mp_trajectory()`)
115+
114116
## Processing Results
115117

116118
`macpan2` does not provide any data manipulation or plotting tools (although there are a few in `macpan2helpers`). The philosophy is to focus on the engine and modelling interface, but to provide outputs in formats that are easy to use with other data processing packages, like `ggplot2`, `dplyr`, and `tidyr`, all of which readily make use of data in long format.

0 commit comments

Comments
 (0)