# NOT RUN {
# load entropy library
library("entropy")
## a single variable: entropy
# observed counts for each bin
y = c(4, 2, 3, 0, 2, 4, 0, 0, 2, 1, 1)
# empirical frequencies
freqs.empirical(y)
# empirical estimate of entropy
entropy.empirical(y)
## examples with two variables: KL and chi-squared divergence
# observed counts for first random variables (observed)
y1 = c(4, 2, 3, 1, 6, 4)
n = sum(y1) # 20
# counts for the second random variable (expected)
freqs.expected = c(0.10, 0.15, 0.35, 0.05, 0.20, 0.15)
y2 = n*freqs.expected
# empirical Kullback-Leibler divergence
KL.div = KL.empirical(y1, y2)
KL.div
# empirical chi-squared divergence
cs.div = chi2.empirical(y1, y2)
cs.div
0.5*cs.div # approximates KL.div
## note: see also Gstat and chi2stat
## joint distribution of two discrete random variables
# contingency table with counts for two discrete variables
y.mat = matrix(c(4, 5, 1, 2, 4, 4), ncol = 2) # 3x2 example matrix of counts
n.mat = sum(y.mat) # 20
# empirical estimate of mutual information
mi = mi.empirical(y.mat)
mi
# empirical chi-squared divergence of independence
cs.indep = chi2indep.empirical(y.mat)
cs.indep
0.5*cs.indep # approximates mi
## note: see also Gstatindep and chi2statindep
# }
Run the code above in your browser using DataLab