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.1.0 |
Built: | 2025-02-21 05:22:35 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(L = 100, k = 4, t0 = 40), 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(L = 100, k = 4, t0 = 40), 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(L = 100, k = 4, t0 = 40), subset = 1:2 ) mod_2 <- dt_potato |> modeler( x = DAP, y = Canopy, grp = Plot, fn = "fn_linear_sat", 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(L = 100, k = 4, t0 = 40), subset = 1:2 ) mod_2 <- dt_potato |> modeler( x = DAP, y = Canopy, grp = Plot, fn = "fn_linear_sat", 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_linear_sat", 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_linear_sat", parameters = c(t1 = 45, t2 = 80, k = 0.9), subset = c(15, 2, 45) ) print(mod_1) coef(mod_1, id = 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_linear_sat", 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_linear_sat", 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_potato
dt_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_vars
A 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_metadata
A data.frame summarizing the metadata.
locals_min_max
A data.frame containing the local minima and maxima of the mean y values over x.
dt_long
A data.frame in long format, with columns for uid, metadata, var, x, and y
metadata
A 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_linear_sat", 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_linear_sat", parameters = c(t1 = 45, t2 = 80, k = 0.9), subset = c(15, 2, 45) ) fitted(mod_1)
Computes a value based on an exponential growth curve and exponential decay model for time.
fn_exp1_exp(t, t1, t2, alpha, beta)
fn_exp1_exp(t, t1, t2, alpha, beta)
t |
Numeric. The time value. |
t1 |
Numeric. The lower threshold time. Assumed to be known. |
t2 |
Numeric. The upper threshold time. |
alpha |
Numeric. The parameter for the first exponential term. Must be greater than 0. |
beta |
Numeric. The parameter for the second exponential term. Must be less than 0. |
A numeric value based on the double exponential model.
If t
is less than t1
, the function returns 0.
If t
is between t1
and t2
(inclusive),
the function returns exp(alpha * (t - t1)) - 1
.
If t
is greater than t2
, the function returns
(exp(alpha * (t2 - t1)) - 1) * exp(beta * (t - t2))
.
library(flexFitR) plot_fn( fn = "fn_exp1_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_exp1_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 )
Computes a value based on an exponential growth curve and linear decay model for time.
fn_exp1_lin(t, t1, t2, alpha, beta)
fn_exp1_lin(t, t1, t2, alpha, beta)
t |
Numeric. The time value. |
t1 |
Numeric. The lower threshold time. Assumed to be known. |
t2 |
Numeric. The upper threshold time. |
alpha |
Numeric. The parameter for the exponential term. Must be greater than 0. |
beta |
Numeric. The parameter for the linear term. Must be less than 0. |
A numeric value based on the exponential linear model.
If t
is less than t1
, the function returns 0.
If t
is between t1
and t2
(inclusive),
the function returns exp(alpha * (t - t1)) - 1
.
If t
is greater than t2
, the function returns
beta * (t - t2) + (exp(alpha * (t2 - t1)) - 1)
.
library(flexFitR) plot_fn( fn = "fn_exp1_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_exp1_lin", params = c(t1 = 35, t2 = 55, alpha = 1 / 20, beta = -1 / 40), interval = c(0, 108), n_points = 2000, auc_label_size = 3 )
Computes a value based on an exponential growth curve and exponential decay model for time.
fn_exp2_exp(t, t1, t2, alpha, beta)
fn_exp2_exp(t, t1, t2, alpha, beta)
t |
Numeric. The time value. |
t1 |
Numeric. The lower threshold time. Assumed to be known. |
t2 |
Numeric. The upper threshold time. |
alpha |
Numeric. The parameter for the first exponential term. Must be greater than 0. |
beta |
Numeric. The parameter for the second exponential term. Must be less than 0. |
A numeric value based on the double exponential model.
If t
is less than t1
, the function returns 0.
If t
is between t1
and t2
(inclusive),
the function returns exp(alpha * (t - t1)^2) - 1
.
If t
is greater than t2
, the function returns
(exp(alpha * (t2 - t1)^2) - 1) * exp(beta * (t - t2))
.
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 )
Computes a value based on an exponential growth curve and linear decay model for time.
fn_exp2_lin(t, t1, t2, alpha, beta)
fn_exp2_lin(t, t1, t2, alpha, beta)
t |
Numeric. The time value. |
t1 |
Numeric. The lower threshold time. Assumed to be known. |
t2 |
Numeric. The upper threshold time. |
alpha |
Numeric. The parameter for the exponential term. Must be greater than 0. |
beta |
Numeric. The parameter for the linear term. Must be less than 0. |
A numeric value based on the exponential linear model.
If t
is less than t1
, the function returns 0.
If t
is between t1
and t2
(inclusive),
the function returns exp(alpha * (t - t1)^2) - 1
.
If t
is greater than t2
, the function returns
beta * (t - t2) + (exp(alpha * (t2 - t1)^2) - 1)
.
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 )
Computes a value based on a linear function.
fn_lin(t, m, b)
fn_lin(t, m, b)
t |
Numeric value. |
m |
Numeric value for the slope coefficient. |
b |
Numeric value for the intercept coefficient. |
A numeric value based on the linear function.
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 )
Linear plateau linear function
fn_lin_pl_lin(t, t1, t2, t3, k, beta)
fn_lin_pl_lin(t, t1, t2, t3, k, beta)
t |
Numeric. The time value. |
t1 |
Numeric. The lower threshold time. Default is 45. |
t2 |
Numeric. The upper threshold time before plateau. Default is 80. |
t3 |
Numeric. The lower threshold time after plateau. Default is 45. |
k |
Numeric. The maximum value of the function. Default is 0.9. |
beta |
Numeric. Slope of the linear decay. |
A numeric value based on the linear plateau linear model.
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 )
Linear plateau linear with constrains
fn_lin_pl_lin2(t, t1, t2, dt, k, beta)
fn_lin_pl_lin2(t, t1, t2, dt, k, beta)
t |
Numeric. The time value. |
t1 |
Numeric. The lower threshold time. |
t2 |
Numeric. The upper threshold time before plateau. |
dt |
Numeric. dt = t3 - t2. |
k |
Numeric. The maximum value of the function. |
beta |
Numeric. Slope of the linear decay. |
A numeric value based on the linear plateau linear model.
library(flexFitR) plot_fn( fn = "fn_lin_pl_lin2", 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_lin_pl_lin2", 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 )
Computes a value based on a linear growth curve reaching a plateau for time.
fn_linear_sat(t, t1 = 45, t2 = 80, k = 0.9)
fn_linear_sat(t, t1 = 45, t2 = 80, k = 0.9)
t |
Numeric. The time value. |
t1 |
Numeric. The lower threshold time. Default is 45. |
t2 |
Numeric. The upper threshold time. Default is 80. |
k |
Numeric. The maximum value of the function. Default is 0.9. Assumed to be known. |
A numeric value based on the threshold model.
If t
is less than t1
, the function returns 0.
If t
is between t1
and t2
(inclusive),
the function returns a value between 0 and k
in a linear trend.
If t
is greater than t2
, the function returns k
.
library(flexFitR) plot_fn( fn = "fn_linear_sat", 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_linear_sat", params = c(t1 = 34.9, t2 = 61.8, k = 100), interval = c(0, 108), n_points = 2000, auc_label_size = 3 )
Computes a value based on a logistic function.
fn_logistic(t, L, k, t0)
fn_logistic(t, L, k, t0)
t |
Numeric value. |
L |
Numeric value. |
k |
Numeric value. |
t0 |
Numeric value. |
A numeric value based on the logistic function.
library(flexFitR) plot_fn( fn = "fn_logistic", params = c(L = 100, k = 0.199, t0 = 47.7), interval = c(0, 108), n_points = 2000 )
library(flexFitR) plot_fn( fn = "fn_logistic", params = c(L = 100, k = 0.199, t0 = 47.7), interval = c(0, 108), n_points = 2000 )
Computes a value based on a quadratic function..
fn_quad(t, a, b, c)
fn_quad(t, a, b, c)
t |
Numeric value. |
a |
Numeric value for coefficient a. |
b |
Numeric value for coefficient b. |
c |
Numeric value for coefficient c. |
A numeric value based on the linear function.
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))
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)
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_linear_sat", 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_linear_sat", 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_linear_sat", 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_linear_sat", 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:
param
Data frame containing optimized parameters and related information.
dt
Data frame with input data, fitted values, and residuals.
metrics
Metrics and summary of the models.
execution
Total execution time for the analysis.
response
Name of the response variable analyzed.
keep
Metadata retained based on the keep
argument.
fun
Name of the curve-fitting function used.
parallel
List containing parallel execution details (if applicable).
fit
List 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_linear_sat", 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_linear_sat", 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_linear_sat", 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(L = 100, k = 4, t0 = 50), 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_linear_sat", 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(L = 100, k = 4, t0 = 50), 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_linear_sat", 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_linear_sat", 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_linear_sat". |
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_linear_sat", 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_linear_sat", 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, color = "red", color_points = "black", parm = NULL, n_points = 1000, title = NULL, 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, color = "red", color_points = "black", parm = NULL, n_points = 1000, title = NULL, 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. |
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_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_linear_sat", 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_linear_sat", 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_linear_sat", 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(L = 100, k = 4, t0 = 50), 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_linear_sat", 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(L = 100, k = 4, t0 = 50), 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, ... )
## 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, ... )
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 |
... |
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_linear_sat", 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_linear_sat", 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_linear_sat", 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_linear_sat", 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_linear_sat", 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_linear_sat", 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(L = 100, k = 4, t0 = 40), 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(L = 100, k = 4, t0 = 40), subset = 1:2 ) print(mod) mod_new <- subset(mod, id = 2) print(mod_new)
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_linear_sat", 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_linear_sat", parameters = c(t1 = 45, t2 = 80, k = 0.9), subset = c(15, 2, 45) ) print(mod_1) vcov(mod_1)