-
Notifications
You must be signed in to change notification settings - Fork 9
/
Copy pathleontief_inverse_create.R
64 lines (52 loc) · 2.3 KB
/
leontief_inverse_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
#' @title Create the inverse of a Leontief-matrix
#'
#' @description Create the Leontief inverse from the technology coefficient matrix.
#' @details The Leontief-inverse is \deqn{L = (I-A)^-1}
#' where B is the input coefficient matrix
#' created by \code{\link{input_coefficient_matrix_create}}. For the similar inverse
#' created from output coefficients, see the
#' \code{\link{ghosh_inverse_create}} function.
#'
#' @param technology_coefficients_matrix A technology coefficient matrix created
#' by the \code{\link{input_coefficient_matrix_create}}.
#' @param digits An integer showing the precision of the technology matrix in
#' digits. Default is \code{NULL} when no rounding is applied.
#' @importFrom dplyr mutate across
#' @family analytic object functions
#' @examples
#' tm <- input_flow_get (
#' data_table = iotable_get(),
#' households = FALSE)
#' I <- leontief_inverse_create( technology_coefficients_matrix = tm )
#' @export
leontief_inverse_create <- function ( technology_coefficients_matrix,
digits=NULL ) {
leontief_matrix<- leontief_matrix_create(
technology_coefficients_matrix = technology_coefficients_matrix
)
Lm <- as.matrix(leontief_matrix[,2:ncol(leontief_matrix)])
inverse <- solve( Lm )
if ( sum(vapply(inverse, function(x) sum(is.nan(x)), numeric(1))) > 0) {
stop ("Error: Could not invert the Leontief-matrix.")
}
named_inverse <- cbind(
as.data.frame(leontief_matrix [,1]),
as.data.frame(inverse)
) %>%
mutate(across(where(is.factor), as.character))
names (named_inverse) <- names (leontief_matrix)
row.names (named_inverse) <- seq_len(nrow(named_inverse))
if ( is.null(digits) ) return (named_inverse) else {
round_table (named_inverse, digits = digits)
}
}
#' @rdname leontief_inverse_create
#' @export
leontieff_inverse_create <- function (technology_coefficients_matrix,
digits=NULL) {
.Deprecated(new = leontief_inverse_create(technology_coefficients_matrix,
digits=NULL),
msg = "leontieff_inverse_create() is spelled correctly as leontief_inverse_create()")
leontief_inverse_create(technology_coefficients_matrix,
digits=NULL)
}