Skip to content
Prev 345780 / 398502 Next

Using sapply instead of for loop

Charles ,

I am not getting an error . The final array A does not have the values in it. Here is the reproducible code.  I have even tried using paasing ervalue as a parameter to the function far.

-------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------

errf<-function(act, res, testsize, flag)
{
j=1
if(flag==1)
{
j<-nrow(d)-testsize
}

print(act)
print(res)
print(flag)
diff<-0
s<-0
# loop for iterating to each value of the actual value and finding the difference with thepredicted value
for (mn in 1:length(act))
{
cat("Value of mn in err", mn)
cat("Value of j in err", j)
cat("Value of res[j] in err", res[j])
diff<-(act[mn]-res[j])
print(act[mn])
print(res[j])
print(diff)
s<-s+(diff*diff)

j<-j+1
}

er1<-sqrt(s/length(act)) #forecasting error
print(er1)
return(er1)
}



far<-function(p)
{

cat("does it come here value of p", p)
tryCatch({
air.model <-Arima(tsa,order=c(i-1,j-1,k-1), seasonal=list(order=c(l-1,m-1,n-1),period=p-1), lambda=lbda)  # the arima model

f<- forecast(air.model,h=testsize1) # for getting the error

ervalue[i,j,k,l,m,n,p]<-errf(act1,f$mean,testsize1,flagarima)

}, error=function(e)
{

return(NA)
}
)
cat("Value of error", ervalue[i,j,k,l,m,n,p])
cat("Value of i,j,k,l,m,n,p", i, j, k, l, m, n,p)
print(ervalue)
return(ervalue)
}
---------------------------
library('TTR')
library('forecast')
library('timeSeries')
library('xts')
library('RODBC')


maxval=2  # set the array size as well as the maximum parameter value here.
pmax=maxval  # set max p value of the ARIMA model
dmax=maxval  # set max d value of the ARIMA model
qmax=maxval  # set max q value of the ARIMA model
Pmax=maxval  # set max P value of the ARIMA model
Dmax=maxval  # set max D value of the ARIMA model
Qmax=maxval  # set max Q value of the ARIMA model
Permax=2     # maximum value of period.
freq=12
d<-c(10, 13, 14, 4, 5, 6, 7, 10, 12, 13, 14, 20, 3, 4, 5, 19, 23, 21, 18, 19, 21, 14, 15, 16, 17, 12, 20, 19, 17)
st=2013   # start year value for getting the time series
month=4
tsa<-ts(d, frequency=freq, start=c(st,month))  # store the data in tsa as  the time

A<-array(, c(maxval,maxval,maxval,maxval,maxval,maxval, 2)) # depdending on the max value set the , also it stores the AIC valuearray size
er<-array(, c(maxval,maxval,maxval,maxval,maxval,maxval,2)) # depdending on the max value set the , stores the error value.array size
ervalue<-array(, c(maxval,maxval,maxval,maxval,maxval,maxval, 2)) # depdending on the max value set the , stores the error value.array size
erval1<-array(, c(maxval,maxval,maxval,maxval,maxval,maxval, 2)) # depdending on the max value set the , stores the error value.array size
for (i in 1:pmax)
{
for (j in 1:dmax)
{
for (k in 1:qmax)
{
for (l in 1:Pmax)
{
for (m in 1:Dmax)
{
for (n in 1:Qmax)
{
A<-sapply((1:Permax),function(p) far(p),simplify=FALSE)

}
}
}
}
}  #for looping through period value
}