0

I have the following xts (south.africa)

structure(c(0.711700911871027, 2.18508552562202, 0.101828936775838, 
0.559018189973415, 0.791510887118818, 0.0702572653418443, 0.545643651047278, 
1.96973394058411, 1.13702436984386, -0.110342572242494, 0.895562224513213, 
1.14344682000116, 1.24275400501261, -0.27136808242183, 0.263419830113065, 
2.34411425782337, 1.33985361856794, 1.01841436556569, 0.866157306469795, 
1.93932019030232, 1.27825249354618, 1.30223261455793, 0.375324910356056, 
1.90149463122147, 1.17893305264101, 1.78893495881187, 2.01345172908223, 
1.15135439226659, 0.429493055158936, 1.30897897154671, 1.59811481462455, 
0.384501108705138, 1.33152847641842, -0.0756364961798539, -0.625567867488819, 
-1.63277895723031, -0.302044860024786, 0.683501644311235, 0.579148593539358, 
1.35072894035346, 0.375566525011983, 1.0714726283032, 0.500230467477891, 
1.45616977552994, 0.289398525763396, 0.863566266240667, 0.508353713803089, 
0.475394391746953, 0.820846564990596, 0.340994768769143, 0.168979388512014, 
0.839854291774778, 1.14128072836799, 0.0124180977749688, 0.967066943385397, 
-0.0617993469917977, 0.765586687222708, 0.271147530795801, 0.648173777980432, 
0.68234658026185, -0.0897710114593053, -0.708258282061003, 0.448438992679812, 
-0.16686150038101, 1.34207945665406, -1.17408818506828, 0.765914568114034, 
0.133331283880445, 2.05881770081913, -1.49604288251695, 0.64064064153655, 
-0.568876991290068, 1.73070039441132, -0.636868179734762, 0.511232708705478, 
-1.58975642052397, 2.70424824256459, -1.52879441672304, -0.0794733364552158, 
-1.02884935859766, -16.1951626038544, 10.9740288232354, 2.0276108060183, 
1.6996167612223, 1.51306289759408, 1.90213612695742, 1.11098177244755, 
0.144156689942943, 1.04824032192408, 3.29391128227785, 3.07063103030947, 
2.79156130819782, 3.54522073492278, 1.36531761632988, 0.273561477276285, 
-0.889626720355619, -1.47407679728495, 0.309985806569024, 0.325381422046789, 
-0.0591542128929312, 1.06059875690399, 0.580413813579916, 0.289504123768669, 
0.414777339697103, 0.756406499801709, 0.557780657388651, 0.806178986320827, 
1.55429806729295, 1.57762672232398, 1.08603082661238, 1.61211690910754, 
1.82455024510801, 2.45718798396846, 2.68426228226435, 2.50978429363213, 
3.02373059291385, 1.36301343061644, 1.56005214831358, 1.82910637591691, 
1.44037258065417, 0.881529697550931, 1.05274032728078, 0.708190049422974, 
0.674295163204164, 0.934457611055617, 1.36025819164436, 1.50804232025443, 
1.48581974120869, 1.73882008402959, 1.27679490711952, 1.12343976330829, 
0.925436930729973, 2.24990447458007, 1.35913624746395, 0.885006549446121, 
1.62977131236151, 1.3877570813734, 1.80222261823024, 1.54945562263986, 
1.44302994559631, 0.761493713563954, 0.350108153311623, 1.86807656796626, 
1.42175556001272, 1.04910912422067, 2.01205755468221, 1.86891746075357, 
1.23492256958846, 1.55102685266497, 1.61000293519873, 0.695481642542095, 
0.625493793378151, 1.48196760068915, 1.05708735900016, 1.0857422205409, 
1.1577949394427, 1.41164544510541, 0.428380530156947, 1.34355343917676, 
0.890871006592686, 1.01595011760134, 0.991035049882694, -0.561127940846706, 
1.55278035816462), .Dim = c(82L, 2L), .Dimnames = list(NULL, 
    c("GDP_SA", "CPI_SA")), index = structure(c(954547200, 962409600, 
970358400, 978307200, 986083200, 993945600, 1001894400, 1009843200, 
1017619200, 1025481600, 1033430400, 1041379200, 1049155200, 1057017600, 
1064966400, 1072915200, 1080777600, 1088640000, 1096588800, 1104537600, 
1112313600, 1120176000, 1128124800, 1136073600, 1143849600, 1151712000, 
1159660800, 1167609600, 1175385600, 1183248000, 1191196800, 1199145600, 
1207008000, 1214870400, 1222819200, 1230768000, 1238544000, 1246406400, 
1254355200, 1262304000, 1270080000, 1277942400, 1285891200, 1293840000, 
1301616000, 1309478400, 1317427200, 1325376000, 1333238400, 1341100800, 
1349049600, 1356998400, 1364774400, 1372636800, 1380585600, 1388534400, 
1396310400, 1404172800, 1412121600, 1420070400, 1427846400, 1435708800, 
1443657600, 1451606400, 1459468800, 1467331200, 1475280000, 1483228800, 
1491004800, 1498867200, 1506816000, 1514764800, 1522540800, 1530403200, 
1538352000, 1546300800, 1554076800, 1561939200, 1569888000, 1577836800, 
1585699200, 1593561600), tzone = "UTC", tclass = "Date"), class = c("xts", 
"zoo"))

From this, I have created an SVAR Model using the vars package and run variance decomposition as well. Looking for a convenient way in which I can create a stacked barchart of the results in decomposition in ggplot2

library(vars)
library(tidyverse)

var.south.africa <- south.africa %>% VAR(.,p=1,type="both",season=NULL)
SVAR.south.africa <- BQ(var.south.africa)

decomposition <- fevd(SVAR.south.africa, n.ahead = 10)
Tanga94
  • 695
  • 6
  • 27

1 Answers1

3

We can follow the approach of this Stack Overflow answer to an semi-unrelated question (as well as pivoting longer afterward) to reshape the data to a more convenient format:

dat <- map_df(decomposition, ~as.data.frame(.x), .id="id") %>%
    mutate(horizon = rep(1:10, 2)) %>%
    pivot_longer(names_to = "var", cols = c(GDP_SA, CPI_SA))
head(dat)
# # A tibble: 6 x 4
#   id     horizon var     value
#   <chr>    <int> <chr>   <dbl>
# 1 GDP_SA       1 GDP_SA 0.987 
# 2 GDP_SA       1 CPI_SA 0.0128
# 3 GDP_SA       2 GDP_SA 0.982 
# 4 GDP_SA       2 CPI_SA 0.0178
# 5 GDP_SA       3 GDP_SA 0.983 
# 6 GDP_SA       3 CPI_SA 0.0173

There id tells you which element of decomposition the row comes from, and var gives the column of the matrix of that element the row's value observation comes from. Then it's just a matter of doing a simple ggplot stacked barchart:

ggplot(data = dat, mapping = aes(x = horizon, y = value, fill = var)) +
    facet_wrap(~id) +
    geom_bar(stat = "identity") +
    theme_bw()

enter image description here

duckmayr
  • 16,303
  • 3
  • 35
  • 53