I have a matrix in R
, namely dat
, and I am trying to create a information matrix using the infotheo
package between all variable combinations. This is the code:
set.seed(1234)
m <- 10
n <- 5
dat <- round(matrix(runif(m * n), m, n))
library(infotheo)
a = combn(seq(ncol(dat)), 2, function(x)condinformation(dat[, x[1]], dat[,x[2]], method = 'emp'))
b <- structure(a, Size = ncol(dat), class = 'dist')
b <- as.matrix(b)
which result in the following:
> b
1 2 3 4 5
1 0.000000000 0.004021743 0.06326870 0.19497599 0.004021743
2 0.004021743 0.000000000 0.03218930 0.01384429 0.291103166
3 0.063268705 0.032189301 0.00000000 0.01384429 0.013844294
4 0.194975994 0.013844294 0.01384429 0.00000000 0.032189301
5 0.004021743 0.291103166 0.01384429 0.03218930 0.000000000
My problem is that while all values are correct, i.e.,condinformation(dat[, 1], dat[,2],method = 'emp')=0.004021743
, the diagonal elements are incorrect. For example
> condinformation(dat[, 1], dat[,1],method = 'emp')
[1] 0.6108643
what I am doing wrong in my code and get this result? Do you have an alternative solution for the desired outcome?
As det mentioned, combn
won't do (1, 1), (2, 2), etc. Try this instead.
b <- matrix(0, n, n)
b[lower.tri(b, TRUE)] <- combn(seq(n + 1), 2, function(x) condinformation(dat[, x[1]], dat[,x[2] - 1], method = 'emp'))
b[upper.tri(b)] <- b[lower.tri(b)]