gini: Calculates the (generalized) Gini coefficient

Description Usage Arguments Details Value Examples

View source: R/gini.R

Description

This function calculates the Gini coefficient. It builds on the 'gini' function from package 'DescTools', and extends this function to the generalized Gini.

Usage

1
2
3
4
5
6
7
8
9
gini(
  mydata,
  generalized = TRUE,
  unbiased = TRUE,
  conf.level = NA,
  R = 1000,
  type = "bca",
  plot = FALSE
)

Arguments

mydata

a vector of N units, with counts of events per unit. The vector should contain at least non-negative elements. The result will be NA, if the vector contains negative elements.

generalized

logical. Should the generalized gini be calculated? (default = TRUE)

unbiased

logical. Should the unbiased Gini be calculated? (default = TRUE)

conf.level

confidence level for the confidence interval, restricted to lie between 0 and 1. If set, bootstrap confidence intervals are calculated. If set to NA (default) no confidence intervals are returned.

R

number of bootstrap replicates. Usually this will be a single positive integer. For importance resampling, some resamples may use one set of weights and others use a different set of weights. In this case R would be a vector of integers where each component gives the number of resamples from each of the rows of weights. This is ignored if no confidence intervals are to be calculated.

type

character string representing the type of interval required. The value should be one out of the c("norm","basic", "stud", "perc" or "bca"). This argument is ignored if no confidence intervals are to be calculated.

plot

logical. Should a kernel density plot of the distribution of R Ginis be printed? (default = FALSE). This argument is ignored if no confidence intervals are to be calculated.

Details

See 'Bernasco, W. and W. Steenbeek (2017). More places than crimes: Implications for evaluating the law of crime concentration at place. Journal of Quantitative Criminology. https://doi.org/10.1007/s10940-016-9324-7' for more information.

Value

If conf.level is set to NA then the result will be a single numeric value and if a conf.level is provided, a named numeric vector with 3 elements: gini (Gini coefficient), lwr.ci (lower bound of the confidence interval), upr.ci (upper bound of the confidence interval)

Examples

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
crimes <- c(10,8,2,0,0,rep(0,5))
crimes
length(crimes) # 10 units of analysis
sum(crimes) # 20 crimes in total
# Standard Gini:
gini(crimes, generalized = FALSE, unbiased = FALSE) # 0.78
# Is the same the generalized Gini, as Ncrimes >= Nunits:
gini(crimes, generalized = TRUE, unbiased = FALSE) # 0.78

crimes <- c(5,2,1,0,0,rep(0,5))
crimes
length(crimes) # 10 units of analysis
sum(crimes) # 8 crimes in total
# Standard Gini:
gini(crimes, generalized = FALSE, unbiased = FALSE) # 0.8
# Is not the same the generalized Gini, as Ncrimes < Nunits:
gini(crimes, generalized = TRUE, unbiased = FALSE) # 0.75

# Example of unbiased standard Gini:
crimes <- c(10, rep(0,9))
crimes
length(crimes) # 10 units of analysis
sum(crimes) # 10 crimes in total
gini(crimes, generalized = FALSE, unbiased = FALSE) # 0.9
# All crime is concentrated in one unit, but the Gini does not equal 1.

crimes <- c(10, rep(0,99))
crimes
gini(crimes, generalized = FALSE, unbiased = FALSE) # 0.99

# As the number of units with 0 crimes inceases, the Gini asymptotically
# approaches 1. This is adjusted in the 'unbiased' Gini, the default in `DescTools`:
crimes <- c(10, rep(0,9))
gini(crimes, generalized = FALSE, unbiased = TRUE) # 1
crimes <- c(10, rep(0,99))
gini(crimes, generalized = FALSE, unbiased = TRUE) # 1

# generate crime events from zero-inflated poisson disribution
set.seed(8722)
crimes <- ifelse(rbinom(1000, size = 1, prob = .1) > 0, 0, rpois(1000, lambda = 1))

# frequency of crimes
table(crimes)

# Bootstrap-based confidence intervals:
set.seed(347)
gini(crimes, conf.level = .95)
# Also output kernel density plot of generated Ginis, with estimated Gini
# indicated as solid red line, and the confidence intervals delineated
# by dashed red lines:
set.seed(347)
gini(crimes, conf.level = .95, plot = TRUE)
# increasing number of simulations
set.seed(347)
gini(crimes, conf.level = .95, R = 20000, plot = TRUE)

wsteenbeek/lorenzgini documentation built on Oct. 11, 2020, 6:56 p.m.