Segmented Linear Regression Models in R
Detect transition points by fitting two linear segments and selecting the best split.
- What is Regrans?
- Main features
- Installation
- Quick start
- How it works
- Functions overview
- Data included
- Documentation
- Authors
- License
Regrans is an R package for fitting segmented linear regressions (piecewise linear models with two segments).
It evaluates candidate split points along the explanatory variable, fits one linear model to the left side and another to the right side, and returns the split that minimizes the total residual sum of squares.
This is especially useful when your data suggests a transition point, breakpoint, or allometric inflection.
- ✅ Fit two-segment linear regressions from numeric vectors (
x,y) - ✅ Automatic search for the best split point
- ✅ Summary methods for left/right fitted models
- ✅ Built-in plotting helper for segmented fits
- ✅ ANCOVA helper to compare segment parameters
- ✅ Example dataset (
simdata) included
# install.packages("devtools")
library(devtools)
install_github("rodrigosantana/Regrans")Download:
Install from terminal:
R CMD INSTALL -l /path/to/your/R/library Regrans_0.0.1.tar.gzOr from R:
install.packages("Regrans_0.0.1.tar.gz", repos = NULL,
lib.loc = "/path/to/your/R/library",
dependencies = TRUE)
-landlib.locare optional and only needed when installing into a custom user library.
Download:
Install from R:
install.packages("Regrans_0.0.1.zip", repos = NULL,
dependencies = TRUE)library(Regrans)
data(simdata)
# Fit segmented regression (minimum points in each segment = 5)
fit <- regrans(simdata$x, simdata$y, n.min = 5)
# Inspect left and right linear models
summary(fit)
# Plot points and fitted segment lines
plot_regrans(fit,
col.lines = c("red", "blue"),
lty.lines = c("solid", "solid"),
lwd.lines = c(2, 2),
pch = 16,
xlab = "X",
ylab = "Y",
main = "Segmented linear regression")
# Optional: compare segment parameters via ANCOVA
ancova_fit <- regrans_ancova(fit)
summary(ancova_fit)Regrans follows a simple and robust strategy:
- Sort data by
x(andyfor ties). - Generate candidate split positions respecting
n.minpoints per side. - For each split:
- fit
lm(Y ~ X)on the left subset, - fit
lm(Y ~ X)on the right subset.
- fit
- Compute residual sum of squares for both models.
- Select the split with the smallest total residual sum of squares.
The returned object stores the two selected linear models (left and right), enabling plotting, summaries, and downstream inference.
| Function | Purpose |
|---|---|
regrans(x, y, n.min = 5) |
Fits segmented linear regression and returns the best two-model split. |
regranslm(x, y, n.min = 5) |
Evaluates all candidate splits and returns complete fitting details. |
summary.regrans(object, ...) |
Summarizes left/right fitted linear models. |
plot_regrans(obj, ...) |
Plots observed data and both fitted segment lines. |
regrans_ancova(obj) |
Fits an ANCOVA model (Y ~ X * model) for segment comparison. |
The package provides simdata, a simulated dataset with:
- 50 observations
x: explanatory variabley: response variable
Use it to quickly test package workflows and examples.
- PDF reference manual: Regrans-manual.pdf
- Rodrigo Sant'Ana
- Fernando Mayer
- Paulo Ricardo Pezzuto
Released under the GNU General Public License v3.0 (GPL-3).