simulate_scenarios: Simulate multiple scenarios of household lifetime finances

View source: R/simulate_scenarios.R

simulate_scenariosR Documentation

Simulate multiple scenarios of household lifetime finances

Description

Simulate multiple scenarios of household lifetime finances

Usage

simulate_scenarios(
  scenarios_parameters,
  household,
  portfolio,
  current_date = get_current_date(),
  monte_carlo_samples = NULL,
  auto_parallel = FALSE,
  use_cache = FALSE,
  debug = FALSE,
  ...
)

Arguments

scenarios_parameters

A tibble with column scenario_id and nested column events. Each scenario has defined one or more events in the tibbles that are stored in as a list in the events column.

household

An R6 object of class Household.

portfolio

A nested tibble of class Portfolio.

current_date

A character. Current date in the format YYYY-MM-DD. By default, it is the output of get_current_date().

monte_carlo_samples

An integer. Number of Monte Carlo samples. If NULL (default), no Monte Carlo samples are generated.

auto_parallel

A logical. If TRUE, the function automatically detects the number of cores and uses parallel processing to speed up the Monte Carlo simulations. The results are cached in the folder set by set_cache().

use_cache

A logical. If TRUE, the function uses memoised functions to speed up the simulation. The results are cached in the folder set by set_cache().

debug

A logical. If TRUE, additional information is printed during the simulation.

...

Additional arguments passed simulation functions.

Value

A tibble with nested columns.

Examples


older_member <- HouseholdMember$new(
  name       = "older",  
  birth_date = "1980-02-15",
  mode       = 80,
  dispersion = 10
)  
household <- Household$new()
household$add_member(older_member)  

household$expected_income <- list(
  "income" = c(
    "members$older$age <= 65 ~ 7000 * 12"
  )
)
household$expected_spending <- list(
  "spending" = c(
    "TRUE ~ 5000 * 12"
  )
)

portfolio <- create_portfolio_template() 
portfolio$accounts$taxable <- c(10000, 30000)
portfolio <- 
  portfolio |> 
  calc_effective_tax_rate(
    tax_rate_ltcg = 0.20, 
    tax_rate_ordinary_income = 0.40
  )
start_ages <- c(60, 65, 70)
scenarios_parameters <- 
  tibble::tibble(
    member    = "older",
    event      = "retirement",
    start_age = start_ages,
    years     = Inf,
    end_age   = Inf
   ) |> 
  dplyr::mutate(scenario_id = start_age) |> 
  tidyr::nest(events = -scenario_id)

scenarios_parameters

scenarios <- 
  simulate_scenarios(
    scenarios_parameters = scenarios_parameters,
    household            = household,
    portfolio            = portfolio,
    current_date         = "2020-07-15"
  )
scenarios$scenario_id |> unique()


R4GoodPersonalFinances documentation built on June 8, 2025, 11:18 a.m.