R語(yǔ)言 時(shí)間序列分析

時(shí)間序列是一系列數(shù)據(jù)點(diǎn),其中每個(gè)數(shù)據(jù)點(diǎn)與時(shí)間戳相關(guān)聯(lián)。 一個(gè)簡(jiǎn)單的例子是股票在某一天的不同時(shí)間點(diǎn)的股票價(jià)格。 另一個(gè)例子是一個(gè)地區(qū)在一年中不同月份的降雨量。 R語(yǔ)言使用許多函數(shù)來(lái)創(chuàng)建,操作和繪制時(shí)間序列數(shù)據(jù)。 時(shí)間序列的數(shù)據(jù)存儲(chǔ)在稱為時(shí)間序列對(duì)象的R對(duì)象中。 它也是一個(gè)R語(yǔ)言數(shù)據(jù)對(duì)象,如矢量或數(shù)據(jù)幀。

使用ts()函數(shù)創(chuàng)建時(shí)間序列對(duì)象。

語(yǔ)法

時(shí)間序列分析中ts()函數(shù)的基本語(yǔ)法是 -

<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是包含在時(shí)間序列中使用的值的向量或矩陣。

  • start以時(shí)間序列指定第一次觀察的開(kāi)始時(shí)間。

  • end指定時(shí)間序列中最后一次觀測(cè)的結(jié)束時(shí)間。

  • frequency指定每單位時(shí)間的觀測(cè)數(shù)。

除了參數(shù)“data”,所有其他參數(shù)是可選的。

考慮從2012年1月開(kāi)始的一個(gè)地方的年降雨量細(xì)節(jié)。我們創(chuàng)建一個(gè)R時(shí)間序列對(duì)象為期12個(gè)月并繪制它。

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

時(shí)間序列圖 -

時(shí)間序列,使用R

不同的時(shí)間間隔

ts()函數(shù)中的頻率參數(shù)值決定了測(cè)量數(shù)據(jù)點(diǎn)的時(shí)間間隔。 值為12表示時(shí)間序列為12個(gè)月。 其他值及其含義如下 -

  • 頻率

    = 12指定一年中每個(gè)月的數(shù)據(jù)點(diǎn)。

  • 頻率= 4每年的每個(gè)季度的數(shù)據(jù)點(diǎn)。

  • 頻率= 6每小時(shí)的10分鐘的數(shù)據(jù)點(diǎn)。

  • 頻率= 24 * 6將一天的每10分鐘的數(shù)據(jù)點(diǎn)固定。

多時(shí)間序列

我們可以通過(guò)將兩個(gè)系列組合成一個(gè)矩陣,在一個(gè)圖表中繪制多個(gè)時(shí)間序列。

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

多時(shí)間序列圖 -

結(jié)合時(shí)間序列,使用R
最后編輯于
?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請(qǐng)聯(lián)系作者
【社區(qū)內(nèi)容提示】社區(qū)部分內(nèi)容疑似由AI輔助生成,瀏覽時(shí)請(qǐng)結(jié)合常識(shí)與多方信息審慎甄別。
平臺(tái)聲明:文章內(nèi)容(如有圖片或視頻亦包括在內(nèi))由作者上傳并發(fā)布,文章內(nèi)容僅代表作者本人觀點(diǎn),簡(jiǎn)書(shū)系信息發(fā)布平臺(tái),僅提供信息存儲(chǔ)服務(wù)。

相關(guān)閱讀更多精彩內(nèi)容

友情鏈接更多精彩內(nèi)容