Learn R Programming

{flexFitR}

{flexFitR} is an R package designed for efficient modeling and analysis of large and complex datasets. It offers powerful tools for parameter estimation, model fitting, and visualization, leveraging the {optimx} package for optimization and the {future} package for parallel processing.

Installation

Install released version from CRAN:

install.packages("flexFitR")

You can also install the development version of flexFitR from GitHub with:

# install.packages("devtools")
devtools::install_github("AparicioJohan/flexFitR")

Features

  • Parameter Estimation: Utilizes {optimx} algorithms to solve and estimate parameters for a given function.
  • Parallelization: Implements parallel processing using the {future} package, enabling efficient fitting of hundreds of curves simultaneously.
  • Visualization Tools: Provides a variety of plots to visualize model fits, correlations, predictions, derivatives, and more.
  • Statistical Rigor: Offers standard errors and p-values for coefficients, as well as for predictions, supporting robust conclusions and interpretations.
  • Prediction: Supports diverse prediction types, including point predictions, area under the curve (AUC), first and second derivatives, and custom expressions based on model parameters.
  • Flexibility: Allows users to fix certain parameters in the model and specify different initial values per grouping factor, accepting both numerical inputs and expressions.
  • Custom Modeling Functions: Equipped with built-in modeling functions for common analysis tasks, while also permitting users to supply their own custom functions.

Example

Here’s a simple example to get you started with {flexFitR}. This example demonstrates fitting a piecewise regression model:

library(flexFitR)

dt <- data.frame(
  time = c(0, 29, 36, 42, 56, 76, 92, 100, 108),
  variable = c(0, 0, 0.67, 15.11, 77.38, 99.81, 99.81, 99.81, 99.81)
)
plot(explorer(dt, time, variable), type = "xy")
fn_lin_plat <- function(t, t1 = 45, t2 = 80, k = 0.9) {
  ifelse(
    test = t < t1,
    yes = 0,
    no = ifelse(t >= t1 & t <= t2, k / (t2 - t1) * (t - t1), k)
  )
}
# Fitting a linear plateau function
mod_1 <- dt |>
  modeler(
    x = time,
    y = variable,
    fn = "fn_lin_plat",
    parameters = c(t1 = 45, t2 = 80, k = 90)
  )
print(mod_1)

Call:
variable ~ fn_lin_plat(time, t1, t2, k) 

Residuals (`Standardized`):
   Min. 1st Qu.  Median    Mean 3rd Qu.    Max. 
 0.0000  0.0000  0.0000  0.2722  0.0000  2.4495 

Optimization Results `head()`:
 uid   t1 t2    k   sse
   1 38.6 61 99.8 0.449

Metrics:
 Groups      Timing Convergence Iterations
      1 0.1081 secs        100%   511 (id)
# Auto plot
plot(mod_1)
# Coefficients
coef(mod_1)
# A tibble: 3 × 7
    uid fn_name     coefficient solution std.error `t value` `Pr(>|t|)`
  <dbl> <chr>       <chr>          <dbl>     <dbl>     <dbl>      <dbl>
1     1 fn_lin_plat t1              38.6    0.0779      496.   4.54e-15
2     1 fn_lin_plat t2              61.0    0.0918      665.   7.82e-16
3     1 fn_lin_plat k               99.8    0.137       730.   4.47e-16
# Variance-Covariance Matrix
vcov(mod_1)
$`1`
              t1           t2            k
t1  6.061705e-03 -0.002940001 1.877072e-07
t2 -2.940001e-03  0.008431400 4.204939e-03
k   1.877072e-07  0.004204939 1.870426e-02
attr(,"fn_name")
[1] "fn_lin_plat"
# Making predictions
predict(mod_1, x = 45)
# A tibble: 1 × 5
    uid fn_name     x_new predicted.value std.error
  <dbl> <chr>       <dbl>           <dbl>     <dbl>
1     1 fn_lin_plat    45            28.5     0.223

Documentation

For detailed documentation and examples, visit flexFitR

Contributing

Contributions to flexFitR are welcome! If you’d like to contribute, please fork the repository and submit a pull request. For significant changes, please open an issue first to discuss your ideas.

Code of Conduct

Please note that the flexFitR project is released with a Contributor Code of Conduct. By contributing to this project, you agree to abide by its terms.

License

flexFitR is licensed under the MIT License. See the LICENSE file for more details.

Copy Link

Version

Install

install.packages('flexFitR')

Monthly Downloads

293

Version

1.2.2

License

MIT + file LICENSE

Issues

Pull Requests

Stars

Forks

Maintainer

Johan Aparicio

Last Published

January 27th, 2026

Functions in flexFitR (1.2.2)

explorer

Explore data
fn_quad

Quadratic function
fn_lin

Linear function
inverse_predict.modeler

Inverse prediction from a modeler object
fn_lin_logis

Linear-logistic function
inverse_predict

Generic for inverse prediction
fn_lin_pl_lin

Linear plateau linear function
fn_lin_plat

Linear plateau function
logLik.modeler

Extract Log-Likelihood for an object of class modeler
metrics

Metrics for an object of class modeler
ff

Function for point estimation
list_methods

Print available methods in flexFitR
print.modeler

Print an object of class modeler
predict.modeler

Predict an object of class modeler
fn_exp_exp

Double-exponential function
list_funs

Print available functions in flexFitR
fn_exp_lin

Exponential-linear function
plot.explorer

Plot an object of class explorer
plot.modeler

Plot an object of class modeler
fn_exp2_lin

Super-exponential linear function
subset.modeler

Subset an object of class modeler
update.modeler

Update a modeler object
series_mutate

Transform variables in a data frame
residuals.modeler

Extract residuals from a modeler object
fn_quad_pl_sm

Smooth Quadratic-plateau function
fn_quad_plat

Quadratic-plateau function
performance

Compare performance of different models
plot.performance

Plot an object of class performance
plot_fn

Plot user-defined function
fn_lpl

Linear plateau linear with constrains
goodness_of_fit

Akaike's An Information Criterion for an object of class modeler
modeler

Modeler: Non-linear regression for curve fitting
fn_qpl

Quadratic–plateau–linear function
vcov.modeler

Variance-Covariance matrix for an object of class modeler
.delta_method

Delta method point estimation
c.modeler

Combine objects of class modeler
augment

Augment a modeler object with influence diagnostics
coef.modeler

Coefficients for an object of class modeler
anova.modeler

Extra Sum-of-Squares F-Test for modeler objects
confint.modeler

Confidence intervals for a modeler object
compute_tangent

Compute tangent line(s) from a modeler object
fitted.modeler

Extract fitted values from a modeler object
fn_exp2_exp

Super-exponential exponential function
ff_auc

Function for AUC estimation
flexFitR-package

flexFitR: Flexible Non-Linear Least Square Model Fitting
.delta_method_gen

Delta method generic function
.delta_method_auc

Delta method AUC estimation
dt_potato

Drone-derived data from a potato breeding trial
.delta_method_deriv

Delta method for derivative estimation
ff_deriv

Function for derivatives
.fitter_curve

General-purpose optimization
fn_lll

Linear–logistic–linear function
fn_logistic

Logistic function