Fitting lactation curves/functions in R

September 3, 2009

(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 repository
tmp <- c("",

## Install the package
install.packages("animSci", contriburl=tmp, dep=TRUE)

## Load the package

## Run the lactation curve examples

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). offers daily e-mail updates about R news and tutorials on topics such as: Data science, Big Data, R jobs, visualization (ggplot2, Boxplots, maps, animation), programming (RStudio, Sweave, LaTeX, SQL, Eclipse, git, hadoop, Web Scraping) statistics (regression, PCA, time series, trading) and more...

If you got this far, why not subscribe for updates from the site? Choose your flavor: e-mail, twitter, RSS, or facebook...

Comments are closed.

Search R-bloggers


Never miss an update!
Subscribe to R-bloggers to receive
e-mails with the latest R posts.
(You will not see this message again.)

Click here to close (This popup will not appear again)