Skip to content

Commit

Permalink
docs and fix #133
Browse files Browse the repository at this point in the history
  • Loading branch information
stevencarlislewalker committed Nov 15, 2023
1 parent 895beb3 commit 72776ec
Show file tree
Hide file tree
Showing 13 changed files with 230 additions and 88 deletions.
1 change: 1 addition & 0 deletions NAMESPACE
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ S3method(Index,Index)
S3method(Index,Partition)
S3method(Index,data.frame)
S3method(Vector,Index)
S3method(Vector,Vector)
S3method(Vector,data.frame)
S3method(Vector,numeric)
S3method(as.data.frame,Index)
Expand Down
16 changes: 16 additions & 0 deletions R/index_to_tmb.R
Original file line number Diff line number Diff line change
@@ -1,3 +1,13 @@
#' TMB Simulator from Dynamic Model
#'
#' @param dynamic_model Object product by \code{\link{dynamic_model}}.
#' @param vectors Named list of named vectors as initial values for the
#' simulations that are referenced in the expression list in the dynamic model.
#' @param unstruc_mats = Named list of objects that can be coerced to
#' numerical matrices that are used in the expression list of the
#' dynamic model.
#' @inheritParams TMBModel
#'
#' @importFrom oor method_apply
#' @export
mp_tmb_simulator = function(dynamic_model
Expand Down Expand Up @@ -46,6 +56,12 @@ mp_tmb_simulator.DynamicModel = function(dynamic_model
indexed_mats = dynamic_model$init_vecs
mats_to_save = names(indexed_mats)
} else {
for (v in names(vectors)) {
vectors[[v]] = Vector(
vectors[[v]],
dynamic_model$init_vecs[[v]]$index
)
}
indexed_mats = lapply(vectors, as.matrix)
}
if (is.null(unstruc_mats)) {
Expand Down
4 changes: 4 additions & 0 deletions R/model_def_run.R
Original file line number Diff line number Diff line change
Expand Up @@ -111,6 +111,10 @@ DynamicModel = function(expr_list = ExprList(), ledgers = list(), init_vecs = li
}


#' Dynamic Model
#'
#'
#'
#' @export
mp_dynamic_model = DynamicModel

Expand Down
18 changes: 13 additions & 5 deletions R/vector.R
Original file line number Diff line number Diff line change
Expand Up @@ -58,13 +58,19 @@ Vector.numeric = function(x, index, ...) {
do.call(v$set_numbers, args)
}

#' @export
Vector.Vector = function(x, index, ...) {
Vector(x$numbers(), index, ...)
}

#' @export
Vector.Index = function(x, ...) {
self = Base()
self$index = x
self$.numbers = zero_vector(self$index$labels())
self$numbers = function(...) {
l = list(...)
## check if all numbers
if (length(l) == 0L) return(self$.numbers)
i = mp_subset(self$index, ...)$labels()
self$.numbers[i]
Expand Down Expand Up @@ -131,15 +137,17 @@ print.Vector = function(x, ...) print(x$numbers())
names.Vector = function(x) x$numbers() |> names()

#' @export
as.matrix.Vector = function(x, ...) x$numbers() |> as.matrix()
as.matrix.Vector = function(x, ...) {
x$numbers() |> as.matrix()
}

zero_vector = function(labels) setNames(rep(0, length(labels)), labels)

#' Stub
#'
#'
#' This documentation was originally in [mp_index()] and should be cleaned up
#' See issue #131
#'
#'
#' #' These labels can be used to create 'multidimensional' names for the elements
#' of vectors. Here is the above example expressed in vector form.
#' ```{r, echo = FALSE}
Expand All @@ -161,7 +169,7 @@ zero_vector = function(labels) setNames(rep(0, length(labels)), labels)
#' ```{r, echo = FALSE}
#' mp_vector(symp)$set_numbers(Epi = c(S = 1000))$set_numbers(Epi = c(I = 1), Symptoms = "severe")
#' ```
#'
#'
#' @examples
#' state = mp_index(
#' Epi = c("S", "I", "S", "I"),
Expand All @@ -173,7 +181,7 @@ zero_vector = function(labels) setNames(rep(0, length(labels)), labels)
#' |> mp_set_numbers(Epi = c(I = 1), Age = "old")
#' )
#' print(state_vector)
#'
#'
#' @export
mp_vector = function(x, ...) UseMethod("mp_vector")

Expand Down
11 changes: 3 additions & 8 deletions man/LedgerDefinition.Rd

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

4 changes: 2 additions & 2 deletions man/mp_aggregate.Rd

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

16 changes: 16 additions & 0 deletions man/mp_dynamic_model.Rd

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

137 changes: 74 additions & 63 deletions man/mp_index.Rd

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

3 changes: 1 addition & 2 deletions man/mp_join.Rd

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

9 changes: 4 additions & 5 deletions man/mp_ledgers.Rd

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Loading

0 comments on commit 72776ec

Please sign in to comment.