test_prophet.R 22 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644
  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. DATA$ds <- prophet:::set_date(DATA$ds)
  9. DATA2$ds <- prophet:::set_date(DATA2$ds)
  10. test_that("fit_predict", {
  11. skip_if_not(Sys.getenv('R_ARCH') != '/i386')
  12. m <- prophet(train)
  13. expect_error(predict(m, future), NA)
  14. })
  15. test_that("fit_predict_no_seasons", {
  16. skip_if_not(Sys.getenv('R_ARCH') != '/i386')
  17. m <- prophet(train, weekly.seasonality = FALSE, yearly.seasonality = FALSE)
  18. expect_error(predict(m, future), NA)
  19. })
  20. test_that("fit_predict_no_changepoints", {
  21. skip_if_not(Sys.getenv('R_ARCH') != '/i386')
  22. expect_warning({
  23. # warning from prophet(), error from predict()
  24. m <- prophet(train, n.changepoints = 0)
  25. expect_error(predict(m, future), NA)
  26. })
  27. })
  28. test_that("fit_predict_changepoint_not_in_history", {
  29. skip_if_not(Sys.getenv('R_ARCH') != '/i386')
  30. train_t <- dplyr::mutate(DATA, ds=prophet:::set_date(ds))
  31. train_t <- dplyr::filter(train_t,
  32. (ds < prophet:::set_date('2013-01-01')) |
  33. (ds > prophet:::set_date('2014-01-01')))
  34. future <- data.frame(ds=DATA$ds)
  35. expect_warning({
  36. # warning from prophet(), error from predict()
  37. m <- prophet(train_t, changepoints=c('2013-06-06'))
  38. expect_error(predict(m, future), NA)
  39. })
  40. })
  41. test_that("fit_predict_duplicates", {
  42. skip_if_not(Sys.getenv('R_ARCH') != '/i386')
  43. train2 <- train
  44. train2$y <- train2$y + 10
  45. train_t <- rbind(train, train2)
  46. m <- prophet(train_t)
  47. expect_error(predict(m, future), NA)
  48. })
  49. test_that("fit_predict_constant_history", {
  50. skip_if_not(Sys.getenv('R_ARCH') != '/i386')
  51. train2 <- train
  52. train2$y <- 20
  53. m <- prophet(train2)
  54. fcst <- predict(m, future)
  55. expect_equal(tail(fcst$yhat, 1), 20)
  56. train2$y <- 0
  57. m <- prophet(train2)
  58. fcst <- predict(m, future)
  59. expect_equal(tail(fcst$yhat, 1), 0)
  60. })
  61. test_that("setup_dataframe", {
  62. history <- train
  63. m <- prophet(history, fit = FALSE)
  64. out <- prophet:::setup_dataframe(m, history, initialize_scales = TRUE)
  65. history <- out$df
  66. expect_true('t' %in% colnames(history))
  67. expect_equal(min(history$t), 0)
  68. expect_equal(max(history$t), 1)
  69. expect_true('y_scaled' %in% colnames(history))
  70. expect_equal(max(history$y_scaled), 1)
  71. })
  72. test_that("logistic_floor", {
  73. skip_if_not(Sys.getenv('R_ARCH') != '/i386')
  74. skip_on_os('mac') # Resolves mysterious CRAN build issue
  75. m <- prophet(growth = 'logistic')
  76. history <- train
  77. history$floor <- 10.
  78. history$cap <- 80.
  79. future1 <- future
  80. future1$cap <- 80.
  81. future1$floor <- 10.
  82. m <- fit.prophet(m, history, algorithm = 'Newton')
  83. expect_true(m$logistic.floor)
  84. expect_true('floor' %in% colnames(m$history))
  85. expect_equal(m$history$y_scaled[1], 1., tolerance = 1e-6)
  86. fcst1 <- predict(m, future1)
  87. m2 <- prophet(growth = 'logistic')
  88. history2 <- history
  89. history2$y <- history2$y + 10.
  90. history2$floor <- history2$floor + 10.
  91. history2$cap <- history2$cap + 10.
  92. future1$cap <- future1$cap + 10.
  93. future1$floor <- future1$floor + 10.
  94. m2 <- fit.prophet(m2, history2, algorithm = 'Newton')
  95. expect_equal(m2$history$y_scaled[1], 1., tolerance = 1e-6)
  96. fcst2 <- predict(m, future1)
  97. fcst2$yhat <- fcst2$yhat - 10.
  98. # Check for approximate shift invariance
  99. expect_true(all(abs(fcst1$yhat - fcst2$yhat) < 1))
  100. })
  101. test_that("get_changepoints", {
  102. history <- train
  103. m <- prophet(history, fit = FALSE)
  104. out <- prophet:::setup_dataframe(m, history, initialize_scales = TRUE)
  105. history <- out$df
  106. m <- out$m
  107. m$history <- history
  108. m <- prophet:::set_changepoints(m)
  109. cp <- m$changepoints.t
  110. expect_equal(length(cp), m$n.changepoints)
  111. expect_true(min(cp) > 0)
  112. expect_true(max(cp) <= history$t[ceiling(0.8 * length(history$t))])
  113. })
  114. test_that("set_changepoint_range", {
  115. history <- train
  116. m <- prophet(history, fit = FALSE, changepoint.range = 0.4)
  117. out <- prophet:::setup_dataframe(m, history, initialize_scales = TRUE)
  118. history <- out$df
  119. m <- out$m
  120. m$history <- history
  121. m <- prophet:::set_changepoints(m)
  122. cp <- m$changepoints.t
  123. expect_equal(length(cp), m$n.changepoints)
  124. expect_true(min(cp) > 0)
  125. expect_true(max(cp) <= history$t[ceiling(0.4 * length(history$t))])
  126. expect_error(prophet(history, changepoint.range = -0.1))
  127. expect_error(prophet(history, changepoint.range = 2))
  128. })
  129. test_that("get_zero_changepoints", {
  130. history <- train
  131. m <- prophet(history, n.changepoints = 0, fit = FALSE)
  132. out <- prophet:::setup_dataframe(m, history, initialize_scales = TRUE)
  133. m <- out$m
  134. history <- out$df
  135. m$history <- history
  136. m <- prophet:::set_changepoints(m)
  137. cp <- m$changepoints.t
  138. expect_equal(length(cp), 1)
  139. expect_equal(cp[1], 0)
  140. })
  141. test_that("override_n_changepoints", {
  142. history <- train[1:20,]
  143. m <- prophet(history, fit = FALSE)
  144. out <- prophet:::setup_dataframe(m, history, initialize_scales = TRUE)
  145. m <- out$m
  146. history <- out$df
  147. m$history <- history
  148. m <- prophet:::set_changepoints(m)
  149. expect_equal(m$n.changepoints, 15)
  150. cp <- m$changepoints.t
  151. expect_equal(length(cp), 15)
  152. })
  153. test_that("fourier_series_weekly", {
  154. true.values <- c(0.7818315, 0.6234898, 0.9749279, -0.2225209, 0.4338837,
  155. -0.9009689)
  156. mat <- prophet:::fourier_series(DATA$ds, 7, 3)
  157. expect_equal(true.values, mat[1, ], tolerance = 1e-6)
  158. })
  159. test_that("fourier_series_yearly", {
  160. true.values <- c(0.7006152, -0.7135393, -0.9998330, 0.01827656, 0.7262249,
  161. 0.6874572)
  162. mat <- prophet:::fourier_series(DATA$ds, 365.25, 3)
  163. expect_equal(true.values, mat[1, ], tolerance = 1e-6)
  164. })
  165. test_that("growth_init", {
  166. history <- DATA[1:468, ]
  167. history$cap <- max(history$y)
  168. m <- prophet(history, growth = 'logistic', fit = FALSE)
  169. out <- prophet:::setup_dataframe(m, history, initialize_scales = TRUE)
  170. m <- out$m
  171. history <- out$df
  172. params <- prophet:::linear_growth_init(history)
  173. expect_equal(params[1], 0.3055671, tolerance = 1e-6)
  174. expect_equal(params[2], 0.5307511, tolerance = 1e-6)
  175. params <- prophet:::logistic_growth_init(history)
  176. expect_equal(params[1], 1.507925, tolerance = 1e-6)
  177. expect_equal(params[2], -0.08167497, tolerance = 1e-6)
  178. })
  179. test_that("piecewise_linear", {
  180. t <- seq(0, 10)
  181. m <- 0
  182. k <- 1.0
  183. deltas <- c(0.5)
  184. changepoint.ts <- c(5)
  185. y <- prophet:::piecewise_linear(t, deltas, k, m, changepoint.ts)
  186. y.true <- c(0, 1, 2, 3, 4, 5, 6.5, 8, 9.5, 11, 12.5)
  187. expect_equal(y, y.true)
  188. t <- t[8:length(t)]
  189. y.true <- y.true[8:length(y.true)]
  190. y <- prophet:::piecewise_linear(t, deltas, k, m, changepoint.ts)
  191. expect_equal(y, y.true)
  192. })
  193. test_that("piecewise_logistic", {
  194. t <- seq(0, 10)
  195. cap <- rep(10, 11)
  196. m <- 0
  197. k <- 1.0
  198. deltas <- c(0.5)
  199. changepoint.ts <- c(5)
  200. y <- prophet:::piecewise_logistic(t, cap, deltas, k, m, changepoint.ts)
  201. y.true <- c(5.000000, 7.310586, 8.807971, 9.525741, 9.820138, 9.933071,
  202. 9.984988, 9.996646, 9.999252, 9.999833, 9.999963)
  203. expect_equal(y, y.true, tolerance = 1e-6)
  204. t <- t[8:length(t)]
  205. y.true <- y.true[8:length(y.true)]
  206. cap <- cap[8:length(cap)]
  207. y <- prophet:::piecewise_logistic(t, cap, deltas, k, m, changepoint.ts)
  208. expect_equal(y, y.true, tolerance = 1e-6)
  209. })
  210. test_that("holidays", {
  211. holidays <- data.frame(ds = c('2016-12-25'),
  212. holiday = c('xmas'),
  213. lower_window = c(-1),
  214. upper_window = c(0))
  215. df <- data.frame(
  216. ds = seq(prophet:::set_date('2016-12-20'),
  217. prophet:::set_date('2016-12-31'), by='d'))
  218. m <- prophet(train, holidays = holidays, fit = FALSE)
  219. out <- prophet:::make_holiday_features(m, df$ds)
  220. feats <- out$holiday.features
  221. priors <- out$prior.scales
  222. names <- out$holiday.names
  223. expect_equal(nrow(feats), nrow(df))
  224. expect_equal(ncol(feats), 2)
  225. expect_equal(sum(colSums(feats) - c(1, 1)), 0)
  226. expect_true(all(priors == c(10., 10.)))
  227. expect_equal(names, c('xmas'))
  228. holidays <- data.frame(ds = c('2016-12-25'),
  229. holiday = c('xmas'),
  230. lower_window = c(-1),
  231. upper_window = c(10))
  232. m <- prophet(train, holidays = holidays, fit = FALSE)
  233. out <- prophet:::make_holiday_features(m, df$ds)
  234. feats <- out$holiday.features
  235. priors <- out$prior.scales
  236. names <- out$holiday.names
  237. expect_equal(nrow(feats), nrow(df))
  238. expect_equal(ncol(feats), 12)
  239. expect_true(all(priors == rep(10, 12)))
  240. expect_equal(names, c('xmas'))
  241. # Check prior specifications
  242. holidays <- data.frame(
  243. ds = prophet:::set_date(c('2016-12-25', '2017-12-25')),
  244. holiday = c('xmas', 'xmas'),
  245. lower_window = c(-1, -1),
  246. upper_window = c(0, 0),
  247. prior_scale = c(5., 5.)
  248. )
  249. m <- prophet(holidays = holidays, fit = FALSE)
  250. out <- prophet:::make_holiday_features(m, df$ds)
  251. priors <- out$prior.scales
  252. names <- out$holiday.names
  253. expect_true(all(priors == c(5., 5.)))
  254. expect_equal(names, c('xmas'))
  255. # 2 different priors
  256. holidays2 <- data.frame(
  257. ds = prophet:::set_date(c('2012-06-06', '2013-06-06')),
  258. holiday = c('seans-bday', 'seans-bday'),
  259. lower_window = c(0, 0),
  260. upper_window = c(1, 1),
  261. prior_scale = c(8, 8)
  262. )
  263. holidays2 <- rbind(holidays, holidays2)
  264. m <- prophet(holidays = holidays2, fit = FALSE)
  265. out <- prophet:::make_holiday_features(m, df$ds)
  266. priors <- out$prior.scales
  267. names <- out$holiday.names
  268. expect_true(all(priors == c(8, 8, 5, 5)))
  269. expect_true(all(sort(names) == c('seans-bday', 'xmas')))
  270. holidays2 <- data.frame(
  271. ds = prophet:::set_date(c('2012-06-06', '2013-06-06')),
  272. holiday = c('seans-bday', 'seans-bday'),
  273. lower_window = c(0, 0),
  274. upper_window = c(1, 1)
  275. )
  276. # manual coercions to avoid below bind_rows() warning
  277. holidays$holiday <- as.character(holidays$holiday)
  278. holidays2$holiday <- as.character(holidays2$holiday)
  279. holidays2 <- dplyr::bind_rows(holidays, holidays2)
  280. # manual factorizing to avoid above bind_rows() warning
  281. holidays2$holiday <- factor(holidays2$holiday)
  282. m <- prophet(holidays = holidays2, fit = FALSE, holidays.prior.scale = 4)
  283. out <- prophet:::make_holiday_features(m, df$ds)
  284. priors <- out$prior.scales
  285. expect_true(all(priors == c(4, 4, 5, 5)))
  286. # Check incompatible priors
  287. holidays <- data.frame(
  288. ds = prophet:::set_date(c('2016-12-25', '2016-12-27')),
  289. holiday = c('xmasish', 'xmasish'),
  290. lower_window = c(-1, -1),
  291. upper_window = c(0, 0),
  292. prior_scale = c(5., 6.)
  293. )
  294. m <- prophet(holidays = holidays, fit = FALSE)
  295. expect_error(prophet:::make_holiday_features(m, df$ds))
  296. })
  297. test_that("fit_with_holidays", {
  298. skip_if_not(Sys.getenv('R_ARCH') != '/i386')
  299. holidays <- data.frame(ds = c('2012-06-06', '2013-06-06'),
  300. holiday = c('seans-bday', 'seans-bday'),
  301. lower_window = c(0, 0),
  302. upper_window = c(1, 1))
  303. m <- prophet(DATA, holidays = holidays, uncertainty.samples = 0)
  304. expect_error(predict(m), NA)
  305. })
  306. test_that("make_future_dataframe", {
  307. skip_if_not(Sys.getenv('R_ARCH') != '/i386')
  308. train.t <- DATA[1:234, ]
  309. m <- prophet(train.t)
  310. future <- make_future_dataframe(m, periods = 3, freq = 'day',
  311. include_history = FALSE)
  312. correct <- prophet:::set_date(c('2013-04-26', '2013-04-27', '2013-04-28'))
  313. expect_equal(future$ds, correct)
  314. future <- make_future_dataframe(m, periods = 3, freq = 'month',
  315. include_history = FALSE)
  316. correct <- prophet:::set_date(c('2013-05-25', '2013-06-25', '2013-07-25'))
  317. expect_equal(future$ds, correct)
  318. })
  319. test_that("auto_weekly_seasonality", {
  320. skip_if_not(Sys.getenv('R_ARCH') != '/i386')
  321. # Should be enabled
  322. N.w <- 15
  323. train.w <- DATA[1:N.w, ]
  324. m <- prophet(train.w, fit = FALSE)
  325. expect_equal(m$weekly.seasonality, 'auto')
  326. m <- fit.prophet(m, train.w)
  327. expect_true('weekly' %in% names(m$seasonalities))
  328. true <- list(
  329. period = 7, fourier.order = 3, prior.scale = 10, mode = 'additive')
  330. for (name in names(true)) {
  331. expect_equal(m$seasonalities$weekly[[name]], true[[name]])
  332. }
  333. # Should be disabled due to too short history
  334. N.w <- 9
  335. train.w <- DATA[1:N.w, ]
  336. m <- prophet(train.w)
  337. expect_false('weekly' %in% names(m$seasonalities))
  338. expect_warning({
  339. # prophet warning: non-zero return code in optimizing
  340. m <- prophet(train.w, weekly.seasonality = TRUE)
  341. expect_true('weekly' %in% names(m$seasonalities))
  342. })
  343. # Should be False due to weekly spacing
  344. train.w <- DATA[seq(1, nrow(DATA), 7), ]
  345. m <- prophet(train.w)
  346. expect_false('weekly' %in% names(m$seasonalities))
  347. m <- prophet(DATA, weekly.seasonality = 2, seasonality.prior.scale = 3)
  348. true <- list(
  349. period = 7, fourier.order = 2, prior.scale = 3, mode = 'additive')
  350. for (name in names(true)) {
  351. expect_equal(m$seasonalities$weekly[[name]], true[[name]])
  352. }
  353. })
  354. test_that("auto_yearly_seasonality", {
  355. skip_if_not(Sys.getenv('R_ARCH') != '/i386')
  356. # Should be enabled
  357. m <- prophet(DATA, fit = FALSE)
  358. expect_equal(m$yearly.seasonality, 'auto')
  359. m <- fit.prophet(m, DATA)
  360. expect_true('yearly' %in% names(m$seasonalities))
  361. true <- list(
  362. period = 365.25, fourier.order = 10, prior.scale = 10, mode = 'additive')
  363. for (name in names(true)) {
  364. expect_equal(m$seasonalities$yearly[[name]], true[[name]])
  365. }
  366. # Should be disabled due to too short history
  367. N.w <- 240
  368. train.y <- DATA[1:N.w, ]
  369. m <- prophet(train.y)
  370. expect_false('yearly' %in% names(m$seasonalities))
  371. m <- prophet(train.y, yearly.seasonality = TRUE)
  372. expect_true('yearly' %in% names(m$seasonalities))
  373. m <- prophet(DATA, yearly.seasonality = 7, seasonality.prior.scale = 3)
  374. true <- list(
  375. period = 365.25, fourier.order = 7, prior.scale = 3, mode = 'additive')
  376. for (name in names(true)) {
  377. expect_equal(m$seasonalities$yearly[[name]], true[[name]])
  378. }
  379. })
  380. test_that("auto_daily_seasonality", {
  381. skip_if_not(Sys.getenv('R_ARCH') != '/i386')
  382. # Should be enabled
  383. m <- prophet(DATA2, fit = FALSE)
  384. expect_equal(m$daily.seasonality, 'auto')
  385. m <- fit.prophet(m, DATA2)
  386. expect_true('daily' %in% names(m$seasonalities))
  387. true <- list(
  388. period = 1, fourier.order = 4, prior.scale = 10, mode = 'additive')
  389. for (name in names(true)) {
  390. expect_equal(m$seasonalities$daily[[name]], true[[name]])
  391. }
  392. # Should be disabled due to too short history
  393. N.d <- 430
  394. train.y <- DATA2[1:N.d, ]
  395. m <- prophet(train.y)
  396. expect_false('daily' %in% names(m$seasonalities))
  397. m <- prophet(train.y, daily.seasonality = TRUE)
  398. expect_true('daily' %in% names(m$seasonalities))
  399. m <- prophet(DATA2, daily.seasonality = 7, seasonality.prior.scale = 3)
  400. true <- list(
  401. period = 1, fourier.order = 7, prior.scale = 3, mode = 'additive')
  402. for (name in names(true)) {
  403. expect_equal(m$seasonalities$daily[[name]], true[[name]])
  404. }
  405. m <- prophet(DATA)
  406. expect_false('daily' %in% names(m$seasonalities))
  407. })
  408. test_that("test_subdaily_holidays", {
  409. skip_if_not(Sys.getenv('R_ARCH') != '/i386')
  410. holidays <- data.frame(ds = c('2017-01-02'),
  411. holiday = c('special_day'))
  412. m <- prophet(DATA2, holidays=holidays)
  413. fcst <- predict(m)
  414. expect_equal(sum(fcst$special_day == 0), 575)
  415. })
  416. test_that("custom_seasonality", {
  417. skip_if_not(Sys.getenv('R_ARCH') != '/i386')
  418. holidays <- data.frame(ds = c('2017-01-02'),
  419. holiday = c('special_day'),
  420. prior_scale = c(4))
  421. m <- prophet(holidays=holidays)
  422. m <- add_seasonality(m, name='monthly', period=30, fourier.order=5)
  423. true <- list(
  424. period = 30, fourier.order = 5, prior.scale = 10, mode = 'additive')
  425. for (name in names(true)) {
  426. expect_equal(m$seasonalities$monthly[[name]], true[[name]])
  427. }
  428. expect_error(
  429. add_seasonality(m, name='special_day', period=30, fourier_order=5)
  430. )
  431. expect_error(
  432. add_seasonality(m, name='trend', period=30, fourier_order=5)
  433. )
  434. m <- add_seasonality(m, name='weekly', period=30, fourier.order=5)
  435. # Test priors
  436. m <- prophet(
  437. holidays = holidays, yearly.seasonality = FALSE,
  438. seasonality.mode = 'multiplicative')
  439. m <- add_seasonality(
  440. m, name='monthly', period=30, fourier.order=5, prior.scale = 2,
  441. mode = 'additive')
  442. m <- fit.prophet(m, DATA)
  443. expect_equal(m$seasonalities$monthly$mode, 'additive')
  444. expect_equal(m$seasonalities$weekly$mode, 'multiplicative')
  445. out <- prophet:::make_all_seasonality_features(m, m$history)
  446. prior.scales <- out$prior.scales
  447. component.cols <- out$component.cols
  448. expect_equal(sum(component.cols$monthly), 10)
  449. expect_equal(sum(component.cols$special_day), 1)
  450. expect_equal(sum(component.cols$weekly), 6)
  451. expect_equal(sum(component.cols$additive_terms), 10)
  452. expect_equal(sum(component.cols$multiplicative_terms), 7)
  453. expect_equal(sum(component.cols$monthly[1:11]), 10)
  454. expect_equal(sum(component.cols$weekly[11:17]), 6)
  455. expect_true(all(prior.scales == c(rep(2, 10), rep(10, 6), 4)))
  456. })
  457. test_that("added_regressors", {
  458. skip_if_not(Sys.getenv('R_ARCH') != '/i386')
  459. m <- prophet()
  460. m <- add_regressor(m, 'binary_feature', prior.scale=0.2)
  461. m <- add_regressor(m, 'numeric_feature', prior.scale=0.5)
  462. m <- add_regressor(
  463. m, 'numeric_feature2', prior.scale=0.5, mode = 'multiplicative')
  464. m <- add_regressor(m, 'binary_feature2', standardize=TRUE)
  465. df <- DATA
  466. df$binary_feature <- c(rep(0, 255), rep(1, 255))
  467. df$numeric_feature <- 0:509
  468. df$numeric_feature2 <- 0:509
  469. # Require all regressors in df
  470. expect_error(
  471. fit.prophet(m, df)
  472. )
  473. df$binary_feature2 <- c(rep(1, 100), rep(0, 410))
  474. m <- fit.prophet(m, df)
  475. # Check that standardizations are correctly set
  476. true <- list(
  477. prior.scale = 0.2, mu = 0, std = 1, standardize = 'auto', mode = 'additive'
  478. )
  479. for (name in names(true)) {
  480. expect_equal(true[[name]], m$extra_regressors$binary_feature[[name]])
  481. }
  482. true <- list(prior.scale = 0.5, mu = 254.5, std = 147.368585)
  483. for (name in names(true)) {
  484. expect_equal(true[[name]], m$extra_regressors$numeric_feature[[name]],
  485. tolerance = 1e-5)
  486. }
  487. expect_equal(m$extra_regressors$numeric_feature2$mode, 'multiplicative')
  488. true <- list(prior.scale = 10., mu = 0.1960784, std = 0.3974183)
  489. for (name in names(true)) {
  490. expect_equal(true[[name]], m$extra_regressors$binary_feature2[[name]],
  491. tolerance = 1e-5)
  492. }
  493. # Check that standardization is done correctly
  494. df2 <- prophet:::setup_dataframe(m, df)$df
  495. expect_equal(df2$binary_feature[1], 0)
  496. expect_equal(df2$numeric_feature[1], -1.726962, tolerance = 1e-4)
  497. expect_equal(df2$binary_feature2[1], 2.022859, tolerance = 1e-4)
  498. # Check that feature matrix and prior scales are correctly constructed
  499. out <- prophet:::make_all_seasonality_features(m, df2)
  500. seasonal.features <- out$seasonal.features
  501. prior.scales <- out$prior.scales
  502. component.cols <- out$component.cols
  503. modes <- out$modes
  504. expect_equal(ncol(seasonal.features), 30)
  505. r_names <- c('binary_feature', 'numeric_feature', 'binary_feature2')
  506. true.priors <- c(0.2, 0.5, 10.)
  507. for (i in seq_along(r_names)) {
  508. name <- r_names[i]
  509. expect_true(name %in% colnames(seasonal.features))
  510. expect_equal(sum(component.cols[[name]]), 1)
  511. expect_equal(sum(prior.scales * component.cols[[name]]), true.priors[i])
  512. }
  513. # Check that forecast components are reasonable
  514. future <- data.frame(
  515. ds = c('2014-06-01'),
  516. binary_feature = c(0),
  517. numeric_feature = c(10),
  518. numeric_feature2 = c(10)
  519. )
  520. expect_error(predict(m, future))
  521. future$binary_feature2 <- 0.
  522. fcst <- predict(m, future)
  523. expect_equal(ncol(fcst), 37)
  524. expect_equal(fcst$binary_feature[1], 0)
  525. expect_equal(fcst$extra_regressors_additive[1],
  526. fcst$numeric_feature[1] + fcst$binary_feature2[1])
  527. expect_equal(fcst$extra_regressors_multiplicative[1],
  528. fcst$numeric_feature2[1])
  529. expect_equal(fcst$additive_terms[1],
  530. fcst$yearly[1] + fcst$weekly[1]
  531. + fcst$extra_regressors_additive[1])
  532. expect_equal(fcst$multiplicative_terms[1],
  533. fcst$extra_regressors_multiplicative[1])
  534. expect_equal(
  535. fcst$yhat[1],
  536. fcst$trend[1] * (1 + fcst$multiplicative_terms[1]) + fcst$additive_terms[1]
  537. )
  538. # Check works with constant extra regressor of 0
  539. df$constant_feature <- 0
  540. m <- prophet()
  541. m <- add_regressor(m, 'constant_feature', standardize = TRUE)
  542. m <- fit.prophet(m, df)
  543. expect_equal(m$extra_regressors$constant_feature$std, 1)
  544. })
  545. test_that("set_seasonality_mode", {
  546. skip_if_not(Sys.getenv('R_ARCH') != '/i386')
  547. m <- prophet()
  548. expect_equal(m$seasonality.mode, 'additive')
  549. m <- prophet(seasonality.mode = 'multiplicative')
  550. expect_equal(m$seasonality.mode, 'multiplicative')
  551. expect_error(prophet(seasonality.mode = 'batman'))
  552. })
  553. test_that("seasonality_modes", {
  554. skip_if_not(Sys.getenv('R_ARCH') != '/i386')
  555. holidays <- data.frame(ds = c('2016-12-25'),
  556. holiday = c('xmas'),
  557. lower_window = c(-1),
  558. upper_window = c(0))
  559. m <- prophet(seasonality.mode = 'multiplicative', holidays = holidays)
  560. m <- add_seasonality(
  561. m, name = 'monthly', period = 30, fourier.order = 3, mode = 'additive')
  562. m <- add_regressor(m, name = 'binary_feature', mode = 'additive')
  563. m <- add_regressor(m, name = 'numeric_feature')
  564. # Construct seasonal features
  565. df <- DATA
  566. df$binary_feature <- c(rep(0, 255), rep(1, 255))
  567. df$numeric_feature <- 0:509
  568. out <- prophet:::setup_dataframe(m, df, initialize_scales = TRUE)
  569. df <- out$df
  570. m <- out$m
  571. m$history <- df
  572. m <- prophet:::set_auto_seasonalities(m)
  573. out <- prophet:::make_all_seasonality_features(m, df)
  574. component.cols <- out$component.cols
  575. modes <- out$modes
  576. expect_equal(sum(component.cols$additive_terms), 7)
  577. expect_equal(sum(component.cols$multiplicative_terms), 29)
  578. expect_equal(
  579. sort(modes$additive),
  580. c('additive_terms', 'binary_feature', 'extra_regressors_additive',
  581. 'monthly')
  582. )
  583. expect_equal(
  584. sort(modes$multiplicative),
  585. c('extra_regressors_multiplicative', 'holidays', 'multiplicative_terms',
  586. 'numeric_feature', 'weekly', 'xmas', 'yearly')
  587. )
  588. })