-
Notifications
You must be signed in to change notification settings - Fork 0
/
IOPC_model_sankey.R
131 lines (99 loc) · 4.57 KB
/
IOPC_model_sankey.R
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
# Model IO-PC for R: Sankey diagram of transactions and cross-industry relations
# aggregate equations taken from Model PC
# Wynne Godley and Marc Lavoie
# Monetary Economics
# Chapter 4
# Version: 9 November 2023
################################################################################
#Upload libraries for Sankey diagram
library(networkD3)
library(htmlwidgets)
library(htmltools)
################################################################################
#A) Sankey diagram of money transactions-flow and nominal changes in stocks
#Create nodes: source, target and flows
nodes = data.frame("name" =
c("Firms outflow", # Node 0
"Households outflow", # Node 1
"Government outflow", # Node 2
"Firms inflow", # Node 3
"Households inflow", # Node 4
"Government inflow", # Node 5
"Wages", # Node 6
"Consumption", # Node 7
"Taxes", # Node 8
"Government spending", # Node 9
"Money (change)", # Node 10
"Interest payments", # Node 11
"Bills (change)", # Node 12
"CB outflow", # Node 13
"CB inflow" # Node 14
))
#Select period
yr=5
#Create the flows
links = as.data.frame(matrix(c(
0, 6, p_c[1,yr]*cons[1,yr]+p_g[1,yr]*g[1,yr],
1, 7, p_c[1,yr]*cons[1,yr] ,
1, 8, t[1,yr],
1, 10, h_h[1,yr]-h_h[1,yr-1],
2, 9, p_g[1,yr]*g[1,yr],
6, 4, p_c[1,yr]*cons[1,yr]+p_g[1,yr]*g[1,yr],
7, 3, p_c[1,yr]*cons[1,yr],
8, 5, t[1,yr],
9, 3, p_g[1,yr]*g[1,yr],
2, 11, r[1,yr-1]*b_h[1,yr-1],
11, 4, r[1,yr-1]*b_h[1,yr-1],
10, 14, h_s[1,yr]-h_s[1,yr-1],
12, 5, b_s[1,yr]-b_s[1,yr-1],
1, 12, b_h[1,yr]-b_h[1,yr-1],
13, 12, b_cb[1,yr]-b_cb[1,yr-1]
),
#Note: each row represents a link. The first number represents the node being
#connected from. The second number represents the node connected to. The third
#number is the value of the node.
byrow = TRUE, ncol = 3))
names(links) = c("source", "target", "value")
my_color <- 'd3.scaleOrdinal() .domain([]) .range(["blue","green","yellow","red","purple","khaki","peru","violet","cyan","pink","orange","beige","white"])'
#Create and plot the network
sankeyNetwork(Links = links, Nodes = nodes,
Source = "source", Target = "target",
Value = "value", NodeID = "name", colourScale=my_color,
fontSize= 25, nodeWidth = 30)
################################################################################
#B) Sankey diagram of cross-industry relations
#Create nodes: source, target and flows
nodes = data.frame("name" =
c("a) Industry 1 output", # Node 0
"b) Industry 2 output", # Node 1
"c) Industry 1 inputs", # Node 2
"d) Industry 2 inputs", # Node 3
"e) Final demand for product 1", # Node 4
"f) Final demand for product 2", # Node 5
"g) Market for product 1", # Node 6
"h) Market for product 2" # Node 7
))
#Select period
yr=5
#Create the flows
links = as.data.frame(matrix(c(
0, 6, x[1,yr,1]*p[1,yr,1],
6, 2, x[1,yr,1]*A[1]*p[1,yr,1],
6, 3, x[1,yr,2]*A[3]*p[1,yr,1],
6, 4, d[1,yr,1]*p[1,yr,1],
1, 7, x[1,yr,2]*p[1,yr,2],
7, 2, x[1,yr,1]*A[2]*p[1,yr,2],
7, 3, x[1,yr,2]*A[4]*p[1,yr,2],
7, 5, d[1,yr,2]*p[1,yr,2]
),
#Note: each row represents a link. The first number represents the node being
#connected from. The second number represents the node connected to. The third
#number is the value of the node.
byrow = TRUE, ncol = 3))
names(links) = c("source", "target", "value")
my_color <- 'd3.scaleOrdinal() .domain([]) .range(["darkgreen","blue","mediumseagreen","turquoise","palegreen","skyblue","seagreen","royalblue"])'
#Create and plot the network
sankeyNetwork(Links = links, Nodes = nodes,
Source = "source", Target = "target",
Value = "value", NodeID = "name", colourScale=my_color,
fontSize= 25, nodeWidth = 30)