Cálculo de la renta media: μ

y <- c(100, 200, 300, 400)
n <- c(50, 25, 75, 25)
#Cálculo de la renta media
renta_media <- function(vector_renta, vector_poblacion) {
  as.vector(crossprod(vector_renta, vector_poblacion) / sum(vector_poblacion))
}
renta_media(vector_renta = y, vector_poblacion = n)
## [1] 242.8571

Cálculo del coeficiente de Gini (versión presentada en el texto de Debraj Ray)

options(scipen = 999999)
gini_debraj_ray <- function(vector_renta, vector_poblacion) {
  # Generación del Dataframe para el cálculo
  library(dplyr)
  df_y <- expand.grid(vector_renta, vector_renta)
  names(df_y) <- c("Yj", "Yk")
  df_n <- expand.grid(vector_poblacion, vector_poblacion)
  names(df_n) <- c("Nj", "Nk")
  gini_data <- bind_cols(df_n, df_y)
  # Cálculo del doble sumatorio
  renta_media(vector_renta = vector_renta,
              vector_poblacion=vector_poblacion)->mu
   N<-sum(vector_poblacion)
  gini_data %>% mutate(doble_sum = Nj * Nk * abs(Yj - Yk)) -> gini_data
  gini_data %>% summarise(Gini=(1/(2*mu*N^2))*sum(doble_sum)) %>% select(Gini)->Gini
  list(gini_data=gini_data,mu=mu,N=N,Gini=Gini[,1])
}

#Data original
g1<-gini_debraj_ray(vector_renta = y,vector_poblacion = n)
## 
## Adjuntando el paquete: 'dplyr'
## The following objects are masked from 'package:stats':
## 
##     filter, lag
## The following objects are masked from 'package:base':
## 
##     intersect, setdiff, setequal, union
print(g1)
## $gini_data
##    Nj Nk  Yj  Yk doble_sum
## 1  50 50 100 100         0
## 2  25 50 200 100    125000
## 3  75 50 300 100    750000
## 4  25 50 400 100    375000
## 5  50 25 100 200    125000
## 6  25 25 200 200         0
## 7  75 25 300 200    187500
## 8  25 25 400 200    125000
## 9  50 75 100 300    750000
## 10 25 75 200 300    187500
## 11 75 75 300 300         0
## 12 25 75 400 300    187500
## 13 50 25 100 400    375000
## 14 25 25 200 400    125000
## 15 75 25 300 400    187500
## 16 25 25 400 400         0
## 
## $mu
## [1] 242.8571
## 
## $N
## [1] 175
## 
## $Gini
## [1] 0.2352941
#Data porcentual
g2<-gini_debraj_ray(vector_renta = prop.table(y),vector_poblacion = prop.table(n))
print(g2)
## $gini_data
##           Nj        Nk  Yj  Yk   doble_sum
## 1  0.2857143 0.2857143 0.1 0.1 0.000000000
## 2  0.1428571 0.2857143 0.2 0.1 0.004081633
## 3  0.4285714 0.2857143 0.3 0.1 0.024489796
## 4  0.1428571 0.2857143 0.4 0.1 0.012244898
## 5  0.2857143 0.1428571 0.1 0.2 0.004081633
## 6  0.1428571 0.1428571 0.2 0.2 0.000000000
## 7  0.4285714 0.1428571 0.3 0.2 0.006122449
## 8  0.1428571 0.1428571 0.4 0.2 0.004081633
## 9  0.2857143 0.4285714 0.1 0.3 0.024489796
## 10 0.1428571 0.4285714 0.2 0.3 0.006122449
## 11 0.4285714 0.4285714 0.3 0.3 0.000000000
## 12 0.1428571 0.4285714 0.4 0.3 0.006122449
## 13 0.2857143 0.1428571 0.1 0.4 0.012244898
## 14 0.1428571 0.1428571 0.2 0.4 0.004081633
## 15 0.4285714 0.1428571 0.3 0.4 0.006122449
## 16 0.1428571 0.1428571 0.4 0.4 0.000000000
## 
## $mu
## [1] 0.2428571
## 
## $N
## [1] 1
## 
## $Gini
## [1] 0.2352941

Mismo Cálculo, usando libreria “ineq”

library(ineq)
library(purrr)
map2(y,n,.f = rep) %>% unlist() %>% Gini()
## [1] 0.2352941
library(ineq)
library(purrr)
map2(y,n,.f = rep) %>% unlist() %>% ineq::Lc() %>% plot()

Lc.mehran(y,n) %>% plot()

Indice de Kuznets

Kuznets.X.Y <- function(vector_renta,
                        vector_poblacion = 1,
                        cuantil_x,
                        cuantil_y) {
  library(purrr)
  map2(vector_renta, vector_poblacion, .f = rep) %>%
    unlist() %>% quantile(probs = c(cuantil_x)) -> Pob.X
  map2(vector_renta, vector_poblacion, .f = rep) %>% 
    unlist() %>% quantile(probs = c(1-cuantil_y)) -> Pob.Y
  list(Kuznets.indice=unname(Pob.Y/Pob.X),Pob.X=Pob.X,Pob.Y=Pob.Y)
}
# Ejemplo de uso
Kuznets.X.Y(vector_renta = y,vector_poblacion = n,cuantil_x = 0.2,cuantil_y =0.2)
## $Kuznets.indice
## [1] 3
## 
## $Pob.X
## 20% 
## 100 
## 
## $Pob.Y
## 80% 
## 300

Indice de Atkinson

Más sobre el indice de Atkinson: Herramientas para el análisis de las desigualdades y del efecto redistributivo de las políticas públicas

library(ineq)
library(purrr)
map2(y,n,.f = rep) %>% unlist() %>% Atkinson(parameter = c(0.5))
## [1] 0.0539507