時間序列是一系列數(shù)據(jù)點,其中每個數(shù)據(jù)點與時間戳相關(guān)聯(lián)。 一個簡單的例子是股票在某一天的不同時間點的股票價格辉懒。 另一個例子是一個地區(qū)在一年中不同月份的降雨量。 R語言使用許多函數(shù)來創(chuàng)建谍失,操作和繪制時間序列數(shù)據(jù)眶俩。 時間序列的數(shù)據(jù)存儲在稱為時間序列對象的R對象中。 它也是一個R語言數(shù)據(jù)對象袱贮,如矢量或數(shù)據(jù)幀仿便。
使用ts()函數(shù)創(chuàng)建時間序列對象。
語法
時間序列分析中ts()函數(shù)的基本語法是 -
<pre class="result notranslate" style="margin: 15px 0px; padding: 10px 5px; position: relative; width: auto; max-width: 700px; box-sizing: border-box; display: block; line-height: 1.7; background: rgb(239, 239, 239); border-radius: 3px; font-size: 14px; font-family: Consolas, "Courier New", Courier, monospace; overflow-x: auto; border: 1px solid rgb(221, 221, 221); word-wrap: break-word !important; white-space: pre-wrap !important; color: rgb(0, 0, 0); font-style: normal; font-variant-ligatures: normal; font-variant-caps: normal; font-weight: normal; letter-spacing: normal; orphans: 2; text-align: start; text-indent: 0px; text-transform: none; widows: 2; word-spacing: 0px; -webkit-text-stroke-width: 0px; text-decoration-style: initial; text-decoration-color: initial;">timeseries.object.name <- ts(data, start, end, frequency)
</pre>
以下是所使用的參數(shù)的描述 -
data是包含在時間序列中使用的值的向量或矩陣攒巍。
start以時間序列指定第一次觀察的開始時間嗽仪。
end指定時間序列中最后一次觀測的結(jié)束時間。
frequency指定每單位時間的觀測數(shù)柒莉。
除了參數(shù)“data”闻坚,所有其他參數(shù)是可選的。
例
考慮從2012年1月開始的一個地方的年降雨量細(xì)節(jié)兢孝。我們創(chuàng)建一個R時間序列對象為期12個月并繪制它窿凤。
<pre class="prettyprint notranslate tryit" style="margin: 15px 0px; padding: 10px 5px; position: relative; width: auto; max-width: 700px; box-sizing: border-box; display: block; line-height: 1.7; background: rgb(239, 239, 239); border-radius: 3px; font-size: 14px; font-family: Consolas, "Courier New", Courier, monospace; overflow-x: auto; border: 1px solid rgb(221, 221, 221); word-wrap: break-word !important; white-space: pre-wrap !important; color: rgb(0, 0, 0); font-style: normal; font-variant-ligatures: normal; font-variant-caps: normal; font-weight: normal; letter-spacing: normal; orphans: 2; text-align: start; text-indent: 0px; text-transform: none; widows: 2; word-spacing: 0px; -webkit-text-stroke-width: 0px; text-decoration-style: initial; text-decoration-color: initial;"># Get the data points in form of a R vector.
rainfall <- c(799,1174.8,865.1,1334.6,635.4,918.5,685.5,998.6,784.2,985,882.8,1071)
Convert it to a time series object.
rainfall.timeseries <- ts(rainfall,start = c(2012,1),frequency = 12)
Print the timeseries data.
print(rainfall.timeseries)
Give the chart file a name.
png(file = "rainfall.png")
Plot a graph of the time series.
plot(rainfall.timeseries)
Save the file.
dev.off()
</pre>
當(dāng)我們執(zhí)行上面的代碼,它產(chǎn)生以下結(jié)果及圖表 -
<pre class="result notranslate" style="margin: 15px 0px; padding: 10px 5px; position: relative; width: auto; max-width: 700px; box-sizing: border-box; display: block; line-height: 1.7; background: rgb(239, 239, 239); border-radius: 3px; font-size: 14px; font-family: Consolas, "Courier New", Courier, monospace; overflow-x: auto; border: 1px solid rgb(221, 221, 221); word-wrap: break-word !important; white-space: pre-wrap !important; color: rgb(0, 0, 0); font-style: normal; font-variant-ligatures: normal; font-variant-caps: normal; font-weight: normal; letter-spacing: normal; orphans: 2; text-align: start; text-indent: 0px; text-transform: none; widows: 2; word-spacing: 0px; -webkit-text-stroke-width: 0px; text-decoration-style: initial; text-decoration-color: initial;">Jan Feb Mar Apr May Jun Jul Aug Sep
2012 799.0 1174.8 865.1 1334.6 635.4 918.5 685.5 998.6 784.2
Oct Nov Dec
2012 985.0 882.8 1071.0
</pre>
時間序列圖 -
不同的時間間隔
ts()函數(shù)中的頻率參數(shù)值決定了測量數(shù)據(jù)點的時間間隔雳殊。 值為12表示時間序列為12個月。 其他值及其含義如下 -
-
頻率
= 12指定一年中每個月的數(shù)據(jù)點窗轩。
頻率= 4每年的每個季度的數(shù)據(jù)點夯秃。
頻率= 6每小時的10分鐘的數(shù)據(jù)點。
頻率= 24 * 6將一天的每10分鐘的數(shù)據(jù)點固定痢艺。
多時間序列
我們可以通過將兩個系列組合成一個矩陣仓洼,在一個圖表中繪制多個時間序列。
<pre class="prettyprint notranslate tryit" style="margin: 15px 0px; padding: 10px 5px; position: relative; width: auto; max-width: 700px; box-sizing: border-box; display: block; line-height: 1.7; background: rgb(239, 239, 239); border-radius: 3px; font-size: 14px; font-family: Consolas, "Courier New", Courier, monospace; overflow-x: auto; border: 1px solid rgb(221, 221, 221); word-wrap: break-word !important; white-space: pre-wrap !important; color: rgb(0, 0, 0); font-style: normal; font-variant-ligatures: normal; font-variant-caps: normal; font-weight: normal; letter-spacing: normal; orphans: 2; text-align: start; text-indent: 0px; text-transform: none; widows: 2; word-spacing: 0px; -webkit-text-stroke-width: 0px; text-decoration-style: initial; text-decoration-color: initial;"># Get the data points in form of a R vector.
rainfall1 <- c(799,1174.8,865.1,1334.6,635.4,918.5,685.5,998.6,784.2,985,882.8,1071)
rainfall2 <-
c(655,1306.9,1323.4,1172.2,562.2,824,822.4,1265.5,799.6,1105.6,1106.7,1337.8)
Convert them to a matrix.
combined.rainfall <- matrix(c(rainfall1,rainfall2),nrow = 12)
Convert it to a time series object.
rainfall.timeseries <- ts(combined.rainfall,start = c(2012,1),frequency = 12)
Print the timeseries data.
print(rainfall.timeseries)
Give the chart file a name.
png(file = "rainfall_combined.png")
Plot a graph of the time series.
plot(rainfall.timeseries, main = "Multiple Time Series")
Save the file.
dev.off()
</pre>
當(dāng)我們執(zhí)行上面的代碼堤舒,它產(chǎn)生以下結(jié)果及圖表 -
<pre class="result notranslate" style="margin: 15px 0px; padding: 10px 5px; position: relative; width: auto; max-width: 700px; box-sizing: border-box; display: block; line-height: 1.7; background: rgb(239, 239, 239); border-radius: 3px; font-size: 14px; font-family: Consolas, "Courier New", Courier, monospace; overflow-x: auto; border: 1px solid rgb(221, 221, 221); word-wrap: break-word !important; white-space: pre-wrap !important; color: rgb(0, 0, 0); font-style: normal; font-variant-ligatures: normal; font-variant-caps: normal; font-weight: normal; letter-spacing: normal; orphans: 2; text-align: start; text-indent: 0px; text-transform: none; widows: 2; word-spacing: 0px; -webkit-text-stroke-width: 0px; text-decoration-style: initial; text-decoration-color: initial;"> Series 1 Series 2
Jan 2012 799.0 655.0
Feb 2012 1174.8 1306.9
Mar 2012 865.1 1323.4
Apr 2012 1334.6 1172.2
May 2012 635.4 562.2
Jun 2012 918.5 824.0
Jul 2012 685.5 822.4
Aug 2012 998.6 1265.5
Sep 2012 784.2 799.6
Oct 2012 985.0 1105.6
Nov 2012 882.8 1106.7
Dec 2012 1071.0 1337.8
</pre>
多時間序列圖 -