fit_custom: Fit Custom Nonlinear Model

View source: R/fit_custom.R

fit_customR Documentation

Fit Custom Nonlinear Model

Description

Fits a user-defined nonlinear model to each bottle in a rumen_gp dataset.

Usage

fit_custom(
  data,
  formula,
  start,
  lower = NULL,
  upper = NULL,
  model_name = "Custom"
)

Arguments

data

A rumen_gp object.

formula

A nonlinear model formula.

start

Named list of starting values.

lower

Optional named numeric vector of lower parameter bounds.

upper

Optional named numeric vector of upper parameter bounds.

model_name

Character string used to label the fitted model.

Details

Overview

This function allows researchers to fit custom nonlinear kinetic equations using minpack.lm::nlsLM().

Custom models integrate directly with:

  • summary()

  • plot_fit()

  • plot_residuals()

  • compare_models()

making them fully compatible with the rumenGP modeling framework.

Formula Requirements

The model formula must:

  • Use Gas_mL as the response variable

  • Use Time_h as the time variable

  • Include all parameters listed in start

Example:

Gas_mL ~
  A *
  (
    Time_h /
    (
      Time_h + K
    )
  )

Starting Values

Starting values are supplied through start.

Example:

start = list(
  A = 150,
  K = 10
)

Good starting values often improve convergence and reduce fitting failures.

Parameter Bounds

Optional lower and upper bounds may be supplied.

Example:

lower = c(
  A = 0,
  K = 0
)

upper = c(
  A = 500,
  K = 100
)

Bounds can improve stability and prevent biologically unrealistic parameter estimates.

Best Practices

  • Start with biologically meaningful equations

  • Use reasonable starting values

  • Apply parameter bounds when appropriate

  • Compare custom models against built-in models

  • Evaluate both fit quality and parameter interpretability

Value

A custom_fit object containing:

  • Model name

  • Formula

  • Starting values

  • Parameter bounds

  • Parameter estimates

  • Model diagnostics

  • Predicted values

  • Residuals

See Also

fit_groot, fit_mm, compare_models, plot_fit, plot_residuals

Examples



files <- example_data()

raw_data <- read_ankom(
  files$ankom
)

metadata <- read_metadata(
  files$metadata
)

gp <- process_ankom(
  raw_data,
  metadata,
  headspace_ml = 210,
  temperature_c = 39
)

# Hyperbolic model
custom_fit <- fit_custom(

  data = gp,

  formula =
    Gas_mL ~
      A *
      (
        Time_h /
        (
          Time_h + K
        )
      ),

  start = list(
    A = 150,
    K = 10
  ),

  lower = c(
    A = 0,
    K = 0
  ),

  model_name = "Hyperbolic"

)

summary(custom_fit)

plot_fit(
  custom_fit,
  head = 1
)

plot_residuals(
  custom_fit,
  head = 1
)

# Compare with built-in models
compare_models(
  Groot = fit_groot(gp),
  Hyperbolic = custom_fit
)




rumenGP documentation built on Oct. 2, 2026, 5:09 p.m.