| Title: | Linear and Nonlinear Regression for Agricultural Data |
| Version: | 0.1.0 |
| Description: | Fit, compare, and visualise linear and nonlinear regression models tailored to field-trial and dose-response agricultural data. Provides S3 classes for mixed-effects models (via 'lme4'), nonlinear growth curves (logistic, 'Gompertz', asymptotic, linear-plateau, quadratic), and four/five-parameter log-logistic dose-response models (via 'drc'). Includes automated starting-value heuristics, goodness-of-fit statistics, residual diagnostics, and 'ggplot2'-based visualisation. Methods are based on Bates and Watts (1988, ISBN:9780471816430), Ritz and others (2015) <doi:10.1371/journal.pone.0146021>, and Bates and others (2015) <doi:10.18637/jss.v067.i01>. |
| Depends: | R (≥ 4.1.0) |
| Imports: | lme4 (≥ 1.1-35), drc (≥ 3.0-1), ggplot2 (≥ 3.4.0), patchwork (≥ 1.1.0), stats, utils |
| Suggests: | broom (≥ 1.0.0), broom.mixed (≥ 0.2.9), emmeans (≥ 1.8.0), nlme (≥ 3.1-0), methods, testthat (≥ 3.0.0), knitr, rmarkdown, lmerTest, car, multcomp |
| License: | MIT + file LICENSE |
| Encoding: | UTF-8 |
| Language: | en-US |
| RoxygenNote: | 7.3.3 |
| VignetteBuilder: | knitr |
| Config/testthat/edition: | 3 |
| NeedsCompilation: | no |
| Packaged: | 2026-03-27 06:10:56 UTC; acer |
| Author: | Sadikul Islam |
| Maintainer: | Sadikul Islam <sadikul.islamiasri@gmail.com> |
| Repository: | CRAN |
| Date/Publication: | 2026-03-31 15:00:23 UTC |
agriReg: Linear and Nonlinear Regression for Agricultural Data
Description
agriReg provides a unified interface for fitting, comparing, and
visualising regression models commonly used in agricultural research:
-
Linear & mixed-effects models via
fit_linear()(wrapslm/lme4::lmer) -
Nonlinear growth curves via
fit_nonlinear()— logistic, Gompertz, asymptotic, linear-plateau, quadratic -
Dose-response models via
fit_dose_response()— 4/5-parameter log-logistic and Weibull (wrapsdrc) -
Model comparison via
compare_models()— AIC, BIC, RMSE side by side -
Diagnostics via
gof_stats(),residual_check(), and S3plot()methods
Typical workflow
library(agriReg)
# 1. Clean field trial data
trial <- clean_agri_data(my_data, yield_col = "yield_tha")
# 2. Linear mixed model (treatment + block structure)
lm_fit <- fit_linear(trial,
formula = "yield_tha ~ nitrogen + phosphorus",
random = "(1|block)")
summary(lm_fit)
plot(lm_fit, type = "residuals")
# 3. Nonlinear growth curve
nl_fit <- fit_nonlinear(trial, x_col = "days", y_col = "biomass",
model = "logistic")
plot(nl_fit)
# 4. Dose-response
dr_fit <- fit_dose_response(herb_data,
dose_col = "dose_g_ha",
resp_col = "weed_control_pct")
ed_estimates(dr_fit, respLev = c(50, 90))
# 5. Compare models
compare_models(linear = lm_fit, nonlinear = nl_fit)
Author(s)
Maintainer: Sadikul Islam sadikul.islamiasri@gmail.com (ORCID)
Clean and validate agricultural field-trial data
Description
Removes rows with missing yield values, coerces the yield column to
numeric, optionally flags IQR-based outliers, and returns an agriData
object with a concise summary.
Usage
clean_agri_data(
df,
yield_col = "yield",
flag_outliers = TRUE,
na_action = c("remove", "warn")
)
Arguments
df |
A |
yield_col |
Character. Name of the column that holds the primary
response variable (e.g. |
flag_outliers |
Logical. When |
na_action |
One of |
Value
A data.frame with additional class "agriData". Attributes
n_removed (rows dropped) and n_flagged (outliers flagged) are
attached.
Examples
df <- data.frame(
yield = c(3.1, 4.2, NA, 8.9, 3.8, 100),
block = c("A", "A", "B", "B", "C", "C")
)
clean_agri_data(df, yield_col = "yield")
Compare multiple agriReg models side by side
Description
Computes AIC, BIC, RMSE, MAE, and R² for a collection of agriLM,
agriNLS, or agriDRC objects and returns them in a tidy data frame
ranked by AIC.
Usage
compare_models(...)
Arguments
... |
Named model objects. Names are used as row identifiers. If
unnamed, models are labelled |
Value
A data.frame with columns model, engine, n_par, AIC,
BIC, RMSE, MAE, R2, and delta_AIC (difference from best).
Examples
dat <- data.frame(
nitrogen = rep(c(0, 40, 80, 120, 160), each = 8),
yield = c(2.1, 2.8, 3.6, 4.1, 4.3,
2.0, 2.9, 3.5, 4.2, 4.4,
2.2, 2.7, 3.7, 4.0, 4.5,
2.1, 3.0, 3.4, 4.3, 4.2,
2.3, 2.8, 3.6, 4.1, 4.3,
2.0, 2.9, 3.5, 4.2, 4.4,
2.2, 2.7, 3.7, 4.0, 4.5,
2.1, 3.0, 3.4, 4.3, 4.2)
)
m1 <- fit_linear(dat, "yield ~ nitrogen")
m2 <- fit_nonlinear(dat, "nitrogen", "yield", "quadratic")
compare_models(linear = m1, quadratic = m2)
Compute effective dose (ED) estimates from a dose-response model
Description
Returns the dose required to achieve a specified percentage of the maximum response, with confidence intervals via the delta method.
Usage
ed_estimates(
drc_fit,
respLev = c(10, 50, 90),
interval = "delta",
level = 0.95
)
Arguments
drc_fit |
An |
respLev |
Numeric vector of response levels (default |
interval |
Type of confidence interval: |
level |
Confidence level (default |
Value
A matrix of ED estimates and confidence bounds (printed and returned invisibly).
Fit a log-logistic or Weibull dose-response model
Description
A wrapper around drc::drm() providing the four model families most
commonly used in herbicide/pesticide and nutrient-response studies.
Returns an agriDRC object with print, summary, and plot methods.
Usage
fit_dose_response(
data,
dose_col,
resp_col,
fct = c("LL.4", "LL.5", "W1.4", "W2.4"),
curveid = NULL,
...
)
Arguments
data |
A data frame containing dose and response columns. |
dose_col |
Character. Name of the dose/concentration column. |
resp_col |
Character. Name of the response column (e.g. percentage inhibition, weed biomass reduction, germination rate). |
fct |
Character. Model function:
|
curveid |
Optional column name for grouping curves (e.g. |
... |
Additional arguments forwarded to |
Value
An object of class "agriDRC" containing:
- fit
The underlying
drcobject.- fct
The model function name.
- dose_col, resp_col
Column names used.
- data
The data used.
- call
The matched call.
Examples
herbicide_trial <- load_example_data("herbicide_trial")
dr <- fit_dose_response(herbicide_trial,
dose_col = "dose_g_ha",
resp_col = "weed_control_pct")
summary(dr)
ed_estimates(dr, respLev = c(10, 50, 90))
plot(dr)
Fit a linear or mixed-effects model for agricultural trials
Description
A wrapper around stats::lm() and lme4::lmer() that returns an agriLM
object with consistent print, summary, plot, coef, fitted,
residuals, and predict S3 methods.
Usage
fit_linear(data, formula, random = NULL, weights = NULL, ...)
Arguments
data |
A data frame or |
formula |
A model formula as a character string or formula object.
Example: |
random |
Optional character string specifying the random-effects term
in |
weights |
Optional numeric vector of observation weights passed to
|
... |
Additional arguments forwarded to |
Value
An object of class "agriLM" (a named list) containing:
- fit
The underlying
lmorlmerModobject.- engine
Character:
"lm"or"lmer".- formula
The fixed-effects formula.
- random
The random-effects term (or
NULL).- data
The data used for fitting.
- call
The matched call.
Examples
wheat_trial <- load_example_data("wheat_trial")
# Ordinary least squares
m1 <- fit_linear(wheat_trial, "yield ~ nitrogen + phosphorus")
# Mixed model with block as random effect
m2 <- fit_linear(wheat_trial,
"yield ~ nitrogen + phosphorus",
random = "(1|block)")
summary(m2)
plot(m2, type = "residuals")
Fit a nonlinear growth or response curve to agricultural data
Description
Provides a unified interface for five model families commonly used in
agronomic research. Automatic starting-value heuristics are applied when
start is not supplied, making the function usable without prior
knowledge of parameter ranges.
Usage
fit_nonlinear(
data,
x_col,
y_col,
model = c("logistic", "gompertz", "asymptotic", "linear_plateau", "quadratic"),
start = NULL,
control = nls.control(maxiter = 500, tol = 1e-06)
)
Arguments
data |
A data frame (or |
x_col |
Character. Name of the independent variable column (e.g.
|
y_col |
Character. Name of the response column (e.g. |
model |
One of:
|
start |
Optional named list of starting parameter values. When
|
control |
A list passed to |
Value
An object of class "agriNLS", a named list containing:
- fit
The
nlsobject.- model
Character: the model name.
- x_col, y_col
Column names used.
- data
The data used for fitting.
- call
The matched call.
Examples
maize_growth <- load_example_data("maize_growth")
nl <- fit_nonlinear(maize_growth, x_col = "days", y_col = "biomass_g",
model = "logistic")
summary(nl)
plot(nl)
Fit a sequence of polynomial linear models and pick the best
Description
Convenience function for testing linear, quadratic, and cubic fits of a single continuous predictor and selecting the best by AIC.
Usage
fit_polynomial(data, x_col, y_col, max_degree = 3)
Arguments
data |
A data frame. |
x_col |
Predictor column name. |
y_col |
Response column name. |
max_degree |
Maximum polynomial degree to test (default |
Value
An agriLM object for the best-fitting model. A comparison table
is printed as a side-effect.
Examples
wheat_trial <- load_example_data("wheat_trial")
best <- fit_polynomial(wheat_trial, x_col = "nitrogen", y_col = "yield")
plot(best)
Compute goodness-of-fit statistics for a model
Description
Extracts R², adjusted-R², RMSE, MAE, AIC, and BIC from an agriLM,
agriNLS, or agriDRC object (or any object with fitted(),
residuals(), and AIC() methods).
Usage
gof_stats(model)
Arguments
model |
An |
Value
A named list with elements R2, adj_R2, RMSE, MAE,
AIC, and BIC.
Simulated herbicide dose-response dataset
Description
Weed control percentage measured across seven herbicide doses for two weed species.
Format
A data frame with 84 rows and 4 variables:
- species
Weed species (
Amaranth,Ryegrass).- dose_g_ha
Herbicide dose (g active ingredient per hectare).
- weed_control_pct
Weed control percentage (0–100).
- rep
Replicate number (1–6).
Source
Simulated for package demonstration.
Load a bundled agriReg example dataset
Description
Reads one of the three example datasets shipped with the package from
inst/extdata. Under normal installation the datasets are available
directly via data(wheat_trial) etc; this function is a fallback that
works even when .rda files have not been compiled.
Usage
load_example_data(name = c("wheat_trial", "maize_growth", "herbicide_trial"))
Arguments
name |
One of |
Value
A data.frame.
Examples
wheat <- load_example_data("wheat_trial")
head(wheat)
Simulated maize growth time-series
Description
Daily above-ground biomass measurements from 10 maize plants tracked from emergence to physiological maturity under well-watered conditions.
Format
A data frame with 200 rows and 4 variables:
- plant_id
Plant identifier (1–10).
- days
Days after emergence.
- biomass_g
Dry biomass (g/plant).
- treatment
Water treatment (
WW= well-watered,DS= drought-stressed).
Source
Simulated for package demonstration.
Comprehensive model summary with fit statistics
Description
Prints a formatted block with coefficient table, goodness-of-fit
statistics, and (for agriLM) an ANOVA table.
Usage
model_summary(model, anova = TRUE)
Arguments
model |
An |
anova |
Logical. Include ANOVA table for |
Value
Invisibly returns the model object (called for its side effect of
printing a formatted summary block containing coefficients,
goodness-of-fit statistics, and, for agriLM models fitted with
engine = "lm", an ANOVA table).
Find the optimal x value (e.g. economic optimum dose)
Description
For quadratic and linear-plateau models, computes the x value that maximises the predicted response.
Usage
optimum_dose(nls_fit)
Arguments
nls_fit |
An |
Value
A named numeric vector with x_opt (optimum x) and y_max
(predicted maximum y).
Flag outliers using multiple methods
Description
A standalone outlier-detection function supporting IQR, Z-score, and modified Z-score (Iglewicz-Hoaglin) methods.
Usage
outlier_flag(df, col, method = c("iqr", "zscore", "modz"), threshold = NULL)
Arguments
df |
A data frame. |
col |
Column name to test. |
method |
One of |
threshold |
Numeric threshold: for |
Value
df with a logical column <col>_outlier.
Plot a fitted dose-response curve
Description
Plot a fitted dose-response curve
Usage
## S3 method for class 'agriDRC'
plot(x, log_dose = TRUE, n_points = 200, ...)
Arguments
x |
An |
log_dose |
Logical. Plot dose on a log10 scale (default |
n_points |
Integer. Points on the smooth curve (default 200). |
... |
Ignored. |
Value
A ggplot object (printed invisibly).
Plot diagnostics for an agriLM model
Description
Plot diagnostics for an agriLM model
Usage
## S3 method for class 'agriLM'
plot(x, type = c("fit", "residuals", "qq", "scale"), ...)
Arguments
x |
An |
type |
One of |
... |
Ignored. |
Value
A ggplot object (printed invisibly).
Plot a fitted nonlinear curve over observed data
Description
Plot a fitted nonlinear curve over observed data
Usage
## S3 method for class 'agriNLS'
plot(x, n_points = 300, show_residuals = FALSE, ...)
Arguments
x |
An |
n_points |
Integer. Number of points on the fitted curve (default 300). |
show_residuals |
Logical. When |
... |
Ignored. |
Value
A ggplot object (printed invisibly).
A convenience wrapper for ggplot2 visualisation of model fit
Description
Dispatches to plot.agriLM, plot.agriNLS, or plot.agriDRC depending
on model class. Useful in pipeline contexts.
Usage
plot_fit(model, ...)
Arguments
model |
An |
... |
Arguments passed to the underlying plot method. |
Value
A ggplot object, returned invisibly. Called primarily for the
side effect of printing the plot. The exact structure of the returned
object depends on the class of model: plot.agriLM, plot.agriNLS,
or plot.agriDRC is dispatched accordingly.
Residual diagnostic plot panel
Description
Generates a 2×2 panel of residual diagnostics: residuals vs fitted, normal Q-Q, scale-location, and a histogram of residuals.
Usage
residual_check(model, ncol = 2)
Arguments
model |
An |
ncol |
Integer. Number of plot columns (default |
Value
Invisibly returns the list of four ggplot objects.
Simulated wheat nitrogen-response trial
Description
A simulated dataset from a randomised complete block design (RCBD) testing four nitrogen rates across three blocks and two phosphorus levels.
Format
A data frame with 72 rows and 6 variables:
- block
Block identifier (
A,B,C).- nitrogen
Nitrogen application rate (kg/ha).
- phosphorus
Phosphorus level (
low,high).- variety
Crop variety (
V1,V2).- yield
Grain yield (t/ha).
- biomass
Above-ground dry biomass (t/ha).
Source
Simulated for package demonstration.
Normalise yield values within groups
Description
Z-score or min-max normalises a numeric column, optionally within groups (e.g. per trial site or year).
Usage
yield_normalize(
df,
yield_col = "yield",
method = c("zscore", "minmax"),
group_by = NULL
)
Arguments
df |
A data frame. |
yield_col |
Character. Column to normalise. |
method |
|
group_by |
Character vector of grouping column names, or |
Value
df with a new column <yield_col>_norm.
Examples
df <- data.frame(yield = c(3, 4, 5, 6), site = c("A","A","B","B"))
yield_normalize(df, group_by = "site")