Git Product home page Git Product logo

Comments (3)

ferdinand-fichtner avatar ferdinand-fichtner commented on September 22, 2024 1

Dear Andrea,

thank you very much for looking into this and working out a solution - which works nicely for me! Looking forward to an official extension of the package, but at this stage, your suggestion works well!

Best
Ferdinand

from bimets.

andrea-luciani avatar andrea-luciani commented on September 22, 2024

Dear @ferdinand-fichtner

Thanks for your interest in the bimets package.

I got your point. As you said, atm incidence_matrix only contains references for actual endogenous variables that contemporary impact the dependent variable.

To meet your goals, you may want to take a look at the behaviorals/identities eqComponentNames attribute in order to build up a chart similar to the chart you found on the bimets pdfs. Take a look at the following code:

rm(list=ls(all.names = T))

library(bimets)
library(igraph)
library(extrafont)
library(hrbrthemes)
library(ggalt)
library(ggplot2)
library(plotly)

#define model
klein1_def <- "
MODEL

COMMENT> Consumption
BEHAVIORAL> cn
TSRANGE 1921 1 1941 1
EQ> cn = a1 + a2*p + a3*TSLAG(p,1) + a4*(w1+w2)
COEFF> a1 a2 a3 a4

COMMENT> Investment
BEHAVIORAL> i
TSRANGE 1921 1 1941 1
EQ> i = b1 + b2*p + b3*TSLAG(p,1) + b4*TSLAG(k,1)
COEFF> b1 b2 b3 b4

COMMENT> Demand for Labor
BEHAVIORAL> w1
TSRANGE 1921 1 1941 1
EQ> w1 = c1 + c2*(y+t-w2) + c3*TSLAG(y+t-w2,1)+c4*time
COEFF> c1 c2 c3 c4

COMMENT> Gross National Product
IDENTITY> y
EQ> y = cn + i + g - t 

COMMENT> Profits
IDENTITY> p
EQ> p = y - (w1+w2)

COMMENT> Capital Stock 
IDENTITY> k
EQ> k = TSLAG(k,1) + i
END
"

#load model
myModel <- LOAD_MODEL(modelText = klein1_def)

#define vertices, i.e. vendog + vexog
vertices_names  <- c(myModel$vendog,myModel$vexog)
vertices_type   <- c(rep("en",length(myModel$vendog)),rep("ex",length(myModel$vexog)))

vertices  <- data.frame(name=vertices_names,
                        type=vertices_type)

#define relations between vertices
relations_from  <- c()
relations_to    <- c()

for (idx in names(myModel$behaviorals))
{
  #get eq components names
  localIncidenceVars <- myModel$behaviorals[[idx]]$eqComponentsNames
  #remove dependant 
  localIncidenceVars <- setdiff(localIncidenceVars,idx)
  
  for (from in localIncidenceVars)
  {
    relations_from  <- c(relations_from,from)
    relations_to    <- c(relations_to,idx)
  }
}

for (idx in names(myModel$identities))
{
  localIncidenceVars <- myModel$identities[[idx]]$eqComponentsNames
  localIncidenceVars <- setdiff(localIncidenceVars,idx)
  
  for (from in localIncidenceVars)
  {
    relations_from  <- c(relations_from,from)
    relations_to    <- c(relations_to,idx)
  }
}
 
relations <- data.frame(from=relations_from,
                        to=relations_to)


#add attribute to graph, TRUE if from-node is an endogenous
relations$fromIsEndogenous <- FALSE
relations$fromIsEndogenous[which(relations$from %in% vertices$name[which(vertices$type=='en')])] <- TRUE

#create graph
g <- graph.data.frame(relations, directed=TRUE, vertices=vertices)

#define edge colors, dark blue if circular
edge_colors <- rep("pink",length(relations$from))
edge_colors[which(E(g)$fromIsEndogenous==TRUE)] <- "skyblue"

#plot stuff
set.seed(18)
plot(g,
     main="Klein model full incidence graph",
     edge.arrow.size=0.3, 
     edge.arrow.width=3,
     edge.width=c(1.3, 2.1)[1+(E(g)$fromIsEndogenous==TRUE)],
     edge.color=edge_colors,
     vertex.color=c("pink", "skyblue")[1+(V(g)$type=="en")],
     vertex.size=c(35,30)[1+(V(g)$type=="en")], 
     vertex.frame.color="#ffffff",
     vertex.label.color=c("#662222", "#222266")[1+(V(g)$type=="en")],
     vertex.label.cex=0.8, 
     vertex.label.family="Arial Black", 
     edge.curved=-0.2,
     layout=layout.davidson.harel
)

Rplot

Limitations: the code above does not work fine (it misses self-loops) if you have TSDELTA, TSDELTAP, TSDELTALOG functions on the left side of your equations (due to the fact that we remove the dependent variable by using the setdiff command), or if you have a self-dependent equation, e.g. EQ> Y = X + 0.1*Y

I have inserted an "extend incidence matrix" task in the bimets to-do list, so hopefully, we will have this new feature in the upcoming bimets versions.

Have a good day.

from bimets.

andrea-luciani avatar andrea-luciani commented on September 22, 2024

last but not least, by changing the value in set.seed(18) ggplot randomly rearranges graphical components, so you can try different layouts.

from bimets.

Related Issues (10)

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.