General-purpose optimization techniques for non-linear least squares problems.
Arguments
- data
A data.frame in a wide format.
- x
The name of the column in `data` that contains x points.
- y
The name of the column in `data` that contain the variable to be analyzed. Must match a var in the data.
- grp
The names of the columns in `data` that contains a grouping variable. (Optional).
- keep
The names of the columns in `data` to keep across the analysis.
- fn
A string specifying the name of the function to be used for the curve fitting. Default is
"fn_linear_sat"
.- parameters
Can be a named numeric vector specifying the initial values for the parameters to be optimized, or a data frame with columns
uid
, and the initial parameter values for each group id. Used for providing specific initial values per group id. Default isNULL
.- lower
Numeric vector specifying the lower bounds for the parameters. Default is
-Inf
for all parameters.- upper
Numeric vector specifying the upper bounds for the parameters. Default is
Inf
for all parameters.- fixed_params
A data frame with columns
uid
, and the fixed parameter values for each group id. Used for fixing certain parameters during optimization.- method
A character vector specifying the optimization methods to be used. Check `optimx::checkallsolvers()` for available methods. Default is
c("subplex", "pracmanm", "anms")
.- subset
An optional vector with levels of `grp` to filter the data. Default is
NULL
, meaning all groups are used.- options
A list of additional options. See `modeler.options()`
add_zero
Logical. If
TRUE
, adds a zero value to the series at the start. Default isFALSE
.check_negative
Logical. If
TRUE
, converts negative values in the data to zero. Default isFALSE
.max_as_last
Logical. If
TRUE
, appends the maximum value after reaching the maximum. Default isFALSE
.progress
Logical. If
TRUE
a progress bar is displayed. Default isFALSE
. Try this before running the function:progressr::handlers("progress", "beepr")
.parallel
Logical. If
TRUE
the model fit is performed in parallel. Default isFALSE
.workers
The number of parallel processes to use. `parallel::detectCores()`
trace
If
TRUE
, convergence monitoring of the current fit is reported in the console.FALSE
by default.return_method
Logical. If
TRUE
, includes the optimization method used in the result. Default isFALSE
.
- control
A list of control parameters to be passed to the optimization function. For example:
list(maxit = 500)
.
Value
An object of class modeler
, which is a list containing the following elements:
param
A data frame containing the optimized parameters and related information.
dt
A data frame with data used and fitted values.
fn
The call used when fitting models.
metrics
Metrics and summary of the models.
execution
Execution time.
response
Response variable.
keep
Metadata to keep across.
fun
Name of the function.
parallel
List returning parallel and workers.
fit
List with the fitted models.
Examples
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)
#>
#> Call:
#> GLI ~ fn_lin_pl_lin(DAP, t1, t2, t3, k, beta)
#>
#> Residuals:
#> Min. 1st Qu. Median Mean 3rd Qu. Max.
#> -0.011076 -0.001003 0.000000 -0.001886 0.000000 0.000000
#>
#> Optimization Results `head()`:
#> uid t1 t2 t3 k beta sse
#> 195 40.1 63.1 91.4 0.325 -0.00809 0.000139
#>
#> Metrics:
#> Groups Timing Convergence Iterations
#> 1 0.7244 secs 100% 3156 (id)
#>
# 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)
#>
#> Call:
#> Canopy ~ fn_linear_sat(DAP, t1, t2, k)
#>
#> Residuals:
#> Min. 1st Qu. Median Mean 3rd Qu. Max.
#> -1.201e-09 0.000e+00 1.911e-10 1.031e-10 3.823e-10 8.791e-10
#>
#> Optimization Results `head()`:
#> uid t1 t2 k sse
#> 195 38.8 61.6 100 2.65e-18
#>
#> Metrics:
#> Groups Timing Convergence Iterations
#> 1 0.4516 secs 100% 351 (id)
#>