nk027 / bvar

Toolkit for the estimation of hierarchical Bayesian vector autoregressions. Implements hierarchical prior selection for conjugate priors in the fashion of Giannone, Lenza & Primiceri (2015). Allows for the computation of impulse responses and forecasts and provides functionality for assessing results.
https://cran.r-project.org/package=BVAR
Other
50 stars 21 forks source link

FEVD plot #75

Open nk027 opened 3 years ago

nk027 commented 3 years ago

We could add a quick and dirty plot for FEVDs:

library("BVAR")

example(bvar) # Use example data
fevd_data <- fevd(x)$fevd
fevd_mean <- apply(fevd_data, c(2, 3, 4), mean) # Or get quantiles from $quant

# Variable 1
v1 <- fevd_mean[1, , ] # FEVD matrix for variable 1

# Prepare data for base R line / area plots
l1 <- v1[, 3]
l2 <- rowSums(v1[, 2:3])
l3 <- rowSums(v1[, 1:3]) # sums to 1

# Just lines for total FEV
plot(l1, type = "l", ylim = c(0, 1), col = 3, main = "FEVD 1")
lines(l2, col = 2)
lines(l3, col = 1)

# Here we fill the area (bottom edge could be tweaked)
plot(l3, type = "l", ylim = c(0, 1), col = "#008080", main = "FEVD 1")
polygon(y = c(0, l3, 0), x = c(1, seq(length(l3)), length(l3)),
  col = "#00808022")
lines(l2, col = "#800000")
polygon(y = c(0, l2, 0), x = c(1, seq(length(l2)), length(l2)),
  col = "#80000022")
lines(l1, col = "#000080")
polygon(y = c(0, l1, 0), x = c(1, seq(length(l1)), length(l1)),
  col = "#00008022")

fevd2

fevd1

oDNAudio commented 3 years ago

Would be a nice addition, indeed. In related packages (e.g. vars), they are visualized through stacked bar charts, might also be a possibility.

nk027 commented 3 years ago

Yeah I think that'd be even better!

oDNAudio commented 3 years ago

Will get onto it!

Abhayprag commented 2 years ago

How to look for the fevd in the number form, contribution of each variable. Is there any way to plot it in the stacked histogram form. I have used fevd(x) , but i can not view the decomposition. Any help in this regard is appreciated.

oDNAudio commented 2 years ago

Hi, you can access the draws of the FEVDs by using obj$fevd where obj <- fevd(x). You can also access the quantiles for the FEVDs by using obj$quants.

A plotting function is yet to be finally implemented, but you can take a look at 69_fevd_plot.R.

Abhayprag commented 2 years ago

1 I have attached the image plot(fevd(x)) is giving error. 2 In this second image is this the variance decomposition, top row being variables and first column period?

oDNAudio commented 2 years ago

For the plotting function to work, you would have to source the code (commenting it out before) in the file that I sent you the link to. Did you do that?

With regards to the fevd object it is an array with dimensions draws x shock x horizon x impact. In the screenshot below, it would be interpreted as follows: "On impact, 100% of the variation in the first variable is due to a shock associated with the first column (or variable). For the second variable, around 95% of the variation is due to a shock of its own, the rest from the first variable (or rather the shock associated with it, ...." Hope that helps.

image

Curtis-dai commented 12 months ago

If I have n variables, how can I reflect the numerical form of this fevd into the matrix form of n x n. I directly check the form of quant as follows: image How do I construct a final variance decomposition matrix?

nk027 commented 11 months ago

Hey Curtis, from the outputs here, you could just subset, e.g., the median at a specific horizon, and should then get a variables variables* matrix. I'm not 100% sure what the order was, but you can call dim(irf[["fevd"]][["quants"]]) and should have one dimension for the quantiles and one for the horizon, which you can identify via their lengths.

nk027 commented 5 months ago

Hey @lqvecon, I always mess these up, but quickly checked again and it seems to me that draws x shock x horizon x impact is correct:

f$fevd[1, , 1, ]
           [,1]       [,2]      [,3]
[1,] 1.00000000 0.00000000 0.0000000
[2,] 0.11747481 0.88252519 0.0000000
[3,] 0.02342656 0.00820949 0.9683639

Here we subset to the first draw (dim-1) on impact (dim-3). In row 1 (dim-2), we have a shock of 1 on 1 etc.

Also see:

f$fevd[1, 1, , ]
           [,1]         [,2]          [,3]
 [1,] 1.0000000 0.0000000000 0.00000000000
 [2,] 0.9990679 0.0008683616 0.00006375813
 [3,] 0.9975037 0.0022802601 0.00021599611
 [4,] 0.9957108 0.0038347691 0.00045438926
 [5,] 0.9939046 0.0053227681 0.00077259284
 [6,] 0.9921923 0.0066457662 0.00116190901
 [7,] 0.9906200 0.0077675053 0.00161247577
 [8,] 0.9892005 0.0086854136 0.00211409150
 [9,] 0.9879293 0.0094139586 0.00265676977
[10,] 0.9867938 0.0099751010 0.00323109692
[11,] 0.9857786 0.0103929632 0.00382844675
[12,] 0.9848679 0.0106909869 0.00444109283

where you can track a shock of 1 (on 1–3 in the columns) over time (rows).