R中

2017-06-12 51 views
0

用時間轉換數據幀,以時間序列我有13列的數據幀,我希望將數據幀轉換爲時間序列,這樣我可以用stl()R中

我的數據框看起來執行季節性分解如下:

> head(wideRawDF) 
    Period.Start.Time DO0182U09A3 DO0182U09B3 DO0182U09C3 DO0182U21A1 DO0182U21A2 DO0182U21A3 
1 2017-01-20 16:30:00  -101.50  -103.37  -103.86  -104.78  -104.95  -105.33 
2 2017-01-20 16:45:00  -101.32  -102.75  -104.22  -104.51  -103.94  -105.29 
3 2017-01-20 17:00:00  -101.45  -103.30  -103.93  -104.70  -104.82  -105.13 
4 2017-01-20 17:15:00  -100.91  -95.92  -99.22  -103.83  -104.72  -105.19 
5 2017-01-20 17:30:00  -100.91  -103.04  -104.09  -102.15  -104.91  -105.18 
6 2017-01-20 17:45:00  -100.97  -103.67  -104.12  -105.07  -104.23  -97.48 
    DO0182U21B1 DO0182U21B2 DO0182U21B3 DO0182U21C1 DO0182U21C2 DO0182U21C3 
1  -102.50  -99.43  -104.05  -104.51  -104.42  -105.17 
2  -102.82  -101.99  -103.94  -104.74  -104.65  -105.25 
3  -103.72  -103.95  -104.25  -105.02  -105.04  -105.32 
4  -103.57  -101.36  -104.09  -103.90  -102.95  -105.16 
5  -103.88  -104.09  -103.96  -104.75  -105.07  -105.23 
6  -103.92  -103.89  -104.01  -105.08  -105.14  -104.89 

正如你所看到的,我的數據是以15分鐘爲間隔。

我曾嘗試使用以下代碼此變換爲時間系列:

wideRawTS <- as.ts(wideRawDF, start = head(index(wideRawDF), 1), end = tail(index(wideRawDF), 1), frequency = 1) 

我使用的頻率等於1,因爲我有1343行數據,每一個代表採樣週期。

1343 /(14 * 24 * 4)= 0.999 => 1個

wideRawTS看起來如下:

head(wideRawTS) 
    Period.Start.Time DO0182U09A3 DO0182U09B3 DO0182U09C3 DO0182U21A1 DO0182U21A2 DO0182U21A3 DO0182U21B1 DO0182U21B2 DO0182U21B3 
[1,]  1484929800  -101.50  -103.37  -103.86  -104.78  -104.95  -105.33  -102.50  -99.43  -104.05 
[2,]  1484930700  -101.32  -102.75  -104.22  -104.51  -103.94  -105.29  -102.82  -101.99  -103.94 
[3,]  1484931600  -101.45  -103.30  -103.93  -104.70  -104.82  -105.13  -103.72  -103.95  -104.25 
[4,]  1484932500  -100.91  -95.92  -99.22  -103.83  -104.72  -105.19  -103.57  -101.36  -104.09 
[5,]  1484933400  -100.91  -103.04  -104.09  -102.15  -104.91  -105.18  -103.88  -104.09  -103.96 
[6,]  1484934300  -100.97  -103.67  -104.12  -105.07  -104.23  -97.48  -103.92  -103.89  -104.01 
    DO0182U21C1 DO0182U21C2 DO0182U21C3 
[1,]  -104.51  -104.42  -105.17 
[2,]  -104.74  -104.65  -105.25 
[3,]  -105.02  -105.04  -105.32 
[4,]  -103.90  -102.95  -105.16 
[5,]  -104.75  -105.07  -105.23 
[6,]  -105.08  -105.14  -104.89 

相信Period.Start.Time變量已經被轉換到紀元這是自1970年1月1日以來秒數的unix表示。

我後來嘗試傳遞時間序列數據,wideRawTS到stl(),但現在得到:

stl(wideRawTS[,2]) 
Error in stl(wideRawTS[, 2]) : 
    series is not periodic or has less than two periods 

我檢查了最初的幾個時代值,它們是原始數據的正確表示,所以我不知道發生了什麼事情!

如果有人會如此友善地向我展示我的方式的錯誤,我將非常感激。

+0

https://stackoverflow.com/questions/21123039/error-when-trying-to-use-stl-and-decompose-functions-in-r我認爲這個問題可以幫助你瞭解你的問題。根據我的理解,頻率是每單位時間(即每年)的數據數量。 15分鐘是一個時間段,但您希望單位時間內有多個時間段,否則您的ts將少於兩個時間段,如錯誤消息中所述。 – TooYoung

回答

1

看來你有指定頻率的數據的問題,看看here,herehere,也許它會有所幫助。

另外,是不是使用xts包進行時間序列操作的選項?

library(xts) 

Sys.setenv(TZ='GMT') 

df <- read.table(sep = ",", header = TRUE, stringsAsFactors = FALSE, text = ' 
    Period.Start.Time,DO0182U09A3,DO0182U09B3,DO0182U09C3,DO0182U21A1,DO0182U21A2,DO0182U21A3\n 
    "2017-01-20 16:30:00",-101.50,-103.37,-103.86,-104.78,-104.95,-105.3\n 
    "2017-01-20 16:45:00",-101.32,-102.75,-104.22,-104.51,-103.94,-105.29\n 
    "2017-01-20 17:00:00",-101.45,-103.30,-103.93,-104.70,-104.82,-105.13\n 
    "2017-01-20 17:15:00",-100.91,-95.92,-99.22,-103.83,-104.72,-105.19\n 
    "2017-01-20 17:30:00",-100.91,-103.04,-104.09,-102.15,-104.91,-105.18\n 
    "2017-01-20 17:45:00",-100.97,-103.67,-104.12,-105.07,-104.23,-97.48 
') 

df2 <- xts(x = df[,-1], order.by = as.POSIXct(df[,1])) 

它適用於具有相同行數的虛擬數據。

dummy <- xts(x = rnorm(1343), order.by = as.POSIXct("2017-01-20 16:30:00") + 15*60*(1:1343)) 
stl(ts(as.numeric(index(dummy)), frequency=12), s.window="periodic", robust=TRUE) 
Components 
      seasonal  trend  remainder 
Jan 1 -1.165038e-07 1484930700 -2.145767e-06 
Feb 1 2.053829e-07 1484931600 -1.192093e-06 
Mar 1 -2.190031e-08 1484932500 2.384186e-07 
Apr 1 -1.643545e-07 1484933400 7.152557e-07 
May 1 -5.919005e-09 1484934300 9.536743e-07 
Jun 1 1.653720e-07 1484935200 2.384186e-07