R中時間序列分析-趨勢預測ARIMA
阿新 • • 發佈:2019-01-05
時間序列預測(time series forecasting)
ARIMA模型(Autoregressive Integrated Moving Average Model)
ARIMA模型,將非平穩時間序列轉化為平穩時間序列,然後將因變數僅對它的滯後值以及隨機誤差項的現值和滯後值進行迴歸所建立的模型。
install.packages(“forecast”)
擬合曲線的方法
auto.arima(ts)
forecast(arimaModel,h)
- arimaModel ARIMA模型
- h 需要預測的時間長度
程式碼實現:
#install.packages ('forecast')
library(forecast)
data <- read.csv("data.csv", fileEncoding="UTF8")
data$均值 <- data$總銷量/data$分店數
plot(data$均值, type='l')
freq <- spec.pgram(data$均值, taper=0, log='no', plot=FALSE);
start <- which(freq$spec==max(freq$spec))
frequency <- 1/freq$freq[which(freq$spec==max(freq$spec ))]
meanTS <- ts(
data$均值[start:length(data$均值)],
frequency=frequency
)
meanARIMA = auto.arima(meanTS)
meanARIMAForecast = forecast(meanARIMA, h=7);
meanARIMAForecast$mean
Time Series:
Start = 8.8
End = 9.7
Frequency = 6.66666666666667
[1] 41.87608 44.42713 41.13537 46.31410 44.36805 43.63064 43.48562
plot(meanARIMAForecast)