shannon.proc <- function(pvect){ # # Procedure to compute the normalized Shannon heterogeneity measure # # Note: the input variable pvect is assumed to be a normalized # probability vector that sums to 1 and is of length M. # Missing values are not allowed, but components of pvector # are allowed to be zero. # M <- length(pvect) if (M == 1){ IShannon <- 0 } else{ normfactor <- log(M) rawvect <- pvect * log(pvect) # # Note: zero components of pvect cause log(pvect) to # evaluate to NaN; here, adopt the convention # that p log (p) = 0 when p = 0 and replace NaN's # rawvect[which(is.na(rawvect))] <- 0 # rawsum <- sum(rawvect) IShannon <- 1 + rawsum/normfactor # } round(IShannon,digits=6) # }