#' iocoef
#'
#' @param adj is a square matrix of intermediate transactions in the input-output table.
#' @param X is a column vector of gross output in the input-output table.
#' @param type which type of consumption coefficient to calculate: "direct" or "complete".
#'
#' @return the consumption coefficient matrix.
#' @export
#'
#' @examples
#' adj <- matrix(sample(25), 5, 5)
#' X <- as.vector(101:105)
#' direct_coef <- iocoef(adj, X, type = "direct")
#' complete_coef <- iocoef(adj, X, type = "complete")
iocoef <- function(adj, X, type = c("direct", "complete")) {
if (dim(adj)[1] != dim(adj)[2]) {
stop("The adjacency matrix is not a square matrix!")
}
if (any(colSums(adj) > X) | any(rowSums(adj) > X)) {
stop("The row or column sum in the adjacency matrix is not greater than the vector!")
}
A <- t(t(adj)/X)
A <- replace(A, is.na(A), 0)
if (type == "direct") {
return(A)
} else if (type == "complete") {
I <- diag(dim(adj)[1])
B <- solve(I-A)-I
return(B)
}
}
Add the following code to your website.
For more information on customizing the embed code, read Embedding Snippets.