[R] Using sapply instead of for loop
Amit Thombre
amitmt at techmahindra.com
Wed Nov 19 15:23:12 CET 2014
If I pass all the variables to the function in the following way then I get the following error "Error in cat("Value of error", ervalue[i, j, k, l, m, n, p]) :
argument "ervalue" is missing, with no default. Finally the A array should have all the root mean square value calculated for each run which is missing even using global variables and even using the variables paaed to t afunction( I get the error so the code does not fill A)
----------------------------------------------------------------
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, i, j, k, l, m, n, ervalue)
{
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, i, j, k, l, m, n, ervalue) far(p, i, j, k, l, m,n, ervalue),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)
}
________________________________
From: Charles Determan Jr [deter088 at umn.edu]
Sent: Wednesday, November 19, 2014 7:05 PM
To: Amit Thombre
Cc: r-help at r-project.org
Subject: Re: [R] Using sapply instead of for loop
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<mailto: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<mailto: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
============================================================================================================================
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]]
More information about the R-help
mailing list