[R] Using sapply instead of for loop

Charles Determan Jr deter088 at umn.edu
Wed Nov 19 14:35:52 CET 2014


Amit,

Your question isn't necessarily complete.  You haven't provided a
reproducible example of your data or an error message.  At first glance you
aren't passing anything to your 'far' function except for 'p' and yet it
uses i,j,k,l,m,n,testsize1, and act1.  You should generally try to avoid
global variables as they can lead to broken code.  You should redefine your
function with all the needed parameters and try again.

Regards,

On Wed, Nov 19, 2014 at 3:47 AM, Amit Thombre <amitmt at techmahindra.com>
wrote:

> 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]]
>
> ______________________________________________
> R-help at r-project.org mailing list
> https://stat.ethz.ch/mailman/listinfo/r-help
> PLEASE do read the posting guide
> http://www.R-project.org/posting-guide.html
> and provide commented, minimal, self-contained, reproducible code.
>



-- 
Dr. Charles Determan, PhD
Integrated Biosciences

	[[alternative HTML version deleted]]



More information about the R-help mailing list