簡體   English   中英

STL時間序列輸出-創建新矢量-R

[英]STL Time Series Output - Creating new vector - 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))

謝謝!

當然,只需分配它。 我沒有您的數據,所以我將使用?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

...

這將導致我們得到相同的結果。

使用樣本數據:

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  ...
# ...

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM