# Beispiele zu "Maße der Variabilität für nominale Daten": # (Version 1.0, 2012, Dirk Enzmann) # Funktion zur Berechnung der Variations-Ratio (VR): VR = function(x) { x = x[!is.na(x)] t = table(x) VR = 1-max(t)/sum(t) names(VR)='VR' VR print(VR,5) } # Funktion zur Berechnung des Index der Qualitativen Variation (IQV): IQV=function(x) { x = x[!is.na(x)] t = table(x) k = dim(t) nom = 0 for (i in 1:(k-1)) { for (j in (i+1):k) { nom = nom+t[i]*t[j] } } denom = k*(k-1)/2*(sum(t)/k)**2 IQV=nom/denom names(IQV)='IQV' IQV print(IQV,5) } # Funktion zur Berechnung der normierten Entropie bzw. Heterogenität (NH): NH = function(x, tabdat=F) { x = x[!is.na(x)] if (class(x)!="table" & tabdat==F) x = table(x) x = x/sum(x) y = x[which(x>0)] NH = -sum(y*log(y))/log(length(x)) names(NH)='NH' NH print(NH,5) } # ------------------------------------------------------------------------------ # Beispiel: # Exekutionsmethoden in den USA 1977-2000 # see: Snell, T.L. (2001). Capital punishment 2000. Bureau of Justice Statistcs # Bulletin, p. 12 executions=factor(rep(c(1:5),c(518,149,11,3,2)), labels=c('lethal injection','electrocution','lethal gas', 'hanging','firing squad')) table(executions) VR(executions) IQV(executions) NH(executions)