# Fitting lactation curves/functions in R

September 3, 2009
By

(This article was first published on Gregor Gorjanc (gg), and kindly contributed to R-bloggers)

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 repositorytmp <- c("http://gregor.gorjanc.googlepages.com",                    contrib.url(repos=getOption("repos"),         type=getOption("pkgType")))## Install the packageinstall.packages("animSci", contriburl=tmp, dep=TRUE)## Load the packagelibrary(package="animSci")## Run the lactation curve examplesexample(topic="lactCurves")

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

Some lactation curves for milk traits in goat