R Under development (unstable) (2024-02-05 r85863 ucrt) -- "Unsuffered Consequences" Copyright (C) 2024 The R Foundation for Statistical Computing Platform: x86_64-w64-mingw32/x64 R is free software and comes with ABSOLUTELY NO WARRANTY. You are welcome to redistribute it under certain conditions. Type 'license()' or 'licence()' for distribution details. R is a collaborative project with many contributors. Type 'contributors()' for more information and 'citation()' on how to cite R or R packages in publications. Type 'demo()' for some demos, 'help()' for on-line help, or 'help.start()' for an HTML browser interface to help. Type 'q()' to quit R. > library(pcalg) > > # Perform tests with only two DAGs, but with permutations of the vertices > # (to check for a bug present till pcalg-2.0.8) > > n.perm <- 5 > > set.seed(123) > > # A: adjacency matrix of DAG; > # B: adjacency matrix of CPDAG > # Setting 3 by courtesy of Jonas Peters: in pcalg <= 2.0.8, > # setting i = 3, k = 3 failed > A <- list( + matrix(c(0,0,0,0,1, 0,0,1,0,1, 0,0,0,1,0, 0,0,0,0,0, 0,0,0,0,0), 5, 5, byrow = TRUE), + matrix(c(0,1,0,0,0, 0,0,0,1,0, 0,0,0,1,0, 0,0,0,0,1, 0,0,0,0,0), 5, 5, byrow = TRUE), + matrix(c(0,0,0,0, 1,0,0,0, 1,1,0,0, 1,1,1,0), 4, 4)) > B <- list( + matrix(c(0,0,0,0,1, 0,0,1,0,1, 0,1,0,1,0, 0,0,1,0,0, 0,0,0,0,0), 5, 5, byrow = TRUE), + matrix(c(0,1,0,0,0, 1,0,0,1,0, 0,0,0,1,0, 0,0,0,0,1, 0,0,0,0,0), 5, 5, byrow = TRUE), + matrix(c(0,1,1,1, 1,0,1,1, 1,1,0,1, 1,1,1,0), 4, 4)) > > for (i in 1:length(A)) { + for (k in 1:n.perm) { + p <- nrow(A[[i]]) + + ind <- if(k == 1) 1:p else sample.int(p) + + g <- as(A[[i]][ind, ind], "graphNEL") + pdag <- dag2cpdag(g) + B.hat <- as(pdag, "matrix") + if (!all(B.hat == B[[i]][ind, ind])) { + stop(sprintf("True CPDAG not found! (setting: i = %d, k = %d)", i, k)) + } + + # par(mfrow = c(1, 2)) + # plot(g) + # plot(pdag) + } + } > > proc.time() user system elapsed 1.00 0.15 1.15