| Title: | Flexible Non-Linear Least Square Model Fitting |
|---|---|
| Description: | Provides tools for flexible non-linear least squares model fitting using general-purpose optimization techniques. The package supports a variety of optimization algorithms, including those provided by the 'optimx' package, making it suitable for handling complex non-linear models. Features include parallel processing support via the 'future' and 'foreach' packages, comprehensive model diagnostics, and visualization capabilities. Implements methods described in Nash and Varadhan (2011, <doi:10.18637/jss.v043.i09>). |
| Authors: | Johan Aparicio [cre, aut], Jeffrey Endelman [aut], University of Wisconsin Madison [cph] |
| Maintainer: | Johan Aparicio <[email protected]> |
| License: | MIT + file LICENSE |
| Version: | 1.2.2 |
| Built: | 2026-05-24 06:43:46 UTC |
| Source: | https://github.com/apariciojohan/flexfitr |
modeler objectsPerform an extra sum-of-squares F-test to compare two nested
models of class modeler. This test assesses whether the additional
parameters in the full model significantly improve the fit compared to the
reduced model.
## S3 method for class 'modeler' anova(object, full_model = NULL, ...)## S3 method for class 'modeler' anova(object, full_model = NULL, ...)
object |
An object of class |
full_model |
An optional object of class |
... |
Additional parameters for future functionality. |
A tibble containing columns with the F-statistic and
corresponding p-values, indicating whether the full model provides a
significantly better fit than the reduced model.
Johan Aparicio [aut]
library(flexFitR) dt <- data.frame(X = 1:6, Y = c(12, 16, 44, 50, 95, 100)) mo_1 <- modeler(dt, X, Y, fn = "fn_lin", param = c(m = 10, b = -5)) plot(mo_1) mo_2 <- modeler(dt, X, Y, fn = "fn_quad", param = c(a = 1, b = 10, c = 5)) plot(mo_2) anova(mo_1, mo_2)library(flexFitR) dt <- data.frame(X = 1:6, Y = c(12, 16, 44, 50, 95, 100)) mo_1 <- modeler(dt, X, Y, fn = "fn_lin", param = c(m = 10, b = -5)) plot(mo_1) mo_2 <- modeler(dt, X, Y, fn = "fn_quad", param = c(a = 1, b = 10, c = 5)) plot(mo_2) anova(mo_1, mo_2)
modeler object with influence diagnosticsThis function computes various influence diagnostics, including
standardized residuals, studentized residuals, and Cook's distance, for an
object of class modeler.
augment(x, id = NULL, metadata = TRUE, ...)augment(x, id = NULL, metadata = TRUE, ...)
x |
An object of class |
id |
Optional unique identifier to filter by a specific group. Default is |
metadata |
Logical. If |
... |
Additional parameters for future functionality. |
A tibble containing the following columns:
uid |
Unique identifier for the group. |
fn_name |
Function name associated with the model. |
x |
Predictor variable values. |
y |
Observed response values. |
.fitted |
Fitted values from the model. |
.resid |
Raw residuals (observed - fitted). |
.hat |
Leverage values for each observation. |
.cooksd |
Cook's distance for each observation. |
.std.resid |
Standardized residuals. |
.stud.resid |
Studentized residuals. |
Johan Aparicio [aut]
library(flexFitR) data(dt_potato) mod_1 <- dt_potato |> modeler( x = DAP, y = Canopy, grp = Plot, fn = "fn_logistic", parameters = c(a = 0.199, t0 = 47.7, k = 100), subset = 2 ) print(mod_1) augment(mod_1)library(flexFitR) data(dt_potato) mod_1 <- dt_potato |> modeler( x = DAP, y = Canopy, grp = Plot, fn = "fn_logistic", parameters = c(a = 0.199, t0 = 47.7, k = 100), subset = 2 ) print(mod_1) augment(mod_1)
modeler
Combine objects of class modeler. Use with caution, some
functions might not work as expected.
## S3 method for class 'modeler' c(...)## S3 method for class 'modeler' c(...)
... |
Objects of class |
A modeler object.
Johan Aparicio [aut]
library(flexFitR) data(dt_potato) mod_1 <- dt_potato |> modeler( x = DAP, y = Canopy, grp = Plot, fn = "fn_logistic", parameters = c(a = 0.199, t0 = 47.7, k = 100), subset = 1:2 ) mod_2 <- dt_potato |> modeler( x = DAP, y = Canopy, grp = Plot, fn = "fn_lin_plat", parameters = c(t1 = 45, t2 = 80, k = 100), subset = 1:2 ) mod <- c(mod_1, mod_2) print(mod) plot(mod, id = 1:2)library(flexFitR) data(dt_potato) mod_1 <- dt_potato |> modeler( x = DAP, y = Canopy, grp = Plot, fn = "fn_logistic", parameters = c(a = 0.199, t0 = 47.7, k = 100), subset = 1:2 ) mod_2 <- dt_potato |> modeler( x = DAP, y = Canopy, grp = Plot, fn = "fn_lin_plat", parameters = c(t1 = 45, t2 = 80, k = 100), subset = 1:2 ) mod <- c(mod_1, mod_2) print(mod) plot(mod, id = 1:2)
modeler
Extract the estimated coefficients from an object of class modeler.
## S3 method for class 'modeler' coef(object, id = NULL, metadata = FALSE, df = FALSE, ...)## S3 method for class 'modeler' coef(object, id = NULL, metadata = FALSE, df = FALSE, ...)
object |
An object of class |
id |
An optional unique identifier to filter by a specific group. Default is |
metadata |
Logical. If |
df |
Logical. If |
... |
Additional parameters for future functionality. |
A data.frame containing the model's estimated coefficients,
standard errors, and optional metadata or degrees of freedom if specified.
Johan Aparicio [aut]
library(flexFitR) data(dt_potato) mod_1 <- dt_potato |> modeler( x = DAP, y = Canopy, grp = Plot, fn = "fn_lin_plat", parameters = c(t1 = 45, t2 = 80, k = 0.9), subset = c(15, 2, 45) ) print(mod_1) coef(mod_1, id = 2)library(flexFitR) data(dt_potato) mod_1 <- dt_potato |> modeler( x = DAP, y = Canopy, grp = Plot, fn = "fn_lin_plat", parameters = c(t1 = 45, t2 = 80, k = 0.9), subset = c(15, 2, 45) ) print(mod_1) coef(mod_1, id = 2)
modeler objectComputes the slope and intercept of the tangent line(s) to a fitted curve at one or more specified x-values.
compute_tangent(object, x = NULL, id = NULL)compute_tangent(object, x = NULL, id = NULL)
object |
A fitted object of class |
x |
A numeric vector of x-values at which to compute tangent lines. A data.frame is also accepted with columns <uid, x>. |
id |
Optional vector of |
A tibble with one row per tangent line and the following columns:
uid: unique identifier of the group.
fn_name: Name of the fitted function.
x: x-value where the tangent line is evaluated.
y: Fitted y-value at x.
slope: First derivative (slope of tangent) at x.
intercept: y-intercept of the tangent line.
library(flexFitR) library(ggplot2) data(dt_potato) mod <- dt_potato |> modeler( x = DAP, y = Canopy, grp = Plot, fn = "fn_logistic", parameters = c(a = 4, t0 = 40, k = 100), subset = 2 ) plot(mod) tl <- compute_tangent(mod, x = c(48.35, 65)) print(tl) plot(mod) + geom_abline( data = tl, mapping = aes(slope = slope, intercept = intercept), linetype = 2, color = "blue" ) + geom_point( data = tl, mapping = aes(x = x, y = y), shape = 8, color = "blue", size = 2 )library(flexFitR) library(ggplot2) data(dt_potato) mod <- dt_potato |> modeler( x = DAP, y = Canopy, grp = Plot, fn = "fn_logistic", parameters = c(a = 4, t0 = 40, k = 100), subset = 2 ) plot(mod) tl <- compute_tangent(mod, x = c(48.35, 65)) print(tl) plot(mod) + geom_abline( data = tl, mapping = aes(slope = slope, intercept = intercept), linetype = 2, color = "blue" ) + geom_point( data = tl, mapping = aes(x = x, y = y), shape = 8, color = "blue", size = 2 )
Extract confidence intervals for the estimated parameters of an
object of class modeler.
## S3 method for class 'modeler' confint(object, parm = NULL, level = 0.95, id = NULL, ...)## S3 method for class 'modeler' confint(object, parm = NULL, level = 0.95, id = NULL, ...)
object |
An object of class |
parm |
A character vector specifying which parameters should have
confidence intervals calculated. If |
level |
A numeric value indicating the confidence level for the intervals. Default is 0.95, corresponding to a 95% confidence interval. |
id |
An optional unique identifier to filter by a specific group.
Default is |
... |
Additional parameters for future functionality. |
A tibble containing the lower and upper confidence limits for
each specified parameter.
Johan Aparicio [aut]
library(flexFitR) data(dt_potato) mod_1 <- dt_potato |> modeler( x = DAP, y = Canopy, grp = Plot, fn = "fn_lin_plat", parameters = c(t1 = 45, t2 = 80, k = 0.9), subset = c(15, 35, 45) ) print(mod_1) confint(mod_1)library(flexFitR) data(dt_potato) mod_1 <- dt_potato |> modeler( x = DAP, y = Canopy, grp = Plot, fn = "fn_lin_plat", parameters = c(t1 = 45, t2 = 80, k = 0.9), subset = c(15, 35, 45) ) print(mod_1) confint(mod_1)
Canopy and Green Leaf Index for a potato trial arranged in a p-rep design.
dt_potatodt_potato
A tibble with 1372 rows and 8 variables:
chr trial name
dbl denoting the unique plot id
dbl denoting the row coordinate
dbl denoting range coordinate
chr denoting the genotype id
dbl denoting Days after planting
dbl Canopy UAV-Derived
dbl Green Leaf Index UAV-Derived
UW - Potato Breeding Program
Explores data from a data frame in wide format.
explorer(data, x, y, id, metadata)explorer(data, x, y, id, metadata)
data |
A |
x |
The name of the column in |
y |
The names of the columns in |
id |
The names of the columns in |
metadata |
The names of the columns in |
This function helps to explore the dataset before being analyzed with modeler().
An object of class explorer, which is a list containing the following elements:
summ_varsA data.frame containing summary statistics for each trait at each x point, including minimum, mean, median, maximum, standard deviation, coefficient of variation, number of non-missing values, percentage of missing values, and percentage of negative values.
summ_metadataA data.frame summarizing the metadata.
locals_min_maxA data.frame containing the local minima and maxima of the mean y values over x.
dt_longA data.frame in long format, with columns for uid, metadata, var, x, and y
metadataA character vector with the names of the variables to keep across.
library(flexFitR) data(dt_potato) results <- dt_potato |> explorer( x = DAP, y = c(Canopy, GLI), id = Plot, metadata = c(gid, Row, Range) ) names(results) head(results$summ_vars) plot(results, label_size = 4, signif = TRUE, n_row = 2) # New data format head(results$dt_long)library(flexFitR) data(dt_potato) results <- dt_potato |> explorer( x = DAP, y = c(Canopy, GLI), id = Plot, metadata = c(gid, Row, Range) ) names(results) head(results$summ_vars) plot(results, label_size = 4, signif = TRUE, n_row = 2) # New data format head(results$dt_long)
modeler objectExtract fitted values from a modeler object
## S3 method for class 'modeler' fitted(object, ...)## S3 method for class 'modeler' fitted(object, ...)
object |
An object of class 'modeler' |
... |
Additional parameters for future functionality. |
A numeric vector of fitted values.
Johan Aparicio [aut]
library(flexFitR) data(dt_potato) mod_1 <- dt_potato |> modeler( x = DAP, y = Canopy, grp = Plot, fn = "fn_lin_plat", parameters = c(t1 = 45, t2 = 80, k = 0.9), subset = c(15, 2, 45) ) fitted(mod_1)library(flexFitR) data(dt_potato) mod_1 <- dt_potato |> modeler( x = DAP, y = Canopy, grp = Plot, fn = "fn_lin_plat", parameters = c(t1 = 45, t2 = 80, k = 0.9), subset = c(15, 2, 45) ) fitted(mod_1)
A piecewise function with two exponential phases. The first exponential phase
occurs between t1 and t2, and the second phase continues after
t2 with a potentially different growth rate. The function ensures
continuity at the transition point but not necessarily smoothness (in derivative).
fn_exp_exp(t, t1, t2, alpha, beta)fn_exp_exp(t, t1, t2, alpha, beta)
t |
A numeric vector of input values (e.g., time). |
t1 |
The onset time of the response. The function is 0 for all values less than |
t2 |
The transition time between the two exponential phases. Must be greater than |
alpha |
The exponential growth rate during the first phase ( |
beta |
The exponential growth rate after |
The function rises from 0 starting at t1 with exponential growth rate
alpha, and transitions to a second exponential phase with rate
beta at t2. The value at the transition point is preserved,
ensuring continuity.
A numeric vector of the same length as t, representing the function values.
library(flexFitR) plot_fn( fn = "fn_exp_exp", params = c(t1 = 35, t2 = 55, alpha = 1 / 20, beta = -1 / 30), interval = c(0, 108), n_points = 2000, auc_label_size = 3, y_auc_label = 0.2 )library(flexFitR) plot_fn( fn = "fn_exp_exp", params = c(t1 = 35, t2 = 55, alpha = 1 / 20, beta = -1 / 30), interval = c(0, 108), n_points = 2000, auc_label_size = 3, y_auc_label = 0.2 )
A piecewise function that models a response with an initial exponential growth phase followed by a linear phase. Commonly used to describe processes with rapid early increases that slow into a linear trend, while maintaining continuity.
fn_exp_lin(t, t1, t2, alpha, beta)fn_exp_lin(t, t1, t2, alpha, beta)
t |
A numeric vector of input values (e.g., time). |
t1 |
The onset time of the response. The function is 0 for all values less than |
t2 |
The transition time between exponential and linear phases. Must be greater than |
alpha |
The exponential growth rate during the exponential phase. |
beta |
The slope of the linear phase after |
The exponential segment starts from 0 at t1, and the linear segment
continues smoothly from the end of the exponential part. This ensures value
continuity at t2, but not necessarily smoothness in slope.
A numeric vector of the same length as t, representing the function values.
library(flexFitR) plot_fn( fn = "fn_exp_lin", params = c(t1 = 35, t2 = 55, alpha = 1 / 20, beta = -1 / 40), interval = c(0, 108), n_points = 2000, auc_label_size = 3 )library(flexFitR) plot_fn( fn = "fn_exp_lin", params = c(t1 = 35, t2 = 55, alpha = 1 / 20, beta = -1 / 40), interval = c(0, 108), n_points = 2000, auc_label_size = 3 )
A piecewise function that models an initial exponential phase with quadratic time dependence, followed by a second exponential phase with a different growth rate.
fn_exp2_exp(t, t1, t2, alpha, beta)fn_exp2_exp(t, t1, t2, alpha, beta)
t |
A numeric vector of input values (e.g., time). |
t1 |
The onset time of the response. The function is 0 for all values less than |
t2 |
The transition time between the two exponential phases. Must be greater than |
alpha |
The curvature-controlled exponential rate during the first phase ( |
beta |
The exponential growth rate after |
A numeric vector of the same length as t, representing the function values.
library(flexFitR) plot_fn( fn = "fn_exp2_exp", params = c(t1 = 35, t2 = 55, alpha = 1 / 600, beta = -1 / 30), interval = c(0, 108), n_points = 2000, auc_label_size = 3, y_auc_label = 0.15 )library(flexFitR) plot_fn( fn = "fn_exp2_exp", params = c(t1 = 35, t2 = 55, alpha = 1 / 600, beta = -1 / 30), interval = c(0, 108), n_points = 2000, auc_label_size = 3, y_auc_label = 0.15 )
A piecewise function that models an initial exponential growth phase based on a squared time difference, followed by a linear phase.
fn_exp2_lin(t, t1, t2, alpha, beta)fn_exp2_lin(t, t1, t2, alpha, beta)
t |
A numeric vector of input values (e.g., time). |
t1 |
The onset time of the response. The function is 0 for all values less than |
t2 |
The transition time between exponential and linear phases. Must be greater than |
alpha |
The exponential growth rate controlling the curvature of the exponential phase. |
beta |
The slope of the linear phase after |
The exponential section rises gradually from 0 at t1 and accelerates
as time increases. The linear section starts at t2 with a value
matching the end of the exponential phase, ensuring continuity but not
necessarily matching the derivative.
A numeric vector of the same length as t, representing the function values.
library(flexFitR) plot_fn( fn = "fn_exp2_lin", params = c(t1 = 35, t2 = 55, alpha = 1 / 600, beta = -1 / 80), interval = c(0, 108), n_points = 2000, auc_label_size = 3 )library(flexFitR) plot_fn( fn = "fn_exp2_lin", params = c(t1 = 35, t2 = 55, alpha = 1 / 600, beta = -1 / 80), interval = c(0, 108), n_points = 2000, auc_label_size = 3 )
A basic linear function of the form f(t) = m * t + b, where m
is the slope and b is the intercept.
fn_lin(t, m, b)fn_lin(t, m, b)
t |
A numeric vector of input values (e.g., time). |
m |
The slope of the line. |
b |
The intercept (function value when |
A numeric vector of the same length as t, giving the linear function values.
library(flexFitR) plot_fn( fn = "fn_lin", params = c(m = 2, b = 10), interval = c(0, 108), n_points = 2000 )library(flexFitR) plot_fn( fn = "fn_lin", params = c(m = 2, b = 10), interval = c(0, 108), n_points = 2000 )
A piecewise function that models an initial linear increase followed by a logistic saturation.
fn_lin_logis(t, t1, t2, k)fn_lin_logis(t, t1, t2, k)
t |
A numeric vector of input values (e.g., time). |
t1 |
The onset time of the response. The function is 0 for all values less than |
t2 |
The transition time between the linear and logistic phases. Must be greater than |
k |
The plateau height. The function transitions toward this value in the logistic phase. |
The linear segment rises from 0 starting at t1, and the logistic segment begins at t2,
smoothly approaching the plateau value k.
A numeric vector of the same length as t, representing the function values.
library(flexFitR) plot_fn( fn = "fn_lin_logis", params = c(t1 = 35, t2 = 50, k = 100), interval = c(0, 108), n_points = 2000, auc_label_size = 3 )library(flexFitR) plot_fn( fn = "fn_lin_logis", params = c(t1 = 35, t2 = 50, k = 100), interval = c(0, 108), n_points = 2000, auc_label_size = 3 )
A piecewise function that models an initial linear increase up to a plateau, maintains that plateau for a duration, and then decreases linearly.
fn_lin_pl_lin(t, t1, t2, t3, k, beta)fn_lin_pl_lin(t, t1, t2, t3, k, beta)
t |
A numeric vector of input values (e.g., time). |
t1 |
The onset time of the response. The function is 0 for all values less than |
t2 |
The time when the linear growth phase ends and the plateau begins. Must be greater than |
t3 |
The time when the plateau ends and the linear decline begins. Must be greater than |
k |
The height of the plateau. The first linear phase increases to this value, which remains constant until |
beta |
The slope of the final linear phase (typically negative), controlling the rate of decline after |
The function transitions continuously between all three phases but is not
differentiable at the transition points t1, t2, and t3.
A numeric vector of the same length as t, representing the function values.
library(flexFitR) plot_fn( fn = "fn_lin_pl_lin", params = c(t1 = 38.7, t2 = 62, t3 = 90, k = 0.32, beta = -0.01), interval = c(0, 108), n_points = 2000, auc_label_size = 3 )library(flexFitR) plot_fn( fn = "fn_lin_pl_lin", params = c(t1 = 38.7, t2 = 62, t3 = 90, k = 0.32, beta = -0.01), interval = c(0, 108), n_points = 2000, auc_label_size = 3 )
A simple piecewise function that models a linear increase from zero to a plateau. The function rises linearly between two time points and then levels off at a constant value.
fn_lin_plat(t, t1 = 45, t2 = 80, k = 0.9)fn_lin_plat(t, t1 = 45, t2 = 80, k = 0.9)
t |
A numeric vector of input values (e.g., time). |
t1 |
The onset time of the response. The function is 0 for all values less than |
t2 |
The time at which the plateau begins. Must be greater than |
k |
The height of the plateau. The function linearly increases from
0 to |
This function is continuous but not differentiable at t1 and t2
due to the piecewise transitions. It is often used in agronomy and ecology
to describe growth until a resource limit or developmental plateau is reached.
A numeric vector of the same length as t, representing the function values.
library(flexFitR) plot_fn( fn = "fn_lin_plat", params = c(t1 = 34.9, t2 = 61.8, k = 100), interval = c(0, 108), n_points = 2000, auc_label_size = 3 )library(flexFitR) plot_fn( fn = "fn_lin_plat", params = c(t1 = 34.9, t2 = 61.8, k = 100), interval = c(0, 108), n_points = 2000, auc_label_size = 3 )
A piecewise function that models (i) an initial linear increase from zero, (ii) a smooth logistic rise toward an upper asymptote, and (iii) a final linear phase.
fn_lll(t, t1, t2, dt, k, beta)fn_lll(t, t1, t2, dt, k, beta)
t |
A numeric vector of input values (e.g., time). |
t1 |
The onset time of the response. The function is 0 for all values
less than or equal to |
t2 |
The time when the initial linear phase ends and the logistic phase
begins. Must be greater than |
dt |
Duration of the logistic phase. Defines |
k |
Upper asymptote (maximum level) of the logistic component. |
beta |
Slope of the final linear phase after |
where .
The function is continuous at t1, t2, and t3. It is
differentiable at t2 by construction (the linear slope matches the
logistic derivative at t2). It is not differentiable at t1, and
it is generally not differentiable at t3 unless beta matches
the logistic derivative at t3.
A numeric vector of the same length as t, representing the
function values.
library(flexFitR) plot_fn( fn = "fn_lll", params = c(t1 = 25, t2 = 35, dt = 45, k = 100, beta = -1), interval = c(0, 100), n_points = 2000, auc_label_size = 3 )library(flexFitR) plot_fn( fn = "fn_lll", params = c(t1 = 25, t2 = 35, dt = 45, k = 100, beta = -1), interval = c(0, 100), n_points = 2000, auc_label_size = 3 )
A standard logistic function commonly used to model sigmoidal growth. The
curve rises from near zero to a maximum value k, with inflection point
at t0 and growth rate a.
fn_logistic(t, a, t0, k)fn_logistic(t, a, t0, k)
t |
A numeric vector of input values (e.g., time). |
a |
The growth rate (steepness of the curve). Higher values lead to a steeper rise. |
t0 |
The time of the inflection point (midpoint of the transition). |
k |
The upper asymptote or plateau (maximum value as |
This is a classic sigmoid (S-shaped) curve that is symmetric around the
inflection point t0.
A numeric vector of the same length as t, representing the logistic function values.
library(flexFitR) plot_fn( fn = "fn_logistic", params = c(a = 0.199, t0 = 47.7, k = 100), interval = c(0, 108), n_points = 2000 )library(flexFitR) plot_fn( fn = "fn_logistic", params = c(a = 0.199, t0 = 47.7, k = 100), interval = c(0, 108), n_points = 2000 )
A piecewise function that models an initial linear increase to a plateau, followed by a specified duration of stability, and then a linear decline. This version parameterizes the plateau using its duration rather than an explicit end time, making it convenient for box type of constraints optimizations.
fn_lpl(t, t1, t2, dt, k, beta)fn_lpl(t, t1, t2, dt, k, beta)
t |
A numeric vector of input values (e.g., time). |
t1 |
The onset time of the response. The function is 0 for all values less than |
t2 |
The time when the linear growth phase ends and the plateau begins. Must be greater than |
dt |
The duration of the plateau phase. The plateau ends at |
k |
The height of the plateau. The linear phase increases to this value, which remains constant for |
beta |
The slope of the decline phase that begins after the plateau. Typically negative. |
A numeric vector of the same length as t, representing the function values.
library(flexFitR) plot_fn( fn = "fn_lpl", params = c(t1 = 38.7, t2 = 62, dt = 28, k = 0.32, beta = -0.01), interval = c(0, 108), n_points = 2000, auc_label_size = 3 )library(flexFitR) plot_fn( fn = "fn_lpl", params = c(t1 = 38.7, t2 = 62, dt = 28, k = 0.32, beta = -0.01), interval = c(0, 108), n_points = 2000, auc_label_size = 3 )
A piecewise function that models an initial quadratic increase from zero up to a plateau, maintains that plateau for a duration, and then changes linearly after the plateau ends.
fn_qpl(t, t1, t2, dt, b, k, beta)fn_qpl(t, t1, t2, dt, b, k, beta)
t |
A numeric vector of input values (e.g., time). |
t1 |
The onset time of the response. The function is 0 for all values
less than |
t2 |
The time when the quadratic growth phase ends and the plateau
begins. Must be greater than |
dt |
Duration of the plateau. Defines |
b |
Linear coefficient of the quadratic growth phase. |
k |
The plateau value (level maintained between |
beta |
Slope of the final linear phase after |
The quadratic phase is parameterized so that the curve reaches exactly
k at t2. Let . The quadratic coefficient
is computed internally as:
where .
The function is continuous at t1, t2, and t3. It is not
differentiable at t3 unless beta = 0.
A numeric vector of the same length as t, representing the
function values.
library(flexFitR) plot_fn( fn = "fn_qpl", params = c(t1 = 30, t2 = 60, dt = 20, b = 0.01, k = 0.9, beta = -0.01), interval = c(0, 100), n_points = 2000, auc_label_size = 3 )library(flexFitR) plot_fn( fn = "fn_qpl", params = c(t1 = 30, t2 = 60, dt = 20, b = 0.01, k = 0.9, beta = -0.01), interval = c(0, 100), n_points = 2000, auc_label_size = 3 )
A standard quadratic function of the form f(t) = a * t^2 + b * t + c, where
a controls curvature, b is the linear coefficient, and c is the intercept.
fn_quad(t, a, b, c)fn_quad(t, a, b, c)
t |
A numeric vector of input values (e.g., time). |
a |
The quadratic coefficient (curvature). |
b |
The linear coefficient (slope at the origin). |
c |
The intercept (function value when |
This function represents a second-degree polynomial. The sign of a
determines whether the parabola opens upward (a > 0) or downward (a < 0).
A numeric vector of the same length as t, representing the
quadratic function values.
library(flexFitR) plot_fn(fn = "fn_quad", params = c(a = 1, b = 10, c = 5))library(flexFitR) plot_fn(fn = "fn_quad", params = c(a = 1, b = 10, c = 5))
A piecewise function that models a quadratic increase from zero to a plateau value. The function is continuous and differentiable, modeling growth processes with a smooth transition to a maximum response.
fn_quad_pl_sm(t, t1, t2, k)fn_quad_pl_sm(t, t1, t2, k)
t |
A numeric vector of input values (e.g., time). |
t1 |
The onset time of the response. The function is 0 for all values less than |
t2 |
The time at which the plateau begins. Must be greater than |
k |
The plateau height. The function transitions to this constant value at |
The coefficients of the quadratic section are chosen such that the curve passes through
(t1, 0) and (t2, k) with a continuous first derivative (i.e., smooth transition).
A numeric vector of the same length as t, representing the function values.
library(flexFitR) plot_fn( fn = "fn_quad_pl_sm", params = c(t1 = 35, t2 = 80, k = 100), interval = c(0, 108), n_points = 2000, auc_label_size = 3 )library(flexFitR) plot_fn( fn = "fn_quad_pl_sm", params = c(t1 = 35, t2 = 80, k = 100), interval = c(0, 108), n_points = 2000, auc_label_size = 3 )
Computes a value based on a quadratic-plateau growth curve.
fn_quad_plat(t, t1 = 45, t2 = 80, b = 1, k = 100)fn_quad_plat(t, t1 = 45, t2 = 80, b = 1, k = 100)
t |
A numeric vector of input values (e.g., time). |
t1 |
The onset time of the response. The function is 0 for all values less than |
t2 |
The time at which the plateau begins. Must be greater than |
b |
The initial slope of the curve at |
k |
The plateau height. The function transitions to this constant value at |
This function allows the user to specify the initial slope b. The curvature term
is automatically calculated so that the function reaches the plateau value k exactly
at t2. The transition to the plateau is continuous in value but not necessarily smooth
in derivative.
A numeric vector of the same length as t, representing the function values.
library(flexFitR) plot_fn( fn = "fn_quad_plat", params = c(t1 = 35, t2 = 80, b = 4, k = 100), interval = c(0, 108), n_points = 2000, auc_label_size = 3 )library(flexFitR) plot_fn( fn = "fn_quad_plat", params = c(t1 = 35, t2 = 80, b = 4, k = 100), interval = c(0, 108), n_points = 2000, auc_label_size = 3 )
modeler
Generic function calculating Akaike's ‘An Information Criterion’
for fitted model object of class modeler.
## S3 method for class 'modeler' AIC(object, ..., k = 2) ## S3 method for class 'modeler' BIC(object, ...)## S3 method for class 'modeler' AIC(object, ..., k = 2) ## S3 method for class 'modeler' BIC(object, ...)
object |
An object inheriting from class |
... |
Further parameters. For future improvements. |
k |
Numeric, the penalty per parameter to be used; the default k = 2 is the classical AIC. |
A tibble with columns giving the corresponding AIC and BIC.
Johan Aparicio [aut]
library(flexFitR) dt <- data.frame(X = 1:6, Y = c(12, 16, 44, 50, 95, 100)) mo_1 <- modeler(dt, X, Y, fn = "fn_lin", param = c(m = 10, b = -5)) mo_2 <- modeler(dt, X, Y, fn = "fn_quad", param = c(a = 1, b = 10, c = 5)) AIC(mo_1) AIC(mo_2) BIC(mo_1) BIC(mo_2)library(flexFitR) dt <- data.frame(X = 1:6, Y = c(12, 16, 44, 50, 95, 100)) mo_1 <- modeler(dt, X, Y, fn = "fn_lin", param = c(m = 10, b = -5)) mo_2 <- modeler(dt, X, Y, fn = "fn_quad", param = c(a = 1, b = 10, c = 5)) AIC(mo_1) AIC(mo_2) BIC(mo_1) BIC(mo_2)
modeler objectComputes the x-value at which a fitted model reaches a user-specified response value (y-value).
## S3 method for class 'modeler' inverse_predict( object, y, id = NULL, interval = NULL, tol = 1e-06, resolution = 1000, ... )## S3 method for class 'modeler' inverse_predict( object, y, id = NULL, interval = NULL, tol = 1e-06, resolution = 1000, ... )
object |
A fitted object of class |
y |
A numeric scalar giving the target y-value for which to compute the corresponding x. |
id |
Optional vector of |
interval |
Optional numeric vector of length 2 specifying the interval in which to search for the root.
If |
tol |
Numerical tolerance passed to |
resolution |
Integer. Number of grid points used to scan the interval. |
... |
Additional parameters for future functionality. |
The function uses numeric root-finding to solve f(t, ...params) = y.
If no root is found in the interval, NA is returned.
A tibble with one row per group, containing:
uid – unique identifier of the group,
fn_name – the name of the fitted function,
lower and upper – the search interval used,
y – the predicted y-value (from the function at the root),
x – the x-value at which the function reaches y.
library(flexFitR) data(dt_potato) mod_1 <- dt_potato |> modeler( x = DAP, y = Canopy, grp = Plot, fn = "fn_lin_plat", parameters = c(t1 = 45, t2 = 80, k = 0.9), subset = c(15, 2, 45) ) print(mod_1) inverse_predict(mod_1, y = 50) inverse_predict(mod_1, y = 75, interval = c(20, 80))library(flexFitR) data(dt_potato) mod_1 <- dt_potato |> modeler( x = DAP, y = Canopy, grp = Plot, fn = "fn_lin_plat", parameters = c(t1 = 45, t2 = 80, k = 0.9), subset = c(15, 2, 45) ) print(mod_1) inverse_predict(mod_1, y = 50) inverse_predict(mod_1, y = 75, interval = c(20, 80))
Print available functions in flexFitR
list_funs()list_funs()
A vector with available functions
library(flexFitR) list_funs()library(flexFitR) list_funs()
Print available methods in flexFitR
list_methods(bounds = FALSE, check_package = FALSE)list_methods(bounds = FALSE, check_package = FALSE)
bounds |
If TRUE, returns methods for box (or bounds) constraints. FALSE by default. |
check_package |
If TRUE, ensures solvers are installed. FALSE by default. |
A vector with available methods
library(flexFitR) list_methods()library(flexFitR) list_methods()
modeler
logLik for an object of class modeler
## S3 method for class 'modeler' logLik(object, ...)## S3 method for class 'modeler' logLik(object, ...)
object |
An object inheriting from class |
... |
Further parameters. For future improvements. |
A tibble with the Log-Likelihood for the fitted models.
Johan Aparicio [aut]
library(flexFitR) dt <- data.frame(X = 1:6, Y = c(12, 16, 44, 50, 95, 100)) mo_1 <- modeler(dt, X, Y, fn = "fn_lin", param = c(m = 10, b = -5)) plot(mo_1) logLik(mo_1)library(flexFitR) dt <- data.frame(X = 1:6, Y = c(12, 16, 44, 50, 95, 100)) mo_1 <- modeler(dt, X, Y, fn = "fn_lin", param = c(m = 10, b = -5)) plot(mo_1) logLik(mo_1)
modeler
Computes various performance metrics for a modeler object. The function calculates Sum of Squared Errors (SSE), Mean Absolute Error (MAE), Mean Squared Error (MSE), Root Mean Squared Error (RMSE), and the Coefficient of Determination (R-squared).
metrics(x, by_grp = TRUE)metrics(x, by_grp = TRUE)
x |
An object of class 'modeler' containing the necessary data to compute the metrics. |
by_grp |
Return the metrics by id? TRUE by default. |
Sum of Squared Errors (SSE):
Mean Absolute Error (MAE):
Mean Squared Error (MSE):
Root Mean Squared Error (RMSE):
Coefficient of Determination (R-squared):
A data frame containing the calculated metrics grouped by uid, metadata, and variables.
library(flexFitR) data(dt_potato) mod_1 <- dt_potato |> modeler( x = DAP, y = Canopy, grp = Plot, fn = "fn_lin_plat", parameters = c(t1 = 45, t2 = 80, k = 0.9), subset = c(1:2) ) plot(mod_1, id = c(1:2)) print(mod_1) metrics(mod_1)library(flexFitR) data(dt_potato) mod_1 <- dt_potato |> modeler( x = DAP, y = Canopy, grp = Plot, fn = "fn_lin_plat", parameters = c(t1 = 45, t2 = 80, k = 0.9), subset = c(1:2) ) plot(mod_1, id = c(1:2)) print(mod_1) metrics(mod_1)
A versatile function for performing non-linear least squares optimization on grouped data. It supports customizable optimization methods, flexible initial/fixed parameters, and parallel processing.
modeler( data, x, y, grp, keep, fn = "fn_lin_plat", parameters = NULL, lower = -Inf, upper = Inf, fixed_params = NULL, method = c("subplex", "pracmanm", "anms"), subset = NULL, options = modeler.options(), control = list() )modeler( data, x, y, grp, keep, fn = "fn_lin_plat", parameters = NULL, lower = -Inf, upper = Inf, fixed_params = NULL, method = c("subplex", "pracmanm", "anms"), subset = NULL, options = modeler.options(), control = list() )
data |
A |
x |
The name of the column in |
y |
The name of the column in |
grp |
Column(s) in |
keep |
Names of columns to retain in the output. Defaults to |
fn |
A string. The name of the function used for curve fitting.
Example: |
parameters |
A numeric vector, named list, or
Defaults to |
lower |
A numeric vector specifying lower bounds for parameters. Defaults to |
upper |
A numeric vector specifying upper bounds for parameters. Defaults to |
fixed_params |
A list or
Defaults to |
method |
A character vector specifying optimization methods.
Check available methods using |
subset |
A vector (optional) containing levels of |
options |
A list of additional options. See
|
control |
A list of control parameters to be passed to the optimization function. For example: |
An object of class modeler, which is a list containing the following elements:
paramData frame containing optimized parameters and related information.
dtData frame with input data, fitted values, and residuals.
metricsMetrics and summary of the models.
executionTotal execution time for the analysis.
responseName of the response variable analyzed.
keepMetadata retained based on the keep argument.
funName of the curve-fitting function used.
parallelList containing parallel execution details (if applicable).
fitList of fitted models for each group.
library(flexFitR) data(dt_potato) explorer <- explorer(dt_potato, x = DAP, y = c(Canopy, GLI), id = Plot) # Example 1 mod_1 <- dt_potato |> modeler( x = DAP, y = GLI, grp = Plot, fn = "fn_lin_pl_lin", parameters = c(t1 = 38.7, t2 = 62, t3 = 90, k = 0.32, beta = -0.01), subset = 195 ) plot(mod_1, id = 195) print(mod_1) # Example 2 mod_2 <- dt_potato |> modeler( x = DAP, y = Canopy, grp = Plot, fn = "fn_lin_plat", parameters = c(t1 = 45, t2 = 80, k = 0.9), subset = 195 ) plot(mod_2, id = 195) print(mod_2)library(flexFitR) data(dt_potato) explorer <- explorer(dt_potato, x = DAP, y = c(Canopy, GLI), id = Plot) # Example 1 mod_1 <- dt_potato |> modeler( x = DAP, y = GLI, grp = Plot, fn = "fn_lin_pl_lin", parameters = c(t1 = 38.7, t2 = 62, t3 = 90, k = 0.32, beta = -0.01), subset = 195 ) plot(mod_1, id = 195) print(mod_1) # Example 2 mod_2 <- dt_potato |> modeler( x = DAP, y = Canopy, grp = Plot, fn = "fn_lin_plat", parameters = c(t1 = 45, t2 = 80, k = 0.9), subset = 195 ) plot(mod_2, id = 195) print(mod_2)
Computes indices of model performance for different models at once and hence allows comparison of indices across models.
performance(..., metrics = "all", metadata = FALSE, digits = 2)performance(..., metrics = "all", metadata = FALSE, digits = 2)
... |
Multiple model objects (only of class 'modeler'). |
metrics |
Can be "all" or a character vector of metrics to be computed (one or more of "logLik", "AIC", "AICc", "BIC", "Sigma", "SSE", "MAE", "MSE", "RMSE", "R2"). "all" by default. |
metadata |
Logical. If |
digits |
An integer. The number of decimal places to round the output. Default is 2. |
A data.frame with performance metrics for models in (...).
library(flexFitR) data(dt_potato) # Model 1 mod_1 <- dt_potato |> modeler( x = DAP, y = Canopy, grp = Plot, fn = "fn_lin_plat", parameters = c(t1 = 45, t2 = 80, k = 90), subset = 40 ) print(mod_1) # Model 2 mod_2 <- dt_potato |> modeler( x = DAP, y = Canopy, grp = Plot, fn = "fn_logistic", parameters = c(a = 0.199, t0 = 47.7, k = 100), subset = 40 ) print(mod_2) # Model 3 mod_3 <- dt_potato |> modeler( x = DAP, y = Canopy, grp = Plot, fn = "fn_lin", parameters = c(m = 20, b = 2), subset = 40 ) print(mod_3) performance(mod_1, mod_2, mod_3, metrics = c("AIC", "AICc", "BIC", "Sigma"))library(flexFitR) data(dt_potato) # Model 1 mod_1 <- dt_potato |> modeler( x = DAP, y = Canopy, grp = Plot, fn = "fn_lin_plat", parameters = c(t1 = 45, t2 = 80, k = 90), subset = 40 ) print(mod_1) # Model 2 mod_2 <- dt_potato |> modeler( x = DAP, y = Canopy, grp = Plot, fn = "fn_logistic", parameters = c(a = 0.199, t0 = 47.7, k = 100), subset = 40 ) print(mod_2) # Model 3 mod_3 <- dt_potato |> modeler( x = DAP, y = Canopy, grp = Plot, fn = "fn_lin", parameters = c(m = 20, b = 2), subset = 40 ) print(mod_3) performance(mod_1, mod_2, mod_3, metrics = c("AIC", "AICc", "BIC", "Sigma"))
This function plots a function over a specified interval and annotates the plot with the calculated Area Under the Curve (AUC) and parameter values. The aim of 'plot_fn' is to allow users to play with different starting values in their functions before fitting any models.
plot_fn( fn = "fn_lin_plat", params = c(t1 = 34.9, t2 = 61.8, k = 100), interval = c(0, 100), n_points = 1000, auc = FALSE, x_auc_label = NULL, y_auc_label = NULL, auc_label_size = 4, param_label_size = 4, base_size = 12, color = "red", label_color = "grey30" )plot_fn( fn = "fn_lin_plat", params = c(t1 = 34.9, t2 = 61.8, k = 100), interval = c(0, 100), n_points = 1000, auc = FALSE, x_auc_label = NULL, y_auc_label = NULL, auc_label_size = 4, param_label_size = 4, base_size = 12, color = "red", label_color = "grey30" )
fn |
A character string representing the name of the function to be plotted. Default is "fn_lin_plat". |
params |
A named numeric vector of parameters to be passed to the function. Default is |
interval |
A numeric vector of length 2 specifying the interval over which the function is to be plotted. Default is |
n_points |
An integer specifying the number of points to be used for plotting. Default is 1000. |
auc |
Print AUC in the plot? Default is |
x_auc_label |
A numeric value specifying the x-coordinate for the AUC label. Default is |
y_auc_label |
A numeric value specifying the y-coordinate for the AUC label. Default is |
auc_label_size |
A numeric value specifying the size of the AUC label text. Default is 3. |
param_label_size |
A numeric value specifying the size of the parameter label text. Default is 3. |
base_size |
A numeric value specifying the base size for the plot's theme. Default is 12. |
color |
A character string specifying the color for the plot lines and area fill. Default is "red". |
label_color |
A character string specifying the color for the labels. Default is "grey30". |
A ggplot object representing the plot.
# Example usage plot_fn( fn = "fn_lin_plat", params = c(t1 = 34.9, t2 = 61.8, k = 100), interval = c(0, 100), n_points = 1000 ) plot_fn( fn = "fn_lin_pl_lin", params <- c(t1 = 38.7, t2 = 62, t3 = 90, k = 0.32, beta = -0.01), interval = c(0, 100), n_points = 1000, base_size = 12 )# Example usage plot_fn( fn = "fn_lin_plat", params = c(t1 = 34.9, t2 = 61.8, k = 100), interval = c(0, 100), n_points = 1000 ) plot_fn( fn = "fn_lin_pl_lin", params <- c(t1 = 38.7, t2 = 62, t3 = 90, k = 0.32, beta = -0.01), interval = c(0, 100), n_points = 1000, base_size = 12 )
explorer
Creates various plots for an object of class explorer.
Depending on the specified type, the function can generate plots that show correlations between variables over x, correlations between x values for each variable, or the evolution of variables over x.
## S3 method for class 'explorer' plot( x, type = "var_by_x", label_size = 4, signif = FALSE, method = "pearson", filter_var = NULL, id = NULL, n_row = NULL, n_col = NULL, base_size = 13, return_gg = FALSE, add_avg = FALSE, ... )## S3 method for class 'explorer' plot( x, type = "var_by_x", label_size = 4, signif = FALSE, method = "pearson", filter_var = NULL, id = NULL, n_row = NULL, n_col = NULL, base_size = 13, return_gg = FALSE, add_avg = FALSE, ... )
x |
An object inheriting from class |
type |
Character string or number specifying the type of plot to generate. Available options are:
|
label_size |
Numeric. Size of the labels in the plot. Default is 4. Only works with type 1 and 2. |
signif |
Logical. If |
method |
Character string specifying the method for correlation calculation. Available options are |
filter_var |
Character vector specifying the variables to exclude from the plot. |
id |
Optional unique identifier to filter the evolution type of plot. Default is |
n_row |
Integer specifying the number of rows to use in |
n_col |
Integer specifying the number of columns to use in |
base_size |
Numeric. Base font size for the plot. Default is 13. |
return_gg |
Logical. If |
add_avg |
Logical. If |
... |
Further graphical parameters for future improvements. |
A ggplot object and an invisible data.frame containing the correlation table when type is "var_by_x" or "x_by_var".
library(flexFitR) data(dt_potato) results <- explorer(dt_potato, x = DAP, y = c(Canopy, GLI), id = Plot) table <- plot(results, label_size = 4, signif = TRUE, n_row = 2) table plot(results, type = "x_by_var", label_size = 4, signif = TRUE)library(flexFitR) data(dt_potato) results <- explorer(dt_potato, x = DAP, y = c(Canopy, GLI), id = Plot) table <- plot(results, label_size = 4, signif = TRUE, n_row = 2) table plot(results, type = "x_by_var", label_size = 4, signif = TRUE)
modeler
Creates several plots for an object of class modeler.
## S3 method for class 'modeler' plot( x, id = NULL, type = 1, label_size = 4, base_size = 14, linewidth = 0.5, color = "red", color_points = "black", parm = NULL, n_points = 1000, title = NULL, add_points = FALSE, add_ci = TRUE, color_ci = "blue", color_pi = "red", add_ribbon_ci = FALSE, add_ribbon_pi = FALSE, color_ribbon_ci = "blue", color_ribbon_pi = "red", ... )## S3 method for class 'modeler' plot( x, id = NULL, type = 1, label_size = 4, base_size = 14, linewidth = 0.5, color = "red", color_points = "black", parm = NULL, n_points = 1000, title = NULL, add_points = FALSE, add_ci = TRUE, color_ci = "blue", color_pi = "red", add_ribbon_ci = FALSE, add_ribbon_pi = FALSE, color_ribbon_ci = "blue", color_ribbon_pi = "red", ... )
x |
An object of class |
id |
An optional group ID to filter the data for plotting, useful for avoiding overcrowded plots. |
type |
Numeric value (1-6) to specify the type of plot to generate. Default is 1.
|
label_size |
Numeric value for the size of labels. Default is 4. |
base_size |
Numeric value for the base font size in pts. Default is 14. |
linewidth |
Numeric value specifying size of line geoms. Default is 0.5. |
color |
Character string specifying the color for the fitted line when |
color_points |
Character string specifying the color for the raw data points when |
parm |
Character vector specifying the parameters to plot for |
n_points |
Numeric value specifying the number of points for interpolation along the x-axis. Default is 2000. |
title |
Optional character string to add a title to the plot. |
add_points |
Logical value indicating whether to add raw observations to the plot for |
add_ci |
Logical value indicating whether to add confidence intervals for |
color_ci |
Character string specifying the color of the confidence interval when |
color_pi |
Character string specifying the color of the prediction interval when |
add_ribbon_ci |
Logical value indicating whether to add a ribbon for confidence intervals in |
add_ribbon_pi |
Logical value indicating whether to add a ribbon for prediction intervals in |
color_ribbon_ci |
Character string specifying the color of the ribbon (ci). Default is "blue". |
color_ribbon_pi |
Character string specifying the color of the ribbon (pi). Default is "red". |
... |
Additional graphical parameters for future extensions. |
A ggplot object representing the specified plot.
Johan Aparicio [aut]
library(flexFitR) data(dt_potato) # Example 1 mod_1 <- dt_potato |> modeler( x = DAP, y = Canopy, grp = Plot, fn = "fn_lin_plat", parameters = c(t1 = 45, t2 = 80, k = 0.9), subset = c(1:3) ) print(mod_1) plot(mod_1, id = 1:2) plot(mod_1, id = 1:3, type = 2, label_size = 10)library(flexFitR) data(dt_potato) # Example 1 mod_1 <- dt_potato |> modeler( x = DAP, y = Canopy, grp = Plot, fn = "fn_lin_plat", parameters = c(t1 = 45, t2 = 80, k = 0.9), subset = c(1:3) ) print(mod_1) plot(mod_1, id = 1:2) plot(mod_1, id = 1:3, type = 2, label_size = 10)
performance
Creates plots for an object of class performance
## S3 method for class 'performance' plot( x, id = NULL, type = 1, rescale = FALSE, linewidth = 1, base_size = 12, return_table = FALSE, ... )## S3 method for class 'performance' plot( x, id = NULL, type = 1, rescale = FALSE, linewidth = 1, base_size = 12, return_table = FALSE, ... )
x |
An object of class |
id |
An optional group ID to filter the data for plotting, useful for avoiding overcrowded plots. This argument is not used when type = 2. |
type |
Numeric value (1-3) to specify the type of plot to generate. Default is 1.
|
rescale |
Logical. If |
linewidth |
Numeric value specifying size of line geoms. |
base_size |
Numeric value for the base font size in pts. Default is 12 |
return_table |
Logical. If |
... |
Additional graphical parameters for future extensions. |
A ggplot object representing the specified plot.
Johan Aparicio [aut]
library(flexFitR) data(dt_potato) # Model 1 mod_1 <- dt_potato |> modeler( x = DAP, y = Canopy, grp = Plot, fn = "fn_lin_plat", parameters = c(t1 = 45, t2 = 80, k = 90), subset = 40 ) # Model 2 mod_2 <- dt_potato |> modeler( x = DAP, y = Canopy, grp = Plot, fn = "fn_logistic", parameters = c(a = 0.199, t0 = 47.7, k = 100), subset = 40 ) # Model 3 mod_3 <- dt_potato |> modeler( x = DAP, y = Canopy, grp = Plot, fn = "fn_lin", parameters = c(m = 20, b = 2), subset = 40 ) plot(performance(mod_1, mod_2, mod_3), type = 1) plot(performance(mod_1, mod_2, mod_3, metrics = c("AICc", "BIC")), type = 3)library(flexFitR) data(dt_potato) # Model 1 mod_1 <- dt_potato |> modeler( x = DAP, y = Canopy, grp = Plot, fn = "fn_lin_plat", parameters = c(t1 = 45, t2 = 80, k = 90), subset = 40 ) # Model 2 mod_2 <- dt_potato |> modeler( x = DAP, y = Canopy, grp = Plot, fn = "fn_logistic", parameters = c(a = 0.199, t0 = 47.7, k = 100), subset = 40 ) # Model 3 mod_3 <- dt_potato |> modeler( x = DAP, y = Canopy, grp = Plot, fn = "fn_lin", parameters = c(m = 20, b = 2), subset = 40 ) plot(performance(mod_1, mod_2, mod_3), type = 1) plot(performance(mod_1, mod_2, mod_3, metrics = c("AICc", "BIC")), type = 3)
modeler
Generate model predictions from an object of class modeler.
This function allows for flexible prediction types, including point predictions,
area under the curve (AUC), first or second order derivatives, and functions
of the parameters.
## S3 method for class 'modeler' predict( object, x = NULL, id = NULL, type = c("point", "auc", "fd", "sd"), se_interval = c("confidence", "prediction"), n_points = 1000, formula = NULL, metadata = FALSE, parallel = FALSE, workers = NULL, ... )## S3 method for class 'modeler' predict( object, x = NULL, id = NULL, type = c("point", "auc", "fd", "sd"), se_interval = c("confidence", "prediction"), n_points = 1000, formula = NULL, metadata = FALSE, parallel = FALSE, workers = NULL, ... )
object |
An object of class |
x |
A numeric value or vector specifying the points at which predictions
are made. For |
id |
Optional unique identifier to filter predictions by a specific group. Default is |
type |
A character string specifying the type of prediction. Default is "point".
|
se_interval |
A character string specifying the type of interval for
standard error calculation. Options are |
n_points |
An integer specifying the number of points used to approximate
the area under the curve (AUC) when |
formula |
A formula specifying a function of the parameters to be estimated (e.g., |
metadata |
Logical. If |
parallel |
Logical. If |
workers |
The number of parallel processes to use. |
... |
Additional parameters for future functionality. |
A data.frame containing the predicted values,
their associated standard errors, and optionally the metadata.
Johan Aparicio [aut]
library(flexFitR) data(dt_potato) mod_1 <- dt_potato |> modeler( x = DAP, y = Canopy, grp = Plot, fn = "fn_lin_plat", parameters = c(t1 = 45, t2 = 80, k = 0.9), subset = c(15, 2, 45) ) print(mod_1) # Point Prediction predict(mod_1, x = 45, type = "point", id = 2) # AUC Prediction predict(mod_1, x = c(0, 108), type = "auc", id = 2) # First Derivative predict(mod_1, x = 45, type = "fd", id = 2) # Second Derivative predict(mod_1, x = 45, type = "sd", id = 2) # Function of the parameters predict(mod_1, formula = ~ t2 - t1, id = 2)library(flexFitR) data(dt_potato) mod_1 <- dt_potato |> modeler( x = DAP, y = Canopy, grp = Plot, fn = "fn_lin_plat", parameters = c(t1 = 45, t2 = 80, k = 0.9), subset = c(15, 2, 45) ) print(mod_1) # Point Prediction predict(mod_1, x = 45, type = "point", id = 2) # AUC Prediction predict(mod_1, x = c(0, 108), type = "auc", id = 2) # First Derivative predict(mod_1, x = 45, type = "fd", id = 2) # Second Derivative predict(mod_1, x = 45, type = "sd", id = 2) # Function of the parameters predict(mod_1, formula = ~ t2 - t1, id = 2)
modeler
Prints information about modeler function.
## S3 method for class 'modeler' print(x, ...)## S3 method for class 'modeler' print(x, ...)
x |
An object fitted with the function |
... |
Options used by the tibble package to format the output. See 'tibble::print()' for more details. |
an object inheriting from class modeler.
Johan Aparicio [aut]
library(flexFitR) data(dt_potato) mod_1 <- dt_potato |> modeler( x = DAP, y = Canopy, grp = Plot, fn = "fn_lin_plat", parameters = c(t1 = 45, t2 = 80, k = 0.9), subset = c(1:5) ) plot(mod_1, id = c(1:4)) print(mod_1)library(flexFitR) data(dt_potato) mod_1 <- dt_potato |> modeler( x = DAP, y = Canopy, grp = Plot, fn = "fn_lin_plat", parameters = c(t1 = 45, t2 = 80, k = 0.9), subset = c(1:5) ) plot(mod_1, id = c(1:4)) print(mod_1)
modeler objectExtract residuals from a modeler object
## S3 method for class 'modeler' residuals(object, ...)## S3 method for class 'modeler' residuals(object, ...)
object |
An object of class 'modeler' |
... |
Additional parameters for future functionality. |
A numeric vector of residuals
Johan Aparicio [aut]
library(flexFitR) data(dt_potato) mod_1 <- dt_potato |> modeler( x = DAP, y = Canopy, grp = Plot, fn = "fn_lin_plat", parameters = c(t1 = 45, t2 = 80, k = 0.9), subset = c(15, 2, 45) ) residuals(mod_1)library(flexFitR) data(dt_potato) mod_1 <- dt_potato |> modeler( x = DAP, y = Canopy, grp = Plot, fn = "fn_lin_plat", parameters = c(t1 = 45, t2 = 80, k = 0.9), subset = c(15, 2, 45) ) residuals(mod_1)
This function performs transformations on specified columns of a data frame, including truncating maximum values, handling negative values, and adding a zero to the series. It allows for grouping and supports retaining metadata in the output.
series_mutate( data, x, y, grp, metadata, max_as_last = FALSE, check_negative = FALSE, add_zero = FALSE, interval = NULL )series_mutate( data, x, y, grp, metadata, max_as_last = FALSE, check_negative = FALSE, add_zero = FALSE, interval = NULL )
data |
A |
x |
The name of the column in |
y |
The name of the column(s) in |
grp |
Column(s) in |
metadata |
Names of columns to retain in the output. Defaults to |
max_as_last |
Logical. If |
check_negative |
Logical. If |
add_zero |
Logical. If |
interval |
A numeric vector of length 2 (start and end) specifying the range to filter the data. Defaults to |
A transformed data.frame with the specified modifications applied.
data(dt_potato) new_data <- series_mutate( data = dt_potato, x = DAP, y = GLI, grp = gid, max_as_last = TRUE, check_negative = TRUE )data(dt_potato) new_data <- series_mutate( data = dt_potato, x = DAP, y = GLI, grp = gid, max_as_last = TRUE, check_negative = TRUE )
modeler
Subset an object of class modeler
## S3 method for class 'modeler' subset(x, id = NULL, ...)## S3 method for class 'modeler' subset(x, id = NULL, ...)
x |
An object of class |
id |
Unique identifier to filter a |
... |
Additional parameters for future functionality. |
A modeler object.
Johan Aparicio [aut]
library(flexFitR) data(dt_potato) mod <- dt_potato |> modeler( x = DAP, y = Canopy, grp = Plot, fn = "fn_logistic", parameters = c(a = 0.199, t0 = 47.7, k = 100), subset = 1:2 ) print(mod) mod_new <- subset(mod, id = 2) print(mod_new)library(flexFitR) data(dt_potato) mod <- dt_potato |> modeler( x = DAP, y = Canopy, grp = Plot, fn = "fn_logistic", parameters = c(a = 0.199, t0 = 47.7, k = 100), subset = 1:2 ) print(mod) mod_new <- subset(mod, id = 2) print(mod_new)
modeler objectIt creates a new fitted object using the parameter values from the current model as initial values. It can also be used to perform a few additional iterations of a model that has not converged.
## S3 method for class 'modeler' update(object, method = NULL, track = TRUE, eps = 1e-06, ...)## S3 method for class 'modeler' update(object, method = NULL, track = TRUE, eps = 1e-06, ...)
object |
An object of class |
method |
A character vector specifying optimization methods.
Check available methods using |
track |
Logical. If |
eps |
Numeric. The minimum change in SSE required to consider a fit improved.
Defaults to |
... |
Additional parameters for future functionality. |
An object of class modeler, which is a list containing the following elements:
paramData frame containing optimized parameters and related information.
dtData frame with input data, fitted values, and residuals.
metricsMetrics and summary of the models.
executionTotal execution time for the analysis.
responseName of the response variable analyzed.
keepMetadata retained based on the keep argument.
funName of the curve-fitting function used.
parallelList containing parallel execution details (if applicable).
fitList of fitted models for each group.
library(flexFitR) data(dt_potato) mo_1 <- dt_potato |> modeler( x = DAP, y = GLI, grp = Plot, fn = "fn_lin_pl_lin", parameters = c(t1 = 10, t2 = 62, t3 = 90, k = 0.32, beta = -0.01), subset = 195 ) plot(mo_1) mo_2 <- update(mo_1) plot(mo_2)library(flexFitR) data(dt_potato) mo_1 <- dt_potato |> modeler( x = DAP, y = GLI, grp = Plot, fn = "fn_lin_pl_lin", parameters = c(t1 = 10, t2 = 62, t3 = 90, k = 0.32, beta = -0.01), subset = 195 ) plot(mo_1) mo_2 <- update(mo_1) plot(mo_2)
modeler
Extract the variance-covariance matrix for the parameter estimates
from an object of class modeler.
## S3 method for class 'modeler' vcov(object, id = NULL, ...)## S3 method for class 'modeler' vcov(object, id = NULL, ...)
object |
An object of class |
id |
An optional unique identifier to filter by a specific group.
Default is |
... |
Additional parameters for future functionality. |
A list of matrices, where each matrix represents the variance-covariance matrix of the estimated parameters for each group or fit.
Johan Aparicio [aut]
library(flexFitR) data(dt_potato) mod_1 <- dt_potato |> modeler( x = DAP, y = Canopy, grp = Plot, fn = "fn_lin_plat", parameters = c(t1 = 45, t2 = 80, k = 0.9), subset = c(15, 2, 45) ) print(mod_1) vcov(mod_1)library(flexFitR) data(dt_potato) mod_1 <- dt_potato |> modeler( x = DAP, y = Canopy, grp = Plot, fn = "fn_lin_plat", parameters = c(t1 = 45, t2 = 80, k = 0.9), subset = c(15, 2, 45) ) print(mod_1) vcov(mod_1)