I've created a for-loop that reads individual time series files (.csv) and exports the forecast values for each time series back to csv. I wanted to also export the individual time series plots to JPEG within the for loop. I am new to R and would like to have some guidance on how to do it. Not sure if creating another for-loop within the existing for-loop is the answer.
I've tried exporting a sample plot for one time series and it worked. I couldn't figure out how to do it within the existing for-loop.
Here's the for-loop code (imports/exports forecast values from/to CSV)
setwd("wd")
for (file in list.files(pattern = "*.csv")) {
library(prophet)
df <- read.csv(file)
m <- forecast(df)
future <- make_future_dataframe(m, periods = 90)
out <- predict(m, future)
write.csv(out, sprintf("out_%s.csv", file))
}
Here's the code that exports a JPEG plot
jpeg('rplot.jpg')
plot(m,forecast)
dev.off()
Edit:
setwd('wd')
files <- list.files(pattern = "\\.csv$")
for (i in seq_along(files)) {
library(prophet)
df <- read.csv(files[i])
m <- prophet(df)
future <- make_future_dataframe(m, periods = 90)
forecast <- predict(m, future)
out <- predict(m, future)
write.csv(out, sprintf("out_%s.csv", files[i]))
jpeg(paste('rplot', files[i], '.jpg'))
plot(m, forecast)
dev.off()
}