Is there a way i can forecast with fb prophet

Viewed 39

I have been trying to forecast attendance using prophet. The date is a daily attendance in two columns as a csv file in R but it is not working: I keep getting this error:

m <- prophet(df)

Error in setup_dataframe(m, history, initialize_scales = TRUE) : Unable to parse date format in column ds. Convert to date format (%Y-%m-%d or %Y-%m-%d %H:%M:%S) and check that there are no NAs.

ds  y
1/1/2020    64
1/2/2020    158
1/3/2020    138
1/4/2020    86
1/5/2020    49
1/6/2020    273
1/7/2020    125
1/8/2020    350
1/9/2020    283
1/10/2020   256
1/11/2020   114
1/12/2020   56
1/13/2020   235
1/14/2020   281
1/15/2020   242
1/16/2020   256
1/17/2020   219
1/18/2020   100
1/19/2020   39
1/20/2020   233
1/21/2020   268
1/22/2020   240
1/23/2020   239
1/24/2020   221
1/25/2020   94
1/26/2020   72
1/27/2020   231
1/28/2020   266
1/29/2020   248
1/30/2020   266
1/31/2020   223
2/1/2020    102
2/2/2020    58
2/3/2020    248
1 Answers

I think there was an issue with importing the data. (Note that I always use the 'Import Dataset' button on the 'Environment' tab of RStudio to help me on this.)

library(tidyverse)
library(prophet)

df <- read_table(
  file = "C:/temp.csv",
  col_types = cols(ds = col_date(format = "%m/%d/%Y"))
  )

Doing so gives you the following:

glimpse(df)
Rows: 34
Columns: 2
$ ds <date> 2020-01-01, 2020-01-02, 2020-01-03, 2020-01-04, 2020-01-05, 2020-01-06, 2020-01-07, 2020-01-08, 2020-01-…
$ y  <dbl> 64, 158, 138, 86, 49, 273, 125, 350, 283, 256, 114, 56, 235, 281, 242, 256, 219, 100, 39, 233, 268, 240, …

And you can then run prophet:

m <- prophet(df)
Disabling yearly seasonality. Run prophet with yearly.seasonality=TRUE to override this.
Disabling daily seasonality. Run prophet with daily.seasonality=TRUE to override this.
  
glimpse(m)
List of 32
 $ growth                 : chr "linear"
 $ changepoints           : POSIXct[1:25], format: "2020-01-02" "2020-01-03" "2020-01-04" "2020-01-05" ...
 $ n.changepoints         : num 25
 $ changepoint.range      : num 0.8
 $ yearly.seasonality     : chr "auto"
 $ weekly.seasonality     : chr "auto"
 $ daily.seasonality      : chr "auto"
 $ holidays               : NULL
 $ seasonality.mode       : chr "additive"
 $ seasonality.prior.scale: num 10
 $ changepoint.prior.scale: num 0.05
 $ holidays.prior.scale   : num 10
 $ mcmc.samples           : num 0
 $ interval.width         : num 0.8
 $ uncertainty.samples    : num 1000
 $ specified.changepoints : logi FALSE
 $ start                  : POSIXct[1:1], format: "2020-01-01"
 $ y.scale                : num 350
 $ logistic.floor         : logi FALSE
 $ t.scale                : num 2851200
 $ changepoints.t         : num [1:25] 0.0303 0.0606 0.0909 0.1212 0.1515 ...
 $ seasonalities          :List of 1
  ..$ weekly:List of 5
  .. ..$ period        : num 7
  .. ..$ fourier.order : num 3
  .. ..$ prior.scale   : num 10
  .. ..$ mode          : chr "additive"
  .. ..$ condition.name: NULL
 $ extra_regressors       : list()
 $ country_holidays       : NULL
 $ stan.fit               :List of 4
  ..$ par        :List of 6
  .. ..$ k        : num 0.16
  .. ..$ m        : num 0.456
  .. ..$ delta    : num [1:25(1d)] 1.75e-09 -2.09e-09 7.43e-10 -2.43e-09 -1.36e-09 ...
  .. ..$ sigma_obs: num 0.129
  .. ..$ beta     : num [1:6(1d)] -0.1881 0.1493 0.1513 0.0209 -0.0693 ...
  .. ..$ trend    : num [1:34(1d)] 0.456 0.461 0.466 0.47 0.475 ...
  ..$ value      : num 52.6
  ..$ return_code: int 0
  ..$ theta_tilde: num [1, 1:68] 1.60e-01 4.56e-01 1.75e-09 -2.09e-09 7.43e-10 ...
  .. ..- attr(*, "dimnames")=List of 2
 $ params                 :List of 6
  ..$ k        : num 0.16
  ..$ m        : num 0.456
  ..$ delta    : num [1, 1:25] 1.75e-09 -2.09e-09 7.43e-10 -2.43e-09 -1.36e-09 ...
  ..$ sigma_obs: num 0.129
  ..$ beta     : num [1, 1:6] -0.1881 0.1493 0.1513 0.0209 -0.0693 ...
  ..$ trend    : num [1:34(1d)] 0.456 0.461 0.466 0.47 0.475 ...
 $ history                : spec_tbl_df [34 × 5] (S3: spec_tbl_df/tbl_df/tbl/data.frame)
  ..$ ds      : POSIXct[1:34], format: "2020-01-01" "2020-01-02" "2020-01-03" "2020-01-04" ...
  ..$ y       : num [1:34] 64 158 138 86 49 273 125 350 283 256 ...
  ..$ floor   : num [1:34] 0 0 0 0 0 0 0 0 0 0 ...
  ..$ t       : num [1:34] 0 0.0303 0.0606 0.0909 0.1212 ...
  ..$ y_scaled: num [1:34] 0.183 0.451 0.394 0.246 0.14 ...
  ..- attr(*, "spec")=
  .. .. cols(
  .. ..   ds = col_date(format = "%m/%d/%Y"),
  .. ..   y = col_double()
  .. .. )
 $ history.dates          : POSIXct[1:34], format: "2020-01-01" "2020-01-02" "2020-01-03" "2020-01-04" ...
 $ train.holiday.names    : NULL
 $ train.component.cols   :'data.frame':    6 obs. of  3 variables:
  ..$ additive_terms      : int [1:6] 1 1 1 1 1 1
  ..$ weekly              : int [1:6] 1 1 1 1 1 1
  ..$ multiplicative_terms: num [1:6] 0 0 0 0 0 0
 $ component.modes        :List of 2
  ..$ additive      : chr [1:4] "weekly" "additive_terms" "extra_regressors_additive" "holidays"
  ..$ multiplicative: chr [1:2] "multiplicative_terms" "extra_regressors_multiplicative"
 $ fit.kwargs             : list()
 - attr(*, "class")= chr [1:2] "prophet" "list"
Related