这是一个团体门票流入的时间序列,试图估算 接下来三个月的流入量。
时间序列预测的新手,尝试通过引用一些示例来逐步实现它。
inflow<-c(487,424,353,2116,1740,3767,4267,5278,4042,7224)
plot(inflow)
# Monthly time series
inflow_ts<-ts(inflow,start=c(2017,10),end=c(2018,7),frequency=12)
print(inflow_ts,calendar = T)
# Plotting the time series
plot.ts(inflow_ts)
#Forecast
#Step1 : Model Identification
#Stationarity Test - Disckey Fuller Test
# Null hypothesis is that Time Series is not stationary
adf.test(inflow_ts,alternative = "stationary")
# Opscomputeinflow_ts is not stationary since p value is > 0.5
#Dickey-Fuller = -1.6092, Lag order = 2, p-value = 0.7213
# Differenciate the time series - Level1
diff1_inflow_ts<- diff(inflow_ts)
plot(diff1_inflow_ts)
adf.test(diff1_inflow_ts,alternative = "stationary")
# diff1_Opscomputeinflow_ts is not stationary since p value is > 0.5
# Dickey-Fuller = -1.1885, Lag order = 2, p-value = 0.8815
# Differenciate the time series - Level2
diff2_inflow_ts<- diff(diff1_inflow_ts)
plot(diff2_inflow_ts)
adf.test(diff2_inflow_ts,alternative = "stationary")
# diff2_Opscomputeinflow_ts is not stationary since p value is > 0.5
# Dickey-Fuller = -1.4501, Lag order = 1, p-value = 0.7819
# Differenciate the time series - Level3
diff3_inflow_ts<- diff(diff2_inflow_ts)
plot(diff3_inflow_ts)
adf.test(diff3_inflow_ts,alternative = "stationary")
# diff3_Opscomputeinflow_ts is not stationary since p value is > 0.5
# Dickey-Fuller = -0.84809, Lag order = 1, p-value = 0.9429
# Differenciate the time series - Level4
diff4_inflow_ts<- diff(diff3_inflow_ts)
plot(diff4_inflow_ts)
adf.test(diff4_inflow_ts,alternative = "stationary")
#Try log to make it stationary
# Take log of the time series
log_inflow_ts<- log(inflow_ts)
plot(log_inflow_ts)
adf.test(log_inflow_ts,alternative = "stationary")
# log_Opscomputeinflow_ts is not stationary since p value is > 0.5
# Dickey-Fuller = -1.1857, Lag order = 2, p-value = 0.8826
# Take log and diff of the time series
diff_log_inflow_ts<- diff(log(inflow_ts))
plot(diff_log_inflow_ts)
adf.test(diff_log_inflow_ts,alternative = "stationary")
# diff_log_Opscomputeinflow_ts is stationary since p value is < 0.5
# Dickey-Fuller = -5.9701, Lag order = 2, p-value = 0.01
# check ACF and PACF plots
acf(diff_log_inflow_ts)
pacf(diff_log_inflow_ts)
# No significance levels of AR or MA
arima(diff_log_inflow_ts,c(0,0,0))
# Step -4 Diagnosis
arima.final<-arima(diff_log_inflow_ts,c(0,0,0))
tsdiag(arima.final)
#forecast using final model
predicted<-predict(arima.final,n.ahead = 3)
predicted
输出:
$pred
Aug Sep Oct
2018 0.2996556 0.2996556 0.2996556
$se
Aug Sep Oct
2018 0.6285461 0.6285461 0.6285461
我试图将其转换为实际值。但无法获得确切的值
使用exp和diffinv获得实际值。
> diffinv(exp(0.2996556))
[1] 0.000000 1.349394