# 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()
#Importing library fpp 加法模型图 共五步
install.packages("fpp")
install.packages("forecast")
install.packages("xts")
library(forecast)
library(fpp)
#Using ausbeer data
data(ausbeer)
#Creating time series for ausbeer dataset
timeserie.beer = tail(head(ausbeer, 17*4+2), 17*4-4)
# Giving a name to the chart file.
png(file = "time.png")
plot(as.ts(timeserie.beer), col="magenta")
# 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()
#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()
#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()
# 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()
#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()
#Importing library Ecdat 乘法模型图
install.packages("Ecdat")
install.packages("Ecfun")
library(Ecfun)
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.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 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.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.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.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)
install.packages("timeSeries")
library(timeDate)
library(timeSeries)
library(fpp)
#Using ausbeer data
data(ausbeer)
#Creating time series
timeserie.beer = tail(head(ausbeer, 17*4+2), 17*4-4)
#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)
decompose.beer = decompose(ts.beer, "additive")
# 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()