Learn R Programming

HARplus (version 1.1.2)

shock_calculate: Calculate Shocks from Initial and Target Values

Description

Computes compounded GEMPACK-style percentage shocks between initial and target values, producing multi-period shock series for dynamic simulation models. The function automatically aligns dimensions across datasets and exports results in HAR format.

Usage

shock_calculate(
  initial_config,
  target_config,
  calc_config,
  output_path,
  long_desc = "Calculated shock values",
  dim_order = NULL,
  lowercase = FALSE
)

Value

Invisibly returns a list containing summary metadata:

  • n_observations: total records processed

  • n_included: records included in shock computation

  • n_excluded: records excluded by configuration

  • output_path: normalized path to output HAR file

Arguments

initial_config

A list created by create_initial_config, defining:

  • Input path, file format, and variable header for the initial dataset

  • Column name of the initial value field ("Value_ini")

target_config

A list created by create_target_config, defining:

  • Path, format, and variable header for the target dataset

  • Target value column ("Value_tar") or numeric target for uniform shock

calc_config

A list created by create_calc_config, specifying:

  • column_mapping: mapping between initial and target columns

  • timeline: sequence of years or periods (e.g., "1-5")

  • exclude_self_trade: logical, whether to drop self-pairs

  • exclusion_values: list of region/sector values to omit

output_path

Path to the output HAR file where calculated shocks will be written.

long_desc

Optional text for header description. Default is "Calculated shock values".

dim_order

Optional dimension ordering specification. Can be:

  • NULL (default): alphabetical A-Z ordering

  • a named list defining order for each dimension (e.g., REG, COMM)

  • a data frame or path to Excel/CSV file containing order definitions

lowercase

Logical; if TRUE, converts dimension elements to lowercase. Default is FALSE.

Author

Pattawee Puangchit

Details

  • Computes percentage shocks using compounded "power of tax" formula

  • Supports multiple periods defined via timeline configuration

  • Compatible with HAR, SL4, CSV, or XLSX input formats

  • Excludes self-trade or specified region-sector pairs when configured

  • Exports results as multi-header HAR file (one header per timeline period)

See Also

shock_calculate_uniform, create_initial_config, create_target_config, create_calc_config, save_har

Examples

Run this code
# Example 1: Target-Based Shock Calculation
har_path <- system.file("extdata", "baserate.har", package = "HARplus")

# Sorting Column
mapping <- list(
  REG = c("USA", "EU", "ROW")
)

# Initial File
initial <- create_initial_config(
  path   = har_path,
  format = "har",
  header = "rTMS"
)

# Target File
target <- create_target_config(
  path   = har_path,
  type   = "har",
  header = "rTMS"
)

# Calculation Setup with Column Mapping
calc <- create_calc_config(
  column_mapping      = c(TRAD_COMM = "TRAD_COMM", REG = "REG", REG.1 = "REG.1"),
  timeline            = "1-5",
  exclude_self_trade  = TRUE
)

# Compute Shock Based on Initial and Target Values
shock_calculate(
  initial_config = initial,
  target_config  = target,
  calc_config    = calc,
  output_path    = file.path(tempdir(), "output_target.har"),
  dim_order      = mapping
)

Run the code above in your browser using DataLab