Skip to content

Commit

Permalink
Merge pull request #184 from mrc-ide/bug/metapop_rowsums
Browse files Browse the repository at this point in the history
modifying mixing matrix warnings
  • Loading branch information
giovannic authored Aug 1, 2022
2 parents d78bf13 + c3bc8f2 commit d3a6131
Show file tree
Hide file tree
Showing 2 changed files with 6 additions and 5 deletions.
9 changes: 5 additions & 4 deletions R/model.R
Original file line number Diff line number Diff line change
Expand Up @@ -130,7 +130,8 @@ run_simulation <- function(
#' @param correlations a list of correlation parameters for each population
#' (default: NULL)
#' @param mixing matrix of mixing coefficients for infectivity towards
#' mosquitoes. Each element must be between 0 and 1 and all rows and columns must sum to 1.
#' mosquitoes. Rows = origin sites, columns = destinations. Each element must
#' be between 0 and 1 and all rows must sum to 1.
#' @return a list of dataframe of results
#' @export
run_metapop_simulation <- function(
Expand All @@ -146,11 +147,11 @@ run_metapop_simulation <- function(
if (nrow(mixing) != length(parameters)) {
stop('mixing matrix rows must match length of parameters')
}
if (!all(round(rowSums(mixing), 1) == 1)) {
if (!all(vlapply(seq_along(parameters), function(x) approx_sum(mixing[x,], 1)))) {
stop('all mixing matrix rows must sum to 1')
}
if (!all(round(colSums(mixing), 1) == 1)) {
stop('all mixing matrix columns must sum to 1')
if (!all(vlapply(seq_along(parameters), function(x) approx_sum(mixing[,x], 1)))) {
warning('mixing matrix is asymmetrical')
}
if (is.null(correlations)) {
correlations <- lapply(parameters, get_correlation_parameters)
Expand Down
2 changes: 1 addition & 1 deletion vignettes/Metapopulation.Rmd
Original file line number Diff line number Diff line change
Expand Up @@ -70,7 +70,7 @@ mix_2 <- matrix(c(0.8, 0.1, 0.1,
nrow = 3, ncol = 3)
# perfectly-mixed
mix_3 <- matrix(rep(.33, 9), nrow = 3, ncol = 3)
mix_3 <- matrix(rep(1/3, 9), nrow = 3, ncol = 3)
# run model
set.seed(123)
Expand Down

0 comments on commit d3a6131

Please sign in to comment.