R Under development (unstable) (2024-09-06 r87103 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) > > # Y-structure MAG > # Encode as adjacency matrix > p <- 4 # total number of variables > V <- c("X1","X2","X3","X4") # variable labels > # amat[i,j] = 0 iff no edge btw i,j > # amat[i,j] = 1 iff i *-o j > # amat[i,j] = 2 iff i *-> j > # amat[i,j] = 3 iff i *-- j > amat <- rbind(c(0,0,2,0), + c(0,0,2,0), + c(3,3,0,2), + c(0,0,3,0)) > rownames(amat)<-V > colnames(amat)<-V > > suffStat<-list(g=amat,verbose=FALSE) > > cat('X1 d-separated from X2? ', dsepAMTest(1,2,S=NULL,suffStat),'\n') ## d-separated X1 d-separated from X2? 1 > cat('X1 d-separated from X2 given X4? ', dsepAMTest(1,2,S=4,suffStat),'\n') ## not d-separated given node 3 X1 d-separated from X2 given X4? 0 > cat('X1 d-separated from X2 given X3 and X4? ', dsepAMTest(1,2,S=c(3,4),suffStat),'\n') ## not d-separated by node 3 and 4 X1 d-separated from X2 given X3 and X4? 0 > > # Derive PAG that represents the Markov equivalence class of the MAG with the FCI algorithm > # Make use of d-separation oracle as "independence test" > indepTest <- dsepAMTest > fci.pag <- fci(suffStat,indepTest,alpha = 0.5,labels = V,verbose=FALSE) > > true.pag <- rbind(c(0,0,2,0), + c(0,0,2,0), + c(1,1,0,2), + c(0,0,3,0)) > rownames(true.pag)<-V > colnames(true.pag)<-V > > cat('True MAG:\n') True MAG: > print(amat) X1 X2 X3 X4 X1 0 0 2 0 X2 0 0 2 0 X3 3 3 0 2 X4 0 0 3 0 > cat('PAG output by FCI:\n') PAG output by FCI: > print(fci.pag@amat) X1 X2 X3 X4 X1 0 0 2 0 X2 0 0 2 0 X3 1 1 0 2 X4 0 0 3 0 > cat('True PAG:\n') True PAG: > print(true.pag) X1 X2 X3 X4 X1 0 0 2 0 X2 0 0 2 0 X3 1 1 0 2 X4 0 0 3 0 > > stopifnot(identical(true.pag,fci.pag@amat)) > > proc.time() user system elapsed 1.06 0.17 1.21