oss.kld | R Documentation |
This function calculates the Kullback-Leibler Divergence score between two probability distributions, P amd Q. In sampling design for digital soil mapping applivcations, it can be used to determine the statistical distance between a sample plan and the population from which the sample plan was derived
oss.kld(p, q, type = NULL, unit = "log2")
p |
numeric |
q |
numeric |
type |
character Can be one of 'prob' or 'count'. |
unit |
character Can be one of log base 2 ('log2'), log base 10 ('log10') or natural log ('log'). |
numeric
#let us create data for testing, we need 2 probability distributions
prob1 <- c(0.002, 0.020, 0.127, 0.343, 0.362, 0.119, 0.025, 0.002)
prob2<- c(0.001, 0.019, 0.325, 0.145, 0.326, 0.028, 0.153, 0.003)
sum(prob1)
sum(prob2)
barplot(prob1)
barplot(prob2)
# we now run the KL Divergence
# it is a non-symmetrical test, which means you get a different score for Q|P and P|Q
# if we enter the same distribution as both P and Q, we confirm a score of 0 or no divergence
oss.kld(p=prob1, q=prob1, type='prob', unit='log2')
# P|Q
oss.kld(p=prob1, q=prob2, type='prob', unit='log2')
# Q|P
oss.kld(p=prob2, q=prob1, type='prob', unit='log2')
Add the following code to your website.
For more information on customizing the embed code, read Embedding Snippets.