An open API service indexing awesome lists of open source software.

https://github.com/mbojan/lspline

R package: Linear Splines with Convenient Parameterizations
https://github.com/mbojan/lspline

generalized-linear-models r r-package splines

Last synced: about 1 month ago
JSON representation

R package: Linear Splines with Convenient Parameterizations

Awesome Lists containing this project

README

        

---
output:
github_document:
html_preview: true
bibliography: vignettes/lspline.bib
editor_options:
chunk_output_type: console
---

```{r, include = FALSE}
knitr::opts_chunk$set(
collapse = TRUE,
comment = "#>",
fig.path = "man/figures/README-",
out.width = "100%"
)
```

# `lspline`: Linear Splines with Convenient Parameterizations

[![R-CMD-check](https://github.com/mbojan/lspline/actions/workflows/R-CMD-check.yaml/badge.svg)](https://github.com/mbojan/lspline/actions/workflows/R-CMD-check.yaml)
[![rstudio mirror downloads](http://cranlogs.r-pkg.org/badges/lspline?color=2ED968)](http://cranlogs.r-pkg.org/)
[![cran version](http://www.r-pkg.org/badges/version/lspline)](https://cran.r-project.org/package=lspline)

Linear splines with convenient parameterizations such that:

- coefficients are slopes of consecutive segments
- coefficients capture slope change at consecutive knots

Knot locations can be specified

- manually (`lspline()`)
- at breaks dividing the range of `x` into `q` equal-frequency intervals (`qlspline()`)
- at breaks dividing the range of `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.

## Installation

From CRAN or development version using **remotes**:

```{r install, eval=FALSE}
remotes::install_github("mbojan/lspline", build_vignettes = TRUE)
```

## Examples

See package [homepage](https://mbojan.github.io/lspline/) and the [vignette](https://mbojan.github.io/lspline/articles/lspline.html).

```{r logo, include=FALSE, eval=FALSE}
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()
```

## References