You are on page 1of 6

Single trait animal model by using R

By: Edgar Quispe and Gustavo Gutirrez


Example: Taking from Chapter 3.1 of R. A. Mrode, Linear Models for the
Prediction of Animal Breeding Values, CABI Publishing, 1996.
Table 1. Data set for pre-weaning gain (WWG)

Animal

Sex

Sire

Dam

WWG(kg)

4
5
6
7
8

Male
Female
Female
Male
Male

1
3
1
4
3

Unknown
2
2
5
6

4.5
2.9
3.9
3.5
5.0

Step 1. Loading R libraries


library(pedigree)
By defect will be charged
package 'pedigree' was built under R version 2.13.2
package 'Matrix' was built under R version 2.13.2
package 'lattice' was built under R version 2.13.2
package 'HaploSim' was built under R version 2.13.2
package 'reshape' was built under R version 2.13.2
package 'plyr' was built under R version 2.13.2
Step 2. Setting the incidence matrix X and Z, the response vector Y, and the
inverse of the numerator relationship matrix.
X Fixed Effects Matrix (sex)
X <- matrix(c(1,0,0,1,1,0,1,1,0,0),5,2)
X
[,1] [,2]
[1,]
1
0
[2,]
0
1
[3,]
0
1
[4,]
1
0
[5,]
1
0
Z Incidence Matrix
Z <- matrix(c(rep(0,15),1,rep(0,5),1,rep(0,5),1,rep(0,5),1,rep(0,5),1),5,8)

Z
[1,]
[2,]
[3,]
[4,]
[5,]

[,1] [,2] [,3] [,4] [,5] [,6] [,7] [,8]


0
0
0
1
0
0
0
0
0
0
0
0
1
0
0
0
0
0
0
0
0
1
0
0
0
0
0
0
0
0
1
0
0
0
0
0
0
0
0
1

Y <- matrix(c(4.5,2.9,3.9,3.5,5),5,1)
Y
[,1]
[1,] 4.5
[2,] 2.9
[3,] 3.9
[4,] 3.5
[5,] 5.0
The inverse of the numerator relationship matrix will be calculated using
package `pedigree` according.
animal <- c(1:8)
dam<-c(0,0,0,0,2,2,5,6)
sire<-c(0,0,0,1,3,1,4,3)
ped<-data.frame(animal,dam,sire)
ped
animal dam sire
1
1
0
0
2
2
0
0
3
3
0
0
4
4
0
1
5
5
2
3
6
6
2
1
7
7
5
4
8
8
6
3
> makeAinv(ped)
[1] TRUE
> Ai<-read.table("Ainv.txt")
> Ai
V1 V2
V3
1
1 1 1.833330
2
2 1 0.500000
3
2 2 2.000000
4
3 2 0.500000
5
3 3 2.000000
6
4 1 -0.666667
7
4 4 1.833330
8
5 2 -1.000000
9
5 3 -1.000000
10 5 4 0.500000
11 5 5 2.500000
12 6 1 -1.000000
13 6 2 -1.000000
14 6 3 0.500000
15 6 6 2.500000
16 7 4 -1.000000
17 7 5 -1.000000
18 7 7 2.000000

19
20
21

8
8
8

3 -1.000000
6 -1.000000
8 2.000000

Fulled:

1 1.833 0.5
2
0.5
2
3
0 0.5
4 -0.667
0
5
0 -1
6
-1 -1
7
0
0
8
0
0

0
0.5
2
0
-1
0.5
0
-1

4
0.667
0
0
1.833
0.5
0
-1
0

0
-1
-1
0.5
2.5
0
-1
0

-1
-1
0.5
0
0
2.5
0
-1

0
0
0
-1
-1
0
2
0

0
0
-1
0
0
-1
0
2

Then data in R:
AINV<- matrix(c(1.833,0.5,0,-0.667,0,-1,0,0,
0.5,2,0.5,0,-1,-1,0,0,
0,0.5,2,0,-1,0.5,0,-1,
-0.667,0,0,1.833,.5,0,-1,0,
0,-1,-1,0.5,2.5,0,-1,0,
-1,-1,0.5,0,0,2.5,0,-1,
0,0,0,-1,-1,0,2,0,
0,0,-1,0,0,-1,0,2),8,8)
AINV

[,1] [,2] [,3]


[,4] [,5] [,6] [,7] [,8]
[1,] 1.833 0.5 0.0 -0.667 0.0 -1.0
0
0
[2,] 0.500 2.0 0.5 0.000 -1.0 -1.0
0
0
[3,] 0.000 0.5 2.0 0.000 -1.0 0.5
0
-1
[4,] -0.667 0.0 0.0 1.833 0.5 0.0
-1
0
[5,] 0.000 -1.0 -1.0 0.500 2.5 0.0
-1
0
[6,] -1.000 -1.0 0.5 0.000 0.0 2.5
0
-1
[7,] 0.000 0.0 0.0 -1.000 -1.0 0.0
2
0
[8,] 0.000 0.0 -1.0 0.000 0.0 -1.0
0
2
Step 3. Building the mixed model equations (MME)

X Z
X X
Z X Z Z A 1

b X y

u Z y

YP <- t(Y)
XP <- t(X)
ZP <- t(Z)
XPX <- XP%*%X
XPZ <- XP%*%Z
ZPX <- ZP%*%X
ZPZ <- ZP%*%Z
XPY <- XP%*%Y
ZPY <- ZP%*%Y
ALPHA=2
ZPZA <- ZPZ+(AINV*ALPHA)
CFX <- matrix(c(XPX,XPZ),2,10)

CFZ <- matrix(c(ZPX,ZPZA),8,10)


CF <- rbind(CFX,CFZ)
RHSY <- rbind(XPY,ZPY)
Step 4. Solving MME
Load MASS package
dam<-c(0,0,0,0,2,2,5,6)
SOLMME<- round(INVCF%*%RHSY,3)
SOLMME
[,1]
[1,] 4.358
[2,] 3.404
[3,] 0.098
[4,] -0.019
[5,] -0.041
[6,] -0.009
[7,] -0.186
[8,] 0.177
[9,] -0.249
[10,] 0.183
The solutions for sex are the first two rows (male=4.358, female=3.404), and
the following rows are breeding values for animal 1 to 8.
Step 5. Estimating accuracy and standard error of prediction (SEP)
C22<-diag(INVCF)[3:10]
C22
[1] 0.4714125 0.4920962 0.4564631 0.4279597 0.4281124 0.4423637 0.4419532
0.4224084
ACCURACY<- sqrt(1-C22*ALPHA)
round(ACCURACY,3)
[1] 0.239 0.126 0.295 0.380 0.379 0.340 0.341 0.394
SEP<- sqrt(C22*40)
round(SEP,3)
[1] 4.342 4.437 4.273 4.137 4.138 4.206 4.205 4.111
Table 2. Breeding value, accuracy, and standard error of prediction (SEP) for
animals
Animal
1
2
3
4
5
6
7
8

Breeding Value
0.098
-0.019
-0.041
-0.009
-0.186
0.177
-0.249
0.183

Accuracy
0.239
0.126
0.295
0.380
0.379
0.340
0.341
0.394

SEP
4.342
4.437
4.273
4.137
4.138
4.206
4.205
4.111

You might also like