【发布时间】:2020-05-05 11:33:35
【问题描述】:
我在下面的链接中找到了如何估计 R 中 VAR 模型的历史方差分解
Historical Variance Error Decompotision Daniel Ryback
Daniel Ryback 在 excel 图中显示结果,但我想用 ggplot 准备它,所以我创建了一些线来获得它,但是,我在 ggplot 中得到的图与 Daniel 在 Excel 中显示的图非常不同。我在 excel 中复制并得到与 Daniel 相同的结果,所以我准备 ggplot 的方式似乎存在错误。有没有人建议得出 excel 结果?
见下面我的代码
library(vars)
library(ggplot2)
library(reshape2)
此代码是在运行上面链接中 Daniel Ryback 开发的代码定义 HD 功能后运行的
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 版本
Excel 版本
【问题讨论】:
-
您知道如何在 BQ SVAR 模型而不是 VAR 上运行 Daniel Ryback 的函数吗?