2

My code is too long to paste here so send an email to sgreenaway@vmware.com and i will send you my code

I have written a code using if functions and the results of each if loop (212 of them) give a result of "n" or the sum of lags from an acf graph. I want to find the minimum value and then recall the corresponding commands within that if-loop

Here ive chosen a few of the if loops so you can see how the ordering works

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

Here are the first 16 results as an example

> 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

I need it to be ordered properly from $a01:$a212 otherwise i get the wrong results when i call the corresponding numbered command. e.g if i call l1[11] i get $a100 instead of $a11

Machavity
  • 30,841
  • 27
  • 92
  • 100
  • It is better to specify the packages used. For e.g. `Arima`. Is it from `forecast` – akrun Oct 22 '14 at 11:59
  • 2
    yes sorry the packages i've used are tseries and forecast – Emma Diamond Oct 22 '14 at 12:01
  • Some objects in the code are not found. `Error in Arima(N, order = orderWA1, seasonal = list(order = orderWS1), : object 'N' not found` It would be better to show a reproducible example. – akrun Oct 22 '14 at 12:21
  • 2
    i was just showing an example of my code, it wasnt meant to be runable...i can send you the whole code via email....sgreenaway@vmware.com email me and ill send it to you – Emma Diamond Oct 22 '14 at 12:32
  • If this is not runnable, it may be better to show only the relevant part and focus on the specific question. – akrun Oct 22 '14 at 12:35
  • It is much easier to help you if you provide a **minimal**, self contained example. Check these links for general ideas, and how to do it in R: [**here**](http://stackoverflow.com/help/mcve), [**here**](http://www.sscce.org/), [**here**](http://adv-r.had.co.nz/Reproducibility.html), and [**here**](http://stackoverflow.com/questions/5963269/how-to-make-a-great-r-reproducible-example/5963610#5963610). – Henrik Oct 22 '14 at 12:36
  • 2
    thank-you for not marking down the question and giving me chance to improve it :) – Emma Diamond Oct 22 '14 at 12:39
  • 2
    ive added in a selection of code that may be better – Emma Diamond Oct 22 '14 at 12:50

1 Answers1

3

Here is a simpler version of your example:

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

The issue is that the list is sorted alphabetically by the list names. What you want to do is re-order them so that it is sorted by the numerical part after the "a".

x.reordered <- x[order(as.numeric(sub("a","",names(x))))]
x.reordered[11]
$a11
[1] "a11"
James
  • 65,548
  • 14
  • 155
  • 193