The below double nested loop runs across 850 branches, 2 service points and 25 specific needs. As it stands the total estimated run time is 70 hours. Please help me optimise so this runs quicker?
Code begins here:
start_time <- Sys.time()
for(i in levels(branch_code)) #850 branches
{
for(j in levels(service_point)) #2 service points
{
for(u in levels(specific_need)) #25 specific needs
{
test<-paste0("SELECT * from dbo.Data_Base where calendar_date <= '2018-11-30' and branch_code = '",i, "' and service_point = '",j, "' and specific_need = '",u, "' order by calendar_date asc")
testtwo<-sqlQuery(con,test)
if(nrow(testtwo)>500)
{
df<-testtwo[,c("volume","calendar_date")]
colnames(df)<-c("y","ds")
m <-prophet(weekly.seasonality = TRUE, yearly.seasonality = TRUE, daily.seasonality = FALSE, holidays=holidays)
m<-add_seasonality(m, name='monthly',period=30.4,fourier.order = 8,prior.scale = 13)
m<-fit.prophet(m,df)
future <- make_future_dataframe(m, periods = 100, freq = "day", include_history = TRUE)
forecast <- predict(m, future)
writeto<-data.frame(forecast$ds,i,j,u,forecast$yhat)
colnames(writeto)<-c("ds","branch_code","service_point","specific_need","Forecast")
writeto_two<-left_join(writeto, df,by = ('ds'))
colnames(writeto_two)<-c("calendar_date","branch_code","service_point","specific_need","Forecast","Volume")
testtwowrite<-data.frame(writeto_two)
dbWriteTable(connec, "dbo.Actual_Volume_write", testtwowrite, append = TRUE)
}
else
{
print(c(i,j,u))
}
}
}
}
end_time <- Sys.time()