行列構(gòu)成的矩陣(稀疏矩陣)
即是矩陣由3列構(gòu)成蕊唐,矩陣的行號归敬,矩陣的列號,矩陣的值硕勿,值為0的部分省略哨毁。
比如:下面矩陣是行列構(gòu)成的稀疏矩陣
Row為矩陣的行,Column為矩陣的列源武,Ainverse為矩陣的在某行某列的值扼褪。
Row Column Ainverse
1 1 1 1.83
2 2 1 0.50
3 2 2 2.03
4 3 1 -1.00
5 3 2 -1.00
6 3 3 2.50
7 4 1 -0.67
8 4 3 0.50
9 4 4 1.83
10 5 2 0.53
11 5 3 -1.00
12 5 4 -1.00
13 5 5 2.53
14 6 2 -1.07
15 6 5 -1.07
16 6 6 2.13
矩陣形式(半三角)
[,1] [,2] [,3] [,4] [,5] [,6]
[1,] 1.83 0.00 0.0 0.00 0.00 0.00
[2,] 0.50 2.03 0.0 0.00 0.00 0.00
[3,] -1.00 -1.00 2.5 0.00 0.00 0.00
[4,] -0.67 0.00 0.5 1.83 0.00 0.00
[5,] 0.00 0.53 -1.0 -1.00 2.53 0.00
[6,] 0.00 -1.07 0.0 0.00 -1.07 2.13
矩陣形式(全三角)
[,1] [,2] [,3] [,4] [,5] [,6]
[1,] 1.83 0.50 -1.0 -0.67 0.00 0.00
[2,] 0.50 2.03 -1.0 0.00 0.53 -1.07
[3,] -1.00 -1.00 2.5 0.50 -1.00 0.00
[4,] -0.67 0.00 0.5 1.83 -1.00 0.00
[5,] 0.00 0.53 -1.0 -1.00 2.53 -1.07
[6,] 0.00 -1.07 0.0 0.00 -1.07 2.13
R語言代碼
id <- c(3,4,5,6)
sire <- c(1,1,4,5)
dam <- c(2,"NA",3,2)
ped <- data.frame(id,sire,dam)
ped
library(asreml)
ainv <- asreml.Ainverse(ped)$ginv
ainv
# method 1
matinv <- sparseMatrix(i = ainv$Row,j = ainv$Column,x = ainv$Ainverse)
round(matinv,2)
matinv[upper.tri(matinv)] <- t(mat)[upper.tri(t(mat))]
round(matinv,2)
# method 2
ani <- ainv
n<-max(ani$Row,ani$Column)
mat=matrix(0,n,n)
mat[cbind(ani$Row,ani$Column)]<-ani$Ainverse
round(mat,2)
mat[upper.tri(mat)]=t(mat)[upper.tri(t(mat))]
round(mat,2)
# method 3
library(asreml)
mat3 <- asreml.sparse2mat(ainv)
round(mat3,2)
親緣關(guān)系矩陣
如果對親緣關(guān)系逆矩陣求逆想幻,就得到親緣關(guān)系矩陣了
代碼如下
rela_mat <- solve(mat)
round(rela_mat,2)
親緣關(guān)系矩陣