knitr::opts_chunk$set( collapse = TRUE, comment = "#>", fig.path = "man/figures/README-", out.width = "100%" )
lspline
: Linear Splines with Convenient ParameterizationsLinear splines with convenient parameterizations such that:
Knot locations can be specified
lspline()
)x
into q
equal-frequency intervals (qlspline()
)x
into n
equal-width intervals (elspline()
)Inspired by Stata command mkspline
and function lspline()
from package ares [@r-ares]. As such, the implementation follows @greene2003econometric, chapter 7.5.2.
From CRAN or development version using remotes:
remotes::install_github("mbojan/lspline", build_vignettes = TRUE)
See package homepage and the vignette.
library(hexSticker) library(tidyverse) library(ggforce) # 1 ---------------------------------------------------------------------------- d1 <- tibble::tribble( ~x, ~y, 0, 0, 4, 1, # b = 1/4 8, -1, # b = 1/2 10, 2 # b = 3 ) d2 <- tibble::tribble( ~x, ~y, 4, 1, 6, 1.5 ) d3 <- tibble::tribble( ~x, ~y, 8, -1, 10, -2, ) arcs <- tibble::tribble( ~x0, ~y0, ~r, ~start, ~end, 4, 1, 0.5, pi/2 - atan(1/4), pi/2 + atan(1/2), 8, -1, 0.5, pi/2 - atan(3/2), pi/2 + atan(1/2) ) d1 |> ggplot() + geom_line(aes(x=x, y=y), data = d1, linetype = "solid") + geom_line(aes(x=x, y=y), data = d2, linetype = "dotted") + geom_line(aes(x=x, y=y), data = d3, linetype = "dotted") + geom_arc(aes(x0 = x0, y0 = y0, r = r, start = start, end = end), data = arcs) + coord_fixed() # 2 ---------------------------------------------------------------------------- d <- tibble( y = c(0, 2, 1, 1.5), x = seq(along = y) ) ggplot() + geom_line(aes(x=x, y=y), data = d, linetype = "solid") + coord_fixed() # 3 ---------------------------------------------------------------------------- d <- tibble::tribble( ~x, ~y, 0, 0, 2, 2, 3, 3, 5, 1, 6, 0 ) |> map(embed, 2) |> imap(~ structure(.x, dimnames=list(NULL, paste0(.y, c('end', ''))))) |> map(as_tibble) |> bind_cols() |> mutate( lt = rep(c("s", "d"), length = 4) ) d |> ggplot(aes(x=x, y=y, xend=xend, yend=yend)) + geom_segment(data = filter(d, lt == "s")) + scale_linetype_manual( breaks = c("s", "d"), values = c("solid", "dotted"), guide = "none" ) # 4 ---------------------------------------------------------------------------- # Generate data for the spline based on the sequence of values for 'y'. y <- c(0, 3, 1, 2) # Solid line d_solid <- tibble(x = seq(along=y), y = y) # Extends d_dashed <- tibble::tribble( ~x, ~y, ~xend, ~yend, 2, 3, 2.5, 4.5, 3, 1, 3.5, 0 ) d_arcs <- tibble::tribble( ~x0, ~y0, ~r, ~start, ~end, 2, 3, 0.5, pi/2 - atan(3), pi/2 + atan(2), 3, 1, 0.5, pi/2 - atan(1), pi/2 + atan(2) ) d_labels <- tibble::tribble( ~x, ~y, ~label, 2, 3, "beta[1]", 3, 1, "beta[2]" ) p <- ggplot() + geom_line(aes(x=x, y=y), data = d_solid) + geom_segment(aes(x=x, y=y, xend=xend, yend=yend), data = d_dashed, linetype = "dotted" ) + geom_arc(aes(x0=x0, y0=y0, r=r, start=start, end=end), data = d_arcs) + geom_label(aes(x=x, y=y, label=label), data = d_labels, parse = TRUE, nudge_x = 0.25, nudge_y = 0) + coord_fixed() + theme_void() sticker( p, s_x = 1, s_y = 1, s_width = 2, s_height = 2, package = "lspline", ) |> plot()
Add the following code to your website.
For more information on customizing the embed code, read Embedding Snippets.