test_prophet.R 21 KB

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