R - tips

grm2asreml grm转Asreml-R V4

2020-03-03  本文已影响0人  董八七

a wrapper of the write.relationshipMatrix function from the synbreed package. still asreml requires attr(G_a,"rowNames")<-as.character(df$Os_id_x);attr(G_a,"colNames")<-as.character(df$Os_id_x);attr(G_a,"INVERSE")<-TRUE. therefore i add them together.

#' A function used to convert a genomic relationship matrix to matrix format allowed in Asreml-R V4
#'
#' @param x A genomic relationship matrix
#' @param digits 
#'
#' @return
#' @export
#'
#' @examples
grm2asreml=function(x, digits = 10){
  rMinv <- ginv(x)
  rMinv <- round(rMinv, digits)
  res <- data.frame(Row = rep(1:nrow(rMinv), nrow(rMinv)), 
                    Column = rep(1:nrow(rMinv), each = nrow(rMinv)), coeff = as.numeric(rMinv), 
                    lower = as.logical(lower.tri(rMinv, diag = TRUE)))
  res <- res[res$lower == TRUE, c("Row", "Column", "coeff")]
  res <- res[order(res$Row, res$Column), ]
  res <- res[res$coeff != 0, ]
  attr(res,"colNames")=attr(res, "rowNames") <- rownames(x)
  attr(res,"INVERSE")<-TRUE
  rm(rMinv, x)
  return(res)
}
上一篇下一篇

猜你喜欢

热点阅读