Learn R Programming

pcalg (version 1.1-4)

gSquareBin: G square Test for (Conditional) Independence of Binary Data

Description

$G^2$ statistic to test for (conditional) independence of binary variables $X$ and $Y$ given the (possibly empty) set of binary variables $S$.

Usage

gSquareBin(x, y, S, dm, verbose = FALSE, adaptDF = FALSE)

Arguments

x,y
position (column number) of variable X (and Y respectively) in the adjacency matrix.
S
position of the conditioning variables in the adjacency set.
dm
data matrix (rows: samples, columns: variables) with binary entries
verbose
logical indicating if detailed output is to be provided.
adaptDF
lower the degrees of freedom by one for each zero count. The value for the degrees of freedom cannot go below 1.

Value

  • The p-value of the test.

Details

The $G^2$ statistic is used to test for (conditional) independence of X and Y given a set S (can be NULL). This function is a specialized version of gSquareDis which is for discrete variables with more than two levels.

References

R.E. Neapolitan (2004). Learning Bayesian Networks. Prentice Hall Series in Artificial Intelligence. Chapter 10.3.1

See Also

gSquareDis for a (conditional) independence test for discrete variables with more than two levels. binCItest for a wrapper of this function that can be easily included in skeleton, pc or fci.

Examples

Run this code
## Simulate data from a chain of 3 variables: x1 -> x2 -> x3
set.seed(123)
b0 <- 0
b1 <- 1
b2 <- 1
n <- 10000
x1 <- sample(c(0,1),n,replace=TRUE)

## NB:  plogis(u) := "expit(u)" := exp(u) / (1 + exp(u))
p2 <- plogis(b0 + b1*x1)

x2 <- numeric(length(x1))
for (i in 1:n) {
  x2[i] <- sample(c(0,1),1,prob=c(1-p2[i],p2[i]))
}
p3 <- plogis(b0 + b2*x2)
x3 <- numeric(length(x2))
for (i in 1:n) {
  x3[i] <- sample(c(0,1),1,prob=c(1-p3[i],p3[i]))
}

xtabs(~ x1+x2+x3)
dat <- cbind(x1,x2,x3)

## Test marginal and conditional independencies
gSquareBin(3,1,NULL,dat, verbose=TRUE)
gSquareBin(3,1, 2,  dat)
gSquareBin(1,3, 2,  dat) # the same
gSquareBin(1,3, 2,  dat, adaptDF=TRUE)
stopifnot(all.equal(gSquareBin(3,1, 2, dat),
                    gSquareBin(1,3, 2, dat)),
          all.equal(gSquareBin(3,1, 2, dat, adaptDF=TRUE),
                    gSquareBin(1,3, 2, dat, adaptDF=TRUE))
)

Run the code above in your browser using DataLab