diff --git a/R/agents.R b/R/agents.R index 9d2e9ca3..f4b4f8a1 100644 --- a/R/agents.R +++ b/R/agents.R @@ -19,7 +19,6 @@ stopifnot_agent <- function(x) { #' @param d,directed Logical scalar. Whether the graph is directed or not. #' @param p Probability of rewiring. #' @export -#' @aliases agents #' @return #' - The 'agents_smallworld' function returns a model with the agents #' loaded. diff --git a/R/cpp11.R b/R/cpp11.R index fc985e2a..bba147dd 100644 --- a/R/cpp11.R +++ b/R/cpp11.R @@ -416,8 +416,8 @@ print_agent_tools_cpp <- function(tools) { .Call(`_epiworldR_print_agent_tools_cpp`, tools) } -set_distribution_tool_cpp <- function(tool, model, tfun) { - .Call(`_epiworldR_set_distribution_tool_cpp`, tool, model, tfun) +set_distribution_tool_cpp <- function(tool, distfun) { + .Call(`_epiworldR_set_distribution_tool_cpp`, tool, distfun) } distribute_tool_randomly_cpp <- function(prevalence, as_proportion) { diff --git a/R/entity.R b/R/entity.R index 61b2bb0b..b2d0f7eb 100644 --- a/R/entity.R +++ b/R/entity.R @@ -5,9 +5,9 @@ stopifnot_entity <- function(entity) { } } -stopifnot_entity_distfun <- function(dist_fun) { - if (!inherits(dist_fun, "epiworld_distribution_entity")) { - stop("Argument 'dist_fun' must be a distribution function.") +stopifnot_entity_distfun <- function(distfun) { + if (!inherits(distfun, "epiworld_distribution_entity")) { + stop("Argument 'distfun' must be a distribution function.") } } @@ -92,6 +92,8 @@ print.epiworld_entities <- function(x, ...) { #' @param prevalence Numeric scalar. Prevalence of the entity. #' @param as_proportion Logical scalar. If `TRUE`, `prevalence` is interpreted #' as a proportion. +#' @param to_unassigned Logical scalar. If `TRUE`, the entity is added to the +#' unassigned pool. #' @return #' - The function `entity` creates an entity object. #' @rdname entities @@ -280,14 +282,14 @@ distribute_entity_to_set <- function( #' @export #' @rdname entities -#' @param dist_fun Distribution function object of class `epiworld_distribution_entity`. +#' @param distfun Distribution function object of class `epiworld_distribution_entity`. set_distribution_entity <- function( entity, - dist_fun + distfun ) { stopifnot_entity(entity) - stopifnot_entity_distfun(dist_fun) + stopifnot_entity_distfun(distfun) set_distribution_entity_cpp(entity, distribution) invisible(entity) diff --git a/R/functions-renamed.R b/R/functions-renamed.R index 466aff13..61556bed 100644 --- a/R/functions-renamed.R +++ b/R/functions-renamed.R @@ -10,15 +10,21 @@ #' @name epiworldR-deprecated NULL -#' @param n Deprecated. Either set the prevalence during the tool/virus -#' initialization or use `set_prevalence_tool`/`set_prevalence_virus`. +#' @param n Deprecated. #' @export #' @rdname epiworldR-deprecated add_tool_n <- function(model, tool, n) { .Deprecated(new = "add_tool") - set_prevalence_tool(tool, n, as_proportion = FALSE) + set_distribution_tool( + tool, + distfun = distribute_tool_randomly( + prevalence = n, + as_proportion = TRUE + ) + ) + add_tool(model, tool) } @@ -28,7 +34,15 @@ add_tool_n <- function(model, tool, n) { add_virus_n <- function(model, virus, n) { .Deprecated(new = "add_virus") - set_prevalence_virus(virus, n, as_proportion = FALSE) + + set_distribution_virus( + virus = virus, + distfun = distribute_virus_randomly( + prevalence = n, + as_proportion = TRUE + ) + ) + add_virus(model, virus) } \ No newline at end of file diff --git a/R/tool.R b/R/tool.R index c44b5334..3cf628ad 100644 --- a/R/tool.R +++ b/R/tool.R @@ -47,7 +47,8 @@ #' #' # To declare a certain number of individuals with the tool #' rm_tool(model_sirconn, 0) # Removing epitool from the model -#' set_prevalence_tool(epitool, 0.1, TRUE) # Setting prevalence to 0.1 +#' # Setting prevalence to 0.1 +#' set_distribution_tool(epitool, distribute_tool_randomly(0.1, TRUE)) #' add_tool(model_sirconn, epitool) #' run(model_sirconn, ndays = 100, seed = 1912) #' @@ -152,8 +153,7 @@ get_name_tool <- function(tool) { #' @export #' @param tool An object of class `epiworld_tool` -#' @param proportion Deprecated. Either set the prevalence during the tool -#' initialization or use `set_prevalence_tool`. +#' @param proportion Deprecated. #' @details #' The `add_tool` function adds the specified tool to the model of class #' [epiworld_model] with specified proportion. @@ -164,10 +164,13 @@ add_tool <- function(model, tool, proportion) { warning( "The 'proportion' argument is deprecated. ", - "Use 'set_prevalence_tool' instead." + "Use 'set_distribution_tool' instead." ) - set_prevalence_tool(tool, proportion, TRUE) + set_distribution_tool( + tool, + distribute_tool_randomly(proportion, TRUE) + ) } @@ -502,12 +505,13 @@ print.epiworld_agents_tools <- function(x, max_print = 10, ...) { #' specified tool of class [epiworld_tool]. The distribution function can be #' created using the functions [distribute_tool_randomly()] and #' [distribute_tool_to_set()]. +#' @param distfun An object of class `epiworld_tool_distfun`. #' @rdname tool set_distribution_tool <- function(tool, distfun) { stopifnot_tool(tool) stopifnot_tool_distfun(distfun) - invisible(set_distribution_tool_cpp(tool, distfun)) + invisible(set_distribution_tool_cpp(tool = tool, distfun = distfun)) } @@ -516,6 +520,9 @@ set_distribution_tool <- function(tool, distfun) { #' @details #' The `distribute_tool_randomly` function creates a distribution function that #' randomly assigns the tool to a proportion of the population. +#' @param as_proportion Logical scalar. If `TRUE`, `prevalence` is interpreted +#' as a proportion of the total number of agents in the model. +#' @param prevalence Numeric scalar. Prevalence of the tool. #' @return #' - The `distribute_tool_randomly` function returns a distribution function of #' class `epiworld_tool_distfun`. diff --git a/R/virus.R b/R/virus.R index 396fda77..51b2d3ea 100644 --- a/R/virus.R +++ b/R/virus.R @@ -43,7 +43,7 @@ #' mseirconn #' #' rm_virus(mseirconn, 0) # Removing the first virus from the model object -#' set_prevalence_virus(delta, 100, as_proportion = FALSE) +#' set_distribution_virus(delta, distribute_virus_randomly(100, as_proportion = FALSE)) #' add_virus(mseirconn, delta) #' #' # Setting parameters for the delta virus manually @@ -154,8 +154,7 @@ get_name_virus <- function(virus) { #' @rdname virus #' @param model An object of class `epiworld_model`. #' @param virus An object of class `epiworld_virus` -#' @param proportion Deprecated. Either set the prevalence during the virus -#' initialization or use `set_prevalence_virus`. +#' @param proportion Deprecated. #' @returns #' - The `add_virus` function does not return a value, instead it adds the #' virus of choice to the model object of class [epiworld_model]. @@ -168,7 +167,10 @@ add_virus <- function(model, virus, proportion) { "the next version." ) - set_prevalence_virus(virus, proportion, as_proportion = TRUE) + set_distribution_virus( + virus=virus, + distfun=distribute_virus_randomly(proportion, as_proportion = TRUE) + ) } @@ -521,12 +523,12 @@ set_incubation_fun <- function(virus, model, vfun) { #' @export #' @rdname virus -#' @param dist_fun An object of class `epiworld_distribution_virus`. -set_distribution_virus <- function(virus, dist_fun) { +#' @param distfun An object of class `epiworld_distribution_virus`. +set_distribution_virus <- function(virus, distfun) { stopifnot_virus(virus) - stopifnot_virus_distfun(dist_fun) - invisible(set_distribution_virus_cpp(virus, dist_fun)) + stopifnot_virus_distfun(distfun) + invisible(set_distribution_virus_cpp(virus, distfun)) } @@ -552,20 +554,20 @@ distribute_virus_randomly <- function( as.double(prevalence), as.logical(as_proportion) ), - class = "epiworld_distribution_virus" + class = "epiworld_virus_distfun" ) } #' @export #' @rdname virus -#' @param agents_id Integer vector. Indices of the agents that will receive the +#' @param agents_ids Integer vector. Indices of the agents that will receive the #' virus. distribute_virus_set <- function(agents_ids) { structure( distribute_virus_set_cpp(as.vector(agents_id)), - class = "epiworld_distribution_virus" + class = "epiworld_virus_distfun" ) } \ No newline at end of file diff --git a/inst/tinytest/test-mixing.R b/inst/tinytest/test-mixing.R index 05899f30..172097cc 100644 --- a/inst/tinytest/test-mixing.R +++ b/inst/tinytest/test-mixing.R @@ -26,9 +26,9 @@ flu_model <- ModelSEIRMixing( # Adding the entities flu_model |> - add_entity_n(e1) |> - add_entity_n(e2) |> - add_entity_n(e3) + add_entity(e1) |> + add_entity(e2) |> + add_entity(e3) run(flu_model, ndays = 100, seed = 1233) summary(flu_model) diff --git a/man/agents_smallworld.Rd b/man/agents_smallworld.Rd index afa19178..36717930 100644 --- a/man/agents_smallworld.Rd +++ b/man/agents_smallworld.Rd @@ -2,7 +2,6 @@ % Please edit documentation in R/agents.R, R/tool.R \name{agents_smallworld} \alias{agents_smallworld} -\alias{agents} \alias{agents_from_edgelist} \alias{get_network} \alias{network} diff --git a/man/entities.Rd b/man/entities.Rd index 92a243d8..95eaed3a 100644 --- a/man/entities.Rd +++ b/man/entities.Rd @@ -44,7 +44,7 @@ distribute_entity_randomly(prevalence, as_proportion, to_unassigned = TRUE) distribute_entity_to_set(agents_ids) -set_distribution_entity(entity, dist_fun) +set_distribution_entity(entity, distfun) } \arguments{ \item{model}{Model object of class \code{epiworld_model}.} @@ -60,6 +60,9 @@ set_distribution_entity(entity, dist_fun) \item{as_proportion}{Logical scalar. If \code{TRUE}, \code{prevalence} is interpreted as a proportion.} +\item{to_unassigned}{Logical scalar. If \code{TRUE}, the entity is added to the +unassigned pool.} + \item{entity}{Entity object of class \code{epiworld_entity}.} \item{agent}{Agent object of class \code{epiworld_agent}.} @@ -74,7 +77,7 @@ as a proportion.} \item{agents_ids}{Integer vector. Ids of the agents to distribute.} -\item{dist_fun}{Distribution function object of class \code{epiworld_distribution_entity}.} +\item{distfun}{Distribution function object of class \code{epiworld_distribution_entity}.} } \value{ \itemize{ diff --git a/man/epiworldR-deprecated.Rd b/man/epiworldR-deprecated.Rd index 82ef7ec5..e58dff4c 100644 --- a/man/epiworldR-deprecated.Rd +++ b/man/epiworldR-deprecated.Rd @@ -27,8 +27,7 @@ globalaction_fun(...) \item{tool}{Tool object of class \code{epiworld_tool}.} -\item{n}{Deprecated. Either set the prevalence during the tool/virus -initialization or use \code{set_prevalence_tool}/\code{set_prevalence_virus}.} +\item{n}{Deprecated.} \item{virus}{Virus object of class \code{epiworld_virus}.} diff --git a/man/tool.Rd b/man/tool.Rd index d27ac4cb..f5beaa0c 100644 --- a/man/tool.Rd +++ b/man/tool.Rd @@ -81,6 +81,11 @@ distribute_tool_to_set(agents_ids) \arguments{ \item{name}{Name of the tool} +\item{prevalence}{Numeric scalar. Prevalence of the tool.} + +\item{as_proportion}{Logical scalar. If \code{TRUE}, \code{prevalence} is interpreted +as a proportion of the total number of agents in the model.} + \item{susceptibility_reduction}{Numeric. Proportion it reduces susceptibility.} \item{transmission_reduction}{Numeric. Proportion it reduces transmission.} @@ -93,8 +98,7 @@ distribute_tool_to_set(agents_ids) \item{model}{Model} -\item{proportion}{Deprecated. Either set the prevalence during the tool -initialization or use \code{set_prevalence_tool}.} +\item{proportion}{Deprecated.} \item{tool_pos}{Positive integer. Index of the tool's position in the model.} @@ -117,6 +121,8 @@ will be added to the tool (see details).} \item{...}{Currently ignored.} +\item{distfun}{An object of class \code{epiworld_tool_distfun}.} + \item{agents_ids}{Integer vector. Indices of the agents to which the tool will be assigned.} } @@ -234,7 +240,8 @@ plot(model_sirconn) # To declare a certain number of individuals with the tool rm_tool(model_sirconn, 0) # Removing epitool from the model -set_prevalence_tool(epitool, 0.1, TRUE) # Setting prevalence to 0.1 +# Setting prevalence to 0.1 +set_distribution_tool(epitool, distribute_tool_randomly(0.1, TRUE)) add_tool(model_sirconn, epitool) run(model_sirconn, ndays = 100, seed = 1912) diff --git a/man/virus.Rd b/man/virus.Rd index 01c9c588..b911a4ef 100644 --- a/man/virus.Rd +++ b/man/virus.Rd @@ -73,7 +73,7 @@ set_incubation_ptr(virus, model, param) set_incubation_fun(virus, model, vfun) -set_distribution_virus(virus, dist_fun) +set_distribution_virus(virus, distfun) distribute_virus_randomly(prevalence, as_proportion) @@ -101,8 +101,7 @@ proportion of the total number of agents in the model.} \item{model}{An object of class \code{epiworld_model}.} -\item{proportion}{Deprecated. Either set the prevalence during the virus -initialization or use \code{set_prevalence_virus}.} +\item{proportion}{Deprecated.} \item{init, end, removed}{states after acquiring a virus, removing a virus, and removing the agent as a result of the virus, respectively.} @@ -122,9 +121,9 @@ will be added to the virus (see details).} \item{vfun}{An object of class \code{epiworld_virus_fun}.} -\item{dist_fun}{An object of class \code{epiworld_distribution_virus}.} +\item{distfun}{An object of class \code{epiworld_distribution_virus}.} -\item{agents_id}{Integer vector. Indices of the agents that will receive the +\item{agents_ids}{Integer vector. Indices of the agents that will receive the virus.} } \value{ @@ -231,7 +230,7 @@ run(mseirconn, ndays = 100, seed = 992) mseirconn rm_virus(mseirconn, 0) # Removing the first virus from the model object -set_prevalence_virus(delta, 100, as_proportion = FALSE) +set_distribution_virus(delta, distribute_virus_randomly(100, as_proportion = FALSE)) add_virus(mseirconn, delta) # Setting parameters for the delta virus manually diff --git a/playground/.gitignore b/playground/.gitignore index 2e1261ba..ece2f076 100755 --- a/playground/.gitignore +++ b/playground/.gitignore @@ -1,2 +1,4 @@ *_files/ *_cache/ + +/.quarto/ diff --git a/src/cpp11.cpp b/src/cpp11.cpp index 5e36863a..a0cf4843 100644 --- a/src/cpp11.cpp +++ b/src/cpp11.cpp @@ -734,10 +734,10 @@ extern "C" SEXP _epiworldR_print_agent_tools_cpp(SEXP tools) { END_CPP11 } // tool.cpp -SEXP set_distribution_tool_cpp(SEXP tool, SEXP model, SEXP tfun); -extern "C" SEXP _epiworldR_set_distribution_tool_cpp(SEXP tool, SEXP model, SEXP tfun) { +SEXP set_distribution_tool_cpp(SEXP tool, SEXP distfun); +extern "C" SEXP _epiworldR_set_distribution_tool_cpp(SEXP tool, SEXP distfun) { BEGIN_CPP11 - return cpp11::as_sexp(set_distribution_tool_cpp(cpp11::as_cpp>(tool), cpp11::as_cpp>(model), cpp11::as_cpp>(tfun))); + return cpp11::as_sexp(set_distribution_tool_cpp(cpp11::as_cpp>(tool), cpp11::as_cpp>(distfun))); END_CPP11 } // tool.cpp @@ -1013,7 +1013,7 @@ static const R_CallMethodDef CallEntries[] = { {"_epiworldR_set_death_reduction_fun_cpp", (DL_FUNC) &_epiworldR_set_death_reduction_fun_cpp, 3}, {"_epiworldR_set_death_reduction_ptr_cpp", (DL_FUNC) &_epiworldR_set_death_reduction_ptr_cpp, 3}, {"_epiworldR_set_distribution_entity_cpp", (DL_FUNC) &_epiworldR_set_distribution_entity_cpp, 2}, - {"_epiworldR_set_distribution_tool_cpp", (DL_FUNC) &_epiworldR_set_distribution_tool_cpp, 3}, + {"_epiworldR_set_distribution_tool_cpp", (DL_FUNC) &_epiworldR_set_distribution_tool_cpp, 2}, {"_epiworldR_set_distribution_virus_cpp", (DL_FUNC) &_epiworldR_set_distribution_virus_cpp, 2}, {"_epiworldR_set_incubation_cpp", (DL_FUNC) &_epiworldR_set_incubation_cpp, 2}, {"_epiworldR_set_incubation_fun_cpp", (DL_FUNC) &_epiworldR_set_incubation_fun_cpp, 3}, diff --git a/src/tool.cpp b/src/tool.cpp index 061867d8..d39f8ab6 100644 --- a/src/tool.cpp +++ b/src/tool.cpp @@ -284,13 +284,11 @@ SEXP print_agent_tools_cpp(SEXP tools) { [[cpp11::register]] SEXP set_distribution_tool_cpp( SEXP tool, - SEXP model, - SEXP tfun + SEXP distfun ) { WrapTool(toolptr)(tool); - external_pointer> mptr(model); - external_pointer> tfunptr(tfun); + external_pointer> tfunptr(distfun); toolptr->set_distribution(*tfunptr);