Convert for loop to apply family function for better performance


Convert for loop to apply family function for better performance



Below I am forecasting for the next 30 days . If the input data is around 100k the for loop is extremely slow (takes about 2 hours) . the code using the for loop as below.


ns<-ncol(TS) # count number of columns to run the loop

output<-matrix(NA,nrow=30,ncol=ns)

for (i in 2:ns)
{
output[,i]<- forecast(auto.arima(TS[,i],allowmean = T,D=1),h=30 )$mean
i=i+1
}



I have tried using lapply as below but the run time remains the same.


lapply(TS, function(x) forecast(auto.arima(x,allowmean = T,D=1),h=30 ))



Is there an alternate function/method I can use to improve the performance?





Please provide a reproducible example
– Emmanuel-Lin
Jul 3 at 9:37





The runtime for apply family and the explicit forloop are morelessly the same. Thereis no gain of efficiency when usingapply family. They arejust implicit for-loops
– Onyambu
Jul 3 at 9:38





That i = i+1 does absolutely nothing.
– LAP
Jul 3 at 9:38


i = i+1





In for loops you don't need to put i = i+1
– Emmanuel-Lin
Jul 3 at 9:38


for


i = i+1





try some parallel lapply
– s.brunel
Jul 3 at 9:43









By clicking "Post Your Answer", you acknowledge that you have read our updated terms of service, privacy policy and cookie policy, and that your continued use of the website is subject to these policies.

Popular posts from this blog

api-platform.com Unable to generate an IRI for the item of type

How to set up datasource with Spring for HikariCP?

Display dokan vendor name on Woocommerce single product pages