The objective of this vignette is to explore all the possibilities of simulating many similar models with the flexible
I'm going to use model SIM to go through this example as it is a very simple model.
As always, we start by writing down the equations and external variables:
eqs <- sfcr_set( TXs ~ TXd, YD ~ W * Ns - TXs, Cd ~ alpha1 * YD + alpha2 * Hh[-1], Hh ~ YD - Cd + Hh[-1], Ns ~ Nd, Nd ~ Y / W, Cs ~ Cd, Gs ~ Gd, Y ~ Cs + Gs, TXd ~ theta * W * Ns, Hs ~ Gd - TXd + Hs[-1] ) external <- sfcr_set( Gd ~ 20, W ~ 1, alpha1 ~ 0.6, alpha2 ~ 0.4, theta ~ 0.2 )
And simulating a baseline model:
baseline <- sfcr_baseline(eqs, external, periods = 60, hidden = c("Hh" = "Hs"))
sfcr_multis() function is a S3 generic that currently can be used in three ways:
The idea of this function is that we want to have many models that are very similar but differs in one specific way. That's why it accept two arguments that are not in the
sfcr_multis(). It is here that we feed the models with its varying characteristics;
periods argument must be defined by the user. Finally, the
... argument accept any extra argument that feed the
sfcr_scenario() functions that are called under the hood.
This option allows the user to select one variable (e.g.
alpha1) and run many models that are only different in their values of that variable.
The first step is to expand the external set with the
sfcr_expand() function. It has three arguments:
x_external <- sfcr_expand(external, alpha1, seq(0.5, 0.9, 0.02))
We then use this variable to simulate all the models at once with
mlt_sim1 <- sfcr_multis(x_external, eqs, 50)
mlt_sim1 object is a
list. Every item in this list is a
sfcr_tbl object that looks like the output generated by
sfcr_scenario() but has an extra column, named as
simulation, that indicates which of the simulation this item represents.
The idea here is to expand a
sfcr_shock set to simulate many scenario models. In this example, I'm going to show how to apply different shocks to the
shock1 <- sfcr_shock( variables = sfcr_set( alpha2 ~ 0.3 ), start = 5, end = 50 ) x_shock <- sfcr_expand(shock1, alpha2, seq(0.1, 0.4, 0.05)) mlt_sim2 <- sfcr_multis(x_shock, baseline, 50)
As we can see, the structure of these models are the same as before:
Finally, the last option is to apply the same shock to many baseline models, generating many scenarios. In this example, I will use the
mlt_sim1 object as the
expanded argument and the
shock1 shock as the
mlt_sim3 <- sfcr_multis(mlt_sim1, fixed = shock1, periods = 50)
To visualize the results, the user must combine all the models in a single
tibble using the
bind_rows() function from
dplyr, or using
base. From there, the usual strategy of reshaping the
tibble into long format and plotting with
ggplot2 should be applied.
Below I present a simplified example:
mlt_sim1 %>% bind_rows() %>% pivot_longer(cols = -c(period, simulation)) %>% filter(name %in% c("Y", "YD", "Cd")) %>% ggplot(aes(x = period, y = value)) + geom_line(aes(color = as_factor(simulation))) + facet_wrap(~name, nrow = 3) + scale_color_hue("alpha1", labels = as.character(seq(0.5, 0.9, 0.02))) + theme(legend.position = "bottom")
As can be seen in this example, the
alpha1 parameter affects how fast the model arrives to the steady state but does not change the steady state itself.
Any scripts or data that you put into this service are public.
Add the following code to your website.
For more information on customizing the embed code, read Embedding Snippets.