test_prophet.R 15 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454
  1. library(prophet)
  2. context("Prophet tests")
  3. DATA <- read.csv('data.csv')
  4. N <- nrow(DATA)
  5. train <- DATA[1:floor(N / 2), ]
  6. future <- DATA[(ceiling(N/2) + 1):N, ]
  7. DATA2 <- read.csv('data2.csv')
  8. test_that("fit_predict", {
  9. skip_if_not(Sys.getenv('R_ARCH') != '/i386')
  10. m <- prophet(train)
  11. expect_error(predict(m, future), NA)
  12. })
  13. test_that("fit_predict_no_seasons", {
  14. skip_if_not(Sys.getenv('R_ARCH') != '/i386')
  15. m <- prophet(train, weekly.seasonality = FALSE, yearly.seasonality = FALSE)
  16. expect_error(predict(m, future), NA)
  17. })
  18. test_that("fit_predict_no_changepoints", {
  19. skip_if_not(Sys.getenv('R_ARCH') != '/i386')
  20. m <- prophet(train, n.changepoints = 0)
  21. expect_error(predict(m, future), NA)
  22. })
  23. test_that("fit_predict_changepoint_not_in_history", {
  24. skip_if_not(Sys.getenv('R_ARCH') != '/i386')
  25. train_t <- dplyr::mutate(DATA, ds=prophet:::set_date(ds))
  26. train_t <- dplyr::filter(train_t,
  27. (ds < prophet:::set_date('2013-01-01')) |
  28. (ds > prophet:::set_date('2014-01-01')))
  29. future <- data.frame(ds=DATA$ds)
  30. m <- prophet(train_t, changepoints=c('2013-06-06'))
  31. expect_error(predict(m, future), NA)
  32. })
  33. test_that("fit_predict_duplicates", {
  34. skip_if_not(Sys.getenv('R_ARCH') != '/i386')
  35. train2 <- train
  36. train2$y <- train2$y + 10
  37. train_t <- rbind(train, train2)
  38. m <- prophet(train_t)
  39. expect_error(predict(m, future), NA)
  40. })
  41. test_that("fit_predict_constant_history", {
  42. skip_if_not(Sys.getenv('R_ARCH') != '/i386')
  43. train2 <- train
  44. train2$y <- 20
  45. m <- prophet(train2)
  46. fcst <- predict(m, future)
  47. expect_equal(tail(fcst$yhat, 1), 20)
  48. train2$y <- 0
  49. m <- prophet(train2)
  50. fcst <- predict(m, future)
  51. expect_equal(tail(fcst$yhat, 1), 0)
  52. })
  53. test_that("setup_dataframe", {
  54. history <- train
  55. m <- prophet(history, fit = FALSE)
  56. out <- prophet:::setup_dataframe(m, history, initialize_scales = TRUE)
  57. history <- out$df
  58. expect_true('t' %in% colnames(history))
  59. expect_equal(min(history$t), 0)
  60. expect_equal(max(history$t), 1)
  61. expect_true('y_scaled' %in% colnames(history))
  62. expect_equal(max(history$y_scaled), 1)
  63. })
  64. test_that("get_changepoints", {
  65. history <- train
  66. m <- prophet(history, fit = FALSE)
  67. out <- prophet:::setup_dataframe(m, history, initialize_scales = TRUE)
  68. history <- out$df
  69. m <- out$m
  70. m$history <- history
  71. m <- prophet:::set_changepoints(m)
  72. cp <- m$changepoints.t
  73. expect_equal(length(cp), m$n.changepoints)
  74. expect_true(min(cp) > 0)
  75. expect_true(max(cp) < N)
  76. mat <- prophet:::get_changepoint_matrix(m)
  77. expect_equal(nrow(mat), floor(N / 2))
  78. expect_equal(ncol(mat), m$n.changepoints)
  79. })
  80. test_that("get_zero_changepoints", {
  81. history <- train
  82. m <- prophet(history, n.changepoints = 0, fit = FALSE)
  83. out <- prophet:::setup_dataframe(m, history, initialize_scales = TRUE)
  84. m <- out$m
  85. history <- out$df
  86. m$history <- history
  87. m <- prophet:::set_changepoints(m)
  88. cp <- m$changepoints.t
  89. expect_equal(length(cp), 1)
  90. expect_equal(cp[1], 0)
  91. mat <- prophet:::get_changepoint_matrix(m)
  92. expect_equal(nrow(mat), floor(N / 2))
  93. expect_equal(ncol(mat), 1)
  94. })
  95. test_that("override_n_changepoints", {
  96. history <- train[1:20,]
  97. m <- prophet(history, fit = FALSE)
  98. out <- prophet:::setup_dataframe(m, history, initialize_scales = TRUE)
  99. m <- out$m
  100. history <- out$df
  101. m$history <- history
  102. m <- prophet:::set_changepoints(m)
  103. expect_equal(m$n.changepoints, 15)
  104. cp <- m$changepoints.t
  105. expect_equal(length(cp), 15)
  106. })
  107. test_that("fourier_series_weekly", {
  108. mat <- prophet:::fourier_series(DATA$ds, 7, 3)
  109. true.values <- c(0.9165623, 0.3998920, 0.7330519, -0.6801727, -0.3302791,
  110. -0.9438833)
  111. expect_equal(true.values, mat[1, ], tolerance = 1e-6)
  112. })
  113. test_that("fourier_series_yearly", {
  114. mat <- prophet:::fourier_series(DATA$ds, 365.25, 3)
  115. true.values <- c(0.69702635, -0.71704551, -0.99959923, 0.02830854,
  116. 0.73648994, 0.67644849)
  117. expect_equal(true.values, mat[1, ], tolerance = 1e-6)
  118. })
  119. test_that("growth_init", {
  120. history <- DATA[1:468, ]
  121. history$cap <- max(history$y)
  122. m <- prophet(history, growth = 'logistic', fit = FALSE)
  123. out <- prophet:::setup_dataframe(m, history, initialize_scales = TRUE)
  124. m <- out$m
  125. history <- out$df
  126. params <- prophet:::linear_growth_init(history)
  127. expect_equal(params[1], 0.3055671, tolerance = 1e-6)
  128. expect_equal(params[2], 0.5307511, tolerance = 1e-6)
  129. params <- prophet:::logistic_growth_init(history)
  130. expect_equal(params[1], 1.507925, tolerance = 1e-6)
  131. expect_equal(params[2], -0.08167497, tolerance = 1e-6)
  132. })
  133. test_that("piecewise_linear", {
  134. t <- seq(0, 10)
  135. m <- 0
  136. k <- 1.0
  137. deltas <- c(0.5)
  138. changepoint.ts <- c(5)
  139. y <- prophet:::piecewise_linear(t, deltas, k, m, changepoint.ts)
  140. y.true <- c(0, 1, 2, 3, 4, 5, 6.5, 8, 9.5, 11, 12.5)
  141. expect_equal(y, y.true)
  142. t <- t[8:length(t)]
  143. y.true <- y.true[8:length(y.true)]
  144. y <- prophet:::piecewise_linear(t, deltas, k, m, changepoint.ts)
  145. expect_equal(y, y.true)
  146. })
  147. test_that("piecewise_logistic", {
  148. t <- seq(0, 10)
  149. cap <- rep(10, 11)
  150. m <- 0
  151. k <- 1.0
  152. deltas <- c(0.5)
  153. changepoint.ts <- c(5)
  154. y <- prophet:::piecewise_logistic(t, cap, deltas, k, m, changepoint.ts)
  155. y.true <- c(5.000000, 7.310586, 8.807971, 9.525741, 9.820138, 9.933071,
  156. 9.984988, 9.996646, 9.999252, 9.999833, 9.999963)
  157. expect_equal(y, y.true, tolerance = 1e-6)
  158. t <- t[8:length(t)]
  159. y.true <- y.true[8:length(y.true)]
  160. cap <- cap[8:length(cap)]
  161. y <- prophet:::piecewise_logistic(t, cap, deltas, k, m, changepoint.ts)
  162. expect_equal(y, y.true, tolerance = 1e-6)
  163. })
  164. test_that("holidays", {
  165. holidays = data.frame(ds = c('2016-12-25'),
  166. holiday = c('xmas'),
  167. lower_window = c(-1),
  168. upper_window = c(0))
  169. df <- data.frame(
  170. ds = seq(prophet:::set_date('2016-12-20'),
  171. prophet:::set_date('2016-12-31'), by='d'))
  172. m <- prophet(train, holidays = holidays, fit = FALSE)
  173. feats <- prophet:::make_holiday_features(m, df$ds)
  174. expect_equal(nrow(feats), nrow(df))
  175. expect_equal(ncol(feats), 2)
  176. expect_equal(sum(colSums(feats) - c(1, 1)), 0)
  177. holidays = data.frame(ds = c('2016-12-25'),
  178. holiday = c('xmas'),
  179. lower_window = c(-1),
  180. upper_window = c(10))
  181. m <- prophet(train, holidays = holidays, fit = FALSE)
  182. feats <- prophet:::make_holiday_features(m, df$ds)
  183. expect_equal(nrow(feats), nrow(df))
  184. expect_equal(ncol(feats), 12)
  185. })
  186. test_that("fit_with_holidays", {
  187. skip_if_not(Sys.getenv('R_ARCH') != '/i386')
  188. holidays <- data.frame(ds = c('2012-06-06', '2013-06-06'),
  189. holiday = c('seans-bday', 'seans-bday'),
  190. lower_window = c(0, 0),
  191. upper_window = c(1, 1))
  192. m <- prophet(DATA, holidays = holidays, uncertainty.samples = 0)
  193. expect_error(predict(m), NA)
  194. })
  195. test_that("make_future_dataframe", {
  196. skip_if_not(Sys.getenv('R_ARCH') != '/i386')
  197. train.t <- DATA[1:234, ]
  198. m <- prophet(train.t)
  199. future <- make_future_dataframe(m, periods = 3, freq = 'day',
  200. include_history = FALSE)
  201. correct <- prophet:::set_date(c('2013-04-26', '2013-04-27', '2013-04-28'))
  202. expect_equal(future$ds, correct)
  203. future <- make_future_dataframe(m, periods = 3, freq = 'month',
  204. include_history = FALSE)
  205. correct <- prophet:::set_date(c('2013-05-25', '2013-06-25', '2013-07-25'))
  206. expect_equal(future$ds, correct)
  207. })
  208. test_that("auto_weekly_seasonality", {
  209. skip_if_not(Sys.getenv('R_ARCH') != '/i386')
  210. # Should be enabled
  211. N.w <- 15
  212. train.w <- DATA[1:N.w, ]
  213. m <- prophet(train.w, fit = FALSE)
  214. expect_equal(m$weekly.seasonality, 'auto')
  215. m <- prophet:::fit.prophet(m, train.w)
  216. expect_true('weekly' %in% names(m$seasonalities))
  217. expect_equal(m$seasonalities[['weekly']], c(7, 3))
  218. # Should be disabled due to too short history
  219. N.w <- 9
  220. train.w <- DATA[1:N.w, ]
  221. m <- prophet(train.w)
  222. expect_false('weekly' %in% names(m$seasonalities))
  223. m <- prophet(train.w, weekly.seasonality = TRUE)
  224. expect_true('weekly' %in% names(m$seasonalities))
  225. # Should be False due to weekly spacing
  226. train.w <- DATA[seq(1, nrow(DATA), 7), ]
  227. m <- prophet(train.w)
  228. expect_false('weekly' %in% names(m$seasonalities))
  229. m <- prophet(DATA, weekly.seasonality=2)
  230. expect_equal(m$seasonalities[['weekly']], c(7, 2))
  231. })
  232. test_that("auto_yearly_seasonality", {
  233. skip_if_not(Sys.getenv('R_ARCH') != '/i386')
  234. # Should be enabled
  235. m <- prophet(DATA, fit = FALSE)
  236. expect_equal(m$yearly.seasonality, 'auto')
  237. m <- prophet:::fit.prophet(m, DATA)
  238. expect_true('yearly' %in% names(m$seasonalities))
  239. expect_equal(m$seasonalities[['yearly']], c(365.25, 10))
  240. # Should be disabled due to too short history
  241. N.w <- 240
  242. train.y <- DATA[1:N.w, ]
  243. m <- prophet(train.y)
  244. expect_false('yearly' %in% names(m$seasonalities))
  245. m <- prophet(train.y, yearly.seasonality = TRUE)
  246. expect_true('yearly' %in% names(m$seasonalities))
  247. m <- prophet(DATA, yearly.seasonality=7)
  248. expect_equal(m$seasonalities[['yearly']], c(365.25, 7))
  249. })
  250. test_that("auto_daily_seasonality", {
  251. skip_if_not(Sys.getenv('R_ARCH') != '/i386')
  252. # Should be enabled
  253. m <- prophet(DATA2, fit = FALSE)
  254. expect_equal(m$daily.seasonality, 'auto')
  255. m <- prophet:::fit.prophet(m, DATA2)
  256. expect_true('daily' %in% names(m$seasonalities))
  257. expect_equal(m$seasonalities[['daily']], c(1, 4))
  258. # Should be disabled due to too short history
  259. N.d <- 430
  260. train.y <- DATA2[1:N.d, ]
  261. m <- prophet(train.y)
  262. expect_false('daily' %in% names(m$seasonalities))
  263. m <- prophet(train.y, daily.seasonality = TRUE)
  264. expect_true('daily' %in% names(m$seasonalities))
  265. m <- prophet(DATA2, daily.seasonality=7)
  266. expect_equal(m$seasonalities[['daily']], c(1, 7))
  267. m <- prophet(DATA)
  268. expect_false('daily' %in% names(m$seasonalities))
  269. })
  270. test_that("test_subdaily_holidays", {
  271. skip_if_not(Sys.getenv('R_ARCH') != '/i386')
  272. holidays <- data.frame(ds = c('2017-01-02'),
  273. holiday = c('special_day'))
  274. m <- prophet(DATA2, holidays=holidays)
  275. fcst <- predict(m)
  276. expect_equal(sum(fcst$special_day == 0), 575)
  277. })
  278. test_that("custom_seasonality", {
  279. skip_if_not(Sys.getenv('R_ARCH') != '/i386')
  280. holidays <- data.frame(ds = c('2017-01-02'),
  281. holiday = c('special_day'))
  282. m <- prophet(holidays=holidays)
  283. m <- add_seasonality(m, name='monthly', period=30, fourier.order=5)
  284. expect_equal(m$seasonalities[['monthly']], c(30, 5))
  285. expect_error(
  286. add_seasonality(m, name='special_day', period=30, fourier_order=5)
  287. )
  288. expect_error(
  289. add_seasonality(m, name='trend', period=30, fourier_order=5)
  290. )
  291. m <- add_seasonality(m, name='weekly', period=30, fourier.order=5)
  292. })
  293. test_that("added_regressors", {
  294. skip_if_not(Sys.getenv('R_ARCH') != '/i386')
  295. m <- prophet()
  296. m <- add_regressor(m, 'binary_feature', prior.scale=0.2)
  297. m <- add_regressor(m, 'numeric_feature', prior.scale=0.5)
  298. m <- add_regressor(m, 'binary_feature2', standardize=TRUE)
  299. df <- DATA
  300. df$binary_feature <- c(rep(0, 255), rep(1, 255))
  301. df$numeric_feature <- 0:509
  302. # Require all regressors in df
  303. expect_error(
  304. fit.prophet(m, df)
  305. )
  306. df$binary_feature2 <- c(rep(1, 100), rep(0, 410))
  307. m <- fit.prophet(m, df)
  308. # Check that standardizations are correctly set
  309. true <- list(prior.scale = 0.2, mu = 0, std = 1, standardize = 'auto')
  310. for (name in names(true)) {
  311. expect_equal(true[[name]], m$extra_regressors$binary_feature[[name]])
  312. }
  313. true <- list(prior.scale = 0.5, mu = 254.5, std = 147.368585)
  314. for (name in names(true)) {
  315. expect_equal(true[[name]], m$extra_regressors$numeric_feature[[name]],
  316. tolerance = 1e-5)
  317. }
  318. true <- list(prior.scale = 10., mu = 0.1960784, std = 0.3974183)
  319. for (name in names(true)) {
  320. expect_equal(true[[name]], m$extra_regressors$binary_feature2[[name]],
  321. tolerance = 1e-5)
  322. }
  323. # Check that standardization is done correctly
  324. df2 <- prophet:::setup_dataframe(m, df)$df
  325. expect_equal(df2$binary_feature[1], 0)
  326. expect_equal(df2$numeric_feature[1], -1.726962, tolerance = 1e-4)
  327. expect_equal(df2$binary_feature2[1], 2.022859, tolerance = 1e-4)
  328. # Check that feature matrix and prior scales are correctly constructed
  329. out <- prophet:::make_all_seasonality_features(m, df2)
  330. seasonal.features <- out$seasonal.features
  331. prior.scales <- out$prior.scales
  332. expect_true('binary_feature' %in% colnames(seasonal.features))
  333. expect_true('numeric_feature' %in% colnames(seasonal.features))
  334. expect_true('binary_feature2' %in% colnames(seasonal.features))
  335. expect_equal(ncol(seasonal.features), 29)
  336. expect_true(all(sort(prior.scales[27:29]) == c(0.2, 0.5, 10.)))
  337. # Check that forecast components are reasonable
  338. future <- data.frame(
  339. ds = c('2014-06-01'), binary_feature = c(0), numeric_feature = c(10))
  340. expect_error(predict(m, future))
  341. future$binary_feature2 <- 0.
  342. fcst <- predict(m, future)
  343. expect_equal(ncol(fcst), 31)
  344. expect_equal(fcst$binary_feature[1], 0)
  345. expect_equal(fcst$extra_regressors[1],
  346. fcst$numeric_feature[1] + fcst$binary_feature2[1])
  347. expect_equal(fcst$seasonalities[1], fcst$yearly[1] + fcst$weekly[1])
  348. expect_equal(fcst$seasonal[1],
  349. fcst$seasonalities[1] + fcst$extra_regressors[1])
  350. expect_equal(fcst$yhat[1], fcst$trend[1] + fcst$seasonal[1])
  351. })
  352. test_that("copy", {
  353. inputs <- list(
  354. growth = c('linear', 'logistic'),
  355. changepoints = c(NULL, c('2016-12-25')),
  356. n.changepoints = c(3),
  357. yearly.seasonality = c(TRUE, FALSE),
  358. weekly.seasonality = c(TRUE, FALSE),
  359. daily.seasonality = c(TRUE, FALSE),
  360. holidays = c(NULL, 'insert_dataframe'),
  361. seasonality.prior.scale = c(1.1),
  362. holidays.prior.scale = c(1.1),
  363. changepoints.prior.scale = c(0.1),
  364. mcmc.samples = c(100),
  365. interval.width = c(0.9),
  366. uncertainty.samples = c(200)
  367. )
  368. products <- expand.grid(inputs)
  369. for (i in 1:length(products)) {
  370. if (products$holidays[i] == 'insert_dataframe') {
  371. holidays <- data.frame(ds=c('2016-12-25'), holiday=c('x'))
  372. } else {
  373. holidays <- NULL
  374. }
  375. m1 <- prophet(
  376. growth = products$growth[i],
  377. changepoints = products$changepoints[i],
  378. n.changepoints = products$n.changepoints[i],
  379. yearly.seasonality = products$yearly.seasonality[i],
  380. weekly.seasonality = products$weekly.seasonality[i],
  381. daily.seasonality = products$daily.seasonality[i],
  382. holidays = holidays,
  383. seasonality.prior.scale = products$seasonality.prior.scale[i],
  384. holidays.prior.scale = products$holidays.prior.scale[i],
  385. changepoints.prior.scale = products$changepoints.prior.scale[i],
  386. mcmc.samples = products$mcmc.samples[i],
  387. interval.width = products$interval.width[i],
  388. uncertainty.samples = products$uncertainty.samples[i],
  389. fit = FALSE
  390. )
  391. m2 <- prophet:::prophet_copy(m1)
  392. # Values should be copied correctly
  393. for (arg in names(inputs)) {
  394. expect_equal(m1[[arg]], m2[[arg]])
  395. }
  396. }
  397. # Check for cutoff
  398. changepoints <- seq.Date(as.Date('2012-06-15'), as.Date('2012-09-15'), by='d')
  399. cutoff <- as.Date('2012-07-25')
  400. m1 <- prophet(DATA, changepoints = changepoints)
  401. m2 <- prophet:::prophet_copy(m1, cutoff)
  402. changepoints <- changepoints[changepoints <= cutoff]
  403. expect_equal(prophet:::set_date(changepoints), m2$changepoints)
  404. })