STL时间序列输出 - 创建新向量 - R.

时间:2016-08-02 19:25:30

标签: r

是否可以将STL分析的输出保存到向量中?

    > stl(satat.ts, s.window = 4)
     Call:
     stl(x = satat.ts, s.window = 4)

    Components
         seasonal    trend  remainder
    2015 Q1 -169.91957477 2914.934  137.98532
    2015 Q2   11.37099404 3155.224   15.40541
    2015 Q3    0.09573424 3395.513 -125.60867
    2015 Q4  165.60565883 3636.489 -132.09422
    2016 Q1 -184.86967286 3877.464  -68.59450
    2016 Q2  -10.47226510 4125.118  121.35381
    2016 Q3   25.14061969 4372.773  115.08665
    2016 Q4  196.59890247 4593.852   33.54917
    2017 Q1 -198.92478464 4814.931   58.99366
    2017 Q2  -45.81852354 5031.778 -123.95919
    2017 Q3   42.63407915 5248.624  -16.25838
    2017 Q4  229.27354553 5461.215   27.51108

我想创建一个趋势向量。如果不手动输入每个值,我将如何做到这一点(即Trend_data< -stl(satat.ts $ trend))

谢谢!

2 个答案:

答案 0 :(得分:1)

当然,只需分配即可。我没有您的数据,因此我将使用?stl底部的示例:

stllc <- stl(log(co2), s.window = 21)

让我们看看那里有什么:

str(stllc)
# List of 8
#  $ time.series: mts [1:468, 1:3] -0.000185 0.00173 0.00367 0.007019 0.00869 ...
#   ..- attr(*, "dimnames")=List of 2
#   .. ..$ : NULL
#   .. ..$ : chr [1:3] "seasonal" "trend" "remainder"
#   ..- attr(*, "tsp")= num [1:3] 1959 1998 12
#   ..- attr(*, "class")= chr [1:3] "mts" "ts" "matrix"
#  $ weights    : num [1:468] 1 1 1 1 1 1 1 1 1 1 ...
#  $ call       : language stl(x = log(co2), s.window = 21)
#  $ win        : Named num [1:3] 21 21 13
#   ..- attr(*, "names")= chr [1:3] "s" "t" "l"
#  $ deg        : Named int [1:3] 0 1 1
#   ..- attr(*, "names")= chr [1:3] "s" "t" "l"
#  $ jump       : Named num [1:3] 3 3 2
#   ..- attr(*, "names")= chr [1:3] "s" "t" "l"
#  $ inner      : int 2
#  $ outer      : int 0
#  - attr(*, "class")= chr "stl"

“时间序列”听起来很有希望,请查看:

str(stllc$time.series)
 # mts [1:468, 1:3] -0.000185 0.00173 0.00367 0.007019 0.00869 ...
 # - attr(*, "dimnames")=List of 2
 #  ..$ : NULL
 #  ..$ : chr [1:3] "seasonal" "trend" "remainder"
 # - attr(*, "tsp")= num [1:3] 1959 1998 12
 # - attr(*, "class")= chr [1:3] "mts" "ts" "matrix"

好的,所以它是一个矩阵,其中一列被命名为“趋势”。

my_trend = stllc$time.series[, "trend"]

看起来不错!

请注意,我们也可以阅读文档以获取帮助。 ?stl说:

  

stl返回带有组件

的类"stl"的对象      

time.series:包含seasonaltrendremainder列的多个时间序列。

     

...

这将导致我们得到相同的结果。

答案 1 :(得分:1)

使用样本数据:

nottem # sample dataset from R
nottem.stl <- stl(nottem, s.window="periodic")

nottem.stl
#            seasonal    trend    remainder
# Jan 1920 -9.3471980 49.68067  0.266525379
# Feb 1920 -9.8552496 49.54552  1.109728805
# Mar 1920 -6.8533008 49.41037  1.842931803
# ...

您现在可以导出所需的数据:

seasonal <- nottem.stl$time.series[, 1]
trend <- nottem.stl$time.series[, 2]
remainder<- nottem.stl$time.series[, 3]

seasonal
#             Jan        Feb        Mar        Apr  ...
# 1920 -9.3471980 -9.8552496 -6.8533008 -2.7634710  ...
# 1921 -9.3471980 -9.8552496 -6.8533008 -2.7634710  ...
# ...