Title: Robust Model Fitting Using the RANSAC Algorithm
Version: 0.1.0
Description: Provides tools for robust regression model fitting using the RANSAC (Random Sample Consensus) algorithm. RANSAC is an iterative method to estimate parameters of a model from a dataset that contains outliers. This package allows fitting both linear lm and nonlinear nls models using RANSAC, helping users obtain more reliable models in the presence of noisy or corrupted data. The methods are particularly useful in contexts where traditional least squares regression fails due to the influence of outliers. Implementations include support for performance metrics such as RMSE, MAE, and R² based on the inlier subset. For further details, see Fischler and Bolles (1981) <doi:10.1145/358669.358692>.
License: MIT + file LICENSE
Encoding: UTF-8
RoxygenNote: 7.3.2
Depends: R (≥ 4.0.0)
Imports: stats
Suggests: knitr, rmarkdown
VignetteBuilder: knitr
NeedsCompilation: no
Packaged: 2025-05-06 10:45:59 UTC; Jadson
Author: Jadson Abreu [aut, cre]
Maintainer: Jadson Abreu <jadson.ap@gmail.com>
Repository: CRAN
Date/Publication: 2025-05-07 10:30:06 UTC

Performance Metrics for RANSAC Models

Description

Calculates the root mean square error (RMSE), mean absolute error (MAE), and coefficient of determination (R²) for a model fitted using the RANSAC algorithm.

Usage

metrics_ransac(model, data)

Arguments

model

A model fitted via RANSAC ('ransac_model' or 'ransac_nls').

data

Data frame containing the model variables.

Value

A data frame with RMSE, MAE, and R² calculated on the inliers.

Examples

set.seed(123)
D <- seq(10, 50, by = 5)
H <- seq(15, 30, length.out = length(D))
V <- 0.01 * D^2 * H + rnorm(length(D), sd = 5)
V[c(3, 7)] <- V[c(3, 7)] + 50  # add outliers
data <- data.frame(D = D, H = H, V = V)

model <- ransac_nls(V ~ a * D^b * H^c, data = data,
                    start = list(a = 0.01, b = 2, c = 1),
                    n_min = 4, n_iter = 100, tol = 10)

metrics_ransac(model, data)


Robust Nonlinear Model Fitting via RANSAC

Description

Fits a robust nonlinear model ('nls') using the RANSAC algorithm.

Usage

ransac_nls(
  formula,
  data,
  start,
  n_min,
  n_iter = 100,
  tol = 0.2,
  verbose = FALSE
)

Arguments

formula

Model formula.

data

Data frame containing the model variables.

start

Named list of initial parameter estimates (as required by 'nls').

n_min

Minimum number of points to fit the model.

n_iter

Number of iterations (higher values make the model more robust).

tol

Absolute tolerance to consider a point as an inlier.

verbose

If 'TRUE', shows progress messages.

Value

An 'nls' model fitted only to the inliers, with an additional class '"ransac_nls"' and an '"inliers"' attribute.

Examples

set.seed(123)
D <- seq(10, 50, by = 5)
H <- seq(15, 30, length.out = length(D))
V <- 0.01 * D^2 * H + rnorm(length(D), sd = 5)
V[c(3, 7)] <- V[c(3, 7)] + 50  # add outliers
data <- data.frame(D = D, H = H, V = V)

model <- ransac_nls(V ~ a * D^b * H^c, data = data,
                    start = list(a = 0.01, b = 2, c = 1),
                    n_min = 4, n_iter = 100, tol = 10)
summary(model)


Robust Linear Model Fitting via RANSAC

Description

Fits a robust linear model ('lm') using the RANSAC algorithm.

Usage

ransac_reg(formula, data, n_min, n_iter = 100, tol = 0.2, verbose = FALSE)

Arguments

formula

Model formula (as in 'lm').

data

Data frame containing the model variables.

n_min

Minimum number of points to fit the model (e.g., 2 for a straight line).

n_iter

Number of iterations (higher values make the model more robust).

tol

Absolute tolerance to consider a point as an inlier.

verbose

If 'TRUE', shows progress messages.

Value

An 'lm' model fitted only to the inliers, with an additional class '"ransac_model"' and an '"inliers"' attribute.

Examples

set.seed(123)
x <- 1:10
y <- c(1, 2, 3, 4, 5, 6, 7, 8, 50, 60) # some outliers
data <- data.frame(x = x, y = y)
model <- ransac_reg(y ~ x, data = data, n_min = 2, tol = 5)
summary(model)