R/computePathMatrix.R

Defines functions computePathMatrix

Documented in computePathMatrix

computePathMatrix <- function(G, spars=FALSE)
    # Copyright (c) 2013 - 2013  Jonas Peters  [peters@stat.math.ethz.ch]
    # All rights reserved.  See the file COPYING for license terms. 
{
    #this function takes an adjacency matrix G from a DAG and computes a path matrix for which 
    # entry(i,j) being one means that there is a directed path from i to j
    # the diagonal will also be one
    p <- dim(G)[2]
    
    if((p > 3000) && (spars == FALSE))
    {
        warning("Maybe you should use the sparse version by using spars=TRUE to increase speed")
    }
    
    if(spars)
    {
        G <- Matrix(G)
        PathMatrix <- Diagonal(p) + G
    } else
    {
        PathMatrix <- diag(1,p) + G
        # PathMatrix <- as(diag(1,p) + G, "sparseMatrix")
        #   sparseMatrix does not seem to lead to a big improvement in speed (in fact, it seems slightly slower)
    }

    k <- ceiling(log(p)/log(2))
    for(i in 1:k)
    {
        PathMatrix <- PathMatrix %*% PathMatrix            
    }
    PathMatrix <- PathMatrix > 0
    
    return(PathMatrix)
}

Try the SID package in your browser

Any scripts or data that you put into this service are public.

SID documentation built on May 2, 2019, 1:05 p.m.