설명은 다음 포스트 참조

2020/12/16 - [Animal Breeding/R for Genetic Evaluation] - Fixed Effect Model for SNP Effects, unweighted analysis

 

Fixed Effect Model for SNP Effects, unweighted analysis

# Linear Models for the Prediction of Animal Breeding Values, 3rd Edition. # Raphael Mrode # Example 11.1 p180 SNP effect를 고정효과로 모형에 넣어 SNP 추정하고 개체의 genotype을 이용..

bhpark.tistory.com

가중치를 EDC의 역수로 주었다.

Data

13 0 0 1 558 9 0.00179211 1.3571429 -0.3571429 0.2857143
14 0 0 1 722 13.4 0.00138504 0.3571429 -0.3571429 -0.7142857
15 13 4 1 300 12.7 0.00333333 0.3571429 0.6428571 1.2857143
16 15 2 1 73 15.4 0.01369863 -0.6428571 -0.3571429 1.2857143
17 15 5 1 52 5.9 0.01923077 -0.6428571 0.6428571 0.2857143
18 14 6 1 87 7.7 0.01149425 0.3571429 0.6428571 -0.7142857
19 14 9 1 64 10.2 0.01562500 -0.6428571 -0.3571429 0.2857143
20 14 9 1 103 4.8 0.00970874 -0.6428571 0.6428571 0.2857143

Pedigree

1 0 0
2 0 0
3 0 0
4 0 0
5 0 0
6 0 0
7 0 0
8 0 0
9 0 0
10 0 0
11 0 0
12 0 0
13 0 0
14 0 0
15 13 4
16 15 2
17 15 5
18 14 6
19 14 9
20 14 9
21 1 3
22 14 8
23 14 11
24 14 10
25 14 7
26 14 12

파라미터 파일

# Parameter file for program renf90; it is translated to parameter
# file for BLUPF90 family programs.
DATAFILE
fem_snp_data2.txt
TRAITS
6
FIELDS_PASSED TO OUTPUT
 
WEIGHT(S)
7 
RESIDUAL_VARIANCE
245
EFFECT
4 cross alpha
EFFECT
8 cov 
EFFECT
9 cov 
EFFECT
10 cov 
EFFECT
1 cross alpha
RANDOM
animal
FILE
fem_snp_pedi.txt
FILE_POS
1 2 3
PED_DEPTH
0
(CO)VARIANCES
35.241
OPTION solv_method FSPAK

Weight 로 7행을 주었다.

 

Renumf90 실행화면

 

Renumf90 실행 로그

 RENUMF90 version 1.145
 renumf90_fem_snp_w.par
 datafile:fem_snp_data2.txt
 traits:           6
 R
   245.0    

 Processing effect  1 of type cross     
 item_kind=alpha     

 Processing effect  2 of type cov       

 Processing effect  3 of type cov       

 Processing effect  4 of type cov       

 Processing effect  5 of type cross     
 item_kind=alpha     
 pedigree file name  "fem_snp_pedi.txt"
 positions of animal, sire, dam, alternate dam, yob, and group     1     2     3     0     0     0     0
 all pedigrees to be included
 Reading (CO)VARIANCES:           1 x           1

 Maximum size of character fields: 20

 Maximum size of record (max_string_readline): 800

 Maximum number of fields for input file (max_field_readline): 100

 Pedigree search method (ped_search): convention

 Order of pedigree animals (animal_order): default

 Order of UPG (upg_order): default

 Missing observation code (missing): 0

 hash tables for effects set up
 first 3 lines of the data file (up to 70 characters)
    13 0 0 1 558 9 0.00179211 1.3571429 -0.3571429 0.2857143
    14 0 0 1 722 13.4 0.00138504 0.3571429 -0.3571429 -0.7142857
    15 13 4 1 300 12.7 0.00333333 0.3571429 0.6428571 1.2857143
 read            8  records
 table with            1  elements sorted
 added count
 Effect group            1  of column            1  with            1  levels
 table expanded from        10000  to        10000  records
 added count
 Effect group            5  of column            1  with            8  levels
 wrote statistics in file "renf90.tables"

 Basic statistics for input data  (missing value code is '0')
 Pos  Min         Max         Mean        SD                 N
   6    4.8000      15.400      9.8875      3.7434           8
   8  -0.64286      1.3571    -0.17857E-01 0.74402           8
   9  -0.35714     0.64286     0.14286     0.53452           8
  10  -0.71429      1.2857     0.28571     0.75593           8

 Correlation matrix
        6     8     9    10
  6   1.00  0.12 -0.60  0.35
  8   0.12  1.00 -0.18 -0.25
  9  -0.60 -0.18  1.00  0.00
 10   0.35 -0.25  0.00  1.00

 Counts of nonzero values (order as above)
          8         8         8         8
          8         8         8         8
          8         8         8         8
          8         8         8         8

 random effect   5
 type:animal    
 opened output pedigree file "renadd05.ped"
 read           26  pedigree records
 loaded           18  parent(s) in round            0

 Pedigree checks
 
 Number of animals with records                  =            8
 Number of parents without records               =           18
 Total number of animals                         =           26

 Wrote parameter file "renf90.par"
 Wrote renumbered data "renf90.dat" 8 records

 

Renumf90 실행 결과 생성 파일

renf90.tables

 Effect group 1 of column 1 with 1 levels, effect # 1
 Value    #    consecutive number
1 8 1 

renf90.data

 9 0.00179211 1 1.3571429 -0.3571429 0.2857143 1
 13.4 0.00138504 1 0.3571429 -0.3571429 -0.7142857 3
 12.7 0.00333333 1 0.3571429 0.6428571 1.2857143 4
 15.4 0.01369863 1 -0.6428571 -0.3571429 1.2857143 5
 5.9 0.01923077 1 -0.6428571 0.6428571 0.2857143 6
 7.7 0.01149425 1 0.3571429 0.6428571 -0.7142857 8
 10.2 0.01562500 1 -0.6428571 -0.3571429 0.2857143 2
 4.8 0.00970874 1 -0.6428571 0.6428571 0.2857143 7

renadd05.ped

26 3 20 1 0 2 0 0 0 26
1 0 0 3 0 0 1 1 0 13
21 9 11 1 0 2 0 0 0 21
13 0 0 3 0 0 0 0 1 5
2 3 17 1 0 2 1 0 0 19
3 0 0 3 0 0 1 8 0 14
22 3 16 1 0 2 0 0 0 22
11 0 0 3 0 0 0 0 1 3
16 0 0 3 0 0 0 0 1 8
4 1 12 1 0 2 1 2 0 15
23 3 19 1 0 2 0 0 0 23
18 0 0 3 0 0 0 0 1 10
9 0 0 3 0 0 0 1 0 1
14 0 0 3 0 0 0 0 1 6
5 4 10 1 0 2 1 0 0 16
24 3 18 1 0 2 0 0 0 24
19 0 0 3 0 0 0 0 1 11
12 0 0 3 0 0 0 0 1 4
6 4 13 1 0 2 1 0 0 17
17 0 0 3 0 0 0 0 2 9
25 3 15 1 0 2 0 0 0 25
20 0 0 3 0 0 0 0 1 12
7 3 17 1 0 2 1 0 0 20
10 0 0 3 0 0 0 0 1 2
8 3 14 1 0 2 1 0 0 18
15 0 0 3 0 0 0 0 1 7

renf90.par

# BLUPF90 parameter file created by RENUMF90
DATAFILE
 renf90.dat
NUMBER_OF_TRAITS
           1
NUMBER_OF_EFFECTS
           5
OBSERVATION(S)
    1
WEIGHT(S)
           2
EFFECTS: POSITIONS_IN_DATAFILE NUMBER_OF_LEVELS TYPE_OF_EFFECT[EFFECT NESTED]
 3         1 cross 
 4 1 cov 
 5 1 cov 
 6 1 cov 
 7        26 cross 
RANDOM_RESIDUAL VALUES
   245.00    
 RANDOM_GROUP
     5
 RANDOM_TYPE
 add_animal   
 FILE
renadd05.ped                                                                    
(CO)VARIANCES
   35.241    
OPTION solv_method FSPAK

 

BLUPF90 실행 화면

 

blupf90 실행 로그

renf90.par
     BLUPF90 ver. 1.68

 Parameter file:             renf90.par
 Data file:                  renf90.dat
 Number of Traits             1
 Number of Effects            5
 Position of Observations      1
 Position of Weight (1)        2
 Value of Missing Trait/Observation           0

EFFECTS
 #  type                position (2)        levels   [positions for nested]
    1  cross-classified       3         1
    2  covariable             4         1
    3  covariable             5         1
    4  covariable             6         1
    5  cross-classified       7        26

 Residual (co)variance Matrix
  245.00    

 Random Effect(s)    5
 Type of Random Effect:      additive animal
 Pedigree File:              renadd05.ped                                                                                                                                                                                                                                              
 trait   effect    (CO)VARIANCES
  1       5     35.24    

 REMARKS
  (1) Weight position 0 means no weights utilized
  (2) Effect positions of 0 for some effects and traits means that such
      effects are missing for specified traits
 

 * The limited number of OpenMP threads = 4

 * solving method (default=PCG):FSPAK               
 
 Data record length =            7
 # equations =           30
 G
  35.241    
 read            8  records in   4.6875000E-02  s,                      50 
  nonzeroes
  read           26  additive pedigrees
 finished peds in   4.6875000E-02  s,                     103  nonzeroes
 solutions stored in file: "solutions"

 

blupf90 실행 결과 : solutions

trait/effect level  solution
   1   1         1         10.14413432
   1   2         1          2.65499583
   1   3         1         -4.64023468
   1   4         1          2.95113755
   1   5         1         -0.00120847
   1   5         2         -0.00172258
   1   5         3          0.00008052
   1   5         4          0.00038506
   1   5         5          0.00242208
   1   5         6         -0.00063088
   1   5         7         -0.00193976
   1   5         8          0.00214389
   1   5         9          0.00000000
   1   5        10          0.00148637
   1   5        11          0.00000000
   1   5        12          0.00065953
   1   5        13         -0.00054894
   1   5        14          0.00140242
   1   5        15          0.00000000
   1   5        16          0.00000000
   1   5        17         -0.00187143
   1   5        18          0.00000000
   1   5        19          0.00000000
   1   5        20          0.00000000
   1   5        21          0.00000000
   1   5        22          0.00004026
   1   5        23          0.00004026
   1   5        24          0.00004026
   1   5        25          0.00004026
   1   5        26          0.00004026

 

1번 효과인 mean effect의 값이 책과 다른데 책의 오타인 것으로 보인다.

 

# Linear Models for the Prediction of Animal Breeding Values, 3rd Edition.

# Raphael Mrode

# Example 11.1 p180

간단한 설명은 다음 참조

2020/12/16 - [Animal Breeding/R for Genetic Evaluation] - Fixed Effect Model for SNP Effects, unweighted analysis

 

Fixed Effect Model for SNP Effects, unweighted analysis

# Linear Models for the Prediction of Animal Breeding Values, 3rd Edition. # Raphael Mrode # Example 11.1 p180 SNP effect를 고정효과로 모형에 넣어 SNP 추정하고 개체의 genotype을 이용..

bhpark.tistory.com

 

Data

13 0 0 1 558 9 0.00179211 1.3571429 -0.3571429 0.2857143
14 0 0 1 722 13.4 0.00138504 0.3571429 -0.3571429 -0.7142857
15 13 4 1 300 12.7 0.00333333 0.3571429 0.6428571 1.2857143
16 15 2 1 73 15.4 0.01369863 -0.6428571 -0.3571429 1.2857143
17 15 5 1 52 5.9 0.01923077 -0.6428571 0.6428571 0.2857143
18 14 6 1 87 7.7 0.01149425 0.3571429 0.6428571 -0.7142857
19 14 9 1 64 10.2 0.01562500 -0.6428571 -0.3571429 0.2857143
20 14 9 1 103 4.8 0.00970874 -0.6428571 0.6428571 0.2857143

 

1 ~ 3 : animal, sire, dam

4 : general mean

5 : EDC(using weight)

6 : Fat DYD

7 : EDC 역수

8 - 10 : SNP1 ~ SNP3의 coding하고 평균을 0으로 scaling한 값

(7 - 10 컬럼은 원래의 자료에서 계산을 하여 입력하여야 한다.)

 

Pedigree

1 0 0
2 0 0
3 0 0
4 0 0
5 0 0
6 0 0
7 0 0
8 0 0
9 0 0
10 0 0
11 0 0
12 0 0
13 0 0
14 0 0
15 13 4
16 15 2
17 15 5
18 14 6
19 14 9
20 14 9
21 1 3
22 14 8
23 14 11
24 14 10
25 14 7
26 14 12

 

Renumf90 Parameter File

# Parameter file for program renf90; it is translated to parameter
# file for BLUPF90 family programs.
DATAFILE
fem_snp_data2.txt
TRAITS
6
FIELDS_PASSED TO OUTPUT
 
WEIGHT(S)
 
RESIDUAL_VARIANCE
245
EFFECT
4 cross alpha
EFFECT
8 cov 
EFFECT
9 cov 
EFFECT
10 cov 
EFFECT
1 cross alpha
RANDOM
animal
FILE
fem_snp_pedi.txt
FILE_POS
1 2 3
PED_DEPTH
0
(CO)VARIANCES
35.241
OPTION solv_method FSPAK

 

실행

명령창에서 다음과 같은 명령어로 renumf90을 실행한다.

renumf90 renumf90_fem_snp_uw.par | tee renumf90_fem_snp_uw_01.log

 

 

Renumf90 실행 로그

 RENUMF90 version 1.145
 renumf90_fem_snp_uw.par
 datafile:fem_snp_data2.txt
 traits:           6
 R
   245.0    

 Processing effect  1 of type cross     
 item_kind=alpha     

 Processing effect  2 of type cov       

 Processing effect  3 of type cov       

 Processing effect  4 of type cov       

 Processing effect  5 of type cross     
 item_kind=alpha     
 pedigree file name  "fem_snp_pedi.txt"
 positions of animal, sire, dam, alternate dam, yob, and group     1     2     3     0     0     0     0
 all pedigrees to be included
 Reading (CO)VARIANCES:           1 x           1

 Maximum size of character fields: 20

 Maximum size of record (max_string_readline): 800

 Maximum number of fields for input file (max_field_readline): 100

 Pedigree search method (ped_search): convention

 Order of pedigree animals (animal_order): default

 Order of UPG (upg_order): default

 Missing observation code (missing): 0

 hash tables for effects set up
 first 3 lines of the data file (up to 70 characters)
    13 0 0 1 558 9 0.00179211 1.3571429 -0.3571429 0.2857143
    14 0 0 1 722 13.4 0.00138504 0.3571429 -0.3571429 -0.7142857
    15 13 4 1 300 12.7 0.00333333 0.3571429 0.6428571 1.2857143
 read            8  records
 table with            1  elements sorted
 added count
 Effect group            1  of column            1  with            1  levels
 table expanded from        10000  to        10000  records
 added count
 Effect group            5  of column            1  with            8  levels
 wrote statistics in file "renf90.tables"

 Basic statistics for input data  (missing value code is '0')
 Pos  Min         Max         Mean        SD                 N
   6    4.8000      15.400      9.8875      3.7434           8
   8  -0.64286      1.3571    -0.17857E-01 0.74402           8
   9  -0.35714     0.64286     0.14286     0.53452           8
  10  -0.71429      1.2857     0.28571     0.75593           8

 Correlation matrix
        6     8     9    10
  6   1.00  0.12 -0.60  0.35
  8   0.12  1.00 -0.18 -0.25
  9  -0.60 -0.18  1.00  0.00
 10   0.35 -0.25  0.00  1.00

 Counts of nonzero values (order as above)
          8         8         8         8
          8         8         8         8
          8         8         8         8
          8         8         8         8

 random effect   5
 type:animal    
 opened output pedigree file "renadd05.ped"
 read           26  pedigree records
 loaded           18  parent(s) in round            0

 Pedigree checks
 
 Number of animals with records                  =            8
 Number of parents without records               =           18
 Total number of animals                         =           26

 Wrote parameter file "renf90.par"
 Wrote renumbered data "renf90.dat" 8 records

 

Renumf90 실형 결과 파일

renf90.tables

 Effect group 1 of column 1 with 1 levels, effect # 1
 Value    #    consecutive number
1 8 1 

 

renadd05.ped

26 3 20 1 0 2 0 0 0 26
1 0 0 3 0 0 1 1 0 13
21 9 11 1 0 2 0 0 0 21
13 0 0 3 0 0 0 0 1 5
2 3 17 1 0 2 1 0 0 19
3 0 0 3 0 0 1 8 0 14
22 3 16 1 0 2 0 0 0 22
11 0 0 3 0 0 0 0 1 3
16 0 0 3 0 0 0 0 1 8
4 1 12 1 0 2 1 2 0 15
23 3 19 1 0 2 0 0 0 23
18 0 0 3 0 0 0 0 1 10
9 0 0 3 0 0 0 1 0 1
14 0 0 3 0 0 0 0 1 6
5 4 10 1 0 2 1 0 0 16
24 3 18 1 0 2 0 0 0 24
19 0 0 3 0 0 0 0 1 11
12 0 0 3 0 0 0 0 1 4
6 4 13 1 0 2 1 0 0 17
17 0 0 3 0 0 0 0 2 9
25 3 15 1 0 2 0 0 0 25
20 0 0 3 0 0 0 0 1 12
7 3 17 1 0 2 1 0 0 20
10 0 0 3 0 0 0 0 1 2
8 3 14 1 0 2 1 0 0 18
15 0 0 3 0 0 0 0 1 7

설명은 이전 포스트 참조

 

renf90.dat

 9 1 1.3571429 -0.3571429 0.2857143 1
 13.4 1 0.3571429 -0.3571429 -0.7142857 3
 12.7 1 0.3571429 0.6428571 1.2857143 4
 15.4 1 -0.6428571 -0.3571429 1.2857143 5
 5.9 1 -0.6428571 0.6428571 0.2857143 6
 7.7 1 0.3571429 0.6428571 -0.7142857 8
 10.2 1 -0.6428571 -0.3571429 0.2857143 2
 4.8 1 -0.6428571 0.6428571 0.2857143 7

 

renf90.par

# BLUPF90 parameter file created by RENUMF90
DATAFILE
 renf90.dat
NUMBER_OF_TRAITS
           1
NUMBER_OF_EFFECTS
           5
OBSERVATION(S)
    1
WEIGHT(S)
 
EFFECTS: POSITIONS_IN_DATAFILE NUMBER_OF_LEVELS TYPE_OF_EFFECT[EFFECT NESTED]
 2         1 cross 
 3 1 cov 
 4 1 cov 
 5 1 cov 
 6        26 cross 
RANDOM_RESIDUAL VALUES
   245.00    
 RANDOM_GROUP
     5
 RANDOM_TYPE
 add_animal   
 FILE
renadd05.ped                                                                    
(CO)VARIANCES
   35.241    
OPTION solv_method FSPAK

 

BLUPF90 실행

다음과 같은 명령어로 blupf90을 실행

blupf90 renf90.par | tee blupf90_fem_snp_uw_01.log

 

실행 화면

 

blupf90 실행 로그

renf90.par
     BLUPF90 ver. 1.68

 Parameter file:             renf90.par
 Data file:                  renf90.dat
 Number of Traits             1
 Number of Effects            5
 Position of Observations      1
 Position of Weight (1)        0
 Value of Missing Trait/Observation           0

EFFECTS
 #  type                position (2)        levels   [positions for nested]
    1  cross-classified       2         1
    2  covariable             3         1
    3  covariable             4         1
    4  covariable             5         1
    5  cross-classified       6        26

 Residual (co)variance Matrix
  245.00    

 Random Effect(s)    5
 Type of Random Effect:      additive animal
 Pedigree File:              renadd05.ped                                                                                                                                                                                                                                              
 trait   effect    (CO)VARIANCES
  1       5     35.24    

 REMARKS
  (1) Weight position 0 means no weights utilized
  (2) Effect positions of 0 for some effects and traits means that such
      effects are missing for specified traits
 

 * The limited number of OpenMP threads = 4

 * solving method (default=PCG):FSPAK               
 
 Data record length =            6
 # equations =           30
 G
  35.241    
 read            8  records in   6.2500000E-02  s,                      50 
  nonzeroes
  read           26  additive pedigrees
 finished peds in   6.2500000E-02  s,                     103  nonzeroes
 solutions stored in file: "solutions"

 

blupf90 실행 결과 : solutions

trait/effect level  solution
   1   1         1          9.89535730
   1   2         1          0.60686511
   1   3         1         -4.08027436
   1   4         1          1.93415480
   1   5         1         -0.29881631
   1   5         2         -0.09223369
   1   5         3          0.25587182
   1   5         4          0.14242845
   1   5         5          0.25423609
   1   5         6         -0.08517366
   1   5         7         -0.18078055
   1   5         8          0.27054660
   1   5         9          0.00000000
   1   5        10          0.12201458
   1   5        11          0.00000000
   1   5        12          0.19455774
   1   5        13         -0.10425859
   1   5        14          0.09507379
   1   5        15          0.00000000
   1   5        16          0.00000000
   1   5        17         -0.26444303
   1   5        18          0.00000000
   1   5        19          0.00000000
   1   5        20          0.00000000
   1   5        21          0.00000000
   1   5        22          0.12793591
   1   5        23          0.12793591
   1   5        24          0.12793591
   1   5        25          0.12793591
   1   5        26          0.12793591

 

2, 3, 4 effect가 SNP effect이다. 이들 SNP를 이용하여 DGV를 계산하는 것은 다음 포스트를 참조한다. 5 effect가 polygenic effect이다.

2020/12/16 - [Animal Breeding/R for Genetic Evaluation] - Fixed Effect Model for SNP Effects, unweighted analysis

 

Fixed Effect Model for SNP Effects, unweighted analysis

# Linear Models for the Prediction of Animal Breeding Values, 3rd Edition. # Raphael Mrode # Example 11.1 p180 SNP effect를 고정효과로 모형에 넣어 SNP 추정하고 개체의 genotype을 이용..

bhpark.tistory.com

 

# Linear Models for the Prediction of Animal Breeding Values, 3rd Edition.

# Raphael Mrode

# Example 11.1 p180

 

모형에 대한 간단한 설명은 아래 글을 참조한다.

2020/12/16 - [Animal Breeding/R for Genetic Evaluation] - Fixed Effect Model for SNP Effects, unweighted analysis

 

Fixed Effect Model for SNP Effects, unweighted analysis

# Linear Models for the Prediction of Animal Breeding Values, 3rd Edition. # Raphael Mrode # Example 11.1 p180 SNP effect를 고정효과로 모형에 넣어 SNP 추정하고 개체의 genotype을 이용..

bhpark.tistory.com

 

여기서는 수소(bull)의  EDC(effective daughter contribution)를 가중치로 주어 모형을 분석한다. 책의 MME에 약간의 오타가 나오는데 아래 프로그램을 보고 올바른 MME를 유추하길 바란다.

 

Data

animal sire dam mean edc fat_dyd snp1 snp2 snp3 snp4 snp5 snp6 snp7 snp8 snp9 snp10
13 0 0 1 558 9 2 0 1 1 0 0 0 2 1 2
14 0 0 1 722 13.4 1 0 0 0 0 2 0 2 1 0
15 13 4 1 300 12.7 1 1 2 1 1 0 0 2 1 2
16 15 2 1 73 15.4 0 0 2 1 0 1 0 2 2 1
17 15 5 1 52 5.9 0 1 1 2 0 0 0 2 1 2
18 14 6 1 87 7.7 1 1 0 1 0 2 0 2 2 1
19 14 9 1 64 10.2 0 0 1 1 0 2 0 2 2 0
20 14 9 1 103 4.8 0 1 1 0 0 1 0 2 2 0
21 1 3 1 13 7.6 2 0 0 0 0 1 2 2 1 2
22 14 8 1 125 8.8 0 0 0 1 1 2 0 2 0 0
23 14 11 1 93 9.8 0 1 1 0 0 1 0 2 2 1
24 14 10 1 66 9.2 1 0 0 0 1 1 0 2 0 0
25 14 7 1 75 11.5 0 0 0 1 1 2 0 2 1 0
26 14 12 1 33 13.3 1 0 1 1 0 2 0 1 0 0

 

Pedigree

animal sire dam
1 0 0
2 0 0
3 0 0
4 0 0
5 0 0
6 0 0
7 0 0
8 0 0
9 0 0
10 0 0
11 0 0
12 0 0
13 0 0
14 0 0
15 13 4
16 15 2
17 15 5
18 14 6
19 14 9
20 14 9
21 1 3
22 14 8
23 14 11
24 14 10
25 14 7
26 14 12

 

Program

# Fixed Effect Model for SNP Effect, weighted analysis

# Linear Models for the Prediction of Animal Breeding Values, 3rd Edition.

# Raphael Mrode

# Example 11.1 p180

# MASS packages
if (!("MASS" %in% installed.packages())) {
  install.packages("MASS", dependencies = TRUE)
}
library(MASS)

# functions

# make design matrix 
desgn <- function(v) {
  if (is.numeric(v)) {
    va = v
    mrow = length(va)
    mcol = max(va)
  }
  if (is.character(v)) {
    vf = factor(v)
    # 각 수준의 인덱스 값을 저장
    va = as.numeric(vf)
    mrow = length(va)
    mcol = length(levels(vf))
  }
  
  # 0으로 채워진 X 준비
  X = matrix(data = c(0), nrow = mrow, ncol = mcol)
  
  for (i in 1:mrow) {
    ic = va[i]
    X[i, ic] = 1
  }
  return(X)
}

# function to make inverse of numerator relationship matrix
ainv = function(pedi) {
  n = nrow(pedi)
  Ainv = matrix(c(0), nrow = n, ncol = n)
  
  for (i in 1:n) {
    animal = pedi[i, 1]
    sire = pedi[i, 2]
    dam = pedi[i, 3]
    
    if (sire == 0 & dam == 0) {
      # both parents unknown
      alpha = 1
      Ainv[animal, animal] = alpha + Ainv[animal, animal]
    } else if (sire != 0 & dam == 0) {
      # sire known
      alpha = 4/3
      Ainv[animal, animal] = alpha + Ainv[animal, animal]
      Ainv[animal, sire] = -alpha/2 + Ainv[animal, sire]
      Ainv[sire, animal] = -alpha/2 + Ainv[sire, animal]
      Ainv[sire, sire] = alpha/4 + Ainv[sire, sire]
    } else if (sire == 0 & dam != 0) {
      # dam known
      alpha = 4/3
      Ainv[animal, animal] = alpha + Ainv[animal, animal]
      Ainv[animal, dam] = -alpha/2 + Ainv[animal, dam]
      Ainv[dam, animal] = -alpha/2 + Ainv[dam, animal]
      Ainv[dam, dam] = alpha/4 + Ainv[dam, dam]
    } else {
      # both parents known
      alpha = 2
      Ainv[animal, animal] = alpha + Ainv[animal, animal]
      Ainv[animal, sire] = -alpha/2 + Ainv[animal, sire]
      Ainv[sire, animal] = -alpha/2 + Ainv[sire, animal]
      Ainv[animal, dam] = -alpha/2 + Ainv[animal, dam]
      Ainv[dam, animal] = -alpha/2 + Ainv[dam, animal]
      Ainv[sire, sire] = alpha/4 + Ainv[sire, sire]
      Ainv[sire, dam] = alpha/4 + Ainv[sire, dam]
      Ainv[dam, sire] = alpha/4 + Ainv[dam, sire]
      Ainv[dam, dam] = alpha/4 + Ainv[dam, dam]
    }
  }
  return(Ainv)
}

# set working directory 
setwd(".") 

# print working directory 
getwd()

# prdigree and data

# read pedigree : animal sire dam
pedi = read.table("fem_snp_pedi.txt", header = TRUE, sep = " ", stringsAsFactors = FALSE)

# print
pedi

# read data : animal, sex, pre-weaning gain
data = read.table("fem_snp_data.txt", header = TRUE, sep = " ", stringsAsFactors = FALSE)

# print
data

# variance component and ratio
sigma_a = 35.241
sigma_e = 245
alpha = sigma_e/sigma_a

# print
sigma_a
sigma_e
alpha

# observation
y = data[1:8, 6]

# print
y

# design matrix

# design matrix of fixed effect
X = matrix(rep(1, 8), 8, 1)

# print
X

# SNP effect
M = as.matrix(data[1:8, 7:9])

# design matrix for SNP effect
# mean of SNP effect
M_all = as.matrix(data[, 7:9])
snp_mean = colMeans(M_all) / 2
Z = sweep(M, 2, snp_mean * 2)
Z

# design matrix for polygenic effect
W = cbind(matrix(rep(0, 8 * 12), 8, 12), diag(8), matrix(rep(0, 8 * 6), 8, 6))
W

# Weight : R from EDC
R = diag(data[1:8, 5])
R
Rinv = ginv(R)
Rinv

# inverse matrix of NRM
ai = ainv(pedi)

# print
ai

# LHS, RHS

# LHS
LHS = rbind(
  cbind(t(X) %*% Rinv %*% X, t(X) %*% Rinv %*% Z, t(X) %*% Rinv %*% W), 
  cbind(t(Z) %*% Rinv %*% X, t(Z) %*% Rinv %*% Z, t(Z) %*% Rinv %*% W),
  cbind(t(W) %*% Rinv %*% X, t(W) %*% Rinv %*% Z, t(W) %*% Rinv %*% W + ai * alpha))

# print
LHS

# RHS
RHS = rbind(t(X) %*% Rinv %*% y, 
            t(Z) %*% Rinv %*% y,
            t(W) %*% Rinv %*% y)

# print
RHS

# Solutions

# generalized inverse of LHS
gi_LHS = ginv(LHS)

# print
gi_LHS

# solution
sol = gi_LHS %*% RHS

# print
sol

# mean effect of solutions
mean_eff = sol[1]
mean_eff

# snp effect of solutions
snp_eff = sol[2:4]
snp_eff

# polygenic effect of solutions
polge_eff = sol[5:30]
polge_eff

# calculate DGV
# genotype of animals

# design matrix of animals
P2 = sweep(M_all, 2, snp_mean * 2)
P2

# DGV
dgv = P2 %*% snp_eff
dgv

# GEBV : animal 13 ~ 26
gebv = dgv + polge_eff[13:26]
gebv

 

실행

RStudio에서 실행한 화면

 

Result

> # Fixed Effect Model for SNP Effect, weighted analysis
> 
> # Linear Models for the Prediction of Animal Breeding Values, 3rd Edition.
> 
> # Raphael Mrode
> 
> # Example 11.1 p180
> 
> # MASS packages
> if (!("MASS" %in% installed.packages())) {
+   install.packages("MASS", dependencies = TRUE)
+ }
> library(MASS)
> 
> # functions
> 
> # make design matrix 
> desgn <- function(v) {
+   if (is.numeric(v)) {
+     va = v
+     mrow = length(va)
+     mcol = max(va)
+   }
+   if (is.character(v)) {
+     vf = factor(v)
+     # 각 수준의 인덱스 값을 저장
+     va = as.numeric(vf)
+     mrow = length(va)
+     mcol = length(levels(vf))
+   }
+   
+   # 0으로 채워진 X 준비
+   X = matrix(data = c(0), nrow = mrow, ncol = mcol)
+   
+   for (i in 1:mrow) {
+     ic = va[i]
+     X[i, ic] = 1
+   }
+   return(X)
+ }
> 
> # function to make inverse of numerator relationship matrix
> ainv = function(pedi) {
+   n = nrow(pedi)
+   Ainv = matrix(c(0), nrow = n, ncol = n)
+   
+   for (i in 1:n) {
+     animal = pedi[i, 1]
+     sire = pedi[i, 2]
+     dam = pedi[i, 3]
+     
+     if (sire == 0 & dam == 0) {
+       # both parents unknown
+       alpha = 1
+       Ainv[animal, animal] = alpha + Ainv[animal, animal]
+     } else if (sire != 0 & dam == 0) {
+       # sire known
+       alpha = 4/3
+       Ainv[animal, animal] = alpha + Ainv[animal, animal]
+       Ainv[animal, sire] = -alpha/2 + Ainv[animal, sire]
+       Ainv[sire, animal] = -alpha/2 + Ainv[sire, animal]
+       Ainv[sire, sire] = alpha/4 + Ainv[sire, sire]
+     } else if (sire == 0 & dam != 0) {
+       # dam known
+       alpha = 4/3
+       Ainv[animal, animal] = alpha + Ainv[animal, animal]
+       Ainv[animal, dam] = -alpha/2 + Ainv[animal, dam]
+       Ainv[dam, animal] = -alpha/2 + Ainv[dam, animal]
+       Ainv[dam, dam] = alpha/4 + Ainv[dam, dam]
+     } else {
+       # both parents known
+       alpha = 2
+       Ainv[animal, animal] = alpha + Ainv[animal, animal]
+       Ainv[animal, sire] = -alpha/2 + Ainv[animal, sire]
+       Ainv[sire, animal] = -alpha/2 + Ainv[sire, animal]
+       Ainv[animal, dam] = -alpha/2 + Ainv[animal, dam]
+       Ainv[dam, animal] = -alpha/2 + Ainv[dam, animal]
+       Ainv[sire, sire] = alpha/4 + Ainv[sire, sire]
+       Ainv[sire, dam] = alpha/4 + Ainv[sire, dam]
+       Ainv[dam, sire] = alpha/4 + Ainv[dam, sire]
+       Ainv[dam, dam] = alpha/4 + Ainv[dam, dam]
+     }
+   }
+   return(Ainv)
+ }
> 
> # set working directory 
> setwd(".") 
> 
> # print working directory 
> getwd()
[1] "D:/users/bhpark/2020/job/공부하기/07_Linear Models for the Prediction of Animal Breeding Values_3rd_Mrode/12_fixed effecct model for SNP effects_weighted"
> 
> # prdigree and data
> 
> # read pedigree : animal sire dam
> pedi = read.table("fem_snp_pedi.txt", header = TRUE, sep = " ", stringsAsFactors = FALSE)
> 
> # print
> pedi
   animal sire dam
1       1    0   0
2       2    0   0
3       3    0   0
4       4    0   0
5       5    0   0
6       6    0   0
7       7    0   0
8       8    0   0
9       9    0   0
10     10    0   0
11     11    0   0
12     12    0   0
13     13    0   0
14     14    0   0
15     15   13   4
16     16   15   2
17     17   15   5
18     18   14   6
19     19   14   9
20     20   14   9
21     21    1   3
22     22   14   8
23     23   14  11
24     24   14  10
25     25   14   7
26     26   14  12
> 
> # read data : animal, sex, pre-weaning gain
> data = read.table("fem_snp_data.txt", header = TRUE, sep = " ", stringsAsFactors = FALSE)
> 
> # print
> data
   animal sire dam mean edc fat_dyd snp1 snp2 snp3 snp4 snp5 snp6 snp7 snp8 snp9 snp10
1      13    0   0    1 558     9.0    2    0    1    1    0    0    0    2    1     2
2      14    0   0    1 722    13.4    1    0    0    0    0    2    0    2    1     0
3      15   13   4    1 300    12.7    1    1    2    1    1    0    0    2    1     2
4      16   15   2    1  73    15.4    0    0    2    1    0    1    0    2    2     1
5      17   15   5    1  52     5.9    0    1    1    2    0    0    0    2    1     2
6      18   14   6    1  87     7.7    1    1    0    1    0    2    0    2    2     1
7      19   14   9    1  64    10.2    0    0    1    1    0    2    0    2    2     0
8      20   14   9    1 103     4.8    0    1    1    0    0    1    0    2    2     0
9      21    1   3    1  13     7.6    2    0    0    0    0    1    2    2    1     2
10     22   14   8    1 125     8.8    0    0    0    1    1    2    0    2    0     0
11     23   14  11    1  93     9.8    0    1    1    0    0    1    0    2    2     1
12     24   14  10    1  66     9.2    1    0    0    0    1    1    0    2    0     0
13     25   14   7    1  75    11.5    0    0    0    1    1    2    0    2    1     0
14     26   14  12    1  33    13.3    1    0    1    1    0    2    0    1    0     0
> 
> # variance component and ratio
> sigma_a = 35.241
> sigma_e = 245
> alpha = sigma_e/sigma_a
> 
> # print
> sigma_a
[1] 35.241
> sigma_e
[1] 245
> alpha
[1] 6.95213
> 
> # observation
> y = data[1:8, 6]
> 
> # print
> y
[1]  9.0 13.4 12.7 15.4  5.9  7.7 10.2  4.8
> 
> # design matrix
> 
> # design matrix of fixed effect
> X = matrix(rep(1, 8), 8, 1)
> 
> # print
> X
     [,1]
[1,]    1
[2,]    1
[3,]    1
[4,]    1
[5,]    1
[6,]    1
[7,]    1
[8,]    1
> 
> # SNP effect
> M = as.matrix(data[1:8, 7:9])
> 
> # design matrix for SNP effect
> # mean of SNP effect
> M_all = as.matrix(data[, 7:9])
> snp_mean = colMeans(M_all) / 2
> Z = sweep(M, 2, snp_mean * 2)
> Z
        snp1       snp2       snp3
1  1.3571429 -0.3571429  0.2857143
2  0.3571429 -0.3571429 -0.7142857
3  0.3571429  0.6428571  1.2857143
4 -0.6428571 -0.3571429  1.2857143
5 -0.6428571  0.6428571  0.2857143
6  0.3571429  0.6428571 -0.7142857
7 -0.6428571 -0.3571429  0.2857143
8 -0.6428571  0.6428571  0.2857143
> 
> # design matrix for polygenic effect
> W = cbind(matrix(rep(0, 8 * 12), 8, 12), diag(8), matrix(rep(0, 8 * 6), 8, 6))
> W
     [,1] [,2] [,3] [,4] [,5] [,6] [,7] [,8] [,9] [,10] [,11] [,12] [,13] [,14] [,15] [,16] [,17] [,18] [,19] [,20] [,21] [,22] [,23] [,24] [,25] [,26]
[1,]    0    0    0    0    0    0    0    0    0     0     0     0     1     0     0     0     0     0     0     0     0     0     0     0     0     0
[2,]    0    0    0    0    0    0    0    0    0     0     0     0     0     1     0     0     0     0     0     0     0     0     0     0     0     0
[3,]    0    0    0    0    0    0    0    0    0     0     0     0     0     0     1     0     0     0     0     0     0     0     0     0     0     0
[4,]    0    0    0    0    0    0    0    0    0     0     0     0     0     0     0     1     0     0     0     0     0     0     0     0     0     0
[5,]    0    0    0    0    0    0    0    0    0     0     0     0     0     0     0     0     1     0     0     0     0     0     0     0     0     0
[6,]    0    0    0    0    0    0    0    0    0     0     0     0     0     0     0     0     0     1     0     0     0     0     0     0     0     0
[7,]    0    0    0    0    0    0    0    0    0     0     0     0     0     0     0     0     0     0     1     0     0     0     0     0     0     0
[8,]    0    0    0    0    0    0    0    0    0     0     0     0     0     0     0     0     0     0     0     1     0     0     0     0     0     0
> 
> # Weight : R from EDC
> R = diag(data[1:8, 5])
> R
     [,1] [,2] [,3] [,4] [,5] [,6] [,7] [,8]
[1,]  558    0    0    0    0    0    0    0
[2,]    0  722    0    0    0    0    0    0
[3,]    0    0  300    0    0    0    0    0
[4,]    0    0    0   73    0    0    0    0
[5,]    0    0    0    0   52    0    0    0
[6,]    0    0    0    0    0   87    0    0
[7,]    0    0    0    0    0    0   64    0
[8,]    0    0    0    0    0    0    0  103
> Rinv = ginv(R)
> Rinv
            [,1]        [,2]        [,3]       [,4]       [,5]       [,6]     [,7]        [,8]
[1,] 0.001792115 0.000000000 0.000000000 0.00000000 0.00000000 0.00000000 0.000000 0.000000000
[2,] 0.000000000 0.001385042 0.000000000 0.00000000 0.00000000 0.00000000 0.000000 0.000000000
[3,] 0.000000000 0.000000000 0.003333333 0.00000000 0.00000000 0.00000000 0.000000 0.000000000
[4,] 0.000000000 0.000000000 0.000000000 0.01369863 0.00000000 0.00000000 0.000000 0.000000000
[5,] 0.000000000 0.000000000 0.000000000 0.00000000 0.01923077 0.00000000 0.000000 0.000000000
[6,] 0.000000000 0.000000000 0.000000000 0.00000000 0.00000000 0.01149425 0.000000 0.000000000
[7,] 0.000000000 0.000000000 0.000000000 0.00000000 0.00000000 0.00000000 0.015625 0.000000000
[8,] 0.000000000 0.000000000 0.000000000 0.00000000 0.00000000 0.00000000 0.000000 0.009708738
> 
> # inverse matrix of NRM
> ai = ainv(pedi)
> 
> # print
> ai
      [,1] [,2] [,3] [,4] [,5] [,6] [,7] [,8] [,9] [,10] [,11] [,12] [,13] [,14] [,15] [,16] [,17] [,18] [,19] [,20] [,21] [,22] [,23] [,24] [,25] [,26]
 [1,]  1.5  0.0  0.5  0.0  0.0  0.0  0.0  0.0    0   0.0   0.0   0.0   0.0   0.0   0.0     0     0     0     0     0    -1     0     0     0     0     0
 [2,]  0.0  1.5  0.0  0.0  0.0  0.0  0.0  0.0    0   0.0   0.0   0.0   0.0   0.0   0.5    -1     0     0     0     0     0     0     0     0     0     0
 [3,]  0.5  0.0  1.5  0.0  0.0  0.0  0.0  0.0    0   0.0   0.0   0.0   0.0   0.0   0.0     0     0     0     0     0    -1     0     0     0     0     0
 [4,]  0.0  0.0  0.0  1.5  0.0  0.0  0.0  0.0    0   0.0   0.0   0.0   0.5   0.0  -1.0     0     0     0     0     0     0     0     0     0     0     0
 [5,]  0.0  0.0  0.0  0.0  1.5  0.0  0.0  0.0    0   0.0   0.0   0.0   0.0   0.0   0.5     0    -1     0     0     0     0     0     0     0     0     0
 [6,]  0.0  0.0  0.0  0.0  0.0  1.5  0.0  0.0    0   0.0   0.0   0.0   0.0   0.5   0.0     0     0    -1     0     0     0     0     0     0     0     0
 [7,]  0.0  0.0  0.0  0.0  0.0  0.0  1.5  0.0    0   0.0   0.0   0.0   0.0   0.5   0.0     0     0     0     0     0     0     0     0     0    -1     0
 [8,]  0.0  0.0  0.0  0.0  0.0  0.0  0.0  1.5    0   0.0   0.0   0.0   0.0   0.5   0.0     0     0     0     0     0     0    -1     0     0     0     0
 [9,]  0.0  0.0  0.0  0.0  0.0  0.0  0.0  0.0    2   0.0   0.0   0.0   0.0   1.0   0.0     0     0     0    -1    -1     0     0     0     0     0     0
[10,]  0.0  0.0  0.0  0.0  0.0  0.0  0.0  0.0    0   1.5   0.0   0.0   0.0   0.5   0.0     0     0     0     0     0     0     0     0    -1     0     0
[11,]  0.0  0.0  0.0  0.0  0.0  0.0  0.0  0.0    0   0.0   1.5   0.0   0.0   0.5   0.0     0     0     0     0     0     0     0    -1     0     0     0
[12,]  0.0  0.0  0.0  0.0  0.0  0.0  0.0  0.0    0   0.0   0.0   1.5   0.0   0.5   0.0     0     0     0     0     0     0     0     0     0     0    -1
[13,]  0.0  0.0  0.0  0.5  0.0  0.0  0.0  0.0    0   0.0   0.0   0.0   1.5   0.0  -1.0     0     0     0     0     0     0     0     0     0     0     0
[14,]  0.0  0.0  0.0  0.0  0.0  0.5  0.5  0.5    1   0.5   0.5   0.5   0.0   5.0   0.0     0     0    -1    -1    -1     0    -1    -1    -1    -1    -1
[15,]  0.0  0.5  0.0 -1.0  0.5  0.0  0.0  0.0    0   0.0   0.0   0.0  -1.0   0.0   3.0    -1    -1     0     0     0     0     0     0     0     0     0
[16,]  0.0 -1.0  0.0  0.0  0.0  0.0  0.0  0.0    0   0.0   0.0   0.0   0.0   0.0  -1.0     2     0     0     0     0     0     0     0     0     0     0
[17,]  0.0  0.0  0.0  0.0 -1.0  0.0  0.0  0.0    0   0.0   0.0   0.0   0.0   0.0  -1.0     0     2     0     0     0     0     0     0     0     0     0
[18,]  0.0  0.0  0.0  0.0  0.0 -1.0  0.0  0.0    0   0.0   0.0   0.0   0.0  -1.0   0.0     0     0     2     0     0     0     0     0     0     0     0
[19,]  0.0  0.0  0.0  0.0  0.0  0.0  0.0  0.0   -1   0.0   0.0   0.0   0.0  -1.0   0.0     0     0     0     2     0     0     0     0     0     0     0
[20,]  0.0  0.0  0.0  0.0  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     2     0     0     0     0     0     0
[21,] -1.0  0.0 -1.0  0.0  0.0  0.0  0.0  0.0    0   0.0   0.0   0.0   0.0   0.0   0.0     0     0     0     0     0     2     0     0     0     0     0
[22,]  0.0  0.0  0.0  0.0  0.0  0.0  0.0 -1.0    0   0.0   0.0   0.0   0.0  -1.0   0.0     0     0     0     0     0     0     2     0     0     0     0
[23,]  0.0  0.0  0.0  0.0  0.0  0.0  0.0  0.0    0   0.0  -1.0   0.0   0.0  -1.0   0.0     0     0     0     0     0     0     0     2     0     0     0
[24,]  0.0  0.0  0.0  0.0  0.0  0.0  0.0  0.0    0  -1.0   0.0   0.0   0.0  -1.0   0.0     0     0     0     0     0     0     0     0     2     0     0
[25,]  0.0  0.0  0.0  0.0  0.0  0.0 -1.0  0.0    0   0.0   0.0   0.0   0.0  -1.0   0.0     0     0     0     0     0     0     0     0     0     2     0
[26,]  0.0  0.0  0.0  0.0  0.0  0.0  0.0  0.0    0   0.0   0.0  -1.0   0.0  -1.0   0.0     0     0     0     0     0     0     0     0     0     0     2
> 
> # LHS, RHS
> 
> # LHS
> LHS = rbind(
+   cbind(t(X) %*% Rinv %*% X, t(X) %*% Rinv %*% Z, t(X) %*% Rinv %*% W), 
+   cbind(t(Z) %*% Rinv %*% X, t(Z) %*% Rinv %*% Z, t(Z) %*% Rinv %*% W),
+   cbind(t(W) %*% Rinv %*% X, t(W) %*% Rinv %*% Z, t(W) %*% Rinv %*% W + ai * alpha))
> 
> # print
> LHS
                           snp1          snp2          snp3                                                                                                                       
      0.076267880 -0.0292324941  0.0165285648  0.0259434918  0.000000  0.000000  0.000000  0.000000  0.000000  0.000000  0.000000  0.000000  0.00000  0.000000  0.000000  0.000000
snp1 -0.029232494  0.0294468531 -0.0028682259 -0.0205676752  0.000000  0.000000  0.000000  0.000000  0.000000  0.000000  0.000000  0.000000  0.00000  0.000000  0.000000  0.000000
snp2  0.016528565 -0.0028682259  0.0222329297 -0.0049215685  0.000000  0.000000  0.000000  0.000000  0.000000  0.000000  0.000000  0.000000  0.00000  0.000000  0.000000  0.000000
snp3  0.025943492 -0.0205676752 -0.0049215685  0.0385101610  0.000000  0.000000  0.000000  0.000000  0.000000  0.000000  0.000000  0.000000  0.00000  0.000000  0.000000  0.000000
      0.000000000  0.0000000000  0.0000000000  0.0000000000 10.428194  0.000000  3.476065  0.000000  0.000000  0.000000  0.000000  0.000000  0.00000  0.000000  0.000000  0.000000
      0.000000000  0.0000000000  0.0000000000  0.0000000000  0.000000 10.428194  0.000000  0.000000  0.000000  0.000000  0.000000  0.000000  0.00000  0.000000  0.000000  0.000000
      0.000000000  0.0000000000  0.0000000000  0.0000000000  3.476065  0.000000 10.428194  0.000000  0.000000  0.000000  0.000000  0.000000  0.00000  0.000000  0.000000  0.000000
      0.000000000  0.0000000000  0.0000000000  0.0000000000  0.000000  0.000000  0.000000 10.428194  0.000000  0.000000  0.000000  0.000000  0.00000  0.000000  0.000000  0.000000
      0.000000000  0.0000000000  0.0000000000  0.0000000000  0.000000  0.000000  0.000000  0.000000 10.428194  0.000000  0.000000  0.000000  0.00000  0.000000  0.000000  0.000000
      0.000000000  0.0000000000  0.0000000000  0.0000000000  0.000000  0.000000  0.000000  0.000000  0.000000 10.428194  0.000000  0.000000  0.00000  0.000000  0.000000  0.000000
      0.000000000  0.0000000000  0.0000000000  0.0000000000  0.000000  0.000000  0.000000  0.000000  0.000000  0.000000 10.428194  0.000000  0.00000  0.000000  0.000000  0.000000
      0.000000000  0.0000000000  0.0000000000  0.0000000000  0.000000  0.000000  0.000000  0.000000  0.000000  0.000000  0.000000 10.428194  0.00000  0.000000  0.000000  0.000000
      0.000000000  0.0000000000  0.0000000000  0.0000000000  0.000000  0.000000  0.000000  0.000000  0.000000  0.000000  0.000000  0.000000 13.90426  0.000000  0.000000  0.000000
      0.000000000  0.0000000000  0.0000000000  0.0000000000  0.000000  0.000000  0.000000  0.000000  0.000000  0.000000  0.000000  0.000000  0.00000 10.428194  0.000000  0.000000
      0.000000000  0.0000000000  0.0000000000  0.0000000000  0.000000  0.000000  0.000000  0.000000  0.000000  0.000000  0.000000  0.000000  0.00000  0.000000 10.428194  0.000000
      0.000000000  0.0000000000  0.0000000000  0.0000000000  0.000000  0.000000  0.000000  0.000000  0.000000  0.000000  0.000000  0.000000  0.00000  0.000000  0.000000 10.428194
      0.001792115  0.0024321557 -0.0006400410  0.0005120328  0.000000  0.000000  0.000000  3.476065  0.000000  0.000000  0.000000  0.000000  0.00000  0.000000  0.000000  0.000000
      0.001385042  0.0004946577 -0.0004946577 -0.0009893154  0.000000  0.000000  0.000000  0.000000  0.000000  3.476065  3.476065  3.476065  6.95213  3.476065  3.476065  3.476065
      0.003333333  0.0011904762  0.0021428571  0.0042857143  0.000000  3.476065  0.000000 -6.952130  3.476065  0.000000  0.000000  0.000000  0.00000  0.000000  0.000000  0.000000
      0.013698630 -0.0088062622 -0.0048923679  0.0176125245  0.000000 -6.952130  0.000000  0.000000  0.000000  0.000000  0.000000  0.000000  0.00000  0.000000  0.000000  0.000000
      0.019230769 -0.0123626374  0.0123626374  0.0054945055  0.000000  0.000000  0.000000  0.000000 -6.952130  0.000000  0.000000  0.000000  0.00000  0.000000  0.000000  0.000000
      0.011494253  0.0041050903  0.0073891626 -0.0082101806  0.000000  0.000000  0.000000  0.000000  0.000000 -6.952130  0.000000  0.000000  0.00000  0.000000  0.000000  0.000000
      0.015625000 -0.0100446429 -0.0055803571  0.0044642857  0.000000  0.000000  0.000000  0.000000  0.000000  0.000000  0.000000  0.000000 -6.95213  0.000000  0.000000  0.000000
      0.009708738 -0.0062413315  0.0062413315  0.0027739251  0.000000  0.000000  0.000000  0.000000  0.000000  0.000000  0.000000  0.000000 -6.95213  0.000000  0.000000  0.000000
      0.000000000  0.0000000000  0.0000000000  0.0000000000 -6.952130  0.000000 -6.952130  0.000000  0.000000  0.000000  0.000000  0.000000  0.00000  0.000000  0.000000  0.000000
      0.000000000  0.0000000000  0.0000000000  0.0000000000  0.000000  0.000000  0.000000  0.000000  0.000000  0.000000  0.000000 -6.952130  0.00000  0.000000  0.000000  0.000000
      0.000000000  0.0000000000  0.0000000000  0.0000000000  0.000000  0.000000  0.000000  0.000000  0.000000  0.000000  0.000000  0.000000  0.00000  0.000000 -6.952130  0.000000
      0.000000000  0.0000000000  0.0000000000  0.0000000000  0.000000  0.000000  0.000000  0.000000  0.000000  0.000000  0.000000  0.000000  0.00000 -6.952130  0.000000  0.000000
      0.000000000  0.0000000000  0.0000000000  0.0000000000  0.000000  0.000000  0.000000  0.000000  0.000000  0.000000 -6.952130  0.000000  0.00000  0.000000  0.000000  0.000000
      0.000000000  0.0000000000  0.0000000000  0.0000000000  0.000000  0.000000  0.000000  0.000000  0.000000  0.000000  0.000000  0.000000  0.00000  0.000000  0.000000 -6.952130
                                                                                                                                                                    
      0.0017921147  0.0013850416  0.003333333  0.013698630  0.019230769  0.011494253  0.015625000  0.009708738  0.00000  0.00000  0.00000  0.00000  0.00000  0.00000
snp1  0.0024321557  0.0004946577  0.001190476 -0.008806262 -0.012362637  0.004105090 -0.010044643 -0.006241331  0.00000  0.00000  0.00000  0.00000  0.00000  0.00000
snp2 -0.0006400410 -0.0004946577  0.002142857 -0.004892368  0.012362637  0.007389163 -0.005580357  0.006241331  0.00000  0.00000  0.00000  0.00000  0.00000  0.00000
snp3  0.0005120328 -0.0009893154  0.004285714  0.017612524  0.005494505 -0.008210181  0.004464286  0.002773925  0.00000  0.00000  0.00000  0.00000  0.00000  0.00000
      0.0000000000  0.0000000000  0.000000000  0.000000000  0.000000000  0.000000000  0.000000000  0.000000000 -6.95213  0.00000  0.00000  0.00000  0.00000  0.00000
      0.0000000000  0.0000000000  3.476064811 -6.952129622  0.000000000  0.000000000  0.000000000  0.000000000  0.00000  0.00000  0.00000  0.00000  0.00000  0.00000
      0.0000000000  0.0000000000  0.000000000  0.000000000  0.000000000  0.000000000  0.000000000  0.000000000 -6.95213  0.00000  0.00000  0.00000  0.00000  0.00000
      3.4760648109  0.0000000000 -6.952129622  0.000000000  0.000000000  0.000000000  0.000000000  0.000000000  0.00000  0.00000  0.00000  0.00000  0.00000  0.00000
      0.0000000000  0.0000000000  3.476064811  0.000000000 -6.952129622  0.000000000  0.000000000  0.000000000  0.00000  0.00000  0.00000  0.00000  0.00000  0.00000
      0.0000000000  3.4760648109  0.000000000  0.000000000  0.000000000 -6.952129622  0.000000000  0.000000000  0.00000  0.00000  0.00000  0.00000  0.00000  0.00000
      0.0000000000  3.4760648109  0.000000000  0.000000000  0.000000000  0.000000000  0.000000000  0.000000000  0.00000  0.00000  0.00000  0.00000 -6.95213  0.00000
      0.0000000000  3.4760648109  0.000000000  0.000000000  0.000000000  0.000000000  0.000000000  0.000000000  0.00000 -6.95213  0.00000  0.00000  0.00000  0.00000
      0.0000000000  6.9521296217  0.000000000  0.000000000  0.000000000  0.000000000 -6.952129622 -6.952129622  0.00000  0.00000  0.00000  0.00000  0.00000  0.00000
      0.0000000000  3.4760648109  0.000000000  0.000000000  0.000000000  0.000000000  0.000000000  0.000000000  0.00000  0.00000  0.00000 -6.95213  0.00000  0.00000
      0.0000000000  3.4760648109  0.000000000  0.000000000  0.000000000  0.000000000  0.000000000  0.000000000  0.00000  0.00000 -6.95213  0.00000  0.00000  0.00000
      0.0000000000  3.4760648109  0.000000000  0.000000000  0.000000000  0.000000000  0.000000000  0.000000000  0.00000  0.00000  0.00000  0.00000  0.00000 -6.95213
     10.4299865473  0.0000000000 -6.952129622  0.000000000  0.000000000  0.000000000  0.000000000  0.000000000  0.00000  0.00000  0.00000  0.00000  0.00000  0.00000
      0.0000000000 34.7620331503  0.000000000  0.000000000  0.000000000 -6.952129622 -6.952129622 -6.952129622  0.00000 -6.95213 -6.95213 -6.95213 -6.95213 -6.95213
     -6.9521296217  0.0000000000 20.859722199 -6.952129622 -6.952129622  0.000000000  0.000000000  0.000000000  0.00000  0.00000  0.00000  0.00000  0.00000  0.00000
      0.0000000000  0.0000000000 -6.952129622 13.917957874  0.000000000  0.000000000  0.000000000  0.000000000  0.00000  0.00000  0.00000  0.00000  0.00000  0.00000
      0.0000000000  0.0000000000 -6.952129622  0.000000000 13.923490013  0.000000000  0.000000000  0.000000000  0.00000  0.00000  0.00000  0.00000  0.00000  0.00000
      0.0000000000 -6.9521296217  0.000000000  0.000000000  0.000000000 13.915753496  0.000000000  0.000000000  0.00000  0.00000  0.00000  0.00000  0.00000  0.00000
      0.0000000000 -6.9521296217  0.000000000  0.000000000  0.000000000  0.000000000 13.919884243  0.000000000  0.00000  0.00000  0.00000  0.00000  0.00000  0.00000
      0.0000000000 -6.9521296217  0.000000000  0.000000000  0.000000000  0.000000000  0.000000000 13.913967981  0.00000  0.00000  0.00000  0.00000  0.00000  0.00000
      0.0000000000  0.0000000000  0.000000000  0.000000000  0.000000000  0.000000000  0.000000000  0.000000000 13.90426  0.00000  0.00000  0.00000  0.00000  0.00000
      0.0000000000 -6.9521296217  0.000000000  0.000000000  0.000000000  0.000000000  0.000000000  0.000000000  0.00000 13.90426  0.00000  0.00000  0.00000  0.00000
      0.0000000000 -6.9521296217  0.000000000  0.000000000  0.000000000  0.000000000  0.000000000  0.000000000  0.00000  0.00000 13.90426  0.00000  0.00000  0.00000
      0.0000000000 -6.9521296217  0.000000000  0.000000000  0.000000000  0.000000000  0.000000000  0.000000000  0.00000  0.00000  0.00000 13.90426  0.00000  0.00000
      0.0000000000 -6.9521296217  0.000000000  0.000000000  0.000000000  0.000000000  0.000000000  0.000000000  0.00000  0.00000  0.00000  0.00000 13.90426  0.00000
      0.0000000000 -6.9521296217  0.000000000  0.000000000  0.000000000  0.000000000  0.000000000  0.000000000  0.00000  0.00000  0.00000  0.00000  0.00000 13.90426
> 
> # RHS
> RHS = rbind(t(X) %*% Rinv %*% y, 
+             t(Z) %*% Rinv %*% y,
+             t(W) %*% Rinv %*% y)
> 
> # print
> RHS
            [,1]
      0.69592505
snp1 -0.26572369
snp2  0.04235790
snp3  0.34506266
      0.00000000
      0.00000000
      0.00000000
      0.00000000
      0.00000000
      0.00000000
      0.00000000
      0.00000000
      0.00000000
      0.00000000
      0.00000000
      0.00000000
      0.01612903
      0.01855956
      0.04233333
      0.21095890
      0.11346154
      0.08850575
      0.15937500
      0.04660194
      0.00000000
      0.00000000
      0.00000000
      0.00000000
      0.00000000
      0.00000000
> 
> # Solutions
> 
> # generalized inverse of LHS
> gi_LHS = ginv(LHS)
> 
> # print
> gi_LHS
               [,1]          [,2]          [,3]          [,4]          [,5]          [,6]          [,7]          [,8]          [,9]         [,10]         [,11]         [,12]
 [1,]  2.920047e+01  1.763942e+01 -2.231416e+01 -1.309408e+01  7.413843e-15 -8.839999e-03 -3.048551e-15 -5.289824e-03  3.231430e-04 -2.516429e-02  1.406606e-14  1.237420e-14
 [2,]  1.763942e+01  6.933510e+01  1.431918e+00  2.532832e+01 -2.176580e-14 -5.022158e-03 -1.027103e-14 -7.728479e-03  2.593618e-02 -2.083596e-02  2.595898e-14 -1.328113e-14
 [3,] -2.231416e+01  1.431918e+00  6.721137e+01  2.439554e+01 -1.212401e-14  1.563587e-02 -2.195249e-14 -2.340188e-02 -3.715804e-02 -3.300104e-03 -1.688944e-14 -3.007048e-14
 [4,] -1.309408e+01  2.532832e+01  2.439554e+01  5.148543e+01 -3.281515e-14 -2.761546e-02  6.079561e-15 -3.312695e-02 -1.354714e-03  2.073423e-02  5.850616e-15 -2.586359e-14
 [5,]  3.854270e-15  6.098944e-15 -1.627167e-14  3.412186e-15  1.438408e-01  3.281081e-17  1.665335e-16  2.460005e-17  1.294570e-18  4.900896e-17 -4.836998e-17 -7.003944e-17
 [6,] -8.839999e-03 -5.022158e-03  1.563587e-02 -2.761546e-02  7.725859e-17  1.438160e-01  4.443646e-17 -1.373273e-06  4.761023e-06 -1.580866e-05 -7.508505e-17 -9.719437e-17
 [7,]  2.268174e-15 -1.182284e-14 -7.722540e-15 -1.550289e-14  5.551115e-17  3.246030e-17  1.438408e-01  1.672703e-17 -5.036466e-19 -8.089353e-17  3.211128e-17  4.198234e-19
 [8,] -5.289824e-03 -7.728479e-03 -2.340188e-02 -3.312695e-02  5.607399e-17 -1.373273e-06  2.890609e-17  1.438310e-01 -1.534947e-05 -4.746415e-07 -3.436971e-17  7.145836e-18
 [9,]  3.231430e-04  2.593618e-02 -3.715804e-02 -1.354714e-03 -2.179995e-17  4.761023e-06  1.791440e-17 -1.534947e-05  1.437976e-01  1.099299e-05 -3.017503e-17 -1.800968e-17
[10,] -2.516429e-02 -2.083596e-02 -3.300104e-03  2.073423e-02 -4.667688e-17 -1.580866e-05 -1.394586e-18 -4.746415e-07  1.099299e-05  1.438223e-01 -4.292573e-17  1.362749e-17
[11,]  3.697444e-15 -1.750360e-16 -2.137471e-16 -1.108517e-14 -1.836047e-17 -5.174046e-17 -6.444924e-17 -1.799527e-17 -5.931322e-17 -1.385134e-16  1.438408e-01 -4.760164e-17
[12,] -1.280829e-14 -4.073019e-15  3.107244e-14  1.216392e-14  6.802225e-18 -6.472936e-17  7.657708e-17 -3.395347e-17 -4.383075e-17  8.026958e-18  1.631712e-16  1.438408e-01
[13,] -2.456054e-02  3.574275e-02  2.634502e-02  2.545956e-02  1.069449e-17  2.380274e-05 -8.101235e-18  1.889982e-05  3.217335e-05  1.078680e-05 -1.139568e-17 -4.671328e-17
[14,] -8.376406e-15 -1.839873e-14  1.950394e-14 -9.523788e-15  8.942065e-18 -2.578767e-17  8.775515e-17 -1.415649e-17 -4.335463e-17  4.526042e-18  8.900807e-17 -1.269771e-16
[15,]  4.888523e-15  2.162299e-14  9.652228e-15  1.121159e-14 -1.000579e-18 -8.186696e-17  3.638188e-17 -8.585356e-18  1.118782e-17 -2.005501e-17  5.235704e-17 -6.995308e-17
[16,] -8.825948e-16 -9.068977e-15  5.327753e-15 -9.894378e-15  2.158874e-17  5.648236e-18 -2.205240e-17 -1.871506e-17 -8.108127e-17  2.608850e-17 -2.733303e-16  1.974181e-16
[17,] -2.007289e-02 -3.825575e-02 -1.375616e-02 -4.014817e-02  3.817602e-18  6.134295e-06  3.610079e-17 -5.497659e-06 -2.782034e-05  1.146763e-05  2.945302e-17  5.202542e-17
[18,] -6.023642e-02  1.016341e-02  3.563529e-02  5.605151e-02 -2.221832e-17  7.293851e-06  6.619617e-19  1.364703e-05  3.841225e-05  1.517008e-06 -4.453104e-18 -1.423568e-16
[19,] -1.797118e-02 -3.072059e-02 -4.198090e-02 -6.976451e-02  5.844438e-17  1.007238e-06  3.244858e-17  7.190288e-02 -3.693437e-05  5.021854e-06 -1.637519e-17  3.630498e-17
[20,] -2.224559e-02 -2.289353e-02  2.463362e-03 -7.630544e-02  1.122973e-16  7.188370e-02  1.802412e-17  3.594938e-02 -1.132565e-05 -2.120206e-05 -4.135747e-17 -1.086394e-17
[21,] -8.500876e-03  2.354398e-02 -7.672751e-02 -3.691433e-02  1.154287e-17  7.645153e-06  4.597242e-17  3.592842e-02  7.183719e-02  1.900041e-05 -5.557656e-17 -4.033050e-17
[22,] -6.786464e-02 -2.617224e-02  1.286749e-02  5.912710e-02 -6.032017e-17 -2.006606e-05  2.865929e-17  6.111555e-06  3.569561e-05  7.189344e-02 -4.421854e-17 -5.923113e-17
[23,] -6.714570e-02  4.561056e-02  7.615665e-02  6.695275e-02 -1.666986e-17  3.695460e-05 -2.783898e-17  1.785869e-05  3.914425e-05  1.138376e-05 -1.404479e-17 -1.295412e-16
[24,] -4.221180e-02  3.603835e-02  1.216868e-02  4.001788e-02  2.115577e-17  1.794473e-05 -1.166689e-17  3.358797e-05  6.361469e-05  1.170686e-05  7.854100e-18 -9.209185e-17
[25,]  3.727857e-15  4.486850e-15  6.169878e-15 -7.616713e-15  7.192041e-02  7.935595e-17  7.192041e-02  2.590943e-17 -2.459203e-17 -1.294269e-17  3.537566e-17 -2.335057e-17
[26,] -3.011821e-02  5.081706e-03  1.781764e-02  2.802575e-02  3.327458e-19  3.646926e-06  3.540119e-17  6.823517e-06  1.920612e-05  7.585038e-07  1.180998e-16  7.192041e-02
[27,] -3.011821e-02  5.081706e-03  1.781764e-02  2.802575e-02  6.046083e-18  3.646926e-06  3.012527e-17  6.823517e-06  1.920612e-05  7.585038e-07 -2.587482e-17 -1.208657e-16
[28,] -3.011821e-02  5.081706e-03  1.781764e-02  2.802575e-02 -5.723745e-18  3.646926e-06  6.832831e-17  6.823517e-06  1.920612e-05  7.585038e-07  6.258824e-17 -1.330166e-16
[29,] -3.011821e-02  5.081706e-03  1.781764e-02  2.802575e-02 -5.791035e-17  3.646926e-06 -4.420883e-17  6.823517e-06  1.920612e-05  7.585038e-07  7.192041e-02 -1.312770e-16
[30,] -3.011821e-02  5.081706e-03  1.781764e-02  2.802575e-02 -6.157599e-18  3.646926e-06 -1.506816e-17  6.823517e-06  1.920612e-05  7.585038e-07 -1.837357e-16  1.964591e-17
              [,13]         [,14]         [,15]         [,16]         [,17]         [,18]         [,19]         [,20]         [,21]         [,22]         [,23]         [,24]
 [1,] -2.456054e-02  1.127386e-14  2.407494e-14  1.531248e-14 -2.007289e-02 -6.023642e-02 -1.797118e-02 -2.224559e-02 -8.500876e-03 -6.786464e-02 -6.714570e-02 -4.221180e-02
 [2,]  3.574275e-02  3.171247e-14  7.965550e-15 -3.177764e-15 -3.825575e-02  1.016341e-02 -3.072059e-02 -2.289353e-02  2.354398e-02 -2.617224e-02  4.561056e-02  3.603835e-02
 [3,]  2.634502e-02 -1.080795e-14 -4.995003e-14 -2.818954e-14 -1.375616e-02  3.563529e-02 -4.198090e-02  2.463362e-03 -7.672751e-02  1.286749e-02  7.615665e-02  1.216868e-02
 [4,]  2.545956e-02  1.475052e-14 -2.039614e-14 -2.245477e-14 -4.014817e-02  5.605151e-02 -6.976451e-02 -7.630544e-02 -3.691433e-02  5.912710e-02  6.695275e-02  4.001788e-02
 [5,]  7.721265e-17  1.005990e-17  3.408727e-17  6.035387e-18  3.991369e-17  3.686344e-17  5.348801e-17  3.946867e-17  5.312898e-17  6.283395e-17  3.211329e-17  7.978079e-17
 [6,]  2.380274e-05 -3.892998e-17 -7.994268e-17 -5.936226e-17  6.134295e-06  7.293851e-06  1.007238e-06  7.188370e-02  7.645153e-06 -2.006606e-05  3.695460e-05  1.794473e-05
 [7,] -2.265078e-17  2.104700e-17 -6.244156e-17  2.113832e-17  4.339012e-17 -4.803139e-17  4.450964e-17  4.576585e-17  2.170105e-17 -1.199753e-16 -2.480241e-17 -4.045046e-17
 [8,]  1.889982e-05 -1.966336e-17 -1.981620e-18  1.267667e-17 -5.497659e-06  1.364703e-05  7.190288e-02  3.594938e-02  3.592842e-02  6.111555e-06  1.785869e-05  3.358797e-05
 [9,]  3.217335e-05 -4.649104e-17  2.781690e-17 -2.191223e-17 -2.782034e-05  3.841225e-05 -3.693437e-05 -1.132565e-05  7.183719e-02  3.569561e-05  3.914425e-05  6.361469e-05
[10,]  1.078680e-05  2.953518e-17  3.697114e-18  2.689264e-18  1.146763e-05  1.517008e-06  5.021854e-06 -2.120206e-05  1.900041e-05  7.189344e-02  1.138376e-05  1.170686e-05
[11,] -4.621383e-17 -9.202127e-18 -1.000710e-16 -7.535890e-17  1.919274e-16 -1.258782e-16  1.246395e-16  8.698977e-17 -1.313935e-17 -1.451779e-16 -1.011369e-16 -8.747060e-17
[12,]  2.444268e-17  3.068831e-17 -1.176433e-16 -5.303253e-17  1.592858e-17  5.622370e-18 -2.659208e-17 -6.020184e-17 -5.736620e-17  2.424513e-17  2.499326e-17  1.070261e-17
[13,]  1.437820e-01  7.157615e-18 -5.193655e-17 -2.778831e-20  1.327353e-05 -4.014701e-05  3.498649e-05  5.319735e-05  6.575327e-05 -3.893299e-06  7.184602e-02  7.183707e-02
[14,]  9.835869e-18  1.438408e-01  2.676875e-17 -1.140285e-17  1.819425e-17  7.153431e-18 -5.566045e-18 -3.965451e-18 -5.302593e-17  1.067757e-17  6.415434e-18  6.785654e-18
[15,] -1.824879e-17 -1.073689e-17  1.438408e-01  1.461301e-17  1.314185e-18  9.103037e-18 -1.665990e-17 -8.136608e-17  1.034184e-17 -1.102141e-17 -3.141744e-18 -5.766190e-18
[16,] -6.346174e-18 -1.180660e-16  1.740054e-16  1.438408e-01  3.596347e-17 -4.121642e-17  7.420429e-18  1.633791e-17 -6.164964e-17 -9.038260e-18 -3.118813e-17 -2.160013e-17
[17,]  1.327353e-05 -3.644435e-18  2.772130e-18  2.535565e-17  1.438185e-01  2.476521e-05  7.190100e-02  3.595970e-02  3.590877e-02  2.958406e-05  2.128556e-05  3.002672e-05
[18,] -4.014701e-05 -1.672890e-20 -3.447643e-17 -6.134678e-17  2.476521e-05  1.437953e-01  3.285316e-05  2.736736e-05  7.404495e-05  7.189994e-02  7.186817e-02  7.184687e-02
[19,]  3.498649e-05 -3.113023e-17  5.688422e-18  2.234742e-17  7.190100e-02  3.285316e-05  1.438048e-01  7.190392e-02  7.184701e-02  2.395936e-05  3.743082e-05  6.539532e-05
[20,]  5.319735e-05 -5.675340e-17 -6.318892e-17 -2.293598e-17  3.595970e-02  2.736736e-05  7.190392e-02  1.437775e-01  3.593497e-02 -1.811941e-05  7.414731e-05  5.961475e-05
[21,]  6.575327e-05 -4.322887e-17  3.058577e-17 -5.851582e-18  3.590877e-02  7.404495e-05  7.184701e-02  3.593497e-02  1.436793e-01  6.552310e-05  7.743179e-05  1.281197e-04
[22,] -3.893299e-06  1.051766e-17 -2.497816e-17 -4.530919e-17  2.958406e-05  7.189994e-02  2.395936e-05 -1.811941e-05  6.552310e-05  1.437901e-01  3.595116e-02  3.594099e-02
[23,]  7.184602e-02  1.927940e-18 -8.488084e-17 -5.620609e-17  2.128556e-05  7.186817e-02  3.743082e-05  7.414731e-05  7.743179e-05  3.595116e-02  1.437335e-01  7.182676e-02
[24,]  7.183707e-02  1.346413e-17 -2.544736e-17 -1.621100e-17  3.002672e-05  7.184687e-02  6.539532e-05  5.961475e-05  1.281197e-04  3.594099e-02  7.182676e-02  1.436942e-01
[25,]  5.213887e-17 -8.519016e-18 -1.384489e-17  2.878586e-17  3.561158e-17 -5.831951e-18  4.669348e-17  8.592713e-17  1.235270e-17 -4.103907e-17  4.746855e-17  2.044665e-17
[26,] -2.007351e-05  3.359176e-17 -1.423844e-16 -2.746152e-17  1.238261e-05  7.189766e-02  1.642658e-05  1.368368e-05  3.702248e-05  3.594997e-02  3.593408e-02  3.592343e-02
[27,] -2.007351e-05 -1.150362e-17  7.192041e-02 -8.296526e-17  1.238261e-05  7.189766e-02  1.642658e-05  1.368368e-05  3.702248e-05  3.594997e-02  3.593408e-02  3.592343e-02
[28,] -2.007351e-05  7.192041e-02 -8.687369e-17 -1.184944e-17  1.238261e-05  7.189766e-02  1.642658e-05  1.368368e-05  3.702248e-05  3.594997e-02  3.593408e-02  3.592343e-02
[29,] -2.007351e-05  5.840608e-18 -8.513414e-17 -7.776410e-17  1.238261e-05  7.189766e-02  1.642658e-05  1.368368e-05  3.702248e-05  3.594997e-02  3.593408e-02  3.592343e-02
[30,] -2.007351e-05 -1.155881e-16  1.022180e-16  7.192041e-02  1.238261e-05  7.189766e-02  1.642658e-05  1.368368e-05  3.702248e-05  3.594997e-02  3.593408e-02  3.592343e-02
              [,25]         [,26]         [,27]         [,28]         [,29]         [,30]
 [1,]  2.332834e-15 -3.011821e-02 -3.011821e-02 -3.011821e-02 -3.011821e-02 -3.011821e-02
 [2,] -1.759614e-14  5.081706e-03  5.081706e-03  5.081706e-03  5.081706e-03  5.081706e-03
 [3,] -1.645643e-14  1.781764e-02  1.781764e-02  1.781764e-02  1.781764e-02  1.781764e-02
 [4,] -1.602331e-14  2.802575e-02  2.802575e-02  2.802575e-02  2.802575e-02  2.802575e-02
 [5,]  7.192041e-02 -8.224665e-17 -5.685184e-17  4.062549e-17  1.599423e-16  1.556634e-17
 [6,]  2.791043e-18  3.646926e-06  3.646926e-06  3.646926e-06  3.646926e-06  3.646926e-06
 [7,]  7.192041e-02  4.094171e-17  6.546335e-17  6.389980e-18 -1.687216e-16 -5.119257e-17
 [8,]  2.163392e-17  6.823517e-06  6.823517e-06  6.823517e-06  6.823517e-06  6.823517e-06
 [9,]  3.497996e-17  1.920612e-05  1.920612e-05  1.920612e-05  1.920612e-05  1.920612e-05
[10,] -5.333961e-18  7.585038e-07  7.585038e-07  7.585038e-07  7.585038e-07  7.585038e-07
[11,]  1.417933e-16 -5.881716e-17 -1.334137e-16 -2.237553e-17  7.192041e-02 -1.420786e-16
[12,]  3.270198e-18  7.192041e-02  2.645242e-16 -6.852686e-17 -3.530294e-16  3.207997e-17
[13,] -5.204749e-20 -2.007351e-05 -2.007351e-05 -2.007351e-05 -2.007351e-05 -2.007351e-05
[14,] -8.856996e-17 -6.257526e-17  8.660753e-17  7.192041e-02  1.549181e-17  1.028841e-17
[15,]  3.298604e-17 -6.074041e-17  7.192041e-02 -3.991132e-17  3.987806e-17  1.385799e-17
[16,] -3.839556e-17 -2.951139e-17 -3.504387e-16  2.601217e-17  3.191725e-16  7.192041e-02
[17,] -3.192034e-18  1.238261e-05  1.238261e-05  1.238261e-05  1.238261e-05  1.238261e-05
[18,] -5.365654e-18  7.189766e-02  7.189766e-02  7.189766e-02  7.189766e-02  7.189766e-02
[19,]  1.009985e-17  1.642658e-05  1.642658e-05  1.642658e-05  1.642658e-05  1.642658e-05
[20,]  3.183542e-18  1.368368e-05  1.368368e-05  1.368368e-05  1.368368e-05  1.368368e-05
[21,]  3.382015e-17  3.702248e-05  3.702248e-05  3.702248e-05  3.702248e-05  3.702248e-05
[22,]  7.859526e-18  3.594997e-02  3.594997e-02  3.594997e-02  3.594997e-02  3.594997e-02
[23,] -3.735889e-17  3.593408e-02  3.593408e-02  3.593408e-02  3.593408e-02  3.593408e-02
[24,]  6.408456e-18  3.592343e-02  3.592343e-02  3.592343e-02  3.592343e-02  3.592343e-02
[25,]  1.438408e-01 -1.738295e-17  1.636175e-18  1.479296e-17  2.889214e-17 -2.276950e-17
[26,] -8.319072e-18  1.438294e-01  3.594883e-02  3.594883e-02  3.594883e-02  3.594883e-02
[27,]  3.244626e-17  3.594883e-02  1.438294e-01  3.594883e-02  3.594883e-02  3.594883e-02
[28,] -7.220066e-17  3.594883e-02  3.594883e-02  1.438294e-01  3.594883e-02  3.594883e-02
[29,]  9.163404e-17  3.594883e-02  3.594883e-02  3.594883e-02  1.438294e-01  3.594883e-02
[30,] -2.102841e-17  3.594883e-02  3.594883e-02  3.594883e-02  3.594883e-02  1.438294e-01
> 
> # solution
> sol = gi_LHS %*% RHS
> 
> # print
> sol
               [,1]
 [1,]  1.014413e+01
 [2,]  2.654992e+00
 [3,] -4.640234e+00
 [4,]  2.951136e+00
 [5,]  1.582177e-15
 [6,]  1.486369e-03
 [7,] -9.591387e-16
 [8,]  6.595328e-04
 [9,] -5.489412e-04
[10,]  1.402426e-03
[11,] -1.224625e-15
[12,] -2.331180e-15
[13,] -1.871431e-03
[14,] -3.404909e-15
[15,]  1.915600e-15
[16,] -1.403082e-15
[17,] -1.208474e-03
[18,]  8.051891e-05
[19,]  3.850622e-04
[20,]  2.422084e-03
[21,] -6.308807e-04
[22,]  2.143898e-03
[23,] -1.722584e-03
[24,] -1.939760e-03
[25,] -9.379962e-16
[26,]  4.025946e-05
[27,]  4.025946e-05
[28,]  4.025946e-05
[29,]  4.025946e-05
[30,]  4.025946e-05
> 
> # mean effect of solutions
> mean_eff = sol[1]
> mean_eff
[1] 10.14413
> 
> # snp effect of solutions
> snp_eff = sol[2:4]
> snp_eff
[1]  2.654992 -4.640234  2.951136
> 
> # polygenic effect of solutions
> polge_eff = sol[5:30]
> polge_eff
 [1]  1.582177e-15  1.486369e-03 -9.591387e-16  6.595328e-04 -5.489412e-04  1.402426e-03 -1.224625e-15 -2.331180e-15 -1.871431e-03 -3.404909e-15  1.915600e-15 -1.403082e-15
[13] -1.208474e-03  8.051891e-05  3.850622e-04  2.422084e-03 -6.308807e-04  2.143898e-03 -1.722584e-03 -1.939760e-03 -9.379962e-16  4.025946e-05  4.025946e-05  4.025946e-05
[25]  4.025946e-05  4.025946e-05
> 
> # calculate DGV
> # genotype of animals
> 
> # design matrix of animals
> P2 = sweep(M_all, 2, snp_mean * 2)
> P2
            snp1       snp2       snp3
 [1,]  1.3571429 -0.3571429  0.2857143
 [2,]  0.3571429 -0.3571429 -0.7142857
 [3,]  0.3571429  0.6428571  1.2857143
 [4,] -0.6428571 -0.3571429  1.2857143
 [5,] -0.6428571  0.6428571  0.2857143
 [6,]  0.3571429  0.6428571 -0.7142857
 [7,] -0.6428571 -0.3571429  0.2857143
 [8,] -0.6428571  0.6428571  0.2857143
 [9,]  1.3571429 -0.3571429 -0.7142857
[10,] -0.6428571 -0.3571429 -0.7142857
[11,] -0.6428571  0.6428571  0.2857143
[12,]  0.3571429 -0.3571429 -0.7142857
[13,] -0.6428571 -0.3571429 -0.7142857
[14,]  0.3571429 -0.3571429  0.2857143
> 
> # DGV
> dgv = P2 %*% snp_eff
> dgv
            [,1]
 [1,]  6.1036121
 [2,]  0.4974832
 [3,]  1.7595226
 [4,]  3.7447638
 [5,] -3.8466063
 [6,] -4.1427504
 [7,]  0.7936273
 [8,] -3.8466063
 [9,]  3.1524756
[10,] -2.1575092
[11,] -3.8466063
[12,]  0.4974832
[13,] -2.1575092
[14,]  3.4486197
> 
> # GEBV : animal 13 ~ 26
> gebv = dgv + polge_eff[13:26]
> gebv
            [,1]
 [1,]  6.1024036
 [2,]  0.4975637
 [3,]  1.7599076
 [4,]  3.7471859
 [5,] -3.8472371
 [6,] -4.1406065
 [7,]  0.7919048
 [8,] -3.8485460
 [9,]  3.1524756
[10,] -2.1574689
[11,] -3.8465660
[12,]  0.4975235
[13,] -2.1574689
[14,]  3.4486600
> 

 

결과 책과 약간 다른다. Mean effect가 책과 결과가 다른데 책의 오타인 것으로 보인다. 그리고 DGV도 좀 다른데 round-off error로 보인다.

 

SNP effect를 고정효과로 처리하여 모형에 넣고, SNP effect를 추정한 다음 개체의 SNP effect의 design matrix 이용하여 개체의 DGV를 추정하였다.

 

# Linear Models for the Prediction of Animal Breeding Values, 3rd Edition.

# Raphael Mrode

# Example 11.1 p180

SNP effect를 고정효과로 모형에 넣어 SNP 추정하고 개체의 genotype을 이용하여 개체의 DGV(direct genomic breeding value)를 추정할 수 있다. SNP effect로 개체의 모든 유전적 효과를 설명할 수 없으므로  polygenic effect를 추가하여 분석한다. 그래서 분석모형은 y = mean + snp effect + polygenic effect + e 가 된다.

 

SNP effect의 design matrix를 만드는 방법은 다음과 같다. SNP gentype을 먼저 0, 1, 2로 코드화 한다. SNP 별로 평균을 구하는데 각 개체별로 두 개의 SNP가 동원된 것으므로 SNP 별 합을 개체의 2배수로 나누어 평균을 구한다. 코드화한 genotype에서 각 SNP의 평균의 2배를 빼 SNP effect의 design matrix를 구한다. 이 개체별 design matrix를 이용하여 나중에 SNP effect 추정 후 DGV를 추정한다. 

 

예제에서는 단지 3개의 SNP만을 이용하는 것을 보여준다.

 

Data

animal sire dam mean edc fat_dyd snp1 snp2 snp3 snp4 snp5 snp6 snp7 snp8 snp9 snp10
13 0 0 1 558 9 2 0 1 1 0 0 0 2 1 2
14 0 0 1 722 13.4 1 0 0 0 0 2 0 2 1 0
15 13 4 1 300 12.7 1 1 2 1 1 0 0 2 1 2
16 15 2 1 73 15.4 0 0 2 1 0 1 0 2 2 1
17 15 5 1 52 5.9 0 1 1 2 0 0 0 2 1 2
18 14 6 1 87 7.7 1 1 0 1 0 2 0 2 2 1
19 14 9 1 64 10.2 0 0 1 1 0 2 0 2 2 0
20 14 9 1 103 4.8 0 1 1 0 0 1 0 2 2 0
21 1 3 1 13 7.6 2 0 0 0 0 1 2 2 1 2
22 14 8 1 125 8.8 0 0 0 1 1 2 0 2 0 0
23 14 11 1 93 9.8 0 1 1 0 0 1 0 2 2 1
24 14 10 1 66 9.2 1 0 0 0 1 1 0 2 0 0
25 14 7 1 75 11.5 0 0 0 1 1 2 0 2 1 0
26 14 12 1 33 13.3 1 0 1 1 0 2 0 1 0 0

 

Pedigree

animal sire dam
1 0 0
2 0 0
3 0 0
4 0 0
5 0 0
6 0 0
7 0 0
8 0 0
9 0 0
10 0 0
11 0 0
12 0 0
13 0 0
14 0 0
15 13 4
16 15 2
17 15 5
18 14 6
19 14 9
20 14 9
21 1 3
22 14 8
23 14 11
24 14 10
25 14 7
26 14 12

 

Program

# Fixed Effect Model for SNP Effect, unweighted analysis

# Linear Models for the Prediction of Animal Breeding Values, 3rd Edition.

# Raphael Mrode

# Example 11.1 p180

# MASS packages
if (!("MASS" %in% installed.packages())) {
  install.packages("MASS", dependencies = TRUE)
}
library(MASS)

# functions

# make design matrix 
desgn <- function(v) {
  if (is.numeric(v)) {
    va = v
    mrow = length(va)
    mcol = max(va)
  }
  if (is.character(v)) {
    vf = factor(v)
    # 각 수준의 인덱스 값을 저장
    va = as.numeric(vf)
    mrow = length(va)
    mcol = length(levels(vf))
  }
  
  # 0으로 채워진 X 준비
  X = matrix(data = c(0), nrow = mrow, ncol = mcol)
  
  for (i in 1:mrow) {
    ic = va[i]
    X[i, ic] = 1
  }
  return(X)
}

# function to make inverse of numerator relationship matrix
ainv = function(pedi) {
  n = nrow(pedi)
  Ainv = matrix(c(0), nrow = n, ncol = n)
  
  for (i in 1:n) {
    animal = pedi[i, 1]
    sire = pedi[i, 2]
    dam = pedi[i, 3]
    
    if (sire == 0 & dam == 0) {
      # both parents unknown
      alpha = 1
      Ainv[animal, animal] = alpha + Ainv[animal, animal]
    } else if (sire != 0 & dam == 0) {
      # sire known
      alpha = 4/3
      Ainv[animal, animal] = alpha + Ainv[animal, animal]
      Ainv[animal, sire] = -alpha/2 + Ainv[animal, sire]
      Ainv[sire, animal] = -alpha/2 + Ainv[sire, animal]
      Ainv[sire, sire] = alpha/4 + Ainv[sire, sire]
    } else if (sire == 0 & dam != 0) {
      # dam known
      alpha = 4/3
      Ainv[animal, animal] = alpha + Ainv[animal, animal]
      Ainv[animal, dam] = -alpha/2 + Ainv[animal, dam]
      Ainv[dam, animal] = -alpha/2 + Ainv[dam, animal]
      Ainv[dam, dam] = alpha/4 + Ainv[dam, dam]
    } else {
      # both parents known
      alpha = 2
      Ainv[animal, animal] = alpha + Ainv[animal, animal]
      Ainv[animal, sire] = -alpha/2 + Ainv[animal, sire]
      Ainv[sire, animal] = -alpha/2 + Ainv[sire, animal]
      Ainv[animal, dam] = -alpha/2 + Ainv[animal, dam]
      Ainv[dam, animal] = -alpha/2 + Ainv[dam, animal]
      Ainv[sire, sire] = alpha/4 + Ainv[sire, sire]
      Ainv[sire, dam] = alpha/4 + Ainv[sire, dam]
      Ainv[dam, sire] = alpha/4 + Ainv[dam, sire]
      Ainv[dam, dam] = alpha/4 + Ainv[dam, dam]
    }
  }
  return(Ainv)
}

# set working directory 
setwd(".") 

# print working directory 
getwd()

# prdigree and data

# read pedigree : animal sire dam
pedi = read.table("fem_snp_pedi.txt", header = TRUE, sep = " ", stringsAsFactors = FALSE)

# print
pedi

# read data : animal, sex, pre-weaning gain
data = read.table("fem_snp_data.txt", header = TRUE, sep = " ", stringsAsFactors = FALSE)

# print
data

# variance component and ratio
sigma_a = 35.241
sigma_e = 245
alpha = sigma_e/sigma_a

# print
sigma_a
sigma_e
alpha

# observation
y = data[1:8, 6]

# print
y

# design matrix

# design matrix of fixed effect
X = matrix(rep(1, 8), 8, 1)

# print
X

# SNP effect
M = as.matrix(data[1:8, 7:9])

# design matrix for SNP effect
# mean of SNP effect
M_all = as.matrix(data[, 7:9])
snp_mean = colMeans(M_all) / 2
Z = sweep(M, 2, snp_mean * 2)
Z

# design matrix for polygenic effect
W = cbind(matrix(rep(0, 8 * 12), 8, 12), diag(8), matrix(rep(0, 8 * 6), 8, 6))
W

# inverse matrix of NRM
ai = ainv(pedi)

# print
ai

# LHS, RHS

# LHS
LHS = rbind(
  cbind(t(X) %*% X, t(X) %*% Z, t(X) %*% W), 
  cbind(t(Z) %*% X, t(Z) %*% Z, t(Z) %*% W),
  cbind(t(W) %*% X, t(W) %*% Z, t(W) %*% W + ai * alpha))

# print
LHS

# RHS
RHS = rbind(t(X) %*% y, 
            t(Z) %*% y,
            t(W) %*% y)

# print
RHS

# Solutions

# generalized inverse of LHS
gi_LHS = ginv(LHS)

# print
gi_LHS

# solution
sol = gi_LHS %*% RHS

# print
sol

# mean effect of solutions
mean_eff = sol[1]
mean_eff

# snp effect of solutions
snp_eff = sol[2:4]
snp_eff

# polygenic effect of solutions
polge_eff = sol[5:30]
polge_eff

# calculate DGV
# genotype of animals

# design matrix of animals
P2 = sweep(M_all, 2, snp_mean * 2)
P2

# DGV
dgv = P2 %*% snp_eff
dgv

# GEBV : animal 13 ~ 26
gebv = dgv + polge_eff[13:26]
gebv

 

Result

RStudio에서 실행한 결과

 

> # Fixed Effect Model for SNP Effect, unweighted analysis
> 
> # Linear Models for the Prediction of Animal Breeding Values, 3rd Edition.
> 
> # Raphael Mrode
> 
> # Example 11.1 p180
> 
> # MASS packages
> if (!("MASS" %in% installed.packages())) {
+   install.packages("MASS", dependencies = TRUE)
+ }
> library(MASS)
> 
> # functions
> 
> # make design matrix 
> desgn <- function(v) {
+   if (is.numeric(v)) {
+     va = v
+     mrow = length(va)
+     mcol = max(va)
+   }
+   if (is.character(v)) {
+     vf = factor(v)
+     # 각 수준의 인덱스 값을 저장
+     va = as.numeric(vf)
+     mrow = length(va)
+     mcol = length(levels(vf))
+   }
+   
+   # 0으로 채워진 X 준비
+   X = matrix(data = c(0), nrow = mrow, ncol = mcol)
+   
+   for (i in 1:mrow) {
+     ic = va[i]
+     X[i, ic] = 1
+   }
+   return(X)
+ }
> 
> # function to make inverse of numerator relationship matrix
> ainv = function(pedi) {
+   n = nrow(pedi)
+   Ainv = matrix(c(0), nrow = n, ncol = n)
+   
+   for (i in 1:n) {
+     animal = pedi[i, 1]
+     sire = pedi[i, 2]
+     dam = pedi[i, 3]
+     
+     if (sire == 0 & dam == 0) {
+       # both parents unknown
+       alpha = 1
+       Ainv[animal, animal] = alpha + Ainv[animal, animal]
+     } else if (sire != 0 & dam == 0) {
+       # sire known
+       alpha = 4/3
+       Ainv[animal, animal] = alpha + Ainv[animal, animal]
+       Ainv[animal, sire] = -alpha/2 + Ainv[animal, sire]
+       Ainv[sire, animal] = -alpha/2 + Ainv[sire, animal]
+       Ainv[sire, sire] = alpha/4 + Ainv[sire, sire]
+     } else if (sire == 0 & dam != 0) {
+       # dam known
+       alpha = 4/3
+       Ainv[animal, animal] = alpha + Ainv[animal, animal]
+       Ainv[animal, dam] = -alpha/2 + Ainv[animal, dam]
+       Ainv[dam, animal] = -alpha/2 + Ainv[dam, animal]
+       Ainv[dam, dam] = alpha/4 + Ainv[dam, dam]
+     } else {
+       # both parents known
+       alpha = 2
+       Ainv[animal, animal] = alpha + Ainv[animal, animal]
+       Ainv[animal, sire] = -alpha/2 + Ainv[animal, sire]
+       Ainv[sire, animal] = -alpha/2 + Ainv[sire, animal]
+       Ainv[animal, dam] = -alpha/2 + Ainv[animal, dam]
+       Ainv[dam, animal] = -alpha/2 + Ainv[dam, animal]
+       Ainv[sire, sire] = alpha/4 + Ainv[sire, sire]
+       Ainv[sire, dam] = alpha/4 + Ainv[sire, dam]
+       Ainv[dam, sire] = alpha/4 + Ainv[dam, sire]
+       Ainv[dam, dam] = alpha/4 + Ainv[dam, dam]
+     }
+   }
+   return(Ainv)
+ }
> 
> # set working directory 
> setwd(".") 
> 
> # print working directory 
> getwd()
[1] "D:/users/bhpark/2020/job/공부하기/07_Linear Models for the Prediction of Animal Breeding Values_3rd_Mrode/11_fixed effecct model for SNP effects_unweighted"
> 
> # prdigree and data
> 
> # read pedigree : animal sire dam
> pedi = read.table("fem_snp_pedi.txt", header = TRUE, sep = " ", stringsAsFactors = FALSE)
> 
> # print
> pedi
   animal sire dam
1       1    0   0
2       2    0   0
3       3    0   0
4       4    0   0
5       5    0   0
6       6    0   0
7       7    0   0
8       8    0   0
9       9    0   0
10     10    0   0
11     11    0   0
12     12    0   0
13     13    0   0
14     14    0   0
15     15   13   4
16     16   15   2
17     17   15   5
18     18   14   6
19     19   14   9
20     20   14   9
21     21    1   3
22     22   14   8
23     23   14  11
24     24   14  10
25     25   14   7
26     26   14  12
> 
> # read data : animal, sex, pre-weaning gain
> data = read.table("fem_snp_data.txt", header = TRUE, sep = " ", stringsAsFactors = FALSE)
> 
> # print
> data
   animal sire dam mean edc fat_dyd snp1 snp2 snp3 snp4 snp5 snp6 snp7 snp8 snp9 snp10
1      13    0   0    1 558     9.0    2    0    1    1    0    0    0    2    1     2
2      14    0   0    1 722    13.4    1    0    0    0    0    2    0    2    1     0
3      15   13   4    1 300    12.7    1    1    2    1    1    0    0    2    1     2
4      16   15   2    1  73    15.4    0    0    2    1    0    1    0    2    2     1
5      17   15   5    1  52     5.9    0    1    1    2    0    0    0    2    1     2
6      18   14   6    1  87     7.7    1    1    0    1    0    2    0    2    2     1
7      19   14   9    1  64    10.2    0    0    1    1    0    2    0    2    2     0
8      20   14   9    1 103     4.8    0    1    1    0    0    1    0    2    2     0
9      21    1   3    1  13     7.6    2    0    0    0    0    1    2    2    1     2
10     22   14   8    1 125     8.8    0    0    0    1    1    2    0    2    0     0
11     23   14  11    1  93     9.8    0    1    1    0    0    1    0    2    2     1
12     24   14  10    1  66     9.2    1    0    0    0    1    1    0    2    0     0
13     25   14   7    1  75    11.5    0    0    0    1    1    2    0    2    1     0
14     26   14  12    1  33    13.3    1    0    1    1    0    2    0    1    0     0
> 
> # variance component and ratio
> sigma_a = 35.241
> sigma_e = 245
> alpha = sigma_e/sigma_a
> 
> # print
> sigma_a
[1] 35.241
> sigma_e
[1] 245
> alpha
[1] 6.95213
> 
> # observation
> y = data[1:8, 6]
> 
> # print
> y
[1]  9.0 13.4 12.7 15.4  5.9  7.7 10.2  4.8
> 
> # design matrix
> 
> # design matrix of fixed effect
> X = matrix(rep(1, 8), 8, 1)
> 
> # print
> X
     [,1]
[1,]    1
[2,]    1
[3,]    1
[4,]    1
[5,]    1
[6,]    1
[7,]    1
[8,]    1
> 
> # SNP effect
> M = as.matrix(data[1:8, 7:9])
> 
> # design matrix for SNP effect
> # mean of SNP effect
> M_all = as.matrix(data[, 7:9])
> snp_mean = colMeans(M_all) / 2
> Z = sweep(M, 2, snp_mean * 2)
> Z
        snp1       snp2       snp3
1  1.3571429 -0.3571429  0.2857143
2  0.3571429 -0.3571429 -0.7142857
3  0.3571429  0.6428571  1.2857143
4 -0.6428571 -0.3571429  1.2857143
5 -0.6428571  0.6428571  0.2857143
6  0.3571429  0.6428571 -0.7142857
7 -0.6428571 -0.3571429  0.2857143
8 -0.6428571  0.6428571  0.2857143
> 
> # design matrix for polygenic effect
> W = cbind(matrix(rep(0, 8 * 12), 8, 12), diag(8), matrix(rep(0, 8 * 6), 8, 6))
> W
     [,1] [,2] [,3] [,4] [,5] [,6] [,7] [,8] [,9] [,10] [,11] [,12] [,13] [,14] [,15] [,16] [,17] [,18] [,19] [,20] [,21] [,22] [,23] [,24] [,25] [,26]
[1,]    0    0    0    0    0    0    0    0    0     0     0     0     1     0     0     0     0     0     0     0     0     0     0     0     0     0
[2,]    0    0    0    0    0    0    0    0    0     0     0     0     0     1     0     0     0     0     0     0     0     0     0     0     0     0
[3,]    0    0    0    0    0    0    0    0    0     0     0     0     0     0     1     0     0     0     0     0     0     0     0     0     0     0
[4,]    0    0    0    0    0    0    0    0    0     0     0     0     0     0     0     1     0     0     0     0     0     0     0     0     0     0
[5,]    0    0    0    0    0    0    0    0    0     0     0     0     0     0     0     0     1     0     0     0     0     0     0     0     0     0
[6,]    0    0    0    0    0    0    0    0    0     0     0     0     0     0     0     0     0     1     0     0     0     0     0     0     0     0
[7,]    0    0    0    0    0    0    0    0    0     0     0     0     0     0     0     0     0     0     1     0     0     0     0     0     0     0
[8,]    0    0    0    0    0    0    0    0    0     0     0     0     0     0     0     0     0     0     0     1     0     0     0     0     0     0
> 
> # inverse matrix of NRM
> ai = ainv(pedi)
> 
> # print
> ai
      [,1] [,2] [,3] [,4] [,5] [,6] [,7] [,8] [,9] [,10] [,11] [,12] [,13] [,14] [,15] [,16] [,17] [,18] [,19] [,20] [,21] [,22] [,23] [,24] [,25]
 [1,]  1.5  0.0  0.5  0.0  0.0  0.0  0.0  0.0    0   0.0   0.0   0.0   0.0   0.0   0.0     0     0     0     0     0    -1     0     0     0     0
 [2,]  0.0  1.5  0.0  0.0  0.0  0.0  0.0  0.0    0   0.0   0.0   0.0   0.0   0.0   0.5    -1     0     0     0     0     0     0     0     0     0
 [3,]  0.5  0.0  1.5  0.0  0.0  0.0  0.0  0.0    0   0.0   0.0   0.0   0.0   0.0   0.0     0     0     0     0     0    -1     0     0     0     0
 [4,]  0.0  0.0  0.0  1.5  0.0  0.0  0.0  0.0    0   0.0   0.0   0.0   0.5   0.0  -1.0     0     0     0     0     0     0     0     0     0     0
 [5,]  0.0  0.0  0.0  0.0  1.5  0.0  0.0  0.0    0   0.0   0.0   0.0   0.0   0.0   0.5     0    -1     0     0     0     0     0     0     0     0
 [6,]  0.0  0.0  0.0  0.0  0.0  1.5  0.0  0.0    0   0.0   0.0   0.0   0.0   0.5   0.0     0     0    -1     0     0     0     0     0     0     0
 [7,]  0.0  0.0  0.0  0.0  0.0  0.0  1.5  0.0    0   0.0   0.0   0.0   0.0   0.5   0.0     0     0     0     0     0     0     0     0     0    -1
 [8,]  0.0  0.0  0.0  0.0  0.0  0.0  0.0  1.5    0   0.0   0.0   0.0   0.0   0.5   0.0     0     0     0     0     0     0    -1     0     0     0
 [9,]  0.0  0.0  0.0  0.0  0.0  0.0  0.0  0.0    2   0.0   0.0   0.0   0.0   1.0   0.0     0     0     0    -1    -1     0     0     0     0     0
[10,]  0.0  0.0  0.0  0.0  0.0  0.0  0.0  0.0    0   1.5   0.0   0.0   0.0   0.5   0.0     0     0     0     0     0     0     0     0    -1     0
[11,]  0.0  0.0  0.0  0.0  0.0  0.0  0.0  0.0    0   0.0   1.5   0.0   0.0   0.5   0.0     0     0     0     0     0     0     0    -1     0     0
[12,]  0.0  0.0  0.0  0.0  0.0  0.0  0.0  0.0    0   0.0   0.0   1.5   0.0   0.5   0.0     0     0     0     0     0     0     0     0     0     0
[13,]  0.0  0.0  0.0  0.5  0.0  0.0  0.0  0.0    0   0.0   0.0   0.0   1.5   0.0  -1.0     0     0     0     0     0     0     0     0     0     0
[14,]  0.0  0.0  0.0  0.0  0.0  0.5  0.5  0.5    1   0.5   0.5   0.5   0.0   5.0   0.0     0     0    -1    -1    -1     0    -1    -1    -1    -1
[15,]  0.0  0.5  0.0 -1.0  0.5  0.0  0.0  0.0    0   0.0   0.0   0.0  -1.0   0.0   3.0    -1    -1     0     0     0     0     0     0     0     0
[16,]  0.0 -1.0  0.0  0.0  0.0  0.0  0.0  0.0    0   0.0   0.0   0.0   0.0   0.0  -1.0     2     0     0     0     0     0     0     0     0     0
[17,]  0.0  0.0  0.0  0.0 -1.0  0.0  0.0  0.0    0   0.0   0.0   0.0   0.0   0.0  -1.0     0     2     0     0     0     0     0     0     0     0
[18,]  0.0  0.0  0.0  0.0  0.0 -1.0  0.0  0.0    0   0.0   0.0   0.0   0.0  -1.0   0.0     0     0     2     0     0     0     0     0     0     0
[19,]  0.0  0.0  0.0  0.0  0.0  0.0  0.0  0.0   -1   0.0   0.0   0.0   0.0  -1.0   0.0     0     0     0     2     0     0     0     0     0     0
[20,]  0.0  0.0  0.0  0.0  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     2     0     0     0     0     0
[21,] -1.0  0.0 -1.0  0.0  0.0  0.0  0.0  0.0    0   0.0   0.0   0.0   0.0   0.0   0.0     0     0     0     0     0     2     0     0     0     0
[22,]  0.0  0.0  0.0  0.0  0.0  0.0  0.0 -1.0    0   0.0   0.0   0.0   0.0  -1.0   0.0     0     0     0     0     0     0     2     0     0     0
[23,]  0.0  0.0  0.0  0.0  0.0  0.0  0.0  0.0    0   0.0  -1.0   0.0   0.0  -1.0   0.0     0     0     0     0     0     0     0     2     0     0
[24,]  0.0  0.0  0.0  0.0  0.0  0.0  0.0  0.0    0  -1.0   0.0   0.0   0.0  -1.0   0.0     0     0     0     0     0     0     0     0     2     0
[25,]  0.0  0.0  0.0  0.0  0.0  0.0 -1.0  0.0    0   0.0   0.0   0.0   0.0  -1.0   0.0     0     0     0     0     0     0     0     0     0     2
[26,]  0.0  0.0  0.0  0.0  0.0  0.0  0.0  0.0    0   0.0   0.0  -1.0   0.0  -1.0   0.0     0     0     0     0     0     0     0     0     0     0
      [,26]
 [1,]     0
 [2,]     0
 [3,]     0
 [4,]     0
 [5,]     0
 [6,]     0
 [7,]     0
 [8,]     0
 [9,]     0
[10,]     0
[11,]     0
[12,]    -1
[13,]     0
[14,]    -1
[15,]     0
[16,]     0
[17,]     0
[18,]     0
[19,]     0
[20,]     0
[21,]     0
[22,]     0
[23,]     0
[24,]     0
[25,]     0
[26,]     2
> 
> # LHS, RHS
> 
> # LHS
> LHS = rbind(
+   cbind(t(X) %*% X, t(X) %*% Z, t(X) %*% W), 
+   cbind(t(Z) %*% X, t(Z) %*% Z, t(Z) %*% W),
+   cbind(t(W) %*% X, t(W) %*% Z, t(W) %*% W + ai * alpha))
> 
> # print
> LHS
                      snp1       snp2       snp3                                                                                                   
      8.0000000 -0.1428571  1.1428571  2.2857143  0.000000  0.000000  0.000000  0.000000  0.000000  0.000000  0.000000  0.000000  0.00000  0.000000
snp1 -0.1428571  3.8775510 -0.5204082 -1.0408163  0.000000  0.000000  0.000000  0.000000  0.000000  0.000000  0.000000  0.000000  0.00000  0.000000
snp2  1.1428571 -0.5204082  2.1632653  0.3265306  0.000000  0.000000  0.000000  0.000000  0.000000  0.000000  0.000000  0.000000  0.00000  0.000000
snp3  2.2857143 -1.0408163  0.3265306  4.6530612  0.000000  0.000000  0.000000  0.000000  0.000000  0.000000  0.000000  0.000000  0.00000  0.000000
      0.0000000  0.0000000  0.0000000  0.0000000 10.428194  0.000000  3.476065  0.000000  0.000000  0.000000  0.000000  0.000000  0.00000  0.000000
      0.0000000  0.0000000  0.0000000  0.0000000  0.000000 10.428194  0.000000  0.000000  0.000000  0.000000  0.000000  0.000000  0.00000  0.000000
      0.0000000  0.0000000  0.0000000  0.0000000  3.476065  0.000000 10.428194  0.000000  0.000000  0.000000  0.000000  0.000000  0.00000  0.000000
      0.0000000  0.0000000  0.0000000  0.0000000  0.000000  0.000000  0.000000 10.428194  0.000000  0.000000  0.000000  0.000000  0.00000  0.000000
      0.0000000  0.0000000  0.0000000  0.0000000  0.000000  0.000000  0.000000  0.000000 10.428194  0.000000  0.000000  0.000000  0.00000  0.000000
      0.0000000  0.0000000  0.0000000  0.0000000  0.000000  0.000000  0.000000  0.000000  0.000000 10.428194  0.000000  0.000000  0.00000  0.000000
      0.0000000  0.0000000  0.0000000  0.0000000  0.000000  0.000000  0.000000  0.000000  0.000000  0.000000 10.428194  0.000000  0.00000  0.000000
      0.0000000  0.0000000  0.0000000  0.0000000  0.000000  0.000000  0.000000  0.000000  0.000000  0.000000  0.000000 10.428194  0.00000  0.000000
      0.0000000  0.0000000  0.0000000  0.0000000  0.000000  0.000000  0.000000  0.000000  0.000000  0.000000  0.000000  0.000000 13.90426  0.000000
      0.0000000  0.0000000  0.0000000  0.0000000  0.000000  0.000000  0.000000  0.000000  0.000000  0.000000  0.000000  0.000000  0.00000 10.428194
      0.0000000  0.0000000  0.0000000  0.0000000  0.000000  0.000000  0.000000  0.000000  0.000000  0.000000  0.000000  0.000000  0.00000  0.000000
      0.0000000  0.0000000  0.0000000  0.0000000  0.000000  0.000000  0.000000  0.000000  0.000000  0.000000  0.000000  0.000000  0.00000  0.000000
      1.0000000  1.3571429 -0.3571429  0.2857143  0.000000  0.000000  0.000000  3.476065  0.000000  0.000000  0.000000  0.000000  0.00000  0.000000
      1.0000000  0.3571429 -0.3571429 -0.7142857  0.000000  0.000000  0.000000  0.000000  0.000000  3.476065  3.476065  3.476065  6.95213  3.476065
      1.0000000  0.3571429  0.6428571  1.2857143  0.000000  3.476065  0.000000 -6.952130  3.476065  0.000000  0.000000  0.000000  0.00000  0.000000
      1.0000000 -0.6428571 -0.3571429  1.2857143  0.000000 -6.952130  0.000000  0.000000  0.000000  0.000000  0.000000  0.000000  0.00000  0.000000
      1.0000000 -0.6428571  0.6428571  0.2857143  0.000000  0.000000  0.000000  0.000000 -6.952130  0.000000  0.000000  0.000000  0.00000  0.000000
      1.0000000  0.3571429  0.6428571 -0.7142857  0.000000  0.000000  0.000000  0.000000  0.000000 -6.952130  0.000000  0.000000  0.00000  0.000000
      1.0000000 -0.6428571 -0.3571429  0.2857143  0.000000  0.000000  0.000000  0.000000  0.000000  0.000000  0.000000  0.000000 -6.95213  0.000000
      1.0000000 -0.6428571  0.6428571  0.2857143  0.000000  0.000000  0.000000  0.000000  0.000000  0.000000  0.000000  0.000000 -6.95213  0.000000
      0.0000000  0.0000000  0.0000000  0.0000000 -6.952130  0.000000 -6.952130  0.000000  0.000000  0.000000  0.000000  0.000000  0.00000  0.000000
      0.0000000  0.0000000  0.0000000  0.0000000  0.000000  0.000000  0.000000  0.000000  0.000000  0.000000  0.000000 -6.952130  0.00000  0.000000
      0.0000000  0.0000000  0.0000000  0.0000000  0.000000  0.000000  0.000000  0.000000  0.000000  0.000000  0.000000  0.000000  0.00000  0.000000
      0.0000000  0.0000000  0.0000000  0.0000000  0.000000  0.000000  0.000000  0.000000  0.000000  0.000000  0.000000  0.000000  0.00000 -6.952130
      0.0000000  0.0000000  0.0000000  0.0000000  0.000000  0.000000  0.000000  0.000000  0.000000  0.000000 -6.952130  0.000000  0.00000  0.000000
      0.0000000  0.0000000  0.0000000  0.0000000  0.000000  0.000000  0.000000  0.000000  0.000000  0.000000  0.000000  0.000000  0.00000  0.000000
                                                                                                                                                    
      0.000000  0.000000  1.0000000  1.0000000  1.0000000  1.0000000  1.0000000  1.0000000  1.0000000  1.0000000  0.00000  0.00000  0.00000  0.00000
snp1  0.000000  0.000000  1.3571429  0.3571429  0.3571429 -0.6428571 -0.6428571  0.3571429 -0.6428571 -0.6428571  0.00000  0.00000  0.00000  0.00000
snp2  0.000000  0.000000 -0.3571429 -0.3571429  0.6428571 -0.3571429  0.6428571  0.6428571 -0.3571429  0.6428571  0.00000  0.00000  0.00000  0.00000
snp3  0.000000  0.000000  0.2857143 -0.7142857  1.2857143  1.2857143  0.2857143 -0.7142857  0.2857143  0.2857143  0.00000  0.00000  0.00000  0.00000
      0.000000  0.000000  0.0000000  0.0000000  0.0000000  0.0000000  0.0000000  0.0000000  0.0000000  0.0000000 -6.95213  0.00000  0.00000  0.00000
      0.000000  0.000000  0.0000000  0.0000000  3.4760648 -6.9521296  0.0000000  0.0000000  0.0000000  0.0000000  0.00000  0.00000  0.00000  0.00000
      0.000000  0.000000  0.0000000  0.0000000  0.0000000  0.0000000  0.0000000  0.0000000  0.0000000  0.0000000 -6.95213  0.00000  0.00000  0.00000
      0.000000  0.000000  3.4760648  0.0000000 -6.9521296  0.0000000  0.0000000  0.0000000  0.0000000  0.0000000  0.00000  0.00000  0.00000  0.00000
      0.000000  0.000000  0.0000000  0.0000000  3.4760648  0.0000000 -6.9521296  0.0000000  0.0000000  0.0000000  0.00000  0.00000  0.00000  0.00000
      0.000000  0.000000  0.0000000  3.4760648  0.0000000  0.0000000  0.0000000 -6.9521296  0.0000000  0.0000000  0.00000  0.00000  0.00000  0.00000
      0.000000  0.000000  0.0000000  3.4760648  0.0000000  0.0000000  0.0000000  0.0000000  0.0000000  0.0000000  0.00000  0.00000  0.00000  0.00000
      0.000000  0.000000  0.0000000  3.4760648  0.0000000  0.0000000  0.0000000  0.0000000  0.0000000  0.0000000  0.00000 -6.95213  0.00000  0.00000
      0.000000  0.000000  0.0000000  6.9521296  0.0000000  0.0000000  0.0000000  0.0000000 -6.9521296 -6.9521296  0.00000  0.00000  0.00000  0.00000
      0.000000  0.000000  0.0000000  3.4760648  0.0000000  0.0000000  0.0000000  0.0000000  0.0000000  0.0000000  0.00000  0.00000  0.00000 -6.95213
     10.428194  0.000000  0.0000000  3.4760648  0.0000000  0.0000000  0.0000000  0.0000000  0.0000000  0.0000000  0.00000  0.00000 -6.95213  0.00000
      0.000000 10.428194  0.0000000  3.4760648  0.0000000  0.0000000  0.0000000  0.0000000  0.0000000  0.0000000  0.00000  0.00000  0.00000  0.00000
      0.000000  0.000000 11.4281944  0.0000000 -6.9521296  0.0000000  0.0000000  0.0000000  0.0000000  0.0000000  0.00000  0.00000  0.00000  0.00000
      3.476065  3.476065  0.0000000 35.7606481  0.0000000  0.0000000  0.0000000 -6.9521296 -6.9521296 -6.9521296  0.00000 -6.95213 -6.95213 -6.95213
      0.000000  0.000000 -6.9521296  0.0000000 21.8563889 -6.9521296 -6.9521296  0.0000000  0.0000000  0.0000000  0.00000  0.00000  0.00000  0.00000
      0.000000  0.000000  0.0000000  0.0000000 -6.9521296 14.9042592  0.0000000  0.0000000  0.0000000  0.0000000  0.00000  0.00000  0.00000  0.00000
      0.000000  0.000000  0.0000000  0.0000000 -6.9521296  0.0000000 14.9042592  0.0000000  0.0000000  0.0000000  0.00000  0.00000  0.00000  0.00000
      0.000000  0.000000  0.0000000 -6.9521296  0.0000000  0.0000000  0.0000000 14.9042592  0.0000000  0.0000000  0.00000  0.00000  0.00000  0.00000
      0.000000  0.000000  0.0000000 -6.9521296  0.0000000  0.0000000  0.0000000  0.0000000 14.9042592  0.0000000  0.00000  0.00000  0.00000  0.00000
      0.000000  0.000000  0.0000000 -6.9521296  0.0000000  0.0000000  0.0000000  0.0000000  0.0000000 14.9042592  0.00000  0.00000  0.00000  0.00000
      0.000000  0.000000  0.0000000  0.0000000  0.0000000  0.0000000  0.0000000  0.0000000  0.0000000  0.0000000 13.90426  0.00000  0.00000  0.00000
      0.000000  0.000000  0.0000000 -6.9521296  0.0000000  0.0000000  0.0000000  0.0000000  0.0000000  0.0000000  0.00000 13.90426  0.00000  0.00000
     -6.952130  0.000000  0.0000000 -6.9521296  0.0000000  0.0000000  0.0000000  0.0000000  0.0000000  0.0000000  0.00000  0.00000 13.90426  0.00000
      0.000000  0.000000  0.0000000 -6.9521296  0.0000000  0.0000000  0.0000000  0.0000000  0.0000000  0.0000000  0.00000  0.00000  0.00000 13.90426
      0.000000  0.000000  0.0000000 -6.9521296  0.0000000  0.0000000  0.0000000  0.0000000  0.0000000  0.0000000  0.00000  0.00000  0.00000  0.00000
      0.000000 -6.952130  0.0000000 -6.9521296  0.0000000  0.0000000  0.0000000  0.0000000  0.0000000  0.0000000  0.00000  0.00000  0.00000  0.00000
                      
      0.00000  0.00000
snp1  0.00000  0.00000
snp2  0.00000  0.00000
snp3  0.00000  0.00000
      0.00000  0.00000
      0.00000  0.00000
      0.00000  0.00000
      0.00000  0.00000
      0.00000  0.00000
      0.00000  0.00000
     -6.95213  0.00000
      0.00000  0.00000
      0.00000  0.00000
      0.00000  0.00000
      0.00000  0.00000
      0.00000 -6.95213
      0.00000  0.00000
     -6.95213 -6.95213
      0.00000  0.00000
      0.00000  0.00000
      0.00000  0.00000
      0.00000  0.00000
      0.00000  0.00000
      0.00000  0.00000
      0.00000  0.00000
      0.00000  0.00000
      0.00000  0.00000
      0.00000  0.00000
     13.90426  0.00000
      0.00000 13.90426
> 
> # RHS
> RHS = rbind(t(X) %*% y, 
+             t(Z) %*% y,
+             t(W) %*% y)
> 
> # print
> RHS
      [,1]
     79.10
snp1  0.95
snp2  2.85
snp3 29.60
      0.00
      0.00
      0.00
      0.00
      0.00
      0.00
      0.00
      0.00
      0.00
      0.00
      0.00
      0.00
      9.00
     13.40
     12.70
     15.40
      5.90
      7.70
     10.20
      4.80
      0.00
      0.00
      0.00
      0.00
      0.00
      0.00
> 
> # Solutions
> 
> # generalized inverse of LHS
> gi_LHS = ginv(LHS)
> 
> # print
> gi_LHS
               [,1]          [,2]          [,3]          [,4]          [,5]          [,6]          [,7]          [,8]          [,9]         [,10]
 [1,]  2.029219e-01 -3.255082e-02 -8.500123e-02 -9.538670e-02 -8.813991e-17 -7.749019e-03 -5.907393e-17 -7.339499e-03 -8.423214e-03 -1.124651e-02
 [2,] -3.255082e-02  3.248575e-01  8.134068e-02  9.542976e-02 -1.098782e-16  1.072783e-02  1.818259e-16 -4.884210e-03  1.164245e-02 -4.992010e-03
 [3,] -8.500123e-02  8.134068e-02  5.604372e-01  2.307770e-02  4.164755e-16  2.081808e-02  1.597493e-16 -1.878828e-02 -1.406575e-02 -1.939051e-02
 [4,] -9.538670e-02  9.542976e-02  2.307770e-02  3.295430e-01 -5.230087e-17 -1.481457e-02  5.306137e-17 -2.760843e-02  5.080273e-03  1.647388e-02
 [5,]  2.329623e-18 -3.804909e-16  6.298383e-19 -2.879471e-16  1.438408e-01  4.622486e-16  1.249001e-16 -5.883868e-16  7.059733e-16 -2.124216e-18
 [6,] -7.749019e-03  1.072783e-02  2.081808e-02 -1.481457e-02  1.074399e-17  1.418405e-01  5.505648e-17 -1.363038e-04  3.553867e-04 -1.322611e-03
 [7,] -9.458291e-17 -2.158422e-16  1.088446e-16 -2.082423e-16  1.249001e-16  4.318604e-16  1.438408e-01 -6.173507e-16  7.389172e-16 -6.170158e-17
 [8,] -7.339499e-03 -4.884210e-03 -1.878828e-02 -2.760843e-02 -6.340412e-17 -1.363038e-04  1.479604e-17  1.424464e-01 -7.088821e-04  1.078415e-04
 [9,] -8.423214e-03  1.164245e-02 -1.406575e-02  5.080273e-03  2.970513e-17  3.553867e-04  3.015109e-17 -7.088821e-04  1.407702e-01  1.005601e-03
[10,] -1.124651e-02 -4.992010e-03 -1.939051e-02  1.647388e-02 -6.298026e-17 -1.322611e-03  7.610779e-18  1.078415e-04  1.005601e-03  1.415624e-01
[11,]  2.602085e-18 -8.933826e-17 -2.775558e-17 -2.263814e-16 -1.173397e-16  2.873136e-18 -1.536369e-16  6.017322e-17 -1.454864e-17  5.895349e-17
[12,] -2.862294e-17  2.602085e-17  2.428613e-17 -5.030698e-17  6.117076e-17  1.610040e-17  1.851744e-16  4.044074e-17 -3.342731e-17  1.669671e-17
[13,] -1.922866e-02  2.429536e-02  5.003082e-03  3.922659e-03 -7.208471e-17  1.958920e-03 -5.429396e-17  1.885193e-03  1.888782e-03  7.119691e-04
[14,] -1.214306e-17  6.765422e-17 -1.474515e-16 -4.336809e-18 -1.062664e-17  1.512462e-17 -1.474799e-16  4.380177e-17  5.109303e-18 -2.287667e-17
[15,]  3.469447e-18 -1.170938e-16  1.214306e-17 -9.020562e-17  1.516989e-16 -5.025277e-17  1.248652e-16  1.647987e-17  1.526015e-17  4.526544e-18
[16,] -1.821460e-17  7.546047e-17 -3.816392e-17  1.040834e-17 -4.032172e-17 -3.680866e-17 -3.779099e-18  7.166576e-17 -1.586323e-17  3.686287e-18
[17,] -2.676957e-02 -5.539375e-02  4.722536e-03 -3.923171e-02  1.830766e-16  4.916905e-04 -2.540570e-17  6.855658e-04 -2.361692e-03  8.977596e-04
[18,] -6.308434e-02  1.860433e-02  2.170085e-02  5.617790e-02 -6.347784e-17  6.532274e-04  3.333540e-17 -4.389663e-04  2.891378e-03  8.778743e-04
[19,] -2.439404e-02 -3.502319e-02 -2.582116e-02 -6.102850e-02  8.483571e-17  4.138959e-05  2.492510e-17  7.017152e-02 -2.244169e-03  6.106421e-04
[20,] -2.382055e-02 -1.419855e-03  1.831654e-02 -5.273610e-02  7.677376e-17  6.894064e-02  7.392803e-17  3.488130e-02 -5.890045e-04 -1.678596e-03
[21,] -2.483184e-02 -4.792207e-05 -3.400920e-02 -2.289384e-02  1.050041e-17  5.537749e-04  6.646180e-17  3.402244e-02  6.619246e-02  1.813723e-03
[22,] -4.841193e-02  1.814151e-03 -1.823534e-02  5.279976e-02 -7.729679e-17 -1.657303e-03 -3.493507e-18 -5.772085e-05  2.954091e-03  6.894169e-02
[23,] -5.362241e-02  3.632630e-02  3.465475e-02  3.278580e-02 -7.449488e-17  2.983928e-03  1.998763e-17  1.035410e-03  2.862601e-03  5.004040e-04
[24,] -4.791926e-02  3.086876e-02 -2.947737e-03  3.123741e-02 -1.102180e-16  1.587140e-03 -3.077451e-17  2.296009e-03  3.806341e-03  1.801408e-03
[25,]  7.898705e-18 -5.436645e-16 -2.488422e-17 -3.772473e-16  7.192041e-02  6.303627e-16  7.192041e-02 -8.573219e-16  1.034442e-15 -2.944094e-17
[26,] -3.154217e-02  9.302166e-03  1.085042e-02  2.808895e-02  2.137914e-17  3.266137e-04  1.717343e-16 -2.194831e-04  1.445689e-03  4.389372e-04
[27,] -3.154217e-02  9.302166e-03  1.085042e-02  2.808895e-02  5.746220e-17  3.266137e-04  1.583557e-16 -2.194831e-04  1.445689e-03  4.389372e-04
[28,] -3.154217e-02  9.302166e-03  1.085042e-02  2.808895e-02 -4.739571e-17  3.266137e-04 -8.756498e-17 -2.194831e-04  1.445689e-03  4.389372e-04
[29,] -3.154217e-02  9.302166e-03  1.085042e-02  2.808895e-02 -1.313957e-16  3.266137e-04 -9.889781e-17 -2.194831e-04  1.445689e-03  4.389372e-04
[30,] -3.154217e-02  9.302166e-03  1.085042e-02  2.808895e-02 -6.701577e-17  3.266137e-04 -2.937504e-17 -2.194831e-04  1.445689e-03  4.389372e-04
              [,11]         [,12]         [,13]         [,14]         [,15]         [,16]         [,17]         [,18]         [,19]         [,20]
 [1,] -5.551115e-17 -7.806256e-18 -1.922866e-02  5.204170e-18 -2.775558e-17 -2.515349e-17 -2.676957e-02 -6.308434e-02 -2.439404e-02 -2.382055e-02
 [2,] -9.540979e-18  6.938894e-18  2.429536e-02 -8.760354e-17  1.066855e-16 -9.714451e-17 -5.539375e-02  1.860433e-02 -3.502319e-02 -1.419855e-03
 [3,] -1.439820e-16 -1.561251e-17  5.003082e-03 -1.422473e-16  7.459311e-17 -1.214306e-17  4.722536e-03  2.170085e-02 -2.582116e-02  1.831654e-02
 [4,] -1.821460e-17 -5.204170e-18  3.922659e-03 -9.194034e-17  3.729655e-17 -4.597017e-17 -3.923171e-02  5.617790e-02 -6.102850e-02 -5.273610e-02
 [5,] -5.247009e-17 -1.715734e-16  2.485646e-16 -1.381342e-16 -1.409332e-16 -2.060732e-16 -7.631449e-17  8.574258e-17 -3.209790e-16  3.571539e-17
 [6,] -2.883978e-17 -1.149254e-17  1.958920e-03  7.155734e-18  6.619054e-17 -3.393553e-17  4.916905e-04  6.532274e-04  4.138959e-05  6.894064e-02
 [7,] -1.441928e-16 -8.548200e-17  3.535717e-16 -2.256486e-16 -2.257681e-16 -3.538410e-17 -2.422365e-17  2.345273e-16 -3.973950e-16  5.911108e-17
 [8,]  4.857226e-17  5.724587e-17  1.885193e-03  6.071532e-17  2.320193e-17  5.822166e-17  6.855658e-04 -4.389663e-04  7.017152e-02  3.488130e-02
 [9,] -7.264155e-18  3.416592e-17  1.888782e-03 -1.091656e-17  4.254138e-17  1.465706e-17 -2.361692e-03  2.891378e-03 -2.244169e-03 -5.890045e-04
[10,]  1.620882e-17  1.859407e-17  7.119691e-04  1.493488e-17 -1.414884e-17  3.930233e-18  8.977596e-04  8.778743e-04  6.106421e-04 -1.678596e-03
[11,]  1.438408e-01 -1.816039e-17 -2.753874e-17  3.339343e-17 -2.152141e-17 -7.746625e-17  1.052760e-16  1.604619e-17  1.476683e-16  9.432559e-17
[12,]  9.698188e-17  1.438408e-01  2.883978e-17 -8.164042e-17  2.222614e-18 -2.423192e-17  3.024924e-17 -3.903128e-18  6.526897e-17  6.112190e-17
[13,]  9.671083e-17 -6.635317e-17  1.390523e-01  3.512815e-17 -4.380177e-17  4.185020e-17  3.589691e-06 -1.659950e-03  2.829584e-03  4.353172e-03
[14,] -5.177065e-17 -5.187907e-17  5.312591e-17  1.438408e-01 -8.407988e-17 -1.859407e-17 -1.561251e-17  1.084202e-17  4.228388e-17  2.556007e-17
[15,] -6.911789e-17 -1.414884e-17  5.204170e-17  1.778092e-17  1.438408e-01 -6.559423e-18  3.469447e-17 -2.645453e-17  4.401861e-17 -8.782038e-18
[16,]  4.591596e-17  1.263096e-17  2.688821e-17 -1.530893e-16 -5.144539e-17  1.438408e-01 -8.565197e-18 -2.038300e-17  5.009014e-17  2.425902e-17
[17,]  3.946496e-17  2.949030e-17  3.589691e-06  4.130810e-17 -1.702197e-17  3.144186e-17  1.407936e-01  3.330344e-03  7.142513e-02  3.645010e-02
[18,]  1.023487e-16 -2.168404e-17 -1.659950e-03  3.122502e-17  6.505213e-18  2.645453e-17  3.330344e-03  1.381869e-01  1.006723e-03  1.483202e-03
[19,]  5.052382e-17  4.727121e-17  2.829584e-03  6.028164e-17 -6.071532e-18  4.531965e-17  7.142513e-02  1.006723e-03  1.409698e-01  7.054701e-02
[20,]  8.104411e-18  5.204170e-17  4.353172e-03  3.932943e-17  6.114900e-17  6.423898e-18  3.645010e-02  1.483202e-03  7.054701e-02  1.386845e-01
[21,]  2.580401e-17  1.886512e-17  4.247966e-03  1.951564e-17  8.890458e-18  2.092510e-17  3.217003e-02  4.840428e-03  6.711867e-02  3.439000e-02
[22,]  6.071532e-17 -8.239937e-18  2.379786e-04  2.298509e-17 -1.257675e-17  6.505213e-18  3.011811e-03  7.041027e-02  1.419324e-03 -1.776292e-03
[23,]  1.227317e-16 -6.461845e-17  6.646977e-02  2.688821e-17 -2.081668e-17  3.209238e-17  1.827191e-03  6.816151e-02  2.466711e-03  5.709247e-03
[24,]  1.079865e-16 -5.854692e-17  6.613409e-02  3.426079e-17 -3.035766e-17  3.339343e-17  1.510333e-03  6.670550e-02  4.199179e-03  4.480300e-03
[25,] -1.427243e-16 -1.969319e-16  3.742461e-16 -2.425523e-16 -2.478455e-16 -1.864753e-16 -5.631687e-17  1.414053e-16 -5.271757e-16  6.804336e-17
[26,]  1.951564e-17  7.192041e-02 -8.299750e-04  1.461505e-16 -1.821460e-17 -5.854692e-17  1.665172e-03  6.909345e-02  5.033613e-04  7.416012e-04
[27,]  9.150666e-17 -3.035766e-17 -8.299750e-04 -6.158268e-17  7.192041e-02  6.114900e-17  1.665172e-03  6.909345e-02  5.033613e-04  7.416012e-04
[28,]  6.028164e-17 -1.387779e-17 -8.299750e-04  7.192041e-02 -1.647987e-17  1.621966e-16  1.665172e-03  6.909345e-02  5.033613e-04  7.416012e-04
[29,]  7.192041e-02 -6.895526e-17 -8.299750e-04 -9.974660e-18  5.247539e-17 -6.938894e-17  1.665172e-03  6.909345e-02  5.033613e-04  7.416012e-04
[30,] -1.734723e-18  9.107298e-18 -8.299750e-04  1.162265e-16 -6.808790e-17  7.192041e-02  1.665172e-03  6.909345e-02  5.033613e-04  7.416012e-04
              [,21]         [,22]         [,23]         [,24]         [,25]         [,26]         [,27]         [,28]         [,29]         [,30]
 [1,] -2.483184e-02 -4.841193e-02 -5.362241e-02 -4.791926e-02 -1.050221e-16 -3.154217e-02 -3.154217e-02 -3.154217e-02 -3.154217e-02 -3.154217e-02
 [2,] -4.792207e-05  1.814151e-03  3.632630e-02  3.086876e-02  9.667407e-17  9.302166e-03  9.302166e-03  9.302166e-03  9.302166e-03  9.302166e-03
 [3,] -3.400920e-02 -1.823534e-02  3.465475e-02 -2.947737e-03  4.748412e-16  1.085042e-02  1.085042e-02  1.085042e-02  1.085042e-02  1.085042e-02
 [4,] -2.289384e-02  5.279976e-02  3.278580e-02  3.123741e-02  4.566415e-18  2.808895e-02  2.808895e-02  2.808895e-02  2.808895e-02  2.808895e-02
 [5,]  2.611025e-16  7.438382e-17  2.170978e-16  2.341855e-16  7.192041e-02  2.074505e-17 -1.276276e-16 -1.703319e-16 -3.906911e-17  2.854173e-17
 [6,]  5.537749e-04 -1.657303e-03  2.983928e-03  1.587140e-03  6.913168e-17  3.266137e-04  3.266137e-04  3.266137e-04  3.266137e-04  3.266137e-04
 [7,]  2.785272e-16  1.014383e-16  3.715807e-16  3.646591e-16  7.192041e-02 -1.650363e-17 -1.828810e-18  1.472670e-16  8.431150e-17 -1.323675e-17
 [8,]  3.402244e-02 -5.772085e-05  1.035410e-03  2.296009e-03  2.494461e-17 -2.194831e-04 -2.194831e-04 -2.194831e-04 -2.194831e-04 -2.194831e-04
 [9,]  6.619246e-02  2.954091e-03  2.862601e-03  3.806341e-03  5.572096e-17  1.445689e-03  1.445689e-03  1.445689e-03  1.445689e-03  1.445689e-03
[10,]  1.813723e-03  6.894169e-02  5.004040e-04  1.801408e-03 -4.553002e-17  4.389372e-04  4.389372e-04  4.389372e-04  4.389372e-04  4.389372e-04
[11,]  7.686993e-17  3.079134e-17 -4.770490e-18 -4.033232e-17  2.049676e-16  1.214306e-17  1.127570e-17  1.778092e-17  7.192041e-02 -6.765422e-17
[12,]  1.832302e-17 -1.474515e-17  9.974660e-18 -1.734723e-17 -8.632881e-17  7.192041e-02  1.040834e-17 -4.380177e-17  2.558717e-17 -2.341877e-17
[13,]  4.247966e-03  2.379786e-04  6.646977e-02  6.613409e-02 -8.332182e-17 -8.299750e-04 -8.299750e-04 -8.299750e-04 -8.299750e-04 -8.299750e-04
[14,]  4.976488e-17 -1.561251e-17  3.512815e-17  1.994932e-17  7.420757e-17 -4.336809e-17 -7.979728e-17  7.192041e-02 -3.946496e-17  2.255141e-17
[15,]  3.491131e-17 -3.165870e-17  7.372575e-18 -1.778092e-17 -2.976979e-16 -1.647987e-17  7.192041e-02  1.301043e-17 -5.854692e-17 -4.336809e-17
[16,]  1.593777e-17 -2.688821e-17  2.168404e-18 -1.257675e-17  1.265510e-16 -2.168404e-18 -2.255141e-17 -1.543904e-16  3.035766e-17  7.192041e-02
[17,]  3.217003e-02  3.011811e-03  1.827191e-03  1.510333e-03  5.811441e-17  1.665172e-03  1.665172e-03  1.665172e-03  1.665172e-03  1.665172e-03
[18,]  4.840428e-03  7.041027e-02  6.816151e-02  6.670550e-02 -1.879806e-17  6.909345e-02  6.909345e-02  6.909345e-02  6.909345e-02  6.909345e-02
[19,]  6.711867e-02  1.419324e-03  2.466711e-03  4.199179e-03  9.221979e-17  5.033613e-04  5.033613e-04  5.033613e-04  5.033613e-04  5.033613e-04
[20,]  3.439000e-02 -1.776292e-03  5.709247e-03  4.480300e-03  1.174630e-16  7.416012e-04  7.416012e-04  7.416012e-04  7.416012e-04  7.416012e-04
[21,]  1.328480e-01  5.140798e-03  5.527257e-03  7.809102e-03  6.274527e-17  2.420214e-03  2.420214e-03  2.420214e-03  2.420214e-03  2.420214e-03
[22,]  5.140798e-03  1.386177e-01  3.483136e-02  3.605486e-02 -7.342940e-17  3.520513e-02  3.520513e-02  3.520513e-02  3.520513e-02  3.520513e-02
[23,]  5.527257e-03  3.483136e-02  1.352606e-01  6.584049e-02 -3.876275e-17  3.408076e-02  3.408076e-02  3.408076e-02  3.408076e-02  3.408076e-02
[24,]  7.809102e-03  3.605486e-02  6.584049e-02  1.331332e-01 -8.314716e-17  3.335275e-02  3.335275e-02  3.335275e-02  3.335275e-02  3.335275e-02
[25,]  3.845722e-16  8.174885e-17  3.219778e-16  3.378198e-16  1.438408e-01 -5.498276e-17 -1.277190e-16 -4.527046e-17 -1.257900e-17 -3.813977e-17
[26,]  2.420214e-03  3.520513e-02  3.408076e-02  3.335275e-02 -6.326058e-17  1.424273e-01  3.454673e-02  3.454673e-02  3.454673e-02  3.454673e-02
[27,]  2.420214e-03  3.520513e-02  3.408076e-02  3.335275e-02 -2.253800e-16  3.454673e-02  1.424273e-01  3.454673e-02  3.454673e-02  3.454673e-02
[28,]  2.420214e-03  3.520513e-02  3.408076e-02  3.335275e-02  4.798244e-17  3.454673e-02  3.454673e-02  1.424273e-01  3.454673e-02  3.454673e-02
[29,]  2.420214e-03  3.520513e-02  3.408076e-02  3.335275e-02  1.484779e-16  3.454673e-02  3.454673e-02  3.454673e-02  1.424273e-01  3.454673e-02
[30,]  2.420214e-03  3.520513e-02  3.408076e-02  3.335275e-02  5.835403e-17  3.454673e-02  3.454673e-02  3.454673e-02  3.454673e-02  1.424273e-01
> 
> # solution
> sol = gi_LHS %*% RHS
> 
> # print
> sol
               [,1]
 [1,]  9.895358e+00
 [2,]  6.068652e-01
 [3,] -4.080274e+00
 [4,]  1.934155e+00
 [5,] -6.311179e-15
 [6,]  1.220146e-01
 [7,] -6.787405e-15
 [8,]  1.945577e-01
 [9,] -1.042586e-01
[10,]  9.507380e-02
[11,] -1.720163e-15
[12,] -1.656048e-15
[13,] -2.644430e-01
[14,]  1.180100e-16
[15,] -2.138676e-15
[16,] -6.615097e-16
[17,] -2.988163e-01
[18,]  2.558718e-01
[19,]  1.424285e-01
[20,]  2.542361e-01
[21,] -8.517367e-02
[22,]  2.705466e-01
[23,] -9.223368e-02
[24,] -1.807806e-01
[25,] -7.584268e-15
[26,]  1.279359e-01
[27,]  1.279359e-01
[28,]  1.279359e-01
[29,]  1.279359e-01
[30,]  1.279359e-01
> 
> # mean effect of solutions
> mean_eff = sol[1]
> mean_eff
[1] 9.895358
> 
> # snp effect of solutions
> snp_eff = sol[2:4]
> snp_eff
[1]  0.6068652 -4.0802744  1.9341548
> 
> # polygenic effect of solutions
> polge_eff = sol[5:30]
> polge_eff
 [1] -6.311179e-15  1.220146e-01 -6.787405e-15  1.945577e-01 -1.042586e-01  9.507380e-02 -1.720163e-15 -1.656048e-15 -2.644430e-01  1.180100e-16
[11] -2.138676e-15 -6.615097e-16 -2.988163e-01  2.558718e-01  1.424285e-01  2.542361e-01 -8.517367e-02  2.705466e-01 -9.223368e-02 -1.807806e-01
[21] -7.584268e-15  1.279359e-01  1.279359e-01  1.279359e-01  1.279359e-01  1.279359e-01
> 
> # calculate DGV
> # genotype of animals
> 
> # design matrix of animals
> P2 = sweep(M_all, 2, snp_mean * 2)
> P2
            snp1       snp2       snp3
 [1,]  1.3571429 -0.3571429  0.2857143
 [2,]  0.3571429 -0.3571429 -0.7142857
 [3,]  0.3571429  0.6428571  1.2857143
 [4,] -0.6428571 -0.3571429  1.2857143
 [5,] -0.6428571  0.6428571  0.2857143
 [6,]  0.3571429  0.6428571 -0.7142857
 [7,] -0.6428571 -0.3571429  0.2857143
 [8,] -0.6428571  0.6428571  0.2857143
 [9,]  1.3571429 -0.3571429 -0.7142857
[10,] -0.6428571 -0.3571429 -0.7142857
[11,] -0.6428571  0.6428571  0.2857143
[12,]  0.3571429 -0.3571429 -0.7142857
[13,] -0.6428571 -0.3571429 -0.7142857
[14,]  0.3571429 -0.3571429  0.2857143
> 
> # DGV
> dgv = P2 %*% snp_eff
> dgv
             [,1]
 [1,]  2.83345926
 [2,]  0.29243931
 [3,]  0.08047441
 [4,]  3.55388367
 [5,] -2.46054553
 [6,] -3.78783512
 [7,]  1.61972891
 [8,] -2.46054553
 [9,]  0.89930449
[10,] -0.31442586
[11,] -2.46054553
[12,]  0.29243931
[13,] -0.31442586
[14,]  2.22659408
> 
> # GEBV : animal 13 ~ 26
> gebv = dgv + polge_eff[13:26]
> gebv
            [,1]
 [1,]  2.5346429
 [2,]  0.5483112
 [3,]  0.2229029
 [4,]  3.8081198
 [5,] -2.5457192
 [6,] -3.5172885
 [7,]  1.5274952
 [8,] -2.6413261
 [9,]  0.8993045
[10,] -0.1864899
[11,] -2.3326096
[12,]  0.4203752
[13,] -0.1864899
[14,]  2.3545300
> 

 

SNP effect를 고정효과로 하여 추정한 후 개체의 SNP effect의 design matrix를 이용하여 DGV를 추정하였다. DGV와 polygenic effect를 합하여 GEBV를 계산하였다.

 

참고 : Linear Models for the Prediction of Animal Breeding Values, 3rd Edtion. 9.3 Random Regression Model

 

임의 회귀 모형은 해를 구하고 나서도 해야할 일이 많다. 개체의 육종가 그래프를 그려 개체별로 비유기 동안 육종가가 어떻게 변하는지 살펴볼 수도 있다. 사실 모든 개체에 대해서 이렇게 그래프를 그려도 비교하기가 어렵기 때문에 사실상 할 일은 아니지만 그래도 개체의 일일 육종가를 구하여 그래프를 그리는 연습을 해 보자. 왜? 책에 그래프가 있으므로. 그리고 6일부터 310일까지의 일일 육종가를 구하면 305일 육종가가 된다. 아마도 이걸 비교하여 개체를 선발할 것이다. 어떤 프로그램을 사용하든 임의 회귀 모형의 해를 구했다고 가정하고 시작하자. 먼저 비유기 동안 분산의 변화부터 살펴보고나서 개체 육종가 그래프와 305일 육종가를 계산하자.

 

주의해야 할 것이 하나 있다. 예를 들어 유전평가를 할 때 DIM(days in milk, 분만후 비유일수)을 4에서 310을 했을 경우 즉 DIM 4에서 310까지의 르장드르 다항식을 사용하였을 경우 뒤의 모든 분석에서 그 다항식을 사용해야 한다. 예를 들어 305일 유지량을 계산하려면 DIM 6에서 DIM 310까지의 매일 매일 육종가를 더하는데 이때 DIM 6에서 310까지의 르장드르 다항식을 새로 만들어 계산하면 안된다. 유전 평가에서 사용했던 DIM 4에서 310의 르장드르 다항식 중 6에서 310까지만 추출해서 써야 한다. 책에는 설명이 안 되어 있고 해 보니 깨닫게 되는 중요한 사항이다.

 

여기서 르장드르 다항식(Legendre Polynomials)을 구하는 것이 중요한데 그것은 인터넷에서 복사한 것임을 밝힌다. 다음 사이트를 참조한다.

 

morotalab.org/Mrode2005/index.html

 

Mrode2005

 

morotalab.org

 

R 스크립트

# plotting the genetic and permanent environmental variance for DIM
# plotting the animals breeding values for DIM


## Given time points covariate and order of fit for Legendre polynomials, return matrix 'M' containing the polynomials of standardized time. 'M' is order t (number of time points) by k (order of Legendre polynomials).

## Arguments 
##	t: a vector of time, age or days
## 	n: order of polynomials
##      tmax: max time (optional)
##      tmin: min time (optional)

## Literature: Mrode, R.A. 2005. Linear Models for the Prediction of Animal Breeding Values. CAB International, Oxon, UK.

## Author: Gota Morota <morota at wisc dot edu>
## Created: 31-Mar-2010
## Last-Modified: 2-Apr-2010
## License: GPLv3 or later

`stdtime` <-
  function(t, n, tmax, tmin){
    if(missing(tmax)) {
      tmax <- t[which.max(t)]
    }
    if(missing(tmin)) {
      tmin <- t[which.min(t)]
    }
    
    N <- n+1
    M <- matrix(0, nrow=length(t), ncol=N)
    a <- -1 + 2*(t-tmin)/(tmax - tmin)
    M[,1] <- 1
    
    for (i in 2:N){
      M[,i] <- a^(i-1)
    }
    
    return(M)
  }

## Return coefficient matrix (lambda) of n-th order Legendre polynomials 

## Arguments
## 	n: order of polynomials
##	gengler: logical value. If TRUE, Genlger's scaling (1999) will be applied. If not specified, TRUE is assumed.

## Literatures
## Mrode, R.A. 2005. Linear Models for the Prediction of Animal Breeding Values. CAB International, Oxon, UK.
## Gengler, N. et. al. 1999. Estimation of (Co)variance Function Coefficients for Test Day Yield with a Expectation-Maximization Restricted Maximum Likelihood Algorithm.  Journal of Dairy Science.  82

## Author: Gota Morota <morota at wisc dot edu>
## Create: 31-Mar-2010
## Last-Modified: 2-Apr-2010
## License: GPLv3 or later

`legendre` <-
  function(n, gengler){
    
    if (nargs()==1){
      gengler <- TRUE	
    }
    
    if (gengler != TRUE & gengler != FALSE){
      gengler=TRUE	
    }
    
    N <- n+1
    L <- matrix(0,nrow=N, ncol=N)
    
    for(i in (1:N)){
      if(i==1){
        L[i,i] <- 1
      }
      else if(i==2){
        L[i,i] <- 1
      }
      else  {
        tmp <- L[i-1,]
        tmp2 <- as.numeric()
        tmp2 <- c(0,tmp[1:(N-1)])
        L[i,] <- (1/(i-2+1))*( (2*(i-2) + 1)*tmp2 -(i-2)*L[i-2,] )
      }
    }
    
    # Normalize
    for (j in (1:N)){	
      L[j,] <- (sqrt( (2*(j-1)+1)/2)  )*L[j,]
    }
    
    
    # Gengler (1999)
    if (gengler==TRUE){
      L <- sqrt(2)*L
    }
    
    return(L)
    
  }

## example
## DIM <- c(4,38,72,106,140,174,208,242,276,310)
## order <- 4
## M <- stdtime(DIM, order)
## Lambda <- legendre(order, gengler=FALSE)
## Phi <- M%*%t(Lambda)
## Phi


# days in milk
dim = c(4:310)
head(dim)
tail(dim)

# order of animal regression coefficients
order = 2

# Mu : the matrix containing the polynomials of the standardized DIM values
M <- stdtime(dim, order)
head(M)
tail(M)

## Lambda : a matrix of order k containing the coefficients of Legendre polynomials.
Lambda <- legendre(order, gengler=FALSE)
head(Lambda)
tail(Lambda)

## Phi : Legendre polynomials of days in milk
Phi <- M %*% t(Lambda)
head(Phi)
tail(Phi)

# genetic variance and covariance
G = matrix(c( 3.297,  0.594, -1.381,
              0.594,  0.921, -0.289,
             -1.321, -0.289,  1.005 ), nrow = 3, byrow = TRUE)
G

P = matrix(c( 6.872, -0.254, -1.101,
             -0.254,  3.171,  0.167,
             -1.101,  0.167,  2.457 ), nrow = 3, byrow = TRUE)
P

# genetic variance for DIM i (vii)
fdim = 3
vii = matrix(rep(0, 3 * 307) , ncol = 3)
head(vii)

for (i in 1 : 307){
    vii[i, 1] = fdim + i
    vii[i, 2] = Phi[i,] %*% G %*% matrix(Phi[i,])
    vii[i, 3] = Phi[i,] %*% P %*% matrix(Phi[i,])
}
head(vii)
tail(vii)

plot(vii[,1], vii[,2], ylim = c(0,12))
par(new = TRUE)
plot(vii[,1], vii[,3], ylim = c(0,12))


# breeding value of each animal for DIM i 
# regression coefficients for animal effects
bvreg = matrix(c(
  1, -0.0583,	 0.0552, -0.0442,
  2, -0.0728, -0.0305, -0.0244,
  3,	0.1312, -0.0247,  0.0685,
  4,	0.3445,	 0.0063, -0.3164,
  5, -0.4538, -0.052 ,  0.2798,
  6, -0.5486,	 0.073 ,  0.1946,
  7,	0.8518, -0.0095, -0.3131,
  8,	0.2208,  0.0127, -0.0174
), nrow = 8, byrow = TRUE)
bvreg

bvdim = matrix(rep(0, 9 * 307), ncol = 9)
head(bvdim)

for(i in 1 : 307){
  bvdim[i, 1] = fdim + i
  for(j in 1 : 8){
    bvdim[i, j + 1] = Phi[i,] %*% matrix(bvreg[j, 2:4])
  }
}
head(bvdim)

plot(bvdim[,1], bvdim[,3], ylim = c(-0.15,0.25))
par(new = TRUE)
plot(bvdim[,1], bvdim[,4], ylim = c(-0.15,0.25))
par(new = TRUE)
plot(bvdim[,1], bvdim[,9], ylim = c(-0.15,0.25))

# 305d breeding value
Phi2 = Phi[3 : 307,]
dim(Phi2)
t = colSums(Phi2)
t

bv_animal = rep(0, 8)
bv_animal

for(i in 1:8){
  bv_animal[i] = t %*% matrix(bvreg[i, 2:4])
}

bv_animal

 

실행 결과

 

> # plotting the genetic and permanent environmental variance for DIM
> # plotting the animals breeding values for DIM
> 
> 
> ## Given time points covariate and order of fit for Legendre polynomials, return matrix 'M' containing the polynomials of standardized time. 'M' is order t (number of time points) by k (order of Legendre polynomials).
> 
> ## Arguments 
> ##	t: a vector of time, age or days
> ## 	n: order of polynomials
> ##      tmax: max time (optional)
> ##      tmin: min time (optional)
> 
> ## Literature: Mrode, R.A. 2005. Linear Models for the Prediction of Animal Breeding Values. CAB International, Oxon, UK.
> 
> ## Author: Gota Morota <morota at wisc dot edu>
> ## Created: 31-Mar-2010
> ## Last-Modified: 2-Apr-2010
> ## License: GPLv3 or later
> 
> `stdtime` <-
+   function(t, n, tmax, tmin){
+     if(missing(tmax)) {
+       tmax <- t[which.max(t)]
+     }
+     if(missing(tmin)) {
+       tmin <- t[which.min(t)]
+     }
+     
+     N <- n+1
+     M <- matrix(0, nrow=length(t), ncol=N)
+     a <- -1 + 2*(t-tmin)/(tmax - tmin)
+     M[,1] <- 1
+     
+     for (i in 2:N){
+       M[,i] <- a^(i-1)
+     }
+     
+     return(M)
+   }
> 
> ## Return coefficient matrix (lambda) of n-th order Legendre polynomials 
> 
> ## Arguments
> ## 	n: order of polynomials
> ##	gengler: logical value. If TRUE, Genlger's scaling (1999) will be applied. If not specified, TRUE is assumed.
> 
> ## Literatures
> ## Mrode, R.A. 2005. Linear Models for the Prediction of Animal Breeding Values. CAB International, Oxon, UK.
> ## Gengler, N. et. al. 1999. Estimation of (Co)variance Function Coefficients for Test Day Yield with a Expectation-Maximization Restricted Maximum Likelihood Algorithm.  Journal of Dairy Science.  82
> 
> ## Author: Gota Morota <morota at wisc dot edu>
> ## Create: 31-Mar-2010
> ## Last-Modified: 2-Apr-2010
> ## License: GPLv3 or later
> 
> `legendre` <-
+   function(n, gengler){
+     
+     if (nargs()==1){
+       gengler <- TRUE	
+     }
+     
+     if (gengler != TRUE & gengler != FALSE){
+       gengler=TRUE	
+     }
+     
+     N <- n+1
+     L <- matrix(0,nrow=N, ncol=N)
+     
+     for(i in (1:N)){
+       if(i==1){
+         L[i,i] <- 1
+       }
+       else if(i==2){
+         L[i,i] <- 1
+       }
+       else  {
+         tmp <- L[i-1,]
+         tmp2 <- as.numeric()
+         tmp2 <- c(0,tmp[1:(N-1)])
+         L[i,] <- (1/(i-2+1))*( (2*(i-2) + 1)*tmp2 -(i-2)*L[i-2,] )
+       }
+     }
+     
+     # Normalize
+     for (j in (1:N)){	
+       L[j,] <- (sqrt( (2*(j-1)+1)/2)  )*L[j,]
+     }
+     
+     
+     # Gengler (1999)
+     if (gengler==TRUE){
+       L <- sqrt(2)*L
+     }
+     
+     return(L)
+     
+   }
> 
> ## example
> ## DIM <- c(4,38,72,106,140,174,208,242,276,310)
> ## order <- 4
> ## M <- stdtime(DIM, order)
> ## Lambda <- legendre(order, gengler=FALSE)
> ## Phi <- M%*%t(Lambda)
> ## Phi
> 
> 
> # days in milk
> dim = c(4:310)
> head(dim)
[1] 4 5 6 7 8 9
> tail(dim)
[1] 305 306 307 308 309 310
> 
> # order of animal regression coefficients
> order = 2
> 
> # Mu : the matrix containing the polynomials of the standardized DIM values
> M <- stdtime(dim, order)
> head(M)
     [,1]       [,2]      [,3]
[1,]    1 -1.0000000 1.0000000
[2,]    1 -0.9934641 0.9869708
[3,]    1 -0.9869281 0.9740271
[4,]    1 -0.9803922 0.9611688
[5,]    1 -0.9738562 0.9483959
[6,]    1 -0.9673203 0.9357085
> tail(M)
       [,1]      [,2]      [,3]
[302,]    1 0.9673203 0.9357085
[303,]    1 0.9738562 0.9483959
[304,]    1 0.9803922 0.9611688
[305,]    1 0.9869281 0.9740271
[306,]    1 0.9934641 0.9869708
[307,]    1 1.0000000 1.0000000
> 
> ## Lambda : a matrix of order k containing the coefficients of Legendre polynomials.
> Lambda <- legendre(order, gengler=FALSE)
> head(Lambda)
           [,1]     [,2]     [,3]
[1,]  0.7071068 0.000000 0.000000
[2,]  0.0000000 1.224745 0.000000
[3,] -0.7905694 0.000000 2.371708
> tail(Lambda)
           [,1]     [,2]     [,3]
[1,]  0.7071068 0.000000 0.000000
[2,]  0.0000000 1.224745 0.000000
[3,] -0.7905694 0.000000 2.371708
> 
> ## Phi : Legendre polynomials of days in milk
> Phi <- M %*% t(Lambda)
> head(Phi)
          [,1]      [,2]     [,3]
[1,] 0.7071068 -1.224745 1.581139
[2,] 0.7071068 -1.216740 1.550237
[3,] 0.7071068 -1.208735 1.519539
[4,] 0.7071068 -1.200730 1.489043
[5,] 0.7071068 -1.192725 1.458749
[6,] 0.7071068 -1.184721 1.428658
> tail(Phi)
            [,1]     [,2]     [,3]
[302,] 0.7071068 1.184721 1.428658
[303,] 0.7071068 1.192725 1.458749
[304,] 0.7071068 1.200730 1.489043
[305,] 0.7071068 1.208735 1.519539
[306,] 0.7071068 1.216740 1.550237
[307,] 0.7071068 1.224745 1.581139
> 
> # genetic variance and covariance
> G = matrix(c( 3.297,  0.594, -1.381,
+               0.594,  0.921, -0.289,
+              -1.321, -0.289,  1.005 ), nrow = 3, byrow = TRUE)
> G
       [,1]   [,2]   [,3]
[1,]  3.297  0.594 -1.381
[2,]  0.594  0.921 -0.289
[3,] -1.321 -0.289  1.005
> 
> P = matrix(c( 6.872, -0.254, -1.101,
+              -0.254,  3.171,  0.167,
+              -1.101,  0.167,  2.457 ), nrow = 3, byrow = TRUE)
> P
       [,1]   [,2]   [,3]
[1,]  6.872 -0.254 -1.101
[2,] -0.254  3.171  0.167
[3,] -1.101  0.167  2.457
> 
> # genetic variance for DIM i (vii)
> fdim = 3
> vii = matrix(rep(0, 3 * 307) , ncol = 3)
> head(vii)
     [,1] [,2] [,3]
[1,]    0    0    0
[2,]    0    0    0
[3,]    0    0    0
[4,]    0    0    0
[5,]    0    0    0
[6,]    0    0    0
> 
> for (i in 1 : 307){
+     vii[i, 1] = fdim + i
+     vii[i, 2] = Phi[i,] %*% G %*% matrix(Phi[i,])
+     vii[i, 3] = Phi[i,] %*% P %*% matrix(Phi[i,])
+ }
> head(vii)
     [,1]     [,2]     [,3]
[1,]    4 2.612026 11.66624
[2,]    5 2.533496 11.42854
[3,]    6 2.457661 11.19690
[4,]    7 2.384484 10.97121
[5,]    8 2.313922 10.75139
[6,]    9 2.245937 10.53735
> tail(vii)
       [,1]     [,2]     [,3]
[302,]  305 2.279769 10.81685
[303,]  306 2.306494 11.05675
[304,]  307 2.334957 11.30292
[305,]  308 2.365192 11.55545
[306,]  309 2.397234 11.81442
[307,]  310 2.431118 12.07994
> 
> plot(vii[,1], vii[,2], ylim = c(0,12))
> par(new = TRUE)
> plot(vii[,1], vii[,3], ylim = c(0,12))
> 
> 
> # breeding value of each animal for DIM i 
> # regression coefficients for animal effects
> bvreg = matrix(c(
+   1, -0.0583,	 0.0552, -0.0442,
+   2, -0.0728, -0.0305, -0.0244,
+   3,	0.1312, -0.0247,  0.0685,
+   4,	0.3445,	 0.0063, -0.3164,
+   5, -0.4538, -0.052 ,  0.2798,
+   6, -0.5486,	 0.073 ,  0.1946,
+   7,	0.8518, -0.0095, -0.3131,
+   8,	0.2208,  0.0127, -0.0174
+ ), nrow = 8, byrow = TRUE)
> bvreg
     [,1]    [,2]    [,3]    [,4]
[1,]    1 -0.0583  0.0552 -0.0442
[2,]    2 -0.0728 -0.0305 -0.0244
[3,]    3  0.1312 -0.0247  0.0685
[4,]    4  0.3445  0.0063 -0.3164
[5,]    5 -0.4538 -0.0520  0.2798
[6,]    6 -0.5486  0.0730  0.1946
[7,]    7  0.8518 -0.0095 -0.3131
[8,]    8  0.2208  0.0127 -0.0174
> 
> bvdim = matrix(rep(0, 9 * 307), ncol = 9)
> head(bvdim)
     [,1] [,2] [,3] [,4] [,5] [,6] [,7] [,8] [,9]
[1,]    0    0    0    0    0    0    0    0    0
[2,]    0    0    0    0    0    0    0    0    0
[3,]    0    0    0    0    0    0    0    0    0
[4,]    0    0    0    0    0    0    0    0    0
[5,]    0    0    0    0    0    0    0    0    0
[6,]    0    0    0    0    0    0    0    0    0
> 
> for(i in 1 : 307){
+   bvdim[i, 1] = fdim + i
+   for(j in 1 : 8){
+     bvdim[i, j + 1] = Phi[i,] %*% matrix(bvreg[j, 2:4])
+   }
+ }
> head(bvdim)
     [,1]       [,2]        [,3]      [,4]       [,5]      [,6]       [,7]      [,8]      [,9]
[1,]    4 -0.1787166 -0.05270244 0.2313316 -0.2643899 0.1852043 -0.1696355 0.1188941 0.1130631
[2,]    5 -0.1769089 -0.05219260 0.2290172 -0.2545623 0.1761419 -0.1750646 0.1284932 0.1137024
[3,]    6 -0.1751101 -0.05168770 0.2267166 -0.2447988 0.1671361 -0.1804542 0.1380290 0.1143383
[4,]    7 -0.1733203 -0.05118774 0.2244299 -0.2350994 0.1581870 -0.1858044 0.1475013 0.1149706
[5,]    8 -0.1715395 -0.05069272 0.2221570 -0.2254641 0.1492946 -0.1911152 0.1569101 0.1155993
[6,]    9 -0.1697676 -0.05020266 0.2198981 -0.2158929 0.1404590 -0.1963865 0.1662555 0.1162246
> 
> plot(bvdim[,1], bvdim[,3], ylim = c(-0.15,0.25))
> par(new = TRUE)
> plot(bvdim[,1], bvdim[,4], ylim = c(-0.15,0.25))
> par(new = TRUE)
> plot(bvdim[,1], bvdim[,9], ylim = c(-0.15,0.25))
> 
> # 305d breeding value
> Phi2 = Phi[3 : 307,]
> dim(Phi2)
[1] 305   3
> t = colSums(Phi2)
> t
[1] 215.667568   2.441485  -1.545070
> 
> bv_animal = rep(0, 8)
> bv_animal
[1] 0 0 0 0 0 0 0 0
> 
> for(i in 1:8){
+   bv_animal[i] = t %*% matrix(bvreg[i, 2:4])
+ }
> 
> bv_animal
[1]  -12.37036  -15.73736   28.12944   74.80172  -98.42921 -118.43767  184.16620   47.67729
> 

 

일일 분산 그래프

 

일일 개체 육종가 그래프

 

 

X축 Y축 제목 넣고 예쁘게 하는 것은 필요할 때 하자.

+ Recent posts