# Loop Of Mutual Information In R

## 21 March 2022 - 1 answer

I have a table with zero and ones in R like this

``````m <- 10
n <- 5
dat <- round(matrix(runif(m * n), m, n))
``````

resulting in:

``````      [,1] [,2] [,3] [,4] [,5]
[1,]    0    1    1    1    0
[2,]    1    0    1    1    0
[3,]    0    1    1    1    0
[4,]    1    1    1    1    0
[5,]    1    0    1    0    0
[6,]    0    1    1    0    0
[7,]    1    0    1    1    0
[8,]    1    0    1    0    0
[9,]    1    0    0    0    0
[10,]    0    1    0    0    1
``````

If i want to find the conditional mutual information with the `condinformation` function of `infotheo` package in `R` between the first two columns with all other as conditionals, i will do this

``````library(infotheo)
condinformation(dat[,1], dat[,2], S=dat[,c(-1,-2)], method="emp")
``````

How can i create a 5x5 matrix containing all the conditional mutual informations? Meaning to use a formula like that `condinformation(dat[,a], dat[,b], S=dat[,c(-a,-b)], method="emp")` in a loop?

``````a = combn(seq(ncol(dat)), 2, function(x)condinformation(dat[, x[1]], dat[,x[2]], S=dat[,-x], method = 'emp'))
structure(a, Size = ncol(dat), class = 'dist')
1          2          3          4
2 -1.3862944
3 -1.0819778 -1.1273805
4 -0.9433484 -0.9887511 -1.2136851
5 -1.0227309 -1.0479980 -1.1343026 -1.1935496
``````