Evolving notes, images and sounds by Luis Apiolaza

Bivariate linear mixed models using ASReml-R with multiple cores

A while ago I wanted to run a quantitative genetic analysis where the performance of genotypes in each site was considered as a different trait. If you think about it, with 70 sites and thousands of genotypes one is trying to fit a 70×70 additive genetic covariance matrix, which requires 70*69/2 = 2,415 covariance components. Besides requiring huge amounts of memory and being subject to all sort of estimation problems there were all sort of connectedness issues that precluded the use of Factor Analytic models to model the covariance matrix. The best next thing was to run over 2,000 bivariate analyses to build a large genetic correlation matrix (which has all sort of issues, I know). This meant leaving the computer running for over a week.

In another unrelated post, Kevin asked if I have ever considered using ASReml-R to run in parallel using a computer with multiple cores. Short answer, I haven’t, but there is always a first time.

library(asreml) # Multivariate mixed models
library(doMC)   # To access multiple cores

registerDoMC()  # to start a "parallel backend"

# Read basic density data
# Phenotypes
dat <- read.table('density09.txt', header = TRUE)

dat <- within(dat, {
  FCLN <- factor(FCLN)
  MCLN <- factor(MCLN)
  REP <- factor(REP)
  SETS <- factor(SETS)
  FAM <- factor(FAM)
  CP <- factor(CP)
  Tree_id <- factor(Tree_id)


# Pedigree
ped <- read.table('pedigree.txt', header = TRUE)
names(ped)[1] <- 'Tree_id'
ped <- within(ped, {
  Tree_id <- factor(Tree_id)
  Mother <- factor(Mother)
  Father <- factor(Father)

# Inverse of the numerator relationship matrix
Ainv <- asreml.Ainverse(ped)$ginv

# Wrap call to a generic bivariate analysis
# (This one uses the same model for all sites
#  but it doesn't need to be so)
bivar <- function(trial1, trial2) {
  t2 <- dat[dat$Trial_id == trial1 | dat$Trial_id == trial2,]
  t2$den1 <- ifelse(t2$Trial_id == trial1, t2$DEN, NA)
  t2$den2 <- ifelse(t2$Trial_id == trial2, t2$DEN, NA)
  t2$Trial_id <- t2$Trial_id[drop = TRUE]

  # Bivariate run
  # Fits overall mean, random correlated additive effects with
  # heterogeneous variances and diagonal matrix for heterogeneous
  # residuals
  den.bi <-  asreml(cbind(den1,den2) ~ trait,
                    random = ~ ped(Tree_id):corgh(trait),
                    data = t2,
                    ginverse = list(Tree_id = Ainv),
                    rcov = ~ units:diag(trait),
                    workspace = 64e06, maxiter=30)

# Returns only log-Likelihood for this example

# Run the bivariate example in parallel for two pairs of sites
# FR38_1 with FR38_2, FR38_1 with FR38_3
foreach(trial1=rep("FR38_1",2), trial2=c("FR38_2", "FR38_3")) %dopar%
bivar(trial1, trial2)

The code runs in my laptop (only two cores) but I still have to test its performance in my desktop (4 cores) and see if it really makes a difference in time versus running the analyses sequentially. Initially my mistake was using the multicore package (require(multicore)) directly, which doesn't start the 'parallel backend' and ran sequentially. Using require(doMC) loads multicore but takes care of starting the 'parallel backend'.

Gratuitous picture: Trees at 8 pm illuminated by bonfire and full moon.

P.S. Thanks to Etienne Laliberté, who sent me an email in early 2010 pointing out that I had to use doMC. One of the few exceptions in the pile of useless emails I have archived.
P.S.2. If you want to learn more about this type of models I recommend two books: Mrode's Linear Models for the Prediction of Animal Breeding Values, which covers multivariate evaluation with lots of gory details, and Lynch and Walsh's Genetics and Analysis of Quantitative Traits, which is the closest thing to the bible in quantitative genetics.
P.S.3. 2012-05-08. I did run the code in my desktop (iMac with 4 cores), making a couple of small changes in the code to have a fairer comparison between using %dopar% (for parallel code) and %par% (for sequential execution).

  1. Added trace = FALSE to the asreml() call, to avoid printing partial iteration details to the screen, which happened when using %do%.
  2. Used 4 pairs of sites, so I could utilize all cores in my desktop.

Execution time for the parallel version—measured with Sys.time()—was, roughly, 1/(number of cores) the time required by the sequential version.

1 Comment

  1. Hewan

    I learned much from this blog. Actually I was able to work on the example. However I failed how to get the predicted values? And How can I present it in the form of graph. I am sorry for bothering with my silly questions.

© 2024 Palimpsest

Theme by Anders NorenUp ↑