test_prophet.R 5.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190
  1. library(prophet)
  2. context("Prophet tests")
  3. DATA <- read.csv('data.csv')
  4. DATA$ds <- as.Date(DATA$ds)
  5. N <- nrow(DATA)
  6. train <- DATA[1:floor(N / 2), ]
  7. future <- DATA[(ceiling(N/2) + 1):N, ]
  8. test_that("load_models", {
  9. expect_error(prophet:::get_prophet_stan_model('linear'), NA)
  10. expect_error(prophet:::get_prophet_stan_model('logistic'), NA)
  11. })
  12. test_that("fit_predict", {
  13. skip_if_not(Sys.getenv('R_ARCH') != '/i386')
  14. m <- prophet(train)
  15. expect_error(predict(m, future), NA)
  16. })
  17. test_that("fit_predict_no_seasons", {
  18. skip_if_not(Sys.getenv('R_ARCH') != '/i386')
  19. m <- prophet(train, weekly.seasonality = FALSE, yearly.seasonality = FALSE)
  20. expect_error(predict(m, future), NA)
  21. })
  22. test_that("fit_predict_no_changepoints", {
  23. skip_if_not(Sys.getenv('R_ARCH') != '/i386')
  24. m <- prophet(train, n.changepoints = 0)
  25. expect_error(predict(m, future), NA)
  26. })
  27. test_that("setup_dataframe", {
  28. history <- train
  29. m <- prophet(history, fit = FALSE)
  30. out <- prophet:::setup_dataframe(m, history, initialize_scales = TRUE)
  31. history <- out$df
  32. expect_true('t' %in% colnames(history))
  33. expect_equal(min(history$t), 0)
  34. expect_equal(max(history$t), 1)
  35. expect_true('y_scaled' %in% colnames(history))
  36. expect_equal(max(history$y_scaled), 1)
  37. })
  38. test_that("get_changepoints", {
  39. history <- train
  40. m <- prophet(history, fit = FALSE)
  41. out <- prophet:::setup_dataframe(m, history, initialize_scales = TRUE)
  42. history <- out$df
  43. m <- out$m
  44. m$history <- history
  45. m <- prophet:::set_changepoints(m)
  46. cp <- prophet:::get_changepoint_indexes(m)
  47. expect_equal(length(cp), m$n.changepoints)
  48. expect_true(min(cp) > 0)
  49. expect_true(max(cp) < N)
  50. mat <- prophet:::get_changepoint_matrix(m)
  51. expect_equal(nrow(mat), floor(N / 2))
  52. expect_equal(ncol(mat), m$n.changepoints)
  53. })
  54. test_that("get_zero_changepoints", {
  55. history <- train
  56. m <- prophet(history, n.changepoints = 0, fit = FALSE)
  57. out <- prophet:::setup_dataframe(m, history, initialize_scales = TRUE)
  58. m <- out$m
  59. history <- out$df
  60. m$history <- history
  61. m <- prophet:::set_changepoints(m)
  62. cp <- prophet:::get_changepoint_indexes(m)
  63. expect_equal(length(cp), 1)
  64. expect_equal(cp[1], 1)
  65. mat <- prophet:::get_changepoint_matrix(m)
  66. expect_equal(nrow(mat), floor(N / 2))
  67. expect_equal(ncol(mat), 1)
  68. })
  69. test_that("fourier_series_weekly", {
  70. mat <- prophet:::fourier_series(DATA$ds, 7, 3)
  71. true.values <- c(0.7818315, 0.6234898, 0.9749279, -0.2225209, 0.4338837,
  72. -0.9009689)
  73. expect_equal(true.values, mat[1, ], tolerance = 1e-6)
  74. })
  75. test_that("fourier_series_yearly", {
  76. mat <- prophet:::fourier_series(DATA$ds, 365.25, 3)
  77. true.values <- c(0.7006152, -0.7135393, -0.9998330, 0.01827656, 0.7262249,
  78. 0.6874572)
  79. expect_equal(true.values, mat[1, ], tolerance = 1e-6)
  80. })
  81. test_that("growth_init", {
  82. history <- DATA
  83. history$cap <- max(history$y)
  84. m <- prophet(history, growth = 'logistic', fit = FALSE)
  85. out <- prophet:::setup_dataframe(m, history, initialize_scales = TRUE)
  86. m <- out$m
  87. history <- out$df
  88. params <- prophet:::linear_growth_init(history)
  89. expect_equal(params[1], 0.3055671, tolerance = 1e-6)
  90. expect_equal(params[2], 0.5307511, tolerance = 1e-6)
  91. params <- prophet:::logistic_growth_init(history)
  92. expect_equal(params[1], 1.507925, tolerance = 1e-6)
  93. expect_equal(params[2], -0.08167497, tolerance = 1e-6)
  94. })
  95. test_that("piecewise_linear", {
  96. t <- seq(0, 10)
  97. m <- 0
  98. k <- 1.0
  99. deltas <- c(0.5)
  100. changepoint.ts <- c(5)
  101. y <- prophet:::piecewise_linear(t, deltas, k, m, changepoint.ts)
  102. y.true <- c(0, 1, 2, 3, 4, 5, 6.5, 8, 9.5, 11, 12.5)
  103. expect_equal(y, y.true)
  104. t <- t[8:length(t)]
  105. y.true <- y.true[8:length(y.true)]
  106. y <- prophet:::piecewise_linear(t, deltas, k, m, changepoint.ts)
  107. expect_equal(y, y.true)
  108. })
  109. test_that("piecewise_logistic", {
  110. t <- seq(0, 10)
  111. cap <- rep(10, 11)
  112. m <- 0
  113. k <- 1.0
  114. deltas <- c(0.5)
  115. changepoint.ts <- c(5)
  116. y <- prophet:::piecewise_logistic(t, cap, deltas, k, m, changepoint.ts)
  117. y.true <- c(5.000000, 7.310586, 8.807971, 9.525741, 9.820138, 9.933071,
  118. 9.984988, 9.996646, 9.999252, 9.999833, 9.999963)
  119. expect_equal(y, y.true, tolerance = 1e-6)
  120. t <- t[8:length(t)]
  121. y.true <- y.true[8:length(y.true)]
  122. cap <- cap[8:length(cap)]
  123. y <- prophet:::piecewise_logistic(t, cap, deltas, k, m, changepoint.ts)
  124. expect_equal(y, y.true, tolerance = 1e-6)
  125. })
  126. test_that("holidays", {
  127. holidays = data.frame(ds = zoo::as.Date(c('2016-12-25')),
  128. holiday = c('xmas'),
  129. lower_window = c(-1),
  130. upper_window = c(0))
  131. df <- data.frame(
  132. ds = seq(zoo::as.Date('2016-12-20'), zoo::as.Date('2016-12-31'), by='d'))
  133. m <- prophet(train, holidays = holidays, fit = FALSE)
  134. feats <- prophet:::make_holiday_features(m, df$ds)
  135. expect_equal(nrow(feats), nrow(df))
  136. expect_equal(ncol(feats), 2)
  137. expect_equal(sum(colSums(feats) - c(1, 1)), 0)
  138. holidays = data.frame(ds = zoo::as.Date(c('2016-12-25')),
  139. holiday = c('xmas'),
  140. lower_window = c(-1),
  141. upper_window = c(10))
  142. m <- prophet(train, holidays = holidays, fit = FALSE)
  143. feats <- prophet:::make_holiday_features(m, df$ds)
  144. expect_equal(nrow(feats), nrow(df))
  145. expect_equal(ncol(feats), 12)
  146. })
  147. test_that("fit_with_holidays", {
  148. skip_if_not(Sys.getenv('R_ARCH') != '/i386')
  149. holidays <- data.frame(ds = zoo::as.Date(c('2012-06-06', '2013-06-06')),
  150. holiday = c('seans-bday', 'seans-bday'),
  151. lower_window = c(0, 0),
  152. upper_window = c(1, 1))
  153. m <- prophet(DATA, holidays = holidays, uncertainty.samples = 0)
  154. expect_error(predict(m), NA)
  155. })