R/leontief.R

Defines functions leontief

Documented in leontief

#' Produce the Leontief inverse matrices for use in the analysis
#'
#' @param data the data frame containing the flowtable and output/demand vectors.
#'
#' @return `Ltype1` - the Leontief inverse with exogenous households.
#' @return `Ltype2` - the Leontief inverse with endogenous household.
#'
#' @export
leontief <- function(data = NULL) {

  ## extract flow table and output/demand vectors

  flowtable <- as.matrix(data[,1:dim(data)[1] ]) # flowtable
  total.output <- as.vector(matrix(data[,"Total Output"]))
  hhold.output <- as.vector(matrix(data[,"Household Output"]))
  hhold.demand <- as.vector(matrix(data[,"Household Demand"]))

  ### Create the type 1 matrix

  ## Calculate coefficient matrix:
  A <- flowtable %*% ((total.output )^-1 * diag(length(total.output)))
  # Show A

  # Identity matrix minus A
  IminusA <- diag(length(total.output)) - A

  # Calculate the Leontief Inverse matrix
  L <- solve(IminusA)


  ## Create the type 2 matrix - endoegenous households
  # create a new flowtable - add the employee output/compensation as an extra row and household demand as an extra columns
  # the household-household transfer on the lead diagonal should be zero (households do not supply/demand from/to each other directly)

  flow.table2 <- rbind(flowtable,hhold.output)        # add employment earnings as an extra row
  flow.table2 <- cbind(flow.table2,c(hhold.demand,0))  # add household spending as an extra column (0 for no inter-household purchases)

  ## Calculate coefficient matrix:
  A2 <- flow.table2 %*% (( c(total.output,sum(total.output)) )^-1 * diag(length( c(total.output,sum(total.output))  ) ) )

  # Identity matrix minus A
  IminusA2 <- diag(length(c(total.output,sum(total.output)))) - A2

  # Calculate the Leontief Inverse matrix
  L2 <- solve(IminusA2)

  return(list(Ltype1 = L,
              Ltype2 = L2))
}
djmorris1989/iomodeltobalc documentation built on June 11, 2020, 12:16 a.m.