# なんちゃって家系図と家系データ

• 分離比解析をやっている
• たくさんの家系データを作りたい
• 結構面倒くさい

• グラフではなくて家系図的にすれば

```n.init <- 1
trio <- matrix(0,n.init,5)
m <- length(trio[1,])
trio[1,1] <- 1
trio[1:n.init,4] <- sample(c(-1,1),n.init,replace=TRUE)
trio[,5] <- 0
trio
cnt <- 1
n.iter <- 10

for(i in 1:n.iter){
s <- sample(trio[,1],1)
if(length(which(trio[,2:3]==s)) > 0){
# 子がいる
tmp <- which(trio[,2:3]==s,arr.ind=TRUE)
parents <- trio[tmp[1],c(2,3)]
trio <- rbind(trio,c(cnt+1,rep(0,m-1)))
trio[cnt+1,2:3] <- parents
trio[cnt+1,4] <- sample(c(-1,1),1)
trio[cnt+1,5] <- trio[tmp[1],5]
cnt <- cnt+1
}else if(length(which(trio[,2:3]==s))==0){
# 子がいない(相手がいない)
trio <- rbind(trio,c(cnt+1,rep(0,m-1)))
num.kids <- sample(1:5,1)
tmp <- matrix(0,num.kids,m)
tmp[,1] <- cnt+1+(1:num.kids)
if(trio[s,4]==1){
tmp[,2] <- s
tmp[,3] <- cnt+1
}else{
tmp[,2] <- cnt+1
tmp[,3] <- s
}
tmp[,4] <- sample(c(-1,1),num.kids,replace=TRUE)
tmp[,5] <- trio[s,5] + 1
trio <- rbind(trio,tmp)
trio[cnt+1,4] <- -trio[s,4]
trio[cnt+1,5] <- trio[s,5]
cnt <- cnt+1+num.kids

}else if(trio[s,2] == 0 & trio[s,3] == 0){
# 親登録がない
trio <- rbind(trio,c(cnt+1,rep(0,m-1)))
trio <- rbind(trio,c(cnt+2,rep(0,m-1)))
trio[cnt+1,4] <- -1
trio[cnt+2,4] <- 1
trio[c(cnt+1,cnt+2),5] <- trio[s,5]-1
trio[s,2] <- cnt+1
trio[s,3] <- cnt+2
cnt <- cnt+2
}
}
trio

el.from.trio <- function(trio){
non.zero <- which(apply((trio[,2:3])^2,1,sum)!=0)
el <- rbind(cbind(trio[non.zero,2],trio[non.zero,1]),cbind(trio[non.zero,3],trio[non.zero,1]))

el
}

library(igraph)
el <- el.from.trio(trio)
g <- graph.edgelist(el)
plot(g,vertex.color=trio[,4]+3)
X1 <- trio[,5]
X2 <- apply(trio[,1:3],1,mean)
#X1 <- (X1-min(X1))/(max(X1)-min(X1))

for(i in min(X1):max(X1)){
s <- which(X1==i)
if(length(s)>0){
X2[s] <- ppoints(length(s))
}
}
#X2 <- (X2-min(X2))/(max(X2)-min(X2))
#X2 <- seq(from=min(X2),to=max(X2),length(X2[,1]))