# [R] Using sapply instead of for loop

Amit Thombre amitmt at techmahindra.com
Wed Nov 19 10:47:48 CET 2014

```I am trying to replace a for loop by using sapply, The code is for forecasting using arima. The code is as follows:-
-------------------------------------------------------
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)
}
---------------------------
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.

st=2013   # start year value for getting the time series
month=4 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)
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
ervalue<-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
}
------------------------------------------------------------------
The sapply replaces the for loop
for (p in 1:Permax)
{
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), lambda=lbda)  # the arima model
A[i,j,k,l,m,n,p]<-AIC(air.model)
f<- forecast(air.model,h=testsize1) # for getting the error
er[i,j,k,l,m,n,p]<-errf(act1,f\$mean,testsize1,flagarima)
}, error=function(e)
{

return(NA)
}
)
cat("Value of error", er[i,j,k,l,m,n,p])
cat("Value of i,j,k,l,m,n,p", i, j, k, l, m, n,p)
}
--------------------------------------------------------------------------
Now the er[I,j,k,l,m,n,p] I.e the error get populated but on every call to the function far() the array loses the previous value and gets replaced with NA and gets the newly calculated error value. Finally the array A gets populated with only the latest value and does not hold the old values. Please help

============================================================================================================================
Disclaimer:  This message and the information contained herein is proprietary and confidential and subject to the Tech Mahindra policy statement, you may review the policy at http://www.techmahindra.com/Disclaimer.html externally http://tim.techmahindra.com/tim/disclaimer.html internally within TechMahindra.
============================================================================================================================

[[alternative HTML version deleted]]

```