test_prophet.R 9.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305
  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("setup_dataframe", {
  42. history <- train
  43. m <- prophet(history, fit = FALSE)
  44. out <- prophet:::setup_dataframe(m, history, initialize_scales = TRUE)
  45. history <- out$df
  46. expect_true('t' %in% colnames(history))
  47. expect_equal(min(history$t), 0)
  48. expect_equal(max(history$t), 1)
  49. expect_true('y_scaled' %in% colnames(history))
  50. expect_equal(max(history$y_scaled), 1)
  51. })
  52. test_that("get_changepoints", {
  53. history <- train
  54. m <- prophet(history, fit = FALSE)
  55. out <- prophet:::setup_dataframe(m, history, initialize_scales = TRUE)
  56. history <- out$df
  57. m <- out$m
  58. m$history <- history
  59. m <- prophet:::set_changepoints(m)
  60. cp <- m$changepoints.t
  61. expect_equal(length(cp), m$n.changepoints)
  62. expect_true(min(cp) > 0)
  63. expect_true(max(cp) < N)
  64. mat <- prophet:::get_changepoint_matrix(m)
  65. expect_equal(nrow(mat), floor(N / 2))
  66. expect_equal(ncol(mat), m$n.changepoints)
  67. })
  68. test_that("get_zero_changepoints", {
  69. history <- train
  70. m <- prophet(history, n.changepoints = 0, fit = FALSE)
  71. out <- prophet:::setup_dataframe(m, history, initialize_scales = TRUE)
  72. m <- out$m
  73. history <- out$df
  74. m$history <- history
  75. m <- prophet:::set_changepoints(m)
  76. cp <- m$changepoints.t
  77. expect_equal(length(cp), 1)
  78. expect_equal(cp[1], 0)
  79. mat <- prophet:::get_changepoint_matrix(m)
  80. expect_equal(nrow(mat), floor(N / 2))
  81. expect_equal(ncol(mat), 1)
  82. })
  83. test_that("fourier_series_weekly", {
  84. mat <- prophet:::fourier_series(DATA$ds, 7, 3)
  85. true.values <- c(0.9165623, 0.3998920, 0.7330519, -0.6801727, -0.3302791,
  86. -0.9438833)
  87. expect_equal(true.values, mat[1, ], tolerance = 1e-6)
  88. })
  89. test_that("fourier_series_yearly", {
  90. mat <- prophet:::fourier_series(DATA$ds, 365.25, 3)
  91. true.values <- c(0.69702635, -0.71704551, -0.99959923, 0.02830854,
  92. 0.73648994, 0.67644849)
  93. expect_equal(true.values, mat[1, ], tolerance = 1e-6)
  94. })
  95. test_that("growth_init", {
  96. history <- DATA[1:468, ]
  97. history$cap <- max(history$y)
  98. m <- prophet(history, growth = 'logistic', fit = FALSE)
  99. out <- prophet:::setup_dataframe(m, history, initialize_scales = TRUE)
  100. m <- out$m
  101. history <- out$df
  102. params <- prophet:::linear_growth_init(history)
  103. expect_equal(params[1], 0.3055671, tolerance = 1e-6)
  104. expect_equal(params[2], 0.5307511, tolerance = 1e-6)
  105. params <- prophet:::logistic_growth_init(history)
  106. expect_equal(params[1], 1.507925, tolerance = 1e-6)
  107. expect_equal(params[2], -0.08167497, tolerance = 1e-6)
  108. })
  109. test_that("piecewise_linear", {
  110. t <- seq(0, 10)
  111. m <- 0
  112. k <- 1.0
  113. deltas <- c(0.5)
  114. changepoint.ts <- c(5)
  115. y <- prophet:::piecewise_linear(t, deltas, k, m, changepoint.ts)
  116. y.true <- c(0, 1, 2, 3, 4, 5, 6.5, 8, 9.5, 11, 12.5)
  117. expect_equal(y, y.true)
  118. t <- t[8:length(t)]
  119. y.true <- y.true[8:length(y.true)]
  120. y <- prophet:::piecewise_linear(t, deltas, k, m, changepoint.ts)
  121. expect_equal(y, y.true)
  122. })
  123. test_that("piecewise_logistic", {
  124. t <- seq(0, 10)
  125. cap <- rep(10, 11)
  126. m <- 0
  127. k <- 1.0
  128. deltas <- c(0.5)
  129. changepoint.ts <- c(5)
  130. y <- prophet:::piecewise_logistic(t, cap, deltas, k, m, changepoint.ts)
  131. y.true <- c(5.000000, 7.310586, 8.807971, 9.525741, 9.820138, 9.933071,
  132. 9.984988, 9.996646, 9.999252, 9.999833, 9.999963)
  133. expect_equal(y, y.true, tolerance = 1e-6)
  134. t <- t[8:length(t)]
  135. y.true <- y.true[8:length(y.true)]
  136. cap <- cap[8:length(cap)]
  137. y <- prophet:::piecewise_logistic(t, cap, deltas, k, m, changepoint.ts)
  138. expect_equal(y, y.true, tolerance = 1e-6)
  139. })
  140. test_that("holidays", {
  141. holidays = data.frame(ds = c('2016-12-25'),
  142. holiday = c('xmas'),
  143. lower_window = c(-1),
  144. upper_window = c(0))
  145. df <- data.frame(
  146. ds = seq(prophet:::set_date('2016-12-20'),
  147. prophet:::set_date('2016-12-31'), by='d'))
  148. m <- prophet(train, holidays = holidays, fit = FALSE)
  149. feats <- prophet:::make_holiday_features(m, df$ds)
  150. expect_equal(nrow(feats), nrow(df))
  151. expect_equal(ncol(feats), 2)
  152. expect_equal(sum(colSums(feats) - c(1, 1)), 0)
  153. holidays = data.frame(ds = c('2016-12-25'),
  154. holiday = c('xmas'),
  155. lower_window = c(-1),
  156. upper_window = c(10))
  157. m <- prophet(train, holidays = holidays, fit = FALSE)
  158. feats <- prophet:::make_holiday_features(m, df$ds)
  159. expect_equal(nrow(feats), nrow(df))
  160. expect_equal(ncol(feats), 12)
  161. })
  162. test_that("fit_with_holidays", {
  163. skip_if_not(Sys.getenv('R_ARCH') != '/i386')
  164. holidays <- data.frame(ds = c('2012-06-06', '2013-06-06'),
  165. holiday = c('seans-bday', 'seans-bday'),
  166. lower_window = c(0, 0),
  167. upper_window = c(1, 1))
  168. m <- prophet(DATA, holidays = holidays, uncertainty.samples = 0)
  169. expect_error(predict(m), NA)
  170. })
  171. test_that("make_future_dataframe", {
  172. skip_if_not(Sys.getenv('R_ARCH') != '/i386')
  173. train.t <- DATA[1:234, ]
  174. m <- prophet(train.t)
  175. future <- make_future_dataframe(m, periods = 3, freq = 'day',
  176. include_history = FALSE)
  177. correct <- prophet:::set_date(c('2013-04-26', '2013-04-27', '2013-04-28'))
  178. expect_equal(future$ds, correct)
  179. future <- make_future_dataframe(m, periods = 3, freq = 'month',
  180. include_history = FALSE)
  181. correct <- prophet:::set_date(c('2013-05-25', '2013-06-25', '2013-07-25'))
  182. expect_equal(future$ds, correct)
  183. })
  184. test_that("auto_weekly_seasonality", {
  185. skip_if_not(Sys.getenv('R_ARCH') != '/i386')
  186. # Should be enabled
  187. N.w <- 15
  188. train.w <- DATA[1:N.w, ]
  189. m <- prophet(train.w, fit = FALSE)
  190. expect_equal(m$weekly.seasonality, 'auto')
  191. m <- prophet:::fit.prophet(m, train.w)
  192. expect_true('weekly' %in% names(m$seasonalities))
  193. expect_equal(m$seasonalities[['weekly']], c(7, 3))
  194. # Should be disabled due to too short history
  195. N.w <- 9
  196. train.w <- DATA[1:N.w, ]
  197. m <- prophet(train.w)
  198. expect_false('weekly' %in% names(m$seasonalities))
  199. m <- prophet(train.w, weekly.seasonality = TRUE)
  200. expect_true('weekly' %in% names(m$seasonalities))
  201. # Should be False due to weekly spacing
  202. train.w <- DATA[seq(1, nrow(DATA), 7), ]
  203. m <- prophet(train.w)
  204. expect_false('weekly' %in% names(m$seasonalities))
  205. m <- prophet(DATA, weekly.seasonality=2)
  206. expect_equal(m$seasonalities[['weekly']], c(7, 2))
  207. })
  208. test_that("auto_yearly_seasonality", {
  209. skip_if_not(Sys.getenv('R_ARCH') != '/i386')
  210. # Should be enabled
  211. m <- prophet(DATA, fit = FALSE)
  212. expect_equal(m$yearly.seasonality, 'auto')
  213. m <- prophet:::fit.prophet(m, DATA)
  214. expect_true('yearly' %in% names(m$seasonalities))
  215. expect_equal(m$seasonalities[['yearly']], c(365.25, 10))
  216. # Should be disabled due to too short history
  217. N.w <- 240
  218. train.y <- DATA[1:N.w, ]
  219. m <- prophet(train.y)
  220. expect_false('yearly' %in% names(m$seasonalities))
  221. m <- prophet(train.y, yearly.seasonality = TRUE)
  222. expect_true('yearly' %in% names(m$seasonalities))
  223. m <- prophet(DATA, yearly.seasonality=7)
  224. expect_equal(m$seasonalities[['yearly']], c(365.25, 7))
  225. })
  226. test_that("auto_daily_seasonality", {
  227. skip_if_not(Sys.getenv('R_ARCH') != '/i386')
  228. # Should be enabled
  229. m <- prophet(DATA2, fit = FALSE)
  230. expect_equal(m$daily.seasonality, 'auto')
  231. m <- prophet:::fit.prophet(m, DATA2)
  232. expect_true('daily' %in% names(m$seasonalities))
  233. expect_equal(m$seasonalities[['daily']], c(1, 4))
  234. # Should be disabled due to too short history
  235. N.d <- 430
  236. train.y <- DATA2[1:N.d, ]
  237. m <- prophet(train.y)
  238. expect_false('daily' %in% names(m$seasonalities))
  239. m <- prophet(train.y, daily.seasonality = TRUE)
  240. expect_true('daily' %in% names(m$seasonalities))
  241. m <- prophet(DATA2, daily.seasonality=7)
  242. expect_equal(m$seasonalities[['daily']], c(1, 7))
  243. m <- prophet(DATA)
  244. expect_false('daily' %in% names(m$seasonalities))
  245. })
  246. test_that("test_subdaily_holidays", {
  247. skip_if_not(Sys.getenv('R_ARCH') != '/i386')
  248. holidays <- data.frame(ds = c('2017-01-02'),
  249. holiday = c('special_day'))
  250. m <- prophet(DATA2, holidays=holidays)
  251. fcst <- predict(m)
  252. expect_equal(sum(fcst$special_day == 0), 575)
  253. })
  254. test_that("custom_seasonality", {
  255. skip_if_not(Sys.getenv('R_ARCH') != '/i386')
  256. holidays <- data.frame(ds = c('2017-01-02'),
  257. holiday = c('special_day'))
  258. m <- prophet(holidays=holidays)
  259. m <- add_seasonality(m, name='monthly', period=30, fourier.order=5)
  260. expect_equal(m$seasonalities[['monthly']], c(30, 5))
  261. })