2015-09-27 148 views
0

我試圖預測一個時間序列,看起來像這樣不工作(這是測試數據)ARIMA時間序列預測中的R

[1] 1 1 1 1 4 1 3 4 5 6 5 1 1 1 1 1 5 3 4 7 5 5 6 4 1 1 1 1 1 1 4 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 8 2 

我從美國的一個州獲得的數據。比方說,阿拉巴馬這樣

res <- subset(d, states == 'Alabama', select = c(levels, weeks)) 

然後我把級別的數據到這樣的時間序列。

tsn = ts(res[[1]]) 

然後我得到最好的ARIMA模型擬合這樣

aa <- auto.arima(tsn) 

對於其結果是這樣的

Series: tsn 
ARIMA(1,0,0) with non-zero mean 

Coefficients: 
     ar1 intercept 
     0.4722  2.2833 
s.e. 0.1252  0.4644 

sigma^2 estimated as 2.989: log likelihood=-94.51 
AIC=195.03 AICc=195.57 BIC=200.64 

然後我嘗試使用預測功能這樣

forecast(aa) 

這爲w母雞我得到這個錯誤

Error in forecast(aa) : unused argument (aa) 

不知道如何做預測的工作?

編輯添加代碼

這是數據看起來的樣子

st  URL       WEBSITE     al  aln  wk   WEEKSEASON 
Alabama http://adph.org/influenza/ Influenza Surveillance Level 1 Minimal Oct-04-2008 40 2008-09 
Alabama http://adph.org/influenza/ Influenza Surveillance Level 1 Minimal Oct-11-2008 41 2008-09 
Alaska http://adph.org/influenza/ Influenza Surveillance Level 1 Minimal Oct-18-2008 42 2008-09 
Alaska http://adph.org/influenza/ Influenza Surveillance Level 1 Minimal Oct-25-2008 43 2008-09 

這是怎樣的代碼看起來像

library(forecast) 
library(tseries) 

#Extracts relevant data from the csv file 
extract_data<-function(){ 

    #open the file. NAME SHOULD BE CHANGED 
    sd <- read.csv(file="sdr.csv",head=TRUE,sep=",") 

    #Extracts the data from the ACTIVITY LEVEL column. Notice that the name of the column was changed on the file 
    #to 'al' to make the reference easier 
    lv_list <- sd$al 
    #Gets only the number from each value getting rid of the word "Level" 
    lvs <- sapply(strsplit(as.character(lv_list), " "), function(x) x[2]) 

    #Gets the ACTIVITY LEVEL NAME. Column name was changed to 'aln' on the file 
    lvn_list <- sd$aln 

    #Gets the state. Column name was changed to 'st' on the file 
    st_list <- sd$st 

    #Gets the week. Column name was changed to 'wk' on the file 
    wlist <- sd$wk 
    #Divides the weeks data in month, day, year 
    wks <- sapply(strsplit(as.character(wlist), "-"), function(x) c(x[1], x[2], x[3])) 

    #Creates a data frame with the selected results. You can choose which data is needed. 
    result<-data.frame("states"=st_list,"levels"=lvs,"lvlnames"=lvn_list,"weeks"=wlist) 

    return(result) 

} 

forecast<-function(){ 

    d=extract_data() 

    #Get data from each state 
    res <- subset(d, states == 'Alabama', select = c(levels, weeks)) 

    #turn data into a time series 
    tsn = ts(res[[1]]) 

    #Plot forecast data with ARIMA models (use differenciated data if needed) 
    aa <- auto.arima(tsn) 
    forecast(aa) 

    return(0) #return results 
} 
+0

'預測(auto.arima(TS(c(1,1,4,1,3,4))))'我的作品。 –

回答

2

變量aa是模型估計數據d。如下使用從aa使用ARIMA(1,0,0)並將其插入forecast.Arima

f <- forecast.Arima(d, order=c(1,0,0)) 

我在blog上做了一些例子。祝你好運!

+0

它實際上是通過將auto.arima的結果賦予forecast.Arima函數來實現的。謝謝! – Atirag