我正在尝试分解xts时间序列,并创建一个包含2列的数据框。
基本数据框具有以下结构:
Date x y z
2016-01-01 40419.35 12595 3.20
2016-01-02 44283.35 13904 3.18
2016-01-03 36277.23 10355 3.50
2016-01-04 42545.05 11929 3.56
2016-01-05 42402.22 13737 3.08
2016-01-06 49919.22 13661 3.65
...
2018-12-30 48719.22 13563 3.65
2018-12-31 49919.22 13661 3.65
这是我到目前为止所做的。
#1. Creating an xts object with weekly frequency
TimeSeries <- xts(x=Data[,-1],order.by=as.Date(Data$Date,"%Y/%m/%d"))
TimeSeriesWeekly <- period.apply(TimeSeriesWeekly, INDEX = endpoints(TimeSeriesWeekly, on = "weeks"), FUN = colSums)
attr(TimeSeriesWeekly, 'frequency') <-52
TimeSeriesWeekly<-TimeSeriesWeekly[,1] #keeping only the X variable I want to decompose
#2. Converting to a TS object in order to apply stl() decomposition formula
TimeSeriesWeekly <- ts(TimeSeriesWeekly[,1],frequency=52)
#3. Decomposing the TS object and distilling the trend
TimeSeriesWeeklyDecomposed<-stl(TimeSeriesWeekly, s.window="periodic")
TimeSeriesWeeklyTrend <- as.data.frame(TimeSeriesWeeklyDecomposed$time.series[,2])
我的问题是TimeSeriesWeeklyTrend数据框不包含日期变量。我该如何通过?