建立ARIMA模型

建立ARIMA模型,r,time-series,arima,R,Time Series,Arima,我的数据是timeseries: library(forecast) library(Mcomp) ts.data<- subset(M3, 12)[[551]] print(ts.data) 如何建立ARIMA模型,然后找到模型的平均绝对误差(MAE) 任何开始的想法都是有帮助的。这里有一种方法可以做到这一点(注意:要正确做到这一点,您还应该在建模之前调查ACF/PACF,以及在生成初始模型之后执行交叉验证): #如果尚未安装包,则安装包:必需的包=>vector 必要的包向量

我的数据是timeseries:

library(forecast)    
library(Mcomp)
ts.data<- subset(M3, 12)[[551]]
print(ts.data)
如何建立ARIMA模型,然后找到模型的平均绝对误差(MAE)


任何开始的想法都是有帮助的。

这里有一种方法可以做到这一点(注意:要正确做到这一点,您还应该在建模之前调查ACF/PACF,以及在生成初始模型之后执行交叉验证):

#如果尚未安装包,则安装包:必需的包=>vector
必要的包向量
新的\u包0){install.packages(新的\u包,dependencies=TRUE)}
#初始化会话中的包:boolean=>stdout列表(控制台)
lapply(必需的包,require,character.only=TRUE)
#将M3数据子集以包含相关系列:

ts.data请参见下面编辑的解决方案!另外,如果我的解决方案符合您的要求,请不要忘记接受!非常感谢@hello_friend,我正在研究你的代码,我会尽快运行它@有什么建议吗?
Series: M551
Type of series: INDUSTRY
Period of series: MONTHLY
Series description: Lumber, softwoods, western pine, production

HISTORICAL data
       Jan   Feb   Mar   Apr   May   Jun   Jul   Aug   Sep   Oct   Nov   Dec
1983  5960  6320  7050  7080  7170  7620  7000  7790  7830  7580  6620  6860
1984  7240  6550  8730  8690  7650  7950  7210  7880  7040  7940  7290  5750
1985  6410  6230  7260  8220  7770  7830  7590  9210  8340  8660  7330  7000
1986  7500  7430  8350  9010  8470  8900  8720  9710 10070  9610  8410  8640
1987  8500  8920 10470  8960  9390 10630  9380 10050  9030 10580  9350  8810
1988  9300 10430 10570 10440 10120  8780  7460  8230  9830 10260  9270  9260
1989  9260  8150  9930  8840  9150 10230  9340 10170  9150 10420  8690  8960
1990  9960  9050 10430  9650  9350  8790  8550  8790  7590  8730  7520  6110
1991  7450  7230  7680  8270  8060  8210  8260  8910  8540  8180  7430  6880
1992  7360  7560  8800  7550  7900  8320  7430                              

FUTURE data
      Jan  Feb  Mar  Apr  May  Jun  Jul  Aug  Sep  Oct  Nov  Dec
1992                                    7650 7460 8580 7300 6530
1993 7070 6940 7060 7470 6190 6310 7090 7310 6310 7270 7240 6410
1994 7470                                   
# Install pacakges if they are not already installed: necessary_packages => vector
necessary_packages <- c("forecast", "Mcomp")

# Create a vector containing the names of any packages needing installation:
# new_pacakges => vector
new_packages <- necessary_packages[!(necessary_packages %in%
                                       installed.packages()[, "Package"])]

# If the vector has more than 0 values, install the new pacakges
# (and their) associated dependencies:
if(length(new_packages) > 0){install.packages(new_packages, dependencies = TRUE)}

# Initialise the packages in the session: list of boolean => stdout (console)
lapply(necessary_packages, require, character.only = TRUE)

# Subset the M3 data to contain the relevant series: 
ts.data <- subset(M3, 12)[[551]]

# Extract the historical data: 
historical_production_ts <- ts.data[["x"]]

# Set the seed for reproducibility: 
set.seed(2020)

# It is clear from this series we have trend and seasonality:
decomposed_ts <- decompose(historical_production_ts)

# Chart the decomposed series: 
plot(decomposed_ts)

# Note: we have to make series stationary in order to compute 
# an ARIMA model, to do this we need to account for trend and
# seasonality: 

# Use a unit root test to determine the number of differences required 
# to make the trend of the series stationary: 
trend_required_diffs <- ndiffs(decomposed_ts$trend)

# Use a unit root test to determin  the number of differences required 
# to make the seasonality of the series stationary: 
seasonality_required_diffs <- ndiffs(decomposed_ts$seasonal)

# Create an auto-arima model and store the result in the fit variable:
fit <- auto.arima(historical_production_ts,
           # Account for trend:
           d = trend_required_diffs, 
           # Account for seasonality: 
           D = seasonality_required_diffs, 
           # Try out alot of different models: 
           stepwise = FALSE,
           # Don't approximate the AIC: 
           approximation = FALSE)

# Check the Mean Absolute Error (MAE) of the model: 
data.frame(accuracy(fit))[,"MAE"]

# Forecast out the number of future observations required:  
aa_fcast <- forecast(fit, length(ts.data$xx))

# Chart the forecast:
autoplot(aa_fcast) + 
  autolayer(ts.data$xx, series = "Actual Production (Future)") +
  autolayer(aa_fcast$mean, series = "Forecasts")

# A function to calculate the MAE: 
mae <- function(actual, pred){
  mean(abs(pred-actual))
}

# Calculate the accuracy of the forecast against the future data: 
mean_abs_error <- mae(ts.data$xx, data.frame(aa_fcast)[,"Point.Forecast", drop = TRUE])