The Wild Animal Modeling Wiki
Repeated measures
Summary
When you have repeated measures, for example across individuals or years, models have to account for the fact that observations are not independent with respect to these variables. The addition of a random effect controls for this and allows the amount of variance caused by this effect to be determined.
This section contains quick-reference code for repeated measures, or longitudinal, analysis. Repeated measures are covered more thoroughly in tutorial in the ecologists' guide to the animal model
Table of contents
Sample Code
ASReml
With repeated measures on individuals it may be of interest to esimate the amount of variance explained by among-individual differences. As a proportion of phenotypic variance this value is the repeatability and sets an upper limit for heritability.ASReml analysis of size with repeated measures ANIMAL !P SIZE SEX !A pedigreedata.ped !skip 1 phenotypicdata.dat !skip 1 !dopart 1 !part 1 SIZE ~ mu !r ide(ANIMAL) !part 2 SIZE ~ mu !r ide(ANIMAL) ANIMAL !part 3 SIZE ~ mu !r ANIMAL ##This will give biased reults
Here the use of "ide" causes pedigree associations to be disregarded. So part 1 would result in a partition of variance into among individual (associated with ide(ANIMAL)) and within individual components. These could be used to calculate repeatability in a suitable .pin file (see "Calculating heritability" section).
Part 2 is an animal model (i.e. using the pedigree) and will partition among-individual variance into components arising from (additive) genetic effects, and from non(additive) genetic effects (i.e so-called permanent environment effects). Heritbility can then be calculated. Note that the permanent environment effect must be included for animal models when there are repeated measures. Failure to do so (as in Part 3) will result in upward bias of VA and heritability.
ASReml-R
Estimating repeatability
With repeated measures on individuals it is often of interest, prior to fitting a genetic model, to see how repeatable a trait is. We can estimate the repeatability of a trait as the proportion of phenotypic variance explained by individual identity using the code below and fitting ide() of the animal, rather than ped which links to the pedigree.model1<-asreml(fixed=SIZE~ 1 , random= ~ide(ANIMAL,var=T,init=1) , data=phenotypicdata ,ginverse=list(ANIMAL=pedigreedata_inverse) , na.method.X="omit", na.method.Y="omit")
Partitioning additive and permanent environment effects
While additive genetic effects will cause among-individual variation, so will other types of effect. Therefore we generally expect that the repeatability will set the upper limit for heritability. Non-additive contributions to fixed among-individual differences are normally referred to as “permanent environment effects”. If a trait has repeated measures then it is necessary to model permanent environment effects in an animal model to prevent upward bias in VA.To obtain an unbiased estimate of VA we have to fit ANIMAL twice, once with, and once without a pedigree attached. To do this fit both ide(ANIMAL) and ped(ANIMAL)
model1<-asreml(fixed=SIZE~ 1 , random= ~ide(ANIMAL,var=T,init=1)+ped(ANIMAL,var=T,init=1) , data=phenotypicdata ,ginverse=list(ANIMAL=pedigreedata_inverse) , na.method.X="omit", na.method.Y="omit")
MCMCglmm
Obtaining the data is very similar to the process for the univariate introductory case.
# obtain the data - note that the location of the data on your computer is up to you, # and the details of specifying its location will vary among operating systems DataRM<-as.data.frame(read.table(file="~/Desktop/JAE_MCMCglmm/gryphonRM.txt",header names(DataRM)[1]<-"animal" # make sure that R understands which parts of the data are intended as factors vs. # continuous numeric data: DataRM$animal<-as.factor(DataRM$animal) DataRM$BYEAR<-as.factor(DataRM$BYEAR) DataRM$AGE<-as.factor(DataRM$AGE) DataRM$YEAR<-as.factor(DataRM$YEAR) DataRM$LAYDATE<-as.numeric(DataRM$LAYDATE) head(DataRM) DataRM$ID<-DataRM$animal head(DataRM)
The first step in an analysis of repeated measures data will often be to determine whether or not the data are repeatable at the level of individuals. This is a matter of fitting an individual effect that is not pedigree-associated.
# a simple set-up for the prior is to split the phenotypic variance p.var<-var(DataRM$LAYDATE,na.rm=TRUE) prior3.1<-list(G=list(G1=list(V=matrix(p.var/2),n=1)), R=list(V=matrix(p.var/2),n=1)) # fit an effect of the individual, without pedigree information model3.1<-MCMCglmm(LAYDATE~1,random=~ID, data=DataRM,prior=prior3.1,verbose=FALSE) posterior.mode(model3.1$VCV) model3.2<-MCMCglmm(LAYDATE~AGE,random=~ID, data=DataRM,prior=prior3.1,verbose=FALSE) plot(model3.2$Sol) plot(model3.2$VCV)
This allows us to test whether or not any of the phenotypic variance is the result of consistent differences among individuals:
posterior.mode(model3.2$VCV)
Given that the trait is repeatable at the level of individuals. A common next step will be to determine whether or not any portion of this repeatability is due to genetic variation among individuals. One might naively, do this by associating the effect of the individual with the pedgiree information:
model3.3<-MCMCglmm(LAYDATE~1+AGE,random=~animal, pedigree=Ped,data=DataRM,prior=prior3.1,verbose=FALSE) posterior.mode(model3.3$VCV)
However, individuals may differ in consistent ways from one another due to the environment, perhaps due to long-lasting effects of environmental variation experienced during development, or due to consistent differences in habitat usage among individuals (assuming habitat choice is not genetically determined). So the model we just fit is probably a dangerous one, as it could conflate a such consistent non-genetic effects on individual phenotypes with genetic effects. The more sensible model is:
p.var<-var(DataRM$LAYDATE,na.rm=TRUE) prior3.4<-list(G=list(G1=list(V=matrix(p.var/3),n=1), G2=list(V=matrix(p.var/3),n=1)), R=list(V=matrix(p.var/3),n=1)) # here we won't risk the loading of consistent environmentally-induced # differences among individuals with genetic differences: model3.4<-MCMCglmm(LAYDATE~1+AGE,random=~animal+ID, pedigree=Ped,data=DataRM,prior=prior3.4,verbose=FALSE) posterior.mode(model3.4$VCV) model3.4.VP<-model3.4$VCV[,"animal"]+model3.4$VCV[,"ID"]+model3.4$VCV[,"units"] model3.4.IDplusVA<-model3.4$VCV[,"animal"]+model3.4$VCV[,"ID"] posterior.mode(model3.4.IDplusVA/model3.4.VP) posterior.mode(model3.4$VCV[,"animal"]/model3.4.VP)
As with any other model, additional fixed or random effects might need to be included, either because they are interesting in themselves, or because they could potentially inflate on of the effects of interest:
p.var<-var(DataRM$LAYDATE,na.rm=TRUE) # here we are just using weak priors where the # phenotypic variation is split among the various # factors: prior3.5<-list(G=list(G1=list(V=matrix(p.var/5),n=1), G2=list(V=matrix(p.var/5),n=1), G3=list(V=matrix(p.var/5),n=1), G4=list(V=matrix(p.var/5),n=1)), R=list(V=matrix(p.var/5),n=1)) model3.5<-MCMCglmm(LAYDATE~1+AGE, random=~animal+ID+YEAR+BYEAR, pedigree=Ped,data=DataRM, prior=prior3.5,verbose=FALSE) posterior.mode(model3.5$VCV)
WOMBAT
Estimating repeatability
With repeated measures on individuals it is often of interest, prior to fitting a genetic model, to see how repeatable a trait is. We can estimate the repeatability of a trait as the proportion of phenotypic variance explained by individual identity using the parameter file below.COMMENT WOMBAT analysis of Gryphon laydate ANALYSIS UNI # PEDS gryphon.ped DATA gryphonRM.dat ANIMAL 900 BYEAR 35 AGE 5 YEAR 35 LAYDATE END DATA MODEL RAN ANIMAL TRAIT LAYDATE END VAR residual 1 1.0 VAR ANIMAL 1 1.0
Whereas before we have associated the term
ANIMAL
with the pedigree file (by adding NRM
in the model specification), here we are interested in estimating the amount of variance explained by individual identity (rather than by additive effects). We do this by fitting ANIMAL
without specifying NRM
, and by removing or commenting out the section where we specify the pedigree file. The resulting partitioning of the phenotypic variance is seen in SumEstimates.out
.Partitioning additive and permanent environment effects
While additive genetic effects will cause among-individual variation, so will other types of effect. Therefore we generally expect that the repeatability will set the upper limit for heritability. Non-additive contributions to fixed among-individual differences are normally referred to as “permanent environment effects”. If a trait has repeated measures then it is necessary to model permanent environment effects in an animal model to prevent upward bias in VA.To obtain an unbiased estimate of VA we have to fit
ANIMAL
twice, once with, and once without a pedigree attached. In WOMBAT this requires the duplication of the ANIMAL
column in the datafile, which we will call PE
(for Permanent Environment). We have done this for you in the gryphonRM_pe.dat
datafile. The parameter file should now look like this:COMMENT WOMBAT analysis of Gryphon laying date ANALYSIS UNI PEDS gryphon.ped DATA gryphonRM_pe.dat ANIMAL 900 # A duplicate of ANIMAL PE 900 BYEAR 35 AGE 5 YEAR 35 LAYDATE END DATA MODEL RAN ANIMAL NRM RAN PE TRAIT LAYDATE END VAR residual 1 1.0 VAR ANIMAL 1 1.0 VAR PE 1 1.0