Forecasting with Prophet

I have this data with daily entries from (2021/04 - 2021/10). Where I have 78 different locations. I am interested to forecast each one of them for the next 3 months using prophet. Please help me to resolve my error.

Here is the code:

model_data <- data1 %>%
  dplyr::select(date, pred, level2code)

Selecting all the 78 locations

key <- model_data %>%
  select(level2code) %>%
  distinct() %>%
  mutate(name = c(1:78))  

model_results <- data.frame()

    for (i in 1:78){
  model <- data1 %>%
    filter(level2code == key$level2code[i]) %>%
    select(date, y = pred) %>%
    prophet()
  assign(paste(key$name[i], "model", sep = "_"), model)

  Predicted_values <- make_future_dataframe(model, periods = 90)

  results <- predict(model, Predicted_values) %>%
    select(date, yhat_lower, yhat_upper, yhat) %>%
    mutate(level2code = key$level2code[i],
           date = as.Date(date)) %>%
    inner_join(data1, by = c("level2code" = "level2code", "date" = "date"))
  model_results <- rbind(model_results, results)
}

But, I get this error:

Error in fit.prophet(m, df, ...) : 
  Dataframe must have columns 'ds' and 'y' with the dates and values respectively

Sample Data set:

date    product level2code  pred
2021-04-01  garri   NG020001    366.1540473
2021-04-01  garri   NG020002    393.3557317
2021-04-01  garri   NG020003    374.3132972
2021-04-01  garri   NG021001    353.1266783
2021-04-01  garri   NG021002    329.9673004
2021-04-01  garri   NG021003    391.1370065
2021-04-01  garri   NG021004    306.1356683
2021-04-01  garri   NG020004    372.8115048
2021-04-01  garri   NG020005    350.0100309
2021-04-01  garri   NG021005    373.0025842
2021-04-01  garri   NG020006    383.1839042
2021-04-01  garri   NG021006    383.5212653
2021-04-01  garri   NG020007    486.8226379
2021-04-01  garri   NG020008    334.8151487
2021-04-01  garri   NG021007    379.89125
2021-04-01  garri   NG021008    370.9376296
2021-04-01  garri   NG021009    389.4640031
2021-04-01  garri   NG021010    365.9266992
2021-04-01  garri   NG020009    380.4655716
2021-04-01  garri   NG020010    352.6139069
2021-04-01  garri   NG020011    243.5701952
2021-04-01  garri   NG021011    366.6883205
2021-04-01  garri   NG021012    326.9402671
2021-04-01  garri   NG020012    403.5666062
2021-04-01  garri   NG021013    405.2860516
2021-04-01  garri   NG021014    348.023662
2021-04-01  garri   NG020013    372.7586167
2021-04-01  garri   NG020014    392.5432373
2021-04-01  garri   NG020015    362.1098193
2021-04-01  garri   NG020016    343.9205353
2021-04-01  garri   NG020017    366.5290601
2021-04-01  garri   NG020018    431.0832709
2021-04-01  garri   NG020019    411.3581413
2021-04-01  garri   NG021015    348.3274897
2021-04-01  garri   NG021016    336.3743799
2021-04-01  garri   NG020020    393.8901327
2021-04-01  garri   NG021017    391.7393648
2021-04-01  garri   NG021018    346.0988762
2021-04-01  garri   NG021019    346.1310667
2021-04-01  garri   NG021020    359.2837766
2021-04-01  garri   NG020021    450.4413674
2021-04-01  garri   NG020022    373.6406838
2021-04-01  garri   NG021021    420.0838824
2021-04-01  garri   NG020023    408.6912783
2021-04-01  garri   NG020024    285.8249281
2021-04-01  garri   NG020025    388.3307999
2021-04-01  garri   NG020026    365.1686898
2021-04-01  garri   NG020027    391.6867595
2021-04-01  garri   NG021022    384.1850593
2021-04-01  garri   NG021023    397.7172044
2021-04-01  garri   NG020028    331.2182171
2021-04-01  garri   NG021024    393.323333
2021-04-01  garri   NG020029    405.5813127
2021-04-01  garri   NG021025    359.658715
2021-04-01  garri   NG021026    370.3166839
2021-04-01  garri   NG021027    391.088788
2021-04-01  garri   NG021028    364.2770249
2021-04-01  garri   NG020030    375.2600611
2021-04-01  garri   NG021029    358.9618615
2021-04-01  garri   NG020031    439.1322423
2021-04-01  garri   NG020032    402.7340519
2021-04-01  garri   NG021030    345.2253201
2021-04-01  garri   NG020033    356.2798309
2021-04-01  garri   NG020034    369.3260162
2021-04-01  garri   NG021031    368.6820545
2021-04-01  garri   NG021032    342.8985896
2021-04-01  garri   NG021033    388.3937476
2021-04-01  garri   NG020035    369.5370586
2021-04-01  garri   NG020036    374.2149029
2021-04-01  garri   NG020037    374.3105323
2021-04-01  garri   NG020038    419.9881972
2021-04-01  garri   NG020039    356.6891081
2021-04-01  garri   NG020040    385.9381759
2021-04-01  garri   NG020041    308.1801669
2021-04-01  garri   NG020042    383.1544687
2021-04-01  garri   NG020043    381.4891835
2021-04-01  garri   NG020044    362.6149694
2021-04-01  garri   NG021034    351.0315915

Try one of these

# using rename
model_data <- data1 %>%
  select(date, pred, level2code, product) %>%
  rename(ds = date) %>%
  select(level2code, ds, pred)

# shorter - just renaming within select
model_data <- data1 %>%
# not sure what your other select was for
  select(level2code, ds = date, pred)

Also have a look at providing a better reproducible dataset as yours is not:

I have made some changes in the code but now I get a different error, please see it at the end. It says the data frame must have ds and y, I have both of them, Don't know why it's not working.

I do not see ds anywhere in your code.

yes, I have changed the name of the date column to ds in the CSV file and it works fine. Thanks for replying.

This topic was automatically closed 21 days after the last reply. New replies are no longer allowed.

If you have a query related to it or one of the replies, start a new topic and refer back with a link.