joaomacalos / sfcr

Simulate Stock-Flow Consistent Models
Other
23 stars 11 forks source link

Error when using sfcr_sankey() #5

Closed fgvillarreal closed 1 year ago

fgvillarreal commented 1 year ago

When trying to generate a Sankey plot from a solved model I get the following error:

'''Error in dplyr::mutate(): ℹ In argument: dplyr::across(-1, ~.eval_matrices(.x)). Caused by error in across(): ! Can't compute column Hogares. Caused by error in map_vec(): ! Can't convert <list>[[1]] to .'''

I've already updated R and relevant packaged to their latest versions.

Here is a reproducible example of the error using the SIM model from the sfcr package's website examples:

libraries

library(sfcr) library(tidyverse)

transaction matrix

tfm_sim <- sfcr_matrix( columns = c("Households", "Firms", "Government"), codes = c("h", "f", "g"), c("Consumption", h = "-Cd", f = "+Cs"), c("Govt. Exp.", f = "+Gs", g = "-Gd"), c("Factor Income", h = "W Ns", f = "-W Ns"), c("Taxes", h = "-TXs", g = "+TXd"), c("Ch. Money", h = "-d(Hh)", g = "d(Hs)") )

Model equations

sim_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] )

Model calibration

sim_ext <- sfcr_set( Gd ~ 20, W ~ 1, alpha1 ~ 0.6, alpha2 ~ 0.4, theta ~ 0.2 )

Basline solution

sim <- sfcr_baseline( equations = sim_eqs, external = sim_ext, periods = 100, hidden = c("Hh" = "Hs"), method = "Broyden")

Sankey plot

sfcr_sankey(tfm_sim, sim)

Thank you!

joaomacalos commented 1 year ago

Hello Francisco, I heard that someone else had a similar problem. It may be related with a new release of R or dplyr's packages. Here's the solution he provided:

sfcr_sankey() failed to run (crashed) until I changed the results in the .eval_matrices subfunction from type double to string:

 .eval_matrices <- function(x) {
     purrr::modify(x, function(y)
     if (stringr::str_length(y) == 0) {
             sprintf("%f", NA_real_)
         } else {
             sprintf("%f", eval(str2expression(y)))
         }
     )
 }

I hadn't had the time yet to fix in the package. Hope it helps. Best, Joao

On Wed, Feb 15, 2023 at 3:52 PM Francisco G. Villarreal < @.***> wrote:

When trying to generate a Sankey plot from a solved model I get the following error:

'''Error in dplyr::mutate(): ℹ In argument: dplyr::across(-1, ~.eval_matrices(.x)). Caused by error in across(): ! Can't compute column Hogares. Caused by error in map_vec(): ! Can't convert [[1]] to .'''

I've already updated R and relevant packaged to their latest versions.

Here is a reproducible example of the error using the SIM model from the sfcr package's website examples: libraries

library(sfcr) library(tidyverse) transaction matrix

tfm_sim <- sfcr_matrix( columns = c("Households", "Firms", "Government"), codes = c("h", "f", "g"), c("Consumption", h = "-Cd", f = "+Cs"), c("Govt. Exp.", f = "+Gs", g = "-Gd"), c("Factor Income", h = "W Ns", f = "-W Ns"), c("Taxes", h = "-TXs", g = "+TXd"), c("Ch. Money", h = "-d(Hh)", g = "d(Hs)") ) Model equations

sim_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] ) Model calibration

sim_ext <- sfcr_set( Gd ~ 20, W ~ 1, alpha1 ~ 0.6, alpha2 ~ 0.4, theta ~ 0.2 ) Basline solution

sim <- sfcr_baseline( equations = sim_eqs, external = sim_ext, periods = 100, hidden = c("Hh" = "Hs"), method = "Broyden") Sankey plot

sfcr_sankey(tfm_sim, sim)

Thank you!

— Reply to this email directly, view it on GitHub https://github.com/joaomacalos/sfcr/issues/5, or unsubscribe https://github.com/notifications/unsubscribe-auth/AJS76HV3P4LLGD5WJGNLLLTWXTURPANCNFSM6AAAAAAU46LMXA . You are receiving this because you are subscribed to this thread.Message ID: @.***>

fgvillarreal commented 1 year ago

Thank you João! That solved the issue. Cheers!

Francisco