Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Historical Variance Error Decomposition plot in R

Tags:

r

ggplot2

I found how to estimate the historical Variance Decomposition for VAR models in R in the below link

Historical Variance Error Decompotision Daniel Ryback

Daniel Ryback presents the result in an excel plot, but I wanted to prepare it with ggplot so I created some lines to get it, nevertheless, the plot I got in ggplot is very different to the one showed by Daniel in Excel. I replicated in excel and got the same result than Daniel so it seems there is an error in the way I am preparing the ggplot. Does anyone have a suggestion to arrive to the excel result?

See below my code

library(vars)
library(ggplot2)
library(reshape2)

this code is run after runing the code developed by Daniel Ryback in the link above to define the HD function

data(Canada)
ab<-VAR(Canada, p = 2, type = "both")
HD <- VARhd(Estimation=ab)
HD[,,1] 

ex <- HD[,,1]
ex1 <- as.data.frame(ex) # transforming the HD matrix as data frame #
ex2 <- ex1[3:84,1:4] # taking our the first 2 rows as they are N/As #
colnames(ex2) <- c("Emplyment", "Productivity", "Real Wages", "Unemplyment") # renaming columns #
ex2$Period <- 1:nrow(ex2) # creating an id column #
col_id <- grep("Period", names(ex2)) # setting the new variable as id #
ex3 <- ex2[, c(col_id, (1:ncol(ex2))[-col_id])] # moving id variable to the first column #
molten.ex <- melt(ex3, id = "Period") # melting the data frame #

ggplot(molten.ex, aes(x = Period, y = value, fill = variable)) + 
geom_bar(stat = "identity") + 
guides(fill = guide_legend(reverse = TRUE))

ggplot version

enter image description here

Excel version

enter image description here

like image 518
asalasa209 Avatar asked Oct 18 '25 04:10

asalasa209


1 Answers

The difference is that ggplot2 is ordering the variable factor and plotting it in a different order than excel. If you reorder the factor before plotting it will put 'unemployment' at the bottom and 'employment' at the top, as in excel:

molten.ex$variable <- factor(molten.ex$variable, levels = c("Unemployment",
                                                "Real Wages",
                                                "Productivity",
                                                "Employment"))

ggplot(molten.ex, aes(x = Period, y = value, fill = variable)) + 
  geom_bar(stat = "identity", width = 0.6) + 
  guides(fill = guide_legend(reverse = TRUE)) +
  # Making the R plot look more like excel for comparison... 
  scale_y_continuous(limits = c(-6,8), breaks = seq(-6,8, by = 2)) +
  scale_fill_manual(name = NULL, 
                    values = c(Unemployment = "#FFc000",  # yellow
                               `Real Wages` = "#A4A4A4",  # grey
                               Productivity = "#EC7C30",  # orange
                               Employment = "#5E99CE")) + # blue
  theme(rect = element_blank(),
        panel.grid.major.y = element_line(colour = "#DADADA"),
        legend.position  = "bottom",
        axis.ticks = element_blank(),
        axis.title = element_blank(),
        legend.key.size = unit(3, "mm"))

Giving:

r plot looking like excel

To roughly match the excel graph in Daniel Ryback's post:

Ryback's post

like image 54
Andy Baxter Avatar answered Oct 19 '25 21:10

Andy Baxter