# R时间序列分析示例详解

## 创建时间序列

R提供ts()函数来创建时间序列。 ts()函数的语法如下：

``Timeseries_object_name<-  ts(data, start, end, frequency)``

S.No Parameter Description
1. data 它是一个向量或矩阵, 其中包含时间序列中使用的值。
2. start 这是第一次观察的开始时间
3. end 这是最后一次观察的结束时间
4. frequency 它指定每单位时间的观察次数。

### 例：

``````# Getting the data points in form of a R vector.
snowfall <- c(790, 1170.8, 860.1, 1330.6, 630.4, 911.5, 683.5, 996.6, 783.2, 982, 881.8, 1021)
# Convertting it into a time series object.
snowfall_timeseries<- ts(snowfall, start = c(2013, 1), frequency = 12)
# Printing the timeseries data.
print(snowfall_timeseries)
# Giving a name to the chart file.
png(file = "snowfall.png")
# Plotting a graph of the time series.
plot(snowfall_timeseries)
# Saving the file.
dev.off()``````

## 什么是固定时间序列？

1. 时间序列的平均值随时间恒定。这意味着趋势分量被声明为空。
2. 差异不应随时间增加。
3. 季节性影响应最小。

## 加法和乘法分解

``````#Importing library fpp
library(fpp)
#Using ausbeer data
data(ausbeer)
#Creating time series for ausbeer dataset
# Giving a name to the chart file.
png(file = "time.png")
plot(as.ts(timeserie_beer), col="magenta")
# Saving the file.
dev.off()``````

``````#Importing library Ecdat
library(Ecdat)
#Using AirPassengers data
data(AirPassengers)
#Creating time series  for AirPassengers dataset
timeserie_air = AirPassengers
# Giving a name to the file.
png(file = "time.png")
plot(as.ts(timeserie_air))
# Saving the file.
dev.off()``````

``````#Detecting trend
trend.beer = ma(timeserie.beer, order = 4, centre = T)
# Giving a name to the file.
png(file = "time.png")
plot(as.ts(timeserie.beer), col="red")
lines(trend.beer, col="red")
plot(as.ts(trend.beer), col="red")
# Saving the file.
dev.off()``````

``````#Detecting trend
trend.air = ma(timeserie.air, order = 12, centre = T)
# Giving a name to the file.
png(file = "time.png")
plot(as.ts(timeserie.air), col="blue")
lines(trend.air, col="blue")
plot(as.ts(trend.air), col="blue")
# Saving the file.
dev.off()``````

``````#Detrend the time series.
detrend.beer=timeserie.beer-trend.beer
# Giving a name to the file.
png(file = "time.png")
plot(as.ts(detrend.beer), col="magenta")
# Saving the file.
dev.off()``````

``````#Detrend of time series
detrend.air=timeserie.air / trend.air
# Giving a name to the file.
png(file = "time.png")
plot(as.ts(detrend.air), col="blue")
# Saving the file.
dev.off()``````

``````#Average the seasonality
m.beer = t(matrix(data = detrend.beer, nrow = 4))
seasonal.beer = colMeans(m.beer, na.rm = T)
# Giving a name to the file.
png(file = "time.png")
plot(as.ts(rep(seasonal.beer, 16)), col="magenta")
# Saving the file.
dev.off()``````

``````#Average the seasonality
m.air = t(matrix(data = detrend.air, nrow = 12))
seasonal.air = colMeans(m.air, na.rm = T)
# Giving a name to the file.
png(file = "time.png")
plot(as.ts(rep(seasonal.air, 12)), col="blue")
# Saving the file.
dev.off()``````

``````# Examining the Remaining Random Noise
random.beer = timeserie.beer - trend.beer - seasonal.beer
# Giving a name to the file.
png(file = "time.png")
plot(as.ts(rep(random.beer)), col="magenta")
# Saving the file.
dev.off()``````

``````# Examining the Remaining Random Noise
random.air = timeserie.air / (trend.air * seasonal.air)
# Giving a name to the file.
png(file = "time.png")
plot(as.ts(random.air), col="blue")
# Saving the file.
dev.off()``````

``````#Reconstruction of original signal
recomposed.beer=trend.beer+seasonal.beer+random.beer
# Giving a name to the file.
png(file = "time.png")
plot(as.ts(recomposed.beer), col="magenta")
# Saving the file.
dev.off()``````

``````#Reconstruction of original signal
recomposed.air = trend.air*seasonal.air*random.air
# Giving a name to the file.
png(file = "time.png")
plot(as.ts(recomposed.air), col="blue")
# Saving the file.
dev.off()``````

### 使用decompose()进行时间序列分解

``````#Importing libraries
library(forecast)
library(timeSeries)
library(fpp)
#Using ausbeer data
data(ausbeer)
#Creating time series
#Detect trend
trend.beer = ma(timeserie.beer, order = 4, centre = T)
#Detrend of time series
detrend.beer=timeserie.beer-trend.beer
#Average the seasonality
m.beer = t(matrix(data = detrend.beer, nrow = 4))
seasonal.beer = colMeans(m.beer, na.rm = T)
#Examine the remaining random noise
random.beer = timeserie.beer - trend.beer - seasonal.beer
#Reconstruct the original signal
recomposed.beer = trend.beer+seasonal.beer+random.beer
#Decomposed the time series
ts.beer = ts(timeserie.beer, frequency = 4)
# Giving a name to the file.
png(file = "time.png")
par(mfrow=c(2, 2))
plot(as.ts(decompose.beer\$seasonal), col="magenta")
plot(as.ts(decompose.beer\$trend), col="magenta")
plot(as.ts(decompose.beer\$random), col="magenta")
plot(decompose.beer, col="magenta")
# Saving the file.
dev.off()``````

``````#Importing libraries
library(forecast)
library(timeSeries)
library(fpp)
library(Ecdat)
#Using Airpassengers data
data(AirPassengers)
#Creating time series
timeseries.air = AirPassengers
#Detect trend
trend.air = ma(timeseries.air, order = 12, centre = T)
#Detrend of time series
detrend.air=timeseries.air / trend.air
#Average the seasonality
m.air = t(matrix(data = detrend.air, nrow = 12))
seasonal.air = colMeans(m.air, na.rm = T)
#Examine the remaining random noise
random.air = timeseries.air / (trend.air * seasonal.air)
#Reconstruct the original signal
recomposed.air = trend.air*seasonal.air*random.air
#Decomposed the time series
ts.air = ts(timeseries.air, frequency = 12)
decompose.air = decompose(ts.air, "multiplicative")

# Giving a name to the file.
png(file = "time.png")

par(mfrow=c(2, 2))

plot(as.ts(decompose.air\$seasonal), col="blue")
plot(as.ts(decompose.air\$trend), col="blue")
plot(as.ts(decompose.air\$random), col="blue")
plot(decompose.air, col="blue")

# Saving the file.
dev.off()``````

• 回顶