R中的历史方差误差分解图 [英] Historical Variance Error Decomposition plot in R

查看:79
本文介绍了R中的历史方差误差分解图的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在下面的链接中找到了如何估算R中VAR模型的历史方差分解

Excel版本

解决方案

不同之处在于 ggplot2 variable 因子进行排序并以与excel不同的顺序进行绘制.如果在绘制之前对因子进行重新排序,它将在底部"放置失业",在顶部放置就业",就像在excel中一样:

 <代码> molten.ex $ variable<-factor(molten.ex $ variable,level = c("Unemployment",实际工资",生产率",就业"))ggplot(molten.ex,aes(x =周期,y =值,填充=变量))+geom_bar(stat ="identity",width = 0.6)+指南(fill = guide_legend(reverse = TRUE))+#使R图看起来更像excel进行比较...scale_y_continuous(limits = c(-6,8),breaks = seq(-6,8,by = 2))+scale_fill_manual(name = NULL,值= c(失业=#FFc000",#黄色`Real Wages` =#A4A4A4",#灰色生产率=#EC7C30",#橙色就业=#5E99CE"))+#蓝色主题(rect = element_blank(),panel.grid.major.y = element_line(colour ="#DADADA"),legend.position =底部",axis.ticks = element_blank(),axis.title = element_blank(),legend.key.size = unit(3,"mm")) 

给予:

要大致匹配Daniel Ryback帖子中的excel图形:

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

Excel version

解决方案

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:

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

这篇关于R中的历史方差误差分解图的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆