test_prophet.R 22 KB

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