2016-08-02 80 views
0

是否可以將STL分析的輸出保存到矢量中?STL時間系列輸出 - 創建新矢量 - R

> 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))

謝謝!

回答

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" 

的「時間序列」聽起來前途,檢查out:

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一多個時間序列。

...

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

+0

感謝您的信息格雷戈爾。 – Starbucks

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