R中时间序列分析-趋势预测ARIMA

时间序列预测(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)

R中时间序列分析-趋势预测ARIMA_第1张图片

你可能感兴趣的:(R数据挖掘篇)