首页 > 解决方案 > R中的结构性中断:绘制趋势

问题描述

我正在尝试使用 复制下一个图形的结构ggplot2,但我没有找到如何制作它。

这是我要复制的图表

我有 ts 数据,我想为单位根检验分析中计算的每个结构中断添加回归线。

 autoplot(merval_usd) +
  labs(x = "Año", y = "Log_Merval en USD")+
  theme_bw()

这是我的数据;它包含对 MERVAL 价格指数的 4,888 个观察值。

> dput(head(merval_usd,20))
structure(c(6.31324002782979, 6.25952410104302, 6.27792086863394, 
6.26998603927151, 6.25789744652059, 6.25377111760352, 6.25553888702416, 
6.25132568135736, 6.32806177591735, 6.33873541097142, 6.34166356744438, 
6.35940084332373, 6.36564536517638, 6.36090516203878, 6.3456363608286, 
6.34242066971575, 6.33416733347983, 6.36629861166262, 6.36406205159053, 
6.34344097026516), class = c("xts", "zoo"), index = structure(c(946857600, 
946944000, 947030400, 947116800, 947203200, 947462400, 947548800, 
947635200, 947721600, 947808000, 948067200, 948153600, 948240000, 
948326400, 948412800, 948672000, 948758400, 948844800, 948931200, 
949017600), tzone = "UTC", tclass = "Date"), .Dim = c(20L, 1L
), .Dimnames = list(NULL, "MERV"))

更好看

> head(merval_usd, 10)
           MERV
2000-01-03 6.313240
2000-01-04 6.259524
2000-01-05 6.277921
2000-01-06 6.269986
2000-01-07 6.257897
2000-01-10 6.253771
2000-01-11 6.255539
2000-01-12 6.251326
2000-01-13 6.328062
2000-01-14 6.338735

通过此函数计算断点。它需要一个名为 ur.ka 的 urca::ur.za 函数的修改版本。这是使用 ur.ka 函数编译和执行的存储库的链接。

Github 仓库

    for (i in 1){
  inicio<- Sys.time()
  urka <- ur.ka(merval_usd$MERV, model = 'trend', bp = 5) #bp5 y 12 lag, trend, se rechaza, con 24 rezagos tmbn es "bueno" el ajuste
  #bp5, 24, both
  fin<-Sys.time()
  print(urka)
  print(stringr::str_c("tiempo de ejecucion ", fin-inicio))
}

plot(urka$testreg$residuals)

plot<- ggplot(merval_usd, aes(x = Index, y = MERV))+params
bp<-vector("list", length = length(urka$bpoints))
count<-0
for(i in urka$bpoints){
  print(merval_usd[i])
  count = count+1
  bp[[count]]<- geom_vline(xintercept = as.Date(index(merval_usd$MERV[i])), color = "black", lwd = 0.5, lty=2)
  print(plot+bp)
}

params<- list(geom_line(linetype=1, lwd=1, colour="steelblue"),
              labs(title = "Log Indice Merval - período 2000-2020"),
              theme_minimal())

这是我做的图

我使用自动绘图来了解数据,但我想用ggplot2图表来完成和个性化。

我运行了我的模型,并且对于每个断点,我估计了截距和趋势系数;我不知道这是否足够。

所以这是我第一次在这里发布问题,所以我为不整洁表示歉意

标签: rggplot2regressionseriesautoplot

解决方案


推荐阅读