knitr::opts_chunk$set( collapse = TRUE, comment = "#>" )
This vignette demonstrates how to apply parameter constraints when modeling biological processes using {flexFitR}. Constraints can help ensure that parameter estimates remain within realistic or biologically meaningful ranges, improving both the interpretability and reliability of model outcomes.
In many biological models, certain relationships between parameters are expected. For example:
This vignette demonstrates how to apply these types of constraints in {flexFitR} to guide the optimization process.
For this example, we use the Green Leaf Index (GLI) derived from UAV imagery to model plant emergence, canopy closure, and senescence. The parameters we are interested in include:
Our expectation is that $0 < t1 < t2 < t3$. We will apply constraints to ensure this relationship hold.
library(flexFitR) library(dplyr) library(kableExtra) library(ggpubr) library(purrr)
We begin with the explorer
function, which provides basic statistical
summaries and visualizations to help understand the temporal evolution of
each plot.
data(dt_potato) explorer <- explorer(dt_potato, x = DAP, y = c(GLI), id = Plot)
p1 <- plot(explorer, type = "evolution", return_gg = TRUE, add_avg = TRUE) p2 <- plot(explorer, type = "x_by_var", return_gg = TRUE) ggarrange(p1, p2, nrow = 1)
kable(mutate_if(explorer$summ_vars, is.numeric, round, 2))
After exploring the data, we define the regression function. Here we use a linear-plateau-linear function with five parameters: t1, t2, t3, k, and $\beta$. The function can be expressed mathematically as follows:
fn_lin_pl_lin()
\begin{equation} f(t; t_1, t_2, t_3, k, \beta) = \begin{cases} 0 & \text{if } t < t_1 \ \dfrac{k}{t_2 - t_1} \cdot (t - t_1) & \text{if } t_1 \leq t \leq t_2 \ k & \text{if } t_2 \leq t \leq t_3 \ k + \beta \cdot (t - t_3) & \text{if } t > t_3 \end{cases} \end{equation}
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), color = "black", base_size = 15 )
To impose constraints, we can reformulate the function. For instance, if we want to ensure that $t3 \geq t2$, we introduce dt as the difference between t3 and t2:
\begin{equation} f(t; t_1, t_2, dt, k, \beta) = \begin{cases} 0 & \text{if } t < t_1 \ \dfrac{k}{t_2 - t_1} \cdot (t - t_1) & \text{if } t_1 \leq t \leq t_2 \ k & \text{if } t_2 \leq t \leq (t_2 + dt) \ k + \beta \cdot (t - (t_2 + dt)) & \text{if } t > (t_2 + dt) \end{cases} \end{equation}
To enforce $dt > 0$ and $\beta < 0$ (i.e., a non-positive slope at the end of the curve), we specify bounds in the modeler function as follows:
# Define constraints and bounds for the model lower_bounds <- c(t1 = 0, t2 = 0, dt = 0, k = 0, beta = -Inf) upper_bounds <- c(t1 = Inf, t2 = Inf, dt = Inf, k = Inf, beta = 0) # Initial values initial_vals <- c(t1 = 38, t2 = 62, dt = 28, k = 0.32, beta = -0.01)
We fit the model with these constraints by passing lower and upper arguments to
modeler
. In this vignette, we fit the model for plots 195 and 40 as a subset
of the total 196 plots.
mod_1 <- dt_potato |> modeler( x = DAP, y = GLI, grp = Plot, fn = "fn_lin_pl_lin2", parameters = initial_vals, lower = lower_bounds, upper = upper_bounds, method = c("nlminb", "L-BFGS-B"), subset = c(195, 40) )
Here:
After fitting, we can inspect the model summary and visualize the fit using
the plot
function:
print(mod_1)
plot(mod_1, id = c(195, 40)) kable(mod_1$param)
Once the model is fitted, we can extract key statistical information, such as coefficients, standard errors, confidence intervals, and the variance-covariance matrix for each plot. These metrics help evaluate parameter reliability and assess uncertainty.
The functions coef
, confint
, and vcov
are used as follows:
coef(mod_1, id = 40)
confint(mod_1, id = 40)
vcov(mod_1, id = 40)
Using type = 2
in the plot
function generates a coefficients plot.
This allows us to view the estimated coefficients and their associated
confidence intervals for each group.
plot(mod_1, type = 2, id = c(195, 40), label_size = 8)
Another option (type = 4
) shows the fitted curve (black line), confidence
interval (blue-dashed line), and prediction interval (red-dashed line).
Additionally, setting type = 5 displays the first derivative, indicating the
rate of change over time.
a <- plot(mod_1, type = 4, color = "black", title = "Fitted Curve + CIs & PIs") b <- plot(mod_1, type = 5, color = "black") ggarrange(a, b)
This vignette showed how to apply constraints in {flexFitR} models to better capture biological realities and improve parameter estimation. Constraints can be an essential tool for ensuring that models produce interpretable and meaningful results.
Any scripts or data that you put into this service are public.
Add the following code to your website.
For more information on customizing the embed code, read Embedding Snippets.