## Loading required package: purrr
An R package to manage piecewise models
# To install the development version from GitHub:
# install.packages("devtools")
devtools::install_github("alvarofranq/piecewise")
require(piecewise)
#> Loading required package: piecewise
set.seed(0)
df_01 <- data.frame(x = 1:5, y = (1:5)+rnorm(5,0,1))
mod_01 <- glm("y ~ x", data = df_01)
con_01 <- "x >= 0 & x < 6"
df_02 <- data.frame(x = 6:10, y = (5:1)+rnorm(5,0,1))
mod_02 <- glm("y ~ x", data = df_02)
con_02 <- "x >= 6 & x <= 10"
p <- piecewise(models = list(mod_01 = mod_01, mod_02 = mod_02),condition = c(con_01, con_02))
# get fitted values for each model
fitted(p)
#> $mod_01
#> 1 2 3 4 5
#> 1.810311 2.800514 3.790718 4.780922 5.771126
#>
#> $mod_02
#> 1 2 3 4 5
#> 3.164728 3.045929 2.927130 2.808330 2.689531
# predict some values
newdata <- data.frame(x = 1.5)
predict(p, newdata = newdata)
#> [[1]]
#> mod_01.1
#> 2.305413
Add the following code to your website.
For more information on customizing the embed code, read Embedding Snippets.