The mi.plugin function works on a joint frequency matrix of two random variables. The joint frequency matrix indicates the number of times that X and Y get specific results x and y. In your example, you would like X to have 3 possible results - x = 1, x = 2, x = 3, and Y should also have 3 possible results, y = 1, y = 2, y = 3. Skip your example and calculate the general frequency matrix:
> X=c(1, 2, 3) > Y=c(1, 2, 3) > freqs=matrix(sapply(seq(max(X)*max(Y)), function(x) length(which(((X-1)*max(Y)+Y)==x))),ncol=max(X)) > freqs [,1] [,2] [,3] [1,] 1 0 0 [2,] 0 1 0 [3,] 0 0 1
This matrix shows the number of occurrences X = x and Y = y. For example, there was one observation for which X = 1 and Y = 1. 0 cases were found for which X = 2 and Y = 1. Now you can use the mi.plugin function:
> mi.plugin(freqs) [1] 1.098612
source share