Skip to contents

Automatically detects violations in fitted experimental models (non-normality, heteroscedasticity, boundary singularities, or overdispersion) and executes optimal guided remedies: Box-Cox power transformations, variance heterogeneity modeling (weights = varIdent()), family transitions to GLMM (Gamma, Negative Binomial, Beta), or non-parametric alternatives (Kruskal-Wallis / Friedman).

Usage

remedy(
  fit,
  method = c("auto", "boxcox", "log", "sqrt", "varIdent", "glmm_gamma", "glmm_nbinom",
    "nonparametric"),
  lambda = NULL,
  ...
)

Arguments

fit

An object of class `"agri_fitted_model"` returned by fit_experiment.

method

Character string specifying remedy method: `"auto"` (default: chooses optimal remedy based on violation audit), `"boxcox"` (optimal Box-Cox power transformation), `"log"` (natural logarithm transformation), `"sqrt"` (square root transformation), `"varIdent"` (heterogeneous variance modeling per treatment group via nlme), `"glmm_gamma"` (transition to Gamma GLMM with log link), `"glmm_nbinom"` (transition to Negative Binomial GLMM for overdispersed counts), or `"nonparametric"` (Kruskal-Wallis or Friedman rank-sum test).

lambda

Optional numeric value for Box-Cox power parameter. If `NULL` and `method = "boxcox"`, it is estimated via maximum profile log-likelihood.

...

Additional arguments passed to refitting functions.

Value

An S3 object of class `"agri_remedy"` containing:

original_fit

Original fitted model object.

remedied_fit

New fitted model object with remedy applied.

remedy_type

Description of the applied remedy.

comparison_table

Side-by-side comparison of AIC, BIC, LogLik, and assumption p-values.

lrt_test

Likelihood Ratio Test comparison (if applicable).

boxcox_details

Details on optimal lambda estimation (if Box-Cox is used).

verdict

Summary evaluation of remediation efficacy.

Examples

data(wheat_splitplot, package = "agriDesignR")
fit <- fit_experiment(
  data = wheat_splitplot,
  response = "grain_yield",
  main_plot = "temperature",
  sub_plot = "genotype",
  block = "block"
)
rem <- remedy(fit, method = "boxcox")
print(rem)
#> 
#> ======================================================================
#>   agriDesignR: Statistical Remedy & Assumption Resolution
#> ======================================================================
#>   Remedy Applied  : Box-Cox Power Transformation (lambda = 1.9)
#> 
#> 
#> Before vs. After Model Comparison
#> ----------------------------------------------------------------------
#>                          Metric                       Original_Model
#>                   Model Formula grain_yield ~ temperature * genotype
#>                 Engine / Family                      lme4 (gaussian)
#>                             AIC                               188.09
#>                             BIC                               210.04
#>  Residual Normality (Shapiro p)                               0.6964
#>        Variance Ratio (max/min)                                21.36
#>           Overall Health Status             ACCEPTABLE_WITH_CAUTIONS
#>                              Remedied_Model
#>  grain_yield_trans ~ temperature * genotype
#>                             lme4 (gaussian)
#>                                      397.53
#>                                      419.49
#>                                      0.5648
#>                                       13.78
#>                    ACCEPTABLE_WITH_CAUTIONS
#> 
#>   Remedy Verdict  : [OK] REMEDY SUBSTANTIALLY IMPROVED: Residual metrics improved significantly and are now well within acceptable limits for robust inference.
#> 
#> ======================================================================
#>   Tip: Use 'remedy_fit$remedied_fit' to extract the corrected model for post-hoc analysis.
#>