使用`pattern`正确排序

时间:2014-10-22 11:39:08

标签: r sorting

我的代码太长,无法粘贴到此处,因此请发送电子邮件至sgreenaway@vmware.com,我将向您发送我的代码

我使用if函数编写了一个代码,每个if循环(其中212个)的结果给出了“n”的结果或来自acf图的滞后总和。我想找到最小值,然后在if-loop

中调用相应的命令

这里我选择了一些if循环,这样你就可以看到排序的工作原理

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否则我在调用相应的编号命令时会得到错误的结果。例如,如果我打电话给l1 [11]我得到$ a100而不是$ a11

1 个答案:

答案 0 :(得分: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"