# Fitting lactation curves/functions in R

[This article was first published on

Want to share your content on R-bloggers? click here if you have a blog, or here if you don't.

**Gregor Gorjanc (gg)**, and kindly contributed to R-bloggers]. (You can report issue about the content on this page here)Want to share your content on R-bloggers? click here if you have a blog, or here if you don't.

It is quite some time ago since I wrote a set of lactation curves/functions in R. I put those functions in the animSci package. However, the package is in a mess for quite some time now – I was adding some new functions, but did not have time to finish the job properly. This is also the reason that package was not published. I got several inquries about the lactation functions. Therefore, I compiled the package and checked that lactation curves work as they should. The package is now published here with a warning.

The key fact to fit a particular lactation curve in R is to create a function that will take a numeric variable (days in milk) and create a design matrix that can be fed to model fitting function such as lm() or similar, e.g.,

someFunc <- function(x, ...) ... lm(y ~ someFunc(x))

It must be noted that this approach is not the most efficient for large datasets, since design matrix can be large, but this is a well known problem with using lm() in R. I implemented four lactation functions: Wood, Wilmink, Ali-Schaeffer, and Guo-Swalve, but others can be added. Bellow is the R code to run the lactation curves example on a data set of four milk traits in goats and the resulting figure. It would be interesting to compare this functions with Legendre polynomials (see here) and splines.

## Add my repository tmp <- c("http://gregor.gorjanc.googlepages.com", contrib.url(repos=getOption("repos"), type=getOption("pkgType"))) ## Install the package install.packages("animSci", contriburl=tmp, dep=TRUE) ## Load the package library(package="animSci") ## Run the lactation curve examples example(topic="lactCurves")

P.S. I got a nice cover up from David Smith.

Some lactation curves for milk traits in goat

To

**leave a comment**for the author, please follow the link and comment on their blog:**Gregor Gorjanc (gg)**.R-bloggers.com offers

**daily e-mail updates**about R news and tutorials about learning R and many other topics. Click here if you're looking to post or find an R/data-science job.

Want to share your content on R-bloggers? click here if you have a blog, or here if you don't.