- 一、變平穩
1.1 去趨勢
差分、取對數
1.2去週期性
濾波、差分
1.3變平穩的原因除了1.1、1.2之外還有
穩定方差、使數據呈現正態分佈、使週期效應累加
ARIMA模型的示例代碼
from pandas import read_csv
from pandas import datetime
from matplotlib import pyplot
from statsmodels.tsa.arima_model import ARIMA
from sklearn.metrics import mean_squared_error
def parser(p):
return datetime.strptime('190'+p,'%Y-%m)
series = read_csv('input.csv',header = 0,parse_dates = [0],index_col = 0,squeeze = True,date_parser = parser)
P = series.values
size = int(len(P)*0.66)
train,test = P[0:size],P[size:len(P)]
history = [p for p in train]
predictions = list()
for t in range(len(test)):
model = ARIMA(history,order=(5,1,0))
model_fit = model.fit(disp=0)
output = model_fit.forecast()
yhat = output[0]
predictions.append(yhat)
obs = test[t]
history.append(obs) # 滑動窗口
print('predicted = %f,expected = %f' %(yhat,obs))
error = mean_squared_error(test,predictions)
print('Test MSE: %.3f' % error)
pyplot.plot(test)
pyplot.plot(predictions,color='red')
pyplot.show()
R代碼
auto.ARIMA模型的優點在於它自動查找數據的順序、趨勢和週期性
asm_weekwise <- read.csv('weekwise.csv',header = TRUE)
asm_weekwise$Week <- NULL
library(MASS)
library(tseries)
library(forecast)
asm_weekwise[is.na(asm_weekwise)] <- 0
asm_weekwise[asm_weekwise <= 0] <- mean(as.matrix(asm_weekwise))
weekjoyforecastvalues <- date.frame("asm"=interger(),"value" = integer(),stringsAaFactors=FALSE)
for (i in 2:ncol(asm_weekwise))
{
asmname <- names(asm_weekwise)[i]
temparimadata <- asm_weekwise[,i]
m <- mean(as.matrix(temparimadata))
s <- sd(temparimadata)
temparimadata <- (temparimadata - m)
temparimadata <- (temparimadata / s)
temparima <- auto.arima(temparimadata,stationary = FALSE,seasonal = TRUE,allowdrift=TRUE,allowmean=FALSE,biasadj=FALSE)
tempforecast <- forecast(temparima,h=12)
temp_forecastes_data <- sum(data.frame(temp_forecastes$mean)*s + m)
weekjoyforecastvalues[nrow(weekjoyforecastvalues) + 1, ] <- c(asmname,temp_forecasted_data)
}
weekjoyforecastvalues$value <- as.integer(weekjoyforecastvalues$value)