Skip to content

UNIVALI-LEMA/Regrans

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

41 Commits
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 

Repository files navigation

Regrans

Segmented Linear Regression Models in R
Detect transition points by fitting two linear segments and selecting the best split.

Build Status R >= 3.2.2 License: GPL-3


Table of contents


What is Regrans?

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.


Main features

  • ✅ 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

Installation

Install from GitHub

# install.packages("devtools")
library(devtools)
install_github("rodrigosantana/Regrans")

Install from package files

Linux / macOS (.tar.gz)

Download:

Install from terminal:

R CMD INSTALL -l /path/to/your/R/library Regrans_0.0.1.tar.gz

Or from R:

install.packages("Regrans_0.0.1.tar.gz", repos = NULL,
                 lib.loc = "/path/to/your/R/library",
                 dependencies = TRUE)

-l and lib.loc are optional and only needed when installing into a custom user library.

Windows (.zip)

Download:

Install from R:

install.packages("Regrans_0.0.1.zip", repos = NULL,
                 dependencies = TRUE)

Quick start

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)

How it works

Regrans follows a simple and robust strategy:

  1. Sort data by x (and y for ties).
  2. Generate candidate split positions respecting n.min points per side.
  3. For each split:
    • fit lm(Y ~ X) on the left subset,
    • fit lm(Y ~ X) on the right subset.
  4. Compute residual sum of squares for both models.
  5. 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.


Functions overview

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.

Data included

The package provides simdata, a simulated dataset with:

  • 50 observations
  • x: explanatory variable
  • y: response variable

Use it to quickly test package workflows and examples.


Documentation


Authors


License

Released under the GNU General Public License v3.0 (GPL-3).

About

No description, website, or topics provided.

Resources

Stars

Watchers

Forks

Releases

No releases published

Packages

 
 
 

Contributors

Languages