koma in
ParallelThis vignette demonstrates how to use the future::plan
function to run koma package functions either sequentially
or in parallel, depending on your operating system and other
preferences.
For more details, see the future package documentation.
Install the future and parallelly
packages:
We will use the simulated data contained in koma to
illustrate the use of future::plan. The setup is the
following:
library(koma)
equations <- "consumption ~ gdp + consumption.L(1),
investment ~ investment.L(1),
exports ~ world_gdp + exports.L(1),
imports ~ domestic_demand + imports.L(1),
prices ~ exchange_rate + oil_price + prices.L(1),
interest_rate ~ prices + interest_rate_germany + prices.L(1),
gdp == 0.6*consumption + 0.6*domestic_demand + 0.5*exports - 0.4*imports,
domestic_demand == 0.6*consumption + 0.4*investment"
exogenous_variables <- c("world_gdp", "interest_rate_germany", "exchange_rate", "oil_price")
dates <- list(
estimation = list(start = c(1996, 1), end = c(2019, 4)),
forecast = list(start = c(2023, 1), end = c(2023, 4))
)
sys_eq <- system_of_equations(equations, exogenous_variables)
series <- names(small_open_economy)
series <- series[!series %in% c("interest_rate", "interest_rate_germany")]
ts_data <- lapply(series, function(x) {
as_ets(small_open_economy[[x]],
series_type = "level", method = "diff_log"
)
})
names(ts_data) <- series
ts_data$interest_rate <- as_ets(small_open_economy$interest_rate,
series_type = "rate", method = "none"
)
ts_data$interest_rate_germany <- as_ets(small_open_economy$interest_rate_germany,
series_type = "rate", method = "none"
)By default, R executes code sequentially. For example:
future::planTo enable parallel execution, you can use future::plan.
The type of parallel execution depends on your operating system or
whether you are running the code on a cluster.
You need to define the number of workers that your parallel job to run on. Here we use all cores except one.
For Windows, you can use multisession:
On Linux, you can use multicore (fork-based, lower overhead):
On macOS, do not use multicore. Apple’s
Accelerate framework (the BLAS/LAPACK backend used by
eigen()) relies on Grand Central Dispatch internally, which
is not fork-safe. Forked workers will segfault with “invalid
permissions” inside eigen().
Use multisession instead — it spawns fresh R processes rather than forking: