-
Notifications
You must be signed in to change notification settings - Fork 9
/
Copy pathleontief_matrix_create.R
66 lines (51 loc) · 2.35 KB
/
leontief_matrix_create.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
61
62
63
64
65
66
#' Create a Leontief matrix
#'
#' Create a Leontief matrix from technology matrix after some basic error
#' handling. Most likely you will need this function as a step to invoke
#' the function to create its inverse:
#' \code{\link{leontief_inverse_create}}.
#' @param technology_coefficients_matrix A technology coefficient
#' matrix created by the \code{\link{input_coefficient_matrix_create}} or
#' \code{\link{output_coefficient_matrix_create}}.
#' @importFrom dplyr mutate across
#' @family analytic object functions
#' @return A Leontief matrix of data.frame class. The column names are
#' ordered, and the row names are in the first, auxiliary metadata column.
#' @examples
#' tm <- input_flow_get (
#' data_table = iotable_get(),
#' households = FALSE)
#' L <- leontief_matrix_create( technology_coefficients_matrix = tm )
#' @export
leontief_matrix_create <- function ( technology_coefficients_matrix ) {
key_column <- as.character(unlist (technology_coefficients_matrix[,1]))
key_column
total_row <- which(c("total", 'cpa_total') %in% tolower(key_column))
total_col <- which(c("total", 'cpa_total') %in% tolower(names(technology_coefficients_matrix)))
if ( length(total_row) > 0 ) {
technology_coefficients_matrix <- technology_coefficients_matrix[-total_row,]
}
if ( length(total_col) > 0 ) {
technology_coefficients_matrix <- technology_coefficients_matrix[,-total_col]
}
Tm <- as.matrix (technology_coefficients_matrix[,2:ncol(technology_coefficients_matrix )])
if ( nrow(Tm) != ncol(Tm)) stop("Error: the input matrix is not symmetric.")
IminusA <- diag( nrow(Tm) ) - Tm
if ( sum(vapply(IminusA, function(x) sum(is.nan(x)), numeric(1))) > 0 ) {
warning ("Warning: There are invalid elements in the Leontief-matrix.")
}
Leontief <- cbind(
as.data.frame(technology_coefficients_matrix [,1]),
as.data.frame(IminusA)
)
names ( Leontief)[1] <- names (technology_coefficients_matrix)[1]
Leontief[,1] <- as.character(Leontief[,1])
Leontief
}
#' @rdname leontief_matrix_create
#' @export
leontieff_matrix_create <- function (technology_coefficients_matrix) {
.Deprecated(new = leontief_matrix_create(technology_coefficients_matrix),
msg = "leontieff_matrix_create() is spelled correctly as leontief_matrix_create()")
leontief_matrix_create(technology_coefficients_matrix)
}