| Title: | Lactation Curve Model Fitting for Dairy Animals |
|---|---|
| Description: | Fits up to 20 nonlinear lactation curve models to dairy animal milk yield data. Models fitted include exponential, polynomial, mixed logarithmic, inverse polynomial, and sigmoid families published between 1923 and 2000. Supports batch processing of multiple animals from a single CSV file, with flexible selection of animals and models. Produces per-animal parameter tables, goodness-of-fit metrics including R-squared (R2), Root Mean Square Error (RMSE), Akaike Information Criterion (AIC), Bayesian Information Criterion (BIC), and a serial autocorrelation statistic, 15 diagnostic figures, and combined cross-animal comparison outputs. References: <doi:10.1085/jgp.5.4.441>, <doi:10.1038/216164a0>, <doi:10.1016/0301-6226(87)90003-0>, <doi:10.4141/cjas87-067>, <doi:10.3168/jds.S0022-0302(00)75136-8>. |
| Authors: | Raja TV [aut, cre], Lalremruati PC [aut], Priyadharshini P [aut], Nidhishree NS [aut], Dheeraj Gurjar [aut], Rani Alex [aut], Vikas Vohra [aut] |
| Maintainer: | Raja TV <[email protected]> |
| License: | GPL-3 |
| Version: | 0.1.5 |
| Built: | 2026-06-04 11:12:44 UTC |
| Source: | https://github.com/cran/LactCurveModels |
Fits up to 20 nonlinear lactation curve models to daily milk yield data from one or more dairy animals. For each animal the package produces:
Four CSV tables (parameter estimates, actual vs predicted values, residuals, and a goodness-of-fit summary).
Fifteen diagnostic figures (model overlays, ranked panels, residual diagnostics, bubble charts, bar charts for R-squared (R2), Root Mean Square Error (RMSE), Akaike Information Criterion (AIC), Bayesian Information Criterion (BIC), Durbin-Watson (DW) statistic, correlation heatmap, and peak-yield charts).
When multiple animals are analysed, three additional combined cross-animal outputs are produced: an R2 comparison figure, an RMSE comparison figure, and a best-model overlay figure, plus combined CSV tables.
Note: out_dir must always be supplied — choose any folder
on your system where you want results saved.
library(LactCurveModels)
# All animals, all 20 models
results <- run_lactation_analysis(
input_csv_path = "path/to/mydata.csv",
out_dir = "path/to/results"
)
# Selected animals and models
results <- run_lactation_analysis(
input_csv_path = "path/to/mydata.csv",
selected_models = c("Wood_1967", "Wilmink_k005", "Ali_Schaeffer"),
selected_animals = c("Animal_A", "Animal_B"),
out_dir = "path/to/results"
)
The CSV file must contain exactly three columns:
animal_idCharacter identifier for each animal.
dimInteger fortnightly time index (1 to 20).
dmyNumeric daily milk yield (kg/day).
Maintainer: Raja TV [email protected]
Authors:
Lalremruati PC [email protected]
Priyadharshini P [email protected]
Nidhishree NS [email protected]
Dheeraj Gurjar [email protected]
Rani Alex [email protected]
Vikas Vohra [email protected]
Useful links:
Report bugs at https://github.com/venkatesanraja/LactCurveModels/issues
Attempts to fit up to 20 nonlinear lactation curve models using
nls() with the "port" algorithm. Models that fail to
converge are silently skipped.
fit_lactation_models(data, selected_models = "all")fit_lactation_models(data, selected_models = "all")
data |
A data frame with columns |
selected_models |
Either |
A named list. Each element corresponds to one successfully fitted
model and contains: model (nls object), metrics (list of
fit statistics), predictions (fitted values), std_errors
(parameter SEs).
# Minimal toy example (auto-tested) toy <- data.frame( x = 1:10, y = c(8, 11, 13, 12, 11, 10, 9, 8, 7, 6), z = (1:10) / 365 ) fits <- fit_lactation_models(toy, selected_models = "Wood_1967") # Full run with all 20 models (takes longer) fits_all <- fit_lactation_models(toy) # Selected models only fits_sub <- fit_lactation_models(toy, selected_models = c("Wood_1967", "Wilmink_k005"))# Minimal toy example (auto-tested) toy <- data.frame( x = 1:10, y = c(8, 11, 13, 12, 11, 10, 9, 8, 7, 6), z = (1:10) / 365 ) fits <- fit_lactation_models(toy, selected_models = "Wood_1967") # Full run with all 20 models (takes longer) fits_all <- fit_lactation_models(toy) # Selected models only fits_sub <- fit_lactation_models(toy, selected_models = c("Wood_1967", "Wilmink_k005"))
Reads a CSV file containing milk yield records, fits up to 20 nonlinear lactation curve models for each animal (or a selected subset of animals and/or models), saves per-animal diagnostic figures and CSV tables, and produces combined cross-animal summary outputs.
run_lactation_analysis( input_csv_path, selected_models = "all", selected_animals = "all", out_dir )run_lactation_analysis( input_csv_path, selected_models = "all", selected_animals = "all", out_dir )
input_csv_path |
Character. Full path to the input CSV file. The file must contain exactly these three columns (any column order):
|
selected_models |
Either |
selected_animals |
Either |
out_dir |
Character. Base output directory. A sub-folder is created
for each animal. Must be supplied explicitly — there is no default.
The directory is created automatically if it does not already exist.
Example: |
A named list (one element per animal) each containing:
Animal ID string.
Named list of fitted model objects and metrics.
Parameter estimates data frame.
Actual vs predicted data frame.
Residuals data frame.
Per-model goodness-of-fit metrics, sorted by R2.
Wide summary metrics data frame.
Returned invisibly so large objects are not auto-printed.
# Build a small example CSV in a temporary directory tmp_csv <- file.path(tempdir(), "example_animals.csv") example_data <- data.frame( animal_id = rep(c("Animal_A", "Animal_B"), each = 20), dim = rep(1:20, times = 2), dmy = c( 8.1, 11.2, 13.0, 12.5, 11.8, 11.0, 10.4, 9.8, 9.3, 8.8, 8.3, 7.9, 7.5, 7.1, 6.8, 6.5, 6.2, 5.9, 5.7, 5.4, 7.5, 10.1, 11.8, 11.3, 10.7, 10.0, 9.4, 8.9, 8.4, 7.9, 7.5, 7.1, 6.8, 6.4, 6.1, 5.8, 5.6, 5.3, 5.1, 4.9 ) ) write.csv(example_data, tmp_csv, row.names = FALSE) # Analyse all animals with two selected models results <- run_lactation_analysis( input_csv_path = tmp_csv, selected_models = c("Wood_1967", "Wilmink_k005"), out_dir = tempdir() ) # Analyse one animal only results2 <- run_lactation_analysis( input_csv_path = tmp_csv, selected_models = c("Wood_1967", "Ali_Schaeffer"), selected_animals = "Animal_A", out_dir = tempdir() )# Build a small example CSV in a temporary directory tmp_csv <- file.path(tempdir(), "example_animals.csv") example_data <- data.frame( animal_id = rep(c("Animal_A", "Animal_B"), each = 20), dim = rep(1:20, times = 2), dmy = c( 8.1, 11.2, 13.0, 12.5, 11.8, 11.0, 10.4, 9.8, 9.3, 8.8, 8.3, 7.9, 7.5, 7.1, 6.8, 6.5, 6.2, 5.9, 5.7, 5.4, 7.5, 10.1, 11.8, 11.3, 10.7, 10.0, 9.4, 8.9, 8.4, 7.9, 7.5, 7.1, 6.8, 6.4, 6.1, 5.8, 5.6, 5.3, 5.1, 4.9 ) ) write.csv(example_data, tmp_csv, row.names = FALSE) # Analyse all animals with two selected models results <- run_lactation_analysis( input_csv_path = tmp_csv, selected_models = c("Wood_1967", "Wilmink_k005"), out_dir = tempdir() ) # Analyse one animal only results2 <- run_lactation_analysis( input_csv_path = tmp_csv, selected_models = c("Wood_1967", "Ali_Schaeffer"), selected_animals = "Animal_A", out_dir = tempdir() )