发布于 2016-01-02 09:34:47 | 969 次阅读 | 评论: 0 | 来源: 网络整理

时间系列是每个数据点与时间戳相关联的数据点。一个简单的例子是股票在股票市场在不同的时间点在某一天的价格。另一例子是降雨量的区域中在不同月份的量。R输入语言使用许多函数来创建,操纵和绘制时间序列数据。时间序列数据被存储在R对象称为时间序列对象。它也是类似向量或数据帧的R数据对象。

时间序列对象是通过使用 ts() 函数来创建。

语法

ts()函数时间序列分析使用的基本语法是:

timeseries.object.name <-  ts(data, start, end, frequency)

以下是所使用的参数的说明:

  • data 包含在时间系列中使用的值的向量或者矩阵。
  • start 指定开始时间在时间系列中。
  • end 指定的结束时间的最后观察的时间序列。
  • frequency 指定的每个单位时间的观测数。

除了参数“data”其他所有参数都是可选的。

示例

考虑从2012年1月开始,地方年降雨量细节。我们创造的R时间序列对象,期限为12个月,并绘制它。

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

当我们上面的代码执行,它会产生以下结果及图表:

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

时间序列图

不同的时间间隔

在频率 ts() 函数参数的值 deices 的时间间隔,其中数据点被测量。 12表示的时间系列是12个月。 其他值和它的含义如下:

  • frequency = 12 钉住数据点,一年中的每个月。
  • frequency = 4 盯住数据点,每年每个季度。
  • frequency = 6 钉住的数据点,一小时中每10分钟。
  • frequency = 24*6 钉住的数据点,每天中的每10分钟。

多时间序列

我们可以通过组合两个串联成一个矩阵的一个绘制多个时间序列图表。

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

当我们上面的代码执行,它会产生以下结果及图表:

         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

多个时间序列图



最新网友评论  共有(0)条评论 发布评论 返回顶部

Copyright © 2007-2017 PHPERZ.COM All Rights Reserved   冀ICP备14009818号  版权声明  广告服务