apply_mec_rules: apply_mec_rules

Description Usage Arguments Examples

Description

Function that applies the 3 rules of PC algorithm.

Usage

1

Arguments

pdag

Examples

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
##---- Should be DIRECTLY executable !! ----
##-- ==>  Define data, use random,
##--	or do  help(data=index)  for the standard data sets.

## The function is currently defined as
function (pdag)
{
    verbose <- TRUE
    g <- as(skel, "matrix")
    p <- as.numeric(dim(g)[1])
    old_pdag <- matrix(0, p, p)
    while (!all(old_pdag == pdag)) {
        old_pdag <- pdag
        ind <- which((pdag == 1 & t(pdag) == 0), arr.ind = TRUE)
        cat("ind in Rule 1: ", ind)
        for (i in seq_len(nrow(ind))) {
            cat("i ", i)
            a <- ind[i, 1]
            cat("a ", a)
            b <- ind[i, 2]
            cat("b ", b)
            indC <- which((pdag[b, ] == 1 & pdag[, b] == 1) &
                (pdag[a, ] == 0 & pdag[, a] == 0))
            cat("indC in Rule 1: ", indC)
            if (length(indC) > 0) {
                pdag[b, indC] <- 1
                pdag[indC, b] <- 0
                if (verbose)
                  cat("\nRule 1:", a, "->", b, " and", b, "-",
                    indC, " where ", a, " and ", indC, " not connected : ",
                    b, "->", indC, "\n")
            }
        }
        ind <- which((pdag == 1 & t(pdag) == 1), arr.ind = TRUE)
        for (i in seq_len(nrow(ind))) {
            a <- ind[i, 1]
            b <- ind[i, 2]
            indC <- which((pdag[a, ] == 1 & pdag[, a] == 0) &
                (pdag[, b] == 1 & pdag[b, ] == 0))
            if (length(indC) > 0) {
                pdag[a, b] <- 1
                pdag[b, a] <- 0
                if (verbose)
                  cat("\nRule 2: Kette ", a, "->", indC, "->",
                    b, ":", a, "->", b, "\n")
            }
        }
        ind <- which((pdag == 1 & t(pdag) == 1), arr.ind = TRUE)
        for (i in seq_len(nrow(ind))) {
            a <- ind[i, 1]
            b <- ind[i, 2]
            indC <- which((pdag[a, ] == 1 & pdag[, a] == 1) &
                (pdag[, b] == 1 & pdag[b, ] == 0))
            if (length(indC) >= 2) {
                g2 <- pdag[indC, indC]
                if (length(g2) <= 1) {
                  g2 <- 0
                }
                else {
                  diag(g2) <- rep(1, length(indC))
                }
                if (any(g2 == 0)) {
                  pdag[a, b] <- 1
                  pdag[b, a] <- 0
                  if (verbose)
                    cat("\nRule 3:", a, "->", b, "\n")
                }
            }
        }
    }
    return(pdag)
  }

gekepals/pcabs documentation built on June 15, 2019, 12:03 a.m.