R/child_list_to_transmat.R

Defines functions child_list_to_transmat

Documented in child_list_to_transmat

#' Create transition matrix from tree children list
#'
#' Create transition matrix from tree children list by parents.
#'
#' @param tree parent-child node list.
#'
#' @return matrix
#' @export
#'
#' @examples
#'
#' child_list_to_transmat(tree =
#'                          list('1' = c(2, 3),
#'                               '2' = c(4),
#'                               '3' = c(6, 7)))
#'
child_list_to_transmat <- function(tree) {

  n_nodes <- max(unlist(tree))
  out <- matrix(NA, nrow = n_nodes, ncol = n_nodes)

  for (i in seq_along(tree)) {

    n_to <- length(tree[[i]])
    out[i, tree[[i]][1]] <- 1/n_to
    out[i, tree[[i]][2]] <- 1/n_to
  }

  return(data.frame(out, check.names = FALSE))
}
n8thangreen/CEdecisiontree documentation built on Sept. 13, 2022, 5:25 a.m.