cond_independence_test: Conditional independence test

Description Usage Arguments Details Value Author(s) Examples

View source: R/cond_independence_test.R

Description

Makes a conditional independece test based on a likelihood ratio test.

Usage

1
cond_independence_test(var1, var2, cond = c(), data, smooth = 0)

Arguments

var1, var2

Variables to test independence for.

cond

Vector of variable names to condition on. If empty, the test is just an independence test.

data

Data with realisations of the variables.

smooth

Additional cell counts when estimating probabilities. May be used to avoid zero probabilities.

Details

The calculated test statistic is

2 ∑ n_comb * log((P(var1, var2|cond)) / (P(var1|cond)P(var2|cond)))

where the sum is over all states of the variables, n_comb is the number of times the current combination of states has been seen in the data and the probability distributions are estimated. This estimation is done with maximum likelihood if smooth is set to 0. If this gives zero probabilities, the smooth argument must be used to prevent it in order to calculate the test size.

The test statistic follows assymptotically a chi-squared distribution with (n1 - 1)(n2 - 1)n_cond degrees of freedom, where n1 and n2 are the number of states for var1 and var2 and n_cond is the number of configurations of the states of the variables to be conditioned on.

Value

A list containing the following components:

Author(s)

Katrine Kirkeby, enir_tak@hotmail.com

Maria Knudsen, mariaknudsen@hotmail.dk

Ninna Vihrs, ninnavihrs@hotmail.dk

Examples

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
set.seed(43)
var1 <- c(sample(c(1, 2), 100, replace = TRUE))
var2 <- var1 + c(sample(c(1, 2), 100, replace = TRUE))
var3 <- var1 + c(sample(c(0, 1), 100, replace = TRUE,
                        prob = c(0.9, 0.1)))
var4 <- c(sample(c(1, 2), 100, replace = TRUE))
var5 <- var2 + var3

data <- data.frame("var1" = as.character(var1),
                   "var2" = as.character(var2),
                   "var3" = as.character(var3),
                   "var4" = as.character(var4),
                   "var5" = as.character(var5))

cond_independence_test("var1", "var4", data = data, smooth = 0.1)
cond_independence_test("var2", "var3", cond = c("var1", "var5"),
                       data = data, smooth = 0.1)

nvihrs14/tcherry documentation built on Aug. 1, 2020, 6:25 p.m.