2013-03-28 58 views
3

你好我使用包預測來做時間序列預測。 我想知道如何在最終預測圖上取消一系列。有了預測軟件包,我不知道如何取消註冊我的系列。這裏有一個例子:在使用包預測時取消一個時間序列

library(forecast) 
data <- AirPassengers 
data <- log(data) #with this AirPassengers data not nessesary to LOG but with my private data it is...because of some high picks... 
ARIMA <- arima(data, order = c(1, 0, 1), list(order = c(12,0, 12), period = 1)) #Just a fake ARIMA in this case... 
plot(forecast(ARIMA, h=24)) #but my question is how to get a forecast plot according to the none log AirPassenger data 

enter image description here

所以圖像被記錄下來。我想要有相同的ARIMA模型,但是沒有數據。

+0

指數函數是對數函數的逆函數。 – ndoogan 2013-03-28 04:02:14

+0

@ndoogan這是真的,但沒有用。 – 2013-03-28 04:30:15

回答

2

這是一個黑客,但它似乎做你想做的。根據您的擬合模型ARIMA

fc<-forecast(ARIMA,h=24) 
fc$mean<-exp(fc$mean) 
fc$upper<-exp(fc$upper) 
fc$lower<-exp(fc$lower) 
fc$x<-exp(fc$x) 

現在繪製它

plot(fc) 

enter image description here

+0

非常感謝。完全是我想要的。 ;) – S12000 2013-03-28 04:55:35

+1

請注意,log(E(y))不等於E(log(y)),即。如果您僅使用上述轉換,則您的預測稍有偏差。 – 2013-03-28 05:59:01

+0

@hemmo本着SO的精神,你能爲OP提供一個新的更好的答案嗎?或者,編輯我發佈的那個。另外,你能提供一個log(exp(y))!= exp(log(y))是什麼意思的例子嗎?這是精確的事情嗎?更深的東西? – ndoogan 2013-03-28 12:15:06

7

與@ ndoogan的回答的問題是,對數不是一個線性變換。這意味着E [exp(y)]!= exp(E [y])。 Jensen's inequality實際給出了E [exp(y)]> = exp(E [y])。這裏有一個簡單的例子:

set.seed(1) 
x<-rnorm(1000) 
mean(exp(x)) 
[1] 1.685356 
exp(mean(x)) 
[1] 0.9884194 

這裏的有關預測的情況下:

# Simulate AR(1) process 
set.seed(1) 
y<-10+arima.sim(model=list(ar=0.9),n=100) 

# Fit on logarithmic scale 
fit<-arima(log(y),c(1,0,0)) 

#Simulate one step ahead 
set.seed(123) 
y_101_log <- fit$coef[2]*(1-fit$coef[1]) + 
      fit$coef[1]*log(y[100]) + rnorm(n=1000,sd=sqrt(fit$sigma2)) 

y_101<-exp(y_101_log) #transform to natural scale 

exp(mean(y_101_log)) # This is exp(E(log(y_101))) 
[1] 5.86717   # Same as exp(predict(fit,n.ahead=1)$pred) 
        # differs bit because simulation 

mean(y_101)   # This is E(exp(log(y_101)))=E(y_101) 
[1] 5.904633 

# 95% Prediction intervals: 

#Naive way: 
pred<-predict(fit,n.ahead=1) 
c(exp(pred$pred-1.96*pred$se),exp(pred$pred+1.96*pred$se)) 
pred$pred pred$pred 
4.762880 7.268523 

# Correct ones: 
quantile(y_101,probs=c(0.025,0.975)) 
    2.5% 97.5% 
4.772363 7.329826 

這也提供了一個解決問題的方法在一般的意義上:

  1. 適合模型
  2. 模擬來自該模型的多個樣本(例如,如上所述的一步預測)
  3. 對於每個模擬樣品,進行逆變換,以獲得值原有規模
  4. 從這些模擬樣品,你可以計算預期值作爲一個普通的平均值,或者如果你需要的置信區間,計算實證位數。
6

這是沒有必要使用由@ndoogan提出的黑客。 forecast.Arima內置了用於撤消轉換的設施。下面的代碼會做需要什麼:

fc <- forecast(ARIMA, h=24, lambda=0) 

更妙的是,建轉變爲模型本身:你需要使用Arima功能從forecast包來完成

ARIMA <- Arima(data, order=c(1,0,1), list(order=c(1,0,1),period=12)), lambda=0) 
fc <- forecast(ARIMA, h=24) 

注,而不是從statsarima功能。

@Hemmo是正確的,這回轉型不會放棄的預測分佈的平均值,所以不是最佳的MSE的預測。但是,它會給出預測分佈的中位數,所以會給出最優的MAE預測。

最後,@ Swiss12000使用的假模型沒有什麼意義,因爲季節性部分的頻率爲1,所以與非季節性部分混淆。我想你可能是指我在上面的代碼中使用過的模型。

+0

我不確定這裏有一個很好的答案。 ARIMA預測以原始尺度返回預測 - 所以返回的結果是以log(Airlinepassengers)爲單位的預測,因爲這是ARIMA傳遞的結果。面臨的挑戰是如何將日誌(Airlinepassengers)的預測返回給Airlinepassengers。我會認爲它和exp一樣簡單(logAirlinepassengers),但我認爲這是正確的,因爲你們有些人指出。我現在面臨這個問題 - 在這篇文章2年後。如果有人有更多的見解,我會很感激。 – Windstorm1981 2016-02-28 00:01:22

+1

將會有一個選項返回預測包的v7中的平均值 - 現在在github上。 – 2016-02-28 00:58:44

相關問題