n <- 100
set.seed(123)
## Simulate *independent data of {0,1}-variables:
x <- rbinom(n, 1, pr=1/2)
y <- rbinom(n, 1, pr=1/2)
z <- rbinom(n, 1, pr=1/2)
dat <- cbind(x,y,z)
binCItest(1,3,2, list(dm = dat, adaptDF = FALSE)) # 0.36, not signif.
binCItest(1,3,2, list(dm = dat, adaptDF = TRUE )) # the same, here
## Simulate data from a chain of 3 variables: x1 -> x2 -> x3
set.seed(12)
b0 <- 0
b1 <- 1
b2 <- 1
n <- 10000
x1 <- rbinom(n, size=1, prob=1/2) ## = sample(c(0,1), n, replace=TRUE)
## NB: plogis(u) := "expit(u)" := exp(u) / (1 + exp(u))
p2 <- plogis(b0 + b1*x1) ; x2 <- rbinom(n, 1, prob = p2) # {0,1}
p3 <- plogis(b0 + b2*x2) ; x3 <- rbinom(n, 1, prob = p2) # {0,1}
ftable(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, verbose = 2)
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