tests/testthat/test_piecewise.R

# test piecewise
## model 1
require(piecewise)
require(dplyr)
context("")

mod_01 <- data.frame(x_1 = rep(1:6,each = 6), x_2 = rep(1:6, times = 6)) %>%
  dplyr::mutate(y = 1*x_1 + 3*x_2) %>%
  glm(formula = "y ~ x_1 + x_2", data = .)
con_01 <- "(x_1 > 0 & x_1 <= 6) & (x_2 > 0 & x_2 <= 6)"

mod_02 <- data.frame(x_1 = rep(1:6,each = 6), x_2 = rep(1:6, times = 6)) %>%
  dplyr::mutate(y = 20 - 1*x_1 + 3*x_2) %>%
  glm(formula = "y ~ x_1 + x_2", data = .)
con_02 <- "x_1 > 6 | x_2 > 6"

p <- piecewise(models = list(mod_01, mod_02), condition = c(con_01, con_02))
pred <- predict(p, newdata = data.frame(x_1 = 1, x_2 = 2))

test_that("",{
  expect_equal(length(p), 2)
  expect_equal(names(p), c("models", "condition"))
  expect_equal(pred[[1]], predict(mod_01, newdata = data.frame(x_1 = 1, x_2 = 2)))
})
alvarofranq/piecewise documentation built on May 27, 2019, 7:43 a.m.