suppressPackageStartupMessages(require(dplyr))
require(dplyr)

This document introduce you to piecewise basic usage. As an example, in this vignette the following piecewise model is build:

$$ f(x) = \Bigg{ \begin{array}{cc} x_1 + 2*x_2 & if & x_i \in [0:6) \ 20 - x_1 - x_2 & if & x_i \in [6:10] \ 0 & otherwise \end{array} $$

First, the three pieces of the model are generated.

# Models generation
# First model generation
mod_01 <- data.frame(x_1 = rep(1:6,each = 6), x_2 = rep(1:6, times = 6)) %>%
  mutate(y = x_1 + 2*x_2) %>%
  glm(formula = "y ~ x_1 + x_2", data = .)

# Second model generation
mod_02 <- data.frame(x_1 = rep(6:10,each = 5), x_2 = rep(6:10, times = 5)) %>%
  mutate(y = 20 - x_1 + x_2) %>%
  glm(formula = "y ~ x_1 + x_2", data = .)

# Third model generation
mod_03 <- data.frame(x_1 = 0, x_2 = 0, y = 0) %>% glm(formula = "y ~ x_1 + x_2", data = .)

Once the models have been created. The conditions of the model are defined

# Conditions generation
# First condition
cond_01 <- "x >= 0 & x < 6"
# Second condition
cond_02 <- "x >= 6 & x <= 10"
# Third condition
cond_03 <- "x < 0 & x > 10"

piecewise model

suppressPackageStartupMessages(require(piecewise))
p <- piecewise(models = list(mod_01, mod_02, mod_03), condition = c(cond_01, cond_02, cond_03))
require(piecewise)
p <- piecewise(models = list(mod_01, mod_02, mod_03), condition = c(cond_01, cond_02, cond_03))


alvarofranq/piecewise documentation built on May 27, 2019, 7:43 a.m.