forked from hfu915/dynamice_ph
-
Notifications
You must be signed in to change notification settings - Fork 1
/
utils.R
61 lines (52 loc) · 1.77 KB
/
utils.R
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
# utils.R
# Functions for supporting utilities in the DynaMICE model (Dynamic Measles Immunisation Calculation Engine)
# ------------------------------------------------------------------------------
#' Expand the matrix to a different dimension
#
#' This function returns an expanded contact matrix with the specified age
#' structure for model inputs.
# ------------------------------------------------------------------------------
#' @param A A matrix to be expanded.
#' @param expand_rows Number of times to repeat each row.
#' @param expand_cols Number of times to repeat each column.
#' @param rescale_rows A logical variable to control whether to re-scale the
#' expanded rows.
#' @param rescale_cols A logical variable to control whether to re-scale the
#' expanded columns.
#' @return An expanded matrix with re-scaling if applicable.
#' @examples
#' expandMatrix (matrix(1:9,3,3), 2, 1, FALSE, FALSE)
expandMatrix <- function (A,
expand_rows = 1,
expand_cols = 1,
rescale_rows = F,
rescale_cols = F) {
if(!is.matrix(A)){
stop("A is not a matrix")
}
matvals <- numeric(0)
rows <- nrow(A)
cols <- ncol(A)
for(c in 1:cols) {
matvals <- c(
matvals,
rep(
A[,c],
expand_cols,
each = expand_rows
)
)
}
B <- matrix (matvals,
nrow = rows * expand_rows,
ncol = cols * expand_cols)
if(rescale_rows & rescale_cols){
B <- B/(expand_rows*expand_cols)
} else if(rescale_rows){
B <- B/expand_rows
} else if(rescale_cols){
B <- B/expand_cols
}
return (B)
} # end of function -- expandMatrix
# ------------------------------------------------------------------------------