test_prophet.R 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387
  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. })
  286. test_that("copy", {
  287. inputs <- list(
  288. growth = c('linear', 'logistic'),
  289. changepoints = c(NULL, c('2016-12-25')),
  290. n.changepoints = c(3),
  291. yearly.seasonality = c(TRUE, FALSE),
  292. weekly.seasonality = c(TRUE, FALSE),
  293. daily.seasonality = c(TRUE, FALSE),
  294. holidays = c(NULL, 'insert_dataframe'),
  295. seasonality.prior.scale = c(1.1),
  296. holidays.prior.scale = c(1.1),
  297. changepoints.prior.scale = c(0.1),
  298. mcmc.samples = c(100),
  299. interval.width = c(0.9),
  300. uncertainty.samples = c(200)
  301. )
  302. products <- expand.grid(inputs)
  303. for (i in 1:length(products)) {
  304. if (products$holidays[i] == 'insert_dataframe') {
  305. holidays <- data.frame(ds=c('2016-12-25'), holiday=c('x'))
  306. } else {
  307. holidays <- NULL
  308. }
  309. m1 <- prophet(
  310. growth = products$growth[i],
  311. changepoints = products$changepoints[i],
  312. n.changepoints = products$n.changepoints[i],
  313. yearly.seasonality = products$yearly.seasonality[i],
  314. weekly.seasonality = products$weekly.seasonality[i],
  315. daily.seasonality = products$daily.seasonality[i],
  316. holidays = holidays,
  317. seasonality.prior.scale = products$seasonality.prior.scale[i],
  318. holidays.prior.scale = products$holidays.prior.scale[i],
  319. changepoints.prior.scale = products$changepoints.prior.scale[i],
  320. mcmc.samples = products$mcmc.samples[i],
  321. interval.width = products$interval.width[i],
  322. uncertainty.samples = products$uncertainty.samples[i],
  323. fit = FALSE
  324. )
  325. m2 <- prophet:::prophet_copy(m1)
  326. # Values should be copied correctly
  327. for (arg in names(inputs)) {
  328. expect_equal(m1[[arg]], m2[[arg]])
  329. }
  330. }
  331. # Check for cutoff
  332. changepoints <- seq.Date(as.Date('2012-06-15'), as.Date('2012-09-15'), by='d')
  333. cutoff <- as.Date('2012-07-25')
  334. m1 <- prophet(DATA, changepoints = changepoints)
  335. m2 <- prophet:::prophet_copy(m1, cutoff)
  336. changepoints <- changepoints[changepoints <= cutoff]
  337. expect_equal(prophet:::set_date(changepoints), m2$changepoints)
  338. })