| Title: | Robust Model Fitting Using the RANSAC Algorithm |
|---|---|
| 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>. |
| Authors: | Jadson Abreu [aut, cre] |
| Maintainer: | Jadson Abreu <[email protected]> |
| License: | MIT + file LICENSE |
| Version: | 0.1.0 |
| Built: | 2026-05-10 05:31:00 UTC |
| Source: | https://github.com/cran/RANSAC |
Calculates the root mean square error (RMSE), mean absolute error (MAE), and coefficient of determination (R²) for a model fitted using the RANSAC algorithm.
metrics_ransac(model, data)metrics_ransac(model, data)
model |
A model fitted via RANSAC ('ransac_model' or 'ransac_nls'). |
data |
Data frame containing the model variables. |
A data frame with RMSE, MAE, and R² calculated on the inliers.
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)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)
Fits a robust nonlinear model ('nls') using the RANSAC algorithm.
ransac_nls( formula, data, start, n_min, n_iter = 100, tol = 0.2, verbose = FALSE )ransac_nls( formula, data, start, n_min, n_iter = 100, tol = 0.2, verbose = FALSE )
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. |
An 'nls' model fitted only to the inliers, with an additional class '"ransac_nls"' and an '"inliers"' attribute.
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)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)
Fits a robust linear model ('lm') using the RANSAC algorithm.
ransac_reg(formula, data, n_min, n_iter = 100, tol = 0.2, verbose = FALSE)ransac_reg(formula, data, n_min, n_iter = 100, tol = 0.2, verbose = FALSE)
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. |
An 'lm' model fitted only to the inliers, with an additional class '"ransac_model"' and an '"inliers"' attribute.
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)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)