【问题标题】:transform "mFilter" object (list of Time-Series) to plot with ggplot2将“mFilter”对象(时间序列列表)转换为使用 ggplot2 绘图
【发布时间】:2017-02-13 16:10:39
【问题描述】:

我正在使用来自mFilter 包的hpfilter,我似乎找不到将hpfilter时间序列 对象列表转换为的简单方法我可以与 ggplot2 一起使用的格式。我意识到我可以将它们全部拆开并重新组合在一起,但我想我忽略了一些简单的方法?我尝试了 SO 讨论中建议的代码 R list to data frame。但是,我找不到以任何简单方式将 Time-Series 对象列表转换为 data.frame 的方法。最终目标是重现mFilter 对象生成的默认图(见下文)

这是一些示例代码

# install.packages(c("mFilter"), dependencies = TRUE)
library(mFilter)
data(unemp)
unemp.hp <- hpfilter(unemp, type=c("lambda"), freq = 1606)
# str(unemp.hp)
class(unemp.hp)
# [1] "mFilter"
plot(unemp.hp)
Hit <Return> to see next plot: 

另外,为什么要我“点击&lt;Return&gt;”才能看到剧情?

【问题讨论】:

    标签: r list plot ggplot2 time-series


    【解决方案1】:

    plot 函数调用 plot.mFilter 参数 ask=interactive() 并设置为 TRUE 用于交互式会话, 您可以通过ask=FALSE 调用plot 来禁用此功能

    plot(unemp.hp,ask=FALSE)
    

    数据:

    library(mFilter)
    library(ggplot2)
    library(gridExtra)
    # library(zoo)
    
    
    data(unemp)
    unemp.hp <- hpfilter(unemp, type=c("lambda"), freq = 1606)
    # str(unemp.hp)
    class(unemp.hp)
    # [1] "mFilter"
    plot(unemp.hp,ask=FALSE)
    

    检查对象unemp.hp的槽

    names(unemp.hp)
    # [1] "cycle"   "trend"   "fmatrix" "title"   "xname"   "call"    "type"    "lambda"  "method" 
    #[10] "x"
    

    相关对象为x(主要的unemp系列)、trendcycle。所有三个对象都属于ts类,我们首先将它们转换为 data.frame 使用自定义函数并使用 ggplotgridExtra 绘图(对于 grid.arrange

    objectList = list(unemp.hp$x,unemp.hp$trend,unemp.hp$cycle)
    names(objectList) = c("unemp","trend","cycle")
    
    
    sapply(objectList,class)
    #unemp trend cycle 
    # "ts"  "ts"  "ts"
    

    ts到data.frame的转换:

    fn_ts_to_DF = function(x)  {
    
    DF = data.frame(date=zoo::as.Date(time(objectList[[x]])),tseries=as.matrix(objectList[[x]])) 
    colnames(DF)[2]=names(objectList)[x]
    return(DF)
    }
    
    
    DFList=lapply(seq_along(objectList),fn_ts_to_DF)
    names(DFList) = c("unemp","trend","cycle")
    
    seriesTrend = merge(DFList$unemp,DFList$trend,by="date")
    cycleSeries = DFList$cycle
    

    情节:

    gSeries = ggplot(melt(seriesTrend,"date"),aes(x=date,y=value,color=variable)) + geom_line() +
              ggtitle('Hodrick-Prescot Filter for unemp') + 
              theme(legend.title = element_blank(),legend.justification = c(0.1, 0.8), legend.position = c(0, 1),
              legend.direction = "horizontal",legend.background = element_rect(fill="transparent",size=.5, linetype="dotted"))
    gCycle = ggplot(cycleSeries,aes(x=date,y=cycle)) + geom_line(color="#619CFF") + ggtitle("Cyclical component (deviations from trend)")
    
    gComb = grid.arrange(gSeries,gCycle,nrow=2)
    

    【讨论】:

    • 令人印象深刻!看起来真的很棒!但是,当我运行您的函数时,我确实收到了一条错误消息。具体来说,当我运行DFList=lapply(seq_along(objectList),fn_ts_to_DF) 行时,我得到Error in as.Date.default(time(objectList[[x]])) : do not know how to convert 'time(objectList[[x]])' to class “Date”。我忽略了什么吗?当我不那么累的时候,我会再看看它。再次感谢!
    • 您能否在 R 会话中检查 time 的输出,它指的是哪个包。默认情况下,它是从stats 包中引用的,即manual page
    • 是的,是namespace:stats 应该是什么?另外,感谢您的及时回复!
    • 它应该参考stats 谢谢,你能检查一下time(objectList[[1]]) 的输出吗
    • 工作就像一个魅力,我重新启动了 R。它可能是它。对不起,我自己没有弄清楚。谢谢!
    【解决方案2】:

    我尝试使用先前的答案,但对我没有用。

    我从 GDP 季度系列中了解趋势和周期。

    这个数据是一个时间序列,所以我做了这个,并且为我工作:

    list <- list(gdp_ln$x, gdp_ln$trend, gdp_ln$cycle)
    
    names(list)=c("gdp","trend","cycle")
    
    gdp<- data.frame((sapply(list,c)))
    

    数据:

    > dput(gdp_ln)
    structure(c(16.0275785360442, 16.0477176062761, 16.0718936895007, 
    16.0899963371452, 16.0875707712141, 16.0981391378223, 16.0988601288276, 
    16.1110815092797, 16.1244321329861, 16.1384685077996, 16.1451472350838, 
    16.148178781735, 16.161163569502, 16.1418894206861, 16.1634877625667, 
    16.1965372621761, 16.2216815829736, 16.2387677536829, 16.249412380526, 
    16.2690521777631, 16.2812185880068, 16.2951024427095, 16.2964024092233, 
    16.3127733881018, 16.3233290487177, 16.3369922768377, 16.3486515031696, 
    16.3489275708763, 16.3451264371757, 16.3524856433069, 16.3666338513045, 
    16.3801691039135, 16.3959993202765, 16.4135937981601, 16.4321203154987, 
    16.4488104165345, 16.4344524213544, 16.4302554348621, 16.4240722287677, 
    16.425087582257, 16.4350803035092, 16.4507216431126, 16.4670532627455, 
    16.4985227751756, 16.5094864456079, 16.5352746165004, 16.5504689966469, 
    16.5594976247513, 16.5754312535087, 16.592641573353, 16.6003340665324, 
    16.6063100774853, 16.6163655606058, 16.6370227688187, 16.6564363783854, 
    16.6577160570216, 16.6543595214556, 16.6773721241902, 16.6911082706925, 
    16.6935398489076, 16.6956102943815, 16.6798673418354, 16.6772670544553, 
    16.6678707780266, 16.6606889172344, 16.6678398460835, 16.6668473810049, 
    16.676020524389, 16.6775934319312, 16.6882821147755, 16.6957985899994, 
    16.7032334217472, 16.6926036544774, 16.7027214366522, 16.7103625977254, 
    16.7105344224572, 16.7042504851486, 16.7063913529457, 16.7100598555556, 
    16.6960591147037, 16.686477079594, 16.5740423808036, 16.6181175035946
    ), .Tsp = c(2000, 2020.5, 4), class = "ts")
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2021-09-23
      • 1970-01-01
      • 2018-07-08
      • 1970-01-01
      • 2014-10-28
      • 2013-06-07
      • 2013-12-02
      • 2015-01-06
      相关资源
      最近更新 更多