@@ -24,7 +24,8 @@ joinStringswithSlashes <- function(...) {
24
24
}
25
25
26
26
# ' 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
28
29
# ' @param matrix A matrix
29
30
# ' @param from_level The level of BEA code this matrix starts at
30
31
# ' @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) {
36
37
to_code <- paste0(" BEA_" , to_level )
37
38
38
39
# 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
41
50
rownames(matrix ) <- gsub(" /.*" ," " ,rownames(matrix ))
42
51
colnames(matrix ) <- gsub(" /.*" ," " ,colnames(matrix ))
43
52
@@ -47,21 +56,26 @@ aggregateMatrix <- function (matrix, from_level, to_level, model) {
47
56
48
57
# Build row aggregation matrix
49
58
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
53
61
Xl <- reshape2 :: acast(row_cw ,as.formula(paste(to_code ," ~" ,from_code )),value.var = " one" , fill = 0 )
54
62
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
+ }
58
68
59
69
# Build col aggregation matrix
60
70
col_cw <- cw [cw [,from_code ] %in% colnames(matrix ),]
61
71
62
72
# Cast row crosswalk into a correspondence matrix of 1s
63
73
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
+
65
79
# Matrix multiply to perform aggregation
66
80
matrix_agg <- Xl %*% matrix %*% Xr
67
81
@@ -134,7 +148,7 @@ calculateOutputRatio <- function(model, output_type = "Commodity") {
134
148
# ' @param percentage_diff A logical value indicating whether to compare percentage difference
135
149
# ' @return A matrix of comparison
136
150
compareMatrices <- function (m1 , m2 , percentage_diff = FALSE ) {
137
- if (dim(m1 )!= dim(m2 )) {
151
+ if (! identical( dim(m1 ), dim(m2 ) )) {
138
152
stop(" Make m1 and m2 have the same dimensions first." )
139
153
}
140
154
if (percentage_diff ) {
0 commit comments