2014-10-22 76 views
2

我的代碼太長,這裏貼上所以發郵件到[email protected],我將使用if函數和送你我的代碼訂購正確使用`pattern`

我已經寫了代碼每個if循環的結果(其中212個)給出「n」的結果或來自acf圖的滯後總和。我想找到的最低值,然後回憶中,如果環

這裏香港專業教育學院選擇了幾個,如果環路的相應命令,所以你可以看到的排序是如何工作的

M<-matrix(c("08Q1","08Q2","08Q3","08Q4","09Q1","09Q2","09Q3","09Q4","10Q1","10Q2","10Q3","10Q4","11Q1","11Q2","11Q3","11Q4","12Q1","12Q2","12Q3","12Q4","13Q1","13Q2","13Q3","13Q4","14Q1","14Q2","14Q3",155782.698,159463.6534,172741.1256,204547.18,126049.3198,139881.9102,140747.2786,251962.9696,182444.2912,207780.8227,189251.1889,318053.6736,230569.1533,247826.8104,237019.5556,383909.5231,265145.4548,264816.362,239607.0146,436403.1441,276767.6893,286337.8543,270022.6845,444672.8604,263717.216,343143.9422,271701.7404),ncol=2,byrow=FALSE) 
Nu <- M[, length(M[1,])] 
Nu <- ts(Nu, deltat=deltaT, start = startY) 
N<-log(Nu) 
orderWA1<-c(0,0,0) 
orderWS1<-c(0,0,0) 
ArimaW1 <- Arima(N, order= orderWA1, seasonal=list(order=orderWS1), method="ML") 
if(aslog=="y"){Arimafit<-exp(fitted(ArimaW1))}else{Arimafit<-fitted(ArimaW1)} 
nnn<-c(N) 
arimab<-c(Arimafit) 
fullres<-nnn-arimab 
v<-acf(fullres,plot=FALSE) 
w<-pacf(fullres,plot=FALSE) 
if(v$acf[2]>0.4|v$acf[2]<(-0.4)|v$acf[3]>0.4|v$acf[3]<(-0.4)|v$acf[4]>0.4|v$acf[4]<(-0.4)|v$acf[5]>0.4|v$acf[5]<(-0.4)|v$acf[6]>0.4|v$acf[6]<(-0.4)|v$acf[7]>0.4|v$acf[7]<(-0.4)|w$acf[1]>0.4|w$acf[1]<(-0.4)|w$acf[2]>0.4|w$acf[2]<(-0.4)|w$acf[3]>0.4|w$acf[3]<(-0.4)|w$acf[4]>0.4|w$acf[4]<(-0.4)|w$acf[5]>0.4|w$acf[5]<(-0.4)|w$acf[6]>0.4|w$acf[6]<(-0.4)) 
    a01<-"n" else 
    {a01<-sum(abs(v$acf[2:7])) 
    b01<-sum(abs(w$acf[1:6]))} 
orderWA2<-c(0,0,0) 
orderWS2<-c(0,0,1) 
ArimaW1 <- Arima(N, order= orderWA2, seasonal=list(order=orderWS2), method="ML") 
if(aslog=="y"){Arimafit<-exp(fitted(ArimaW1))}else{Arimafit<-fitted(ArimaW1)} 
nnn<-c(N) 
arimab<-c(Arimafit) 
fullres<-nnn-arimab 
v<-acf(fullres,plot=FALSE) 
w<-pacf(fullres,plot=FALSE) 
if(v$acf[2]>0.4|v$acf[2]<(-0.4)|v$acf[3]>0.4|v$acf[3]<(-0.4)|v$acf[4]>0.4|v$acf[4]<(-0.4)|v$acf[5]>0.4|v$acf[5]<(-0.4)|v$acf[6]>0.4|v$acf[6]<(-0.4)|v$acf[7]>0.4|v$acf[7]<(-0.4)|w$acf[1]>0.4|w$acf[1]<(-0.4)|w$acf[2]>0.4|w$acf[2]<(-0.4)|w$acf[3]>0.4|w$acf[3]<(-0.4)|w$acf[4]>0.4|w$acf[4]<(-0.4)|w$acf[5]>0.4|w$acf[5]<(-0.4)|w$acf[6]>0.4|w$acf[6]<(-0.4)) 
    a02<-"n" else 
    {a02<-sum(abs(v$acf[2:7])) 
    b02<-sum(abs(w$acf[1:6]))} 
orderWA10<-c(0,0,0) 
orderWS10<-c(1,0,0) 
ArimaW1 <- Arima(N, order= orderWA10, seasonal=list(order=orderWS10), method="ML") 
if(aslog=="y"){Arimafit<-exp(fitted(ArimaW1))}else{Arimafit<-fitted(ArimaW1)} 
nnn<-c(N) 
arimab<-c(Arimafit) 
fullres<-nnn-arimab 
v<-acf(fullres,plot=FALSE) 
w<-pacf(fullres,plot=FALSE) 
if(v$acf[2]>0.4|v$acf[2]<(-0.4)|v$acf[3]>0.4|v$acf[3]<(-0.4)|v$acf[4]>0.4|v$acf[4]<(-0.4)|v$acf[5]>0.4|v$acf[5]<(-0.4)|v$acf[6]>0.4|v$acf[6]<(-0.4)|v$acf[7]>0.4|v$acf[7]<(-0.4)|w$acf[1]>0.4|w$acf[1]<(-0.4)|w$acf[2]>0.4|w$acf[2]<(-0.4)|w$acf[3]>0.4|w$acf[3]<(-0.4)|w$acf[4]>0.4|w$acf[4]<(-0.4)|w$acf[5]>0.4|w$acf[5]<(-0.4)|w$acf[6]>0.4|w$acf[6]<(-0.4)) 
    a10<-"n" else 
    {a10<-sum(abs(v$acf[2:7])) 
    b10<-sum(abs(w$acf[1:6]))} 
orderWA11<-c(0,0,0) 
orderWS11<-c(1,0,1) 
ArimaW1 <- Arima(N, order= orderWA11, seasonal=list(order=orderWS11), method="ML") 
if(aslog=="y"){Arimafit<-exp(fitted(ArimaW1))}else{Arimafit<-fitted(ArimaW1)} 
nnn<-c(N) 
arimab<-c(Arimafit) 
fullres<-nnn-arimab 
v<-acf(fullres,plot=FALSE) 
w<-pacf(fullres,plot=FALSE) 
if(v$acf[2]>0.4|v$acf[2]<(-0.4)|v$acf[3]>0.4|v$acf[3]<(-0.4)|v$acf[4]>0.4|v$acf[4]<(-0.4)|v$acf[5]>0.4|v$acf[5]<(-0.4)|v$acf[6]>0.4|v$acf[6]<(-0.4)|v$acf[7]>0.4|v$acf[7]<(-0.4)|w$acf[1]>0.4|w$acf[1]<(-0.4)|w$acf[2]>0.4|w$acf[2]<(-0.4)|w$acf[3]>0.4|w$acf[3]<(-0.4)|w$acf[4]>0.4|w$acf[4]<(-0.4)|w$acf[5]>0.4|w$acf[5]<(-0.4)|w$acf[6]>0.4|w$acf[6]<(-0.4)) 
    a11<-"n" else 
    {a11<-sum(abs(v$acf[2:7])) 
    b11<-sum(abs(w$acf[1:6]))} 
orderWA12<-c(0,0,0) 
orderWS12<-c(1,0,2) 
ArimaW1 <- Arima(N, order= orderWA12, seasonal=list(order=orderWS12), method="ML") 
if(aslog=="y"){Arimafit<-exp(fitted(ArimaW1))}else{Arimafit<-fitted(ArimaW1)} 
nnn<-c(N) 
arimab<-c(Arimafit) 
fullres<-nnn-arimab 
v<-acf(fullres,plot=FALSE) 
w<-pacf(fullres,plot=FALSE) 
if(v$acf[2]>0.4|v$acf[2]<(-0.4)|v$acf[3]>0.4|v$acf[3]<(-0.4)|v$acf[4]>0.4|v$acf[4]<(-0.4)|v$acf[5]>0.4|v$acf[5]<(-0.4)|v$acf[6]>0.4|v$acf[6]<(-0.4)|v$acf[7]>0.4|v$acf[7]<(-0.4)|w$acf[1]>0.4|w$acf[1]<(-0.4)|w$acf[2]>0.4|w$acf[2]<(-0.4)|w$acf[3]>0.4|w$acf[3]<(-0.4)|w$acf[4]>0.4|w$acf[4]<(-0.4)|w$acf[5]>0.4|w$acf[5]<(-0.4)|w$acf[6]>0.4|w$acf[6]<(-0.4)) 
    a12<-"n" else 
    {a12<-sum(abs(v$acf[2:7])) 
    b12<-sum(abs(w$acf[1:6]))} 
orderWA100<-c(0,2,0) 
orderWS100<-c(2,0,2) 
ArimaW1 <- Arima(N, order= orderWA100, seasonal=list(order=orderWS100), method="ML") 
if(aslog=="y"){Arimafit<-exp(fitted(ArimaW1))}else{Arimafit<-fitted(ArimaW1)} 
nnn<-c(N) 
arimab<-c(Arimafit) 
fullres<-nnn-arimab 
v<-acf(fullres,plot=FALSE) 
w<-pacf(fullres,plot=FALSE) 
if(v$acf[2]>0.4|v$acf[2]<(-0.4)|v$acf[3]>0.4|v$acf[3]<(-0.4)|v$acf[4]>0.4|v$acf[4]<(-0.4)|v$acf[5]>0.4|v$acf[5]<(-0.4)|v$acf[6]>0.4|v$acf[6]<(-0.4)|v$acf[7]>0.4|v$acf[7]<(-0.4)|w$acf[1]>0.4|w$acf[1]<(-0.4)|w$acf[2]>0.4|w$acf[2]<(-0.4)|w$acf[3]>0.4|w$acf[3]<(-0.4)|w$acf[4]>0.4|w$acf[4]<(-0.4)|w$acf[5]>0.4|w$acf[5]<(-0.4)|w$acf[6]>0.4|w$acf[6]<(-0.4)) 
    a100<-"n" else 
    {a100<-sum(abs(v$acf[2:7])) 
    b100<-sum(abs(w$acf[1:6]))} 
orderWA101<-c(0,2,1) 
orderWS101<-c(0,0,0) 
ArimaW1 <- Arima(N, order= orderWA101, seasonal=list(order=orderWS101), method="ML") 
if(aslog=="y"){Arimafit<-exp(fitted(ArimaW1))}else{Arimafit<-fitted(ArimaW1)} 
nnn<-c(N) 
arimab<-c(Arimafit) 
fullres<-nnn-arimab 
v<-acf(fullres,plot=FALSE) 
w<-pacf(fullres,plot=FALSE) 
if(v$acf[2]>0.4|v$acf[2]<(-0.4)|v$acf[3]>0.4|v$acf[3]<(-0.4)|v$acf[4]>0.4|v$acf[4]<(-0.4)|v$acf[5]>0.4|v$acf[5]<(-0.4)|v$acf[6]>0.4|v$acf[6]<(-0.4)|v$acf[7]>0.4|v$acf[7]<(-0.4)|w$acf[1]>0.4|w$acf[1]<(-0.4)|w$acf[2]>0.4|w$acf[2]<(-0.4)|w$acf[3]>0.4|w$acf[3]<(-0.4)|w$acf[4]>0.4|w$acf[4]<(-0.4)|w$acf[5]>0.4|w$acf[5]<(-0.4)|w$acf[6]>0.4|w$acf[6]<(-0.4)) 
    a101<-"n" else 
    {a101<-sum(abs(v$acf[2:7])) 
    b101<-sum(abs(w$acf[1:6]))} 
l1<-mget(ls(pattern="^a\\d+")) 
k<-unlist(l1) 
j<-match(min(k),k) 
orderWA<-paste("orderWA",j,sep="") 
orderWS<-paste("orderWS",j,sep="") 
if(orderWA=="orderWA1") 
{orderWA<-orderWA1 
orderWS<-orderWS1} else 
    if(orderWA=="orderWA2") 
    {orderWA<-orderWA2 
    orderWS<-orderWS2} else 
if(orderWA=="orderWA10") 
    {orderWA<-orderWA10 
     orderWS<-orderWS10}else 
     if(orderWA=="orderWA11") 
      {orderWA<-orderWA11 
      orderWS<-orderWS11}else 
      if(orderWA=="orderWA12") 
       {orderWA<-orderWA12 
       orderWS<-orderWS12} else 
        if(orderWA=="orderWA100") 
         {orderWA<-orderWA100 
         orderWS<-orderWS100}else 
         if(orderWA=="orderWA101") 
         {orderWA<-orderWA101 
         orderWS<-orderWS101}else 
          NULL 

這裏是第一16分的結果作爲一個例子

> l1 
$a01 
[1] "n" 

$a02 
[1] "n" 

$a03 
[1] 1.210138 

$a04 
[1] "n" 

$a05 
[1] "n" 

$a06 
[1] "n" 

$a07 
[1] "n" 

$a08 
[1] "n" 

$a09 
[1] "n" 

$a10 
[1] "n" 

$a100 
[1] "n" 

$a101 
[1] "n" 

$a102 
[1] "n" 

$a103 
[1] "n" 

$a104 
[1] 0.8426679 

$a105 
[1] 0.7266627 

我需要它來正確地從$ A01訂購:$ A212否則我得到錯誤的結果時,我調用相應編號的命令。 e.g如果我叫L1 [11]我得到$ A100,而不是$ A11

+0

最好指定使用的軟件包。對於例如'Arima'。是否來自'預測' – akrun 2014-10-22 11:59:03

+2

是的抱歉,我使用的軟件包是系列和預測 – 2014-10-22 12:01:05

+0

找不到代碼中的一些對象。 'Arima錯誤(N,order = orderWA1,seasonal = list(order = orderWS1),: object'N'not found'這樣會更好地展示一個可重複的例子 – akrun 2014-10-22 12:21:54

回答

3

這裏就是你們的榜樣的簡化版本:

x <- sapply(sort(paste0("a",sprintf("%02d",1:212))),identity,simplify=FALSE) 
x[11] 
$a100 
[1] "a100" 

的問題是,該列表由列表names按字母順序排序。你想要做的是重新排列它們,以便按照「a」之後的數字部分進行排序。

x.reordered <- x[order(as.numeric(sub("a","",names(x))))] 
x.reordered[11] 
$a11 
[1] "a11" 
+2

這完美的作品!謝謝你這麼好! – 2014-10-22 14:33:22