Skip to content

Commit a7bcc1d

Browse files
author
WesIngwersen
committed
Aligns row/col order for aggregateMatrix.
Fix comparison of dimensions for compareMatrices
1 parent bb13348 commit a7bcc1d

File tree

1 file changed

+25
-11
lines changed

1 file changed

+25
-11
lines changed

R/UtilityFunctions.R

Lines changed: 25 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -24,7 +24,8 @@ joinStringswithSlashes <- function(...) {
2424
}
2525

2626
#' Aggregate matrix from one BEA level to another by rows and columns
27-
#'
27+
#' Uses a matrix formula Xl %*% matrix %*% Xr where Xl is a left crosswalk to
28+
#' aggregate rows and Xr is a right crosswalk to align columns
2829
#' @param matrix A matrix
2930
#' @param from_level The level of BEA code this matrix starts at
3031
#' @param to_level The level of BEA code this matrix will be aggregated to
@@ -36,8 +37,16 @@ aggregateMatrix <- function (matrix, from_level, to_level, model) {
3637
to_code <- paste0("BEA_", to_level)
3738

3839
#Clean up matrix, remove location from row and col names
39-
rownames_before <- rownames(matrix)
40-
colnames_before <- colnames(matrix)
40+
rows <- rownames(matrix)
41+
cols <- colnames(matrix)
42+
43+
# Get vectors of locations for rows and cols
44+
codeLength <- nchar(gsub("/.*", "", rows))
45+
row_locs <- substr(rows,codeLength+1,nchar(rows))
46+
codeLength <- nchar(gsub("/.*", "", cols))
47+
col_locs <- substr(cols,codeLength+1,nchar(cols))
48+
49+
# Remove locations for mapping
4150
rownames(matrix) <- gsub("/.*","",rownames(matrix))
4251
colnames(matrix) <- gsub("/.*","",colnames(matrix))
4352

@@ -47,21 +56,26 @@ aggregateMatrix <- function (matrix, from_level, to_level, model) {
4756

4857
# Build row aggregation matrix
4958
row_cw <- cw[cw[,from_code] %in% rownames(matrix),]
50-
identical(row.names(row_cw),)
51-
52-
# Cast row crosswalk into a correspondence matrix of 1s
59+
60+
# Cast row crosswalk into a correspondence matrix of 1s
5361
Xl <- reshape2::acast(row_cw,as.formula(paste(to_code,"~",from_code)),value.var = "one", fill = 0)
5462

55-
#! Check ordering
56-
Xl <- Xl[,order(rownames(matrix))]
57-
identical(colnames(Xl),rownames(matrix))
63+
# Ordering
64+
Xl <- Xl[,rownames(matrix)]
65+
if(!identical(colnames(Xl),rownames(matrix))) {
66+
stop("Left crosswalk matrix colnames do not match rownames of passed matrix.")
67+
}
5868

5969
# Build col aggregation matrix
6070
col_cw<- cw[cw[,from_code] %in% colnames(matrix),]
6171

6272
# Cast row crosswalk into a correspondence matrix of 1s
6373
Xr <- reshape2::acast(col_cw,as.formula(paste(from_code,"~",to_code)),value.var = "one", fill = 0)
64-
74+
Xr <- Xr[colnames(matrix),]
75+
if(!identical(colnames(matrix), rownames(Xr),)) {
76+
stop("Column names of passed matrix do not match right crosswalk matrix row names.")
77+
}
78+
6579
# Matrix multiply to perform aggregation
6680
matrix_agg <- Xl %*% matrix %*% Xr
6781

@@ -134,7 +148,7 @@ calculateOutputRatio <- function(model, output_type = "Commodity") {
134148
#' @param percentage_diff A logical value indicating whether to compare percentage difference
135149
#' @return A matrix of comparison
136150
compareMatrices <- function(m1, m2, percentage_diff = FALSE) {
137-
if (dim(m1)!=dim(m2)) {
151+
if (!identical(dim(m1),dim(m2))) {
138152
stop("Make m1 and m2 have the same dimensions first.")
139153
}
140154
if (percentage_diff) {

0 commit comments

Comments
 (0)