Skip to content

Instantly share code, notes, and snippets.

@sientifiko
Last active June 21, 2021 21:17
Show Gist options
  • Save sientifiko/66f67b59dcecdfe081cc85513310e986 to your computer and use it in GitHub Desktop.
Save sientifiko/66f67b59dcecdfe081cc85513310e986 to your computer and use it in GitHub Desktop.
Script permite modelar desigualdad "natural"
library(tidyverse)
library(ineq)
options(scipen = 999)
theme_set(theme_classic())
base <- rep(1000, 1000)
lista <- list()
for (i in 1:100) {
lista[[i]] <- base
base <- base * (1 + runif(100, 0.0000001, 1))
}
data <- do.call("rbind", lista) %>% t() %>% as.data.frame()
rm(i)
# nrow(data)
# ncol(data)
data2 <- data %>%
gather("simulation", "value", 1:100)
data2$simulation <- str_replace_all(data2$simulation, "V", "") %>%
as.numeric()
ginis <- data2 %>%
group_by(simulation) %>%
summarize(gini = ineq(value, type = "Gini"))
ggplot(ginis, aes(simulation, gini)) +
geom_line() +
labs(x="Nº simulación", y ="Gini")
lorenz <- data2 %>%
group_by(simulation, value) %>%
summarize(n = n()) %>%
mutate(p = n/sum(n)) %>%
mutate(acumulado.pop = cumsum(p)) %>%
mutate(valrelat = p * value) %>%
mutate(acumulado.val = cumsum(valrelat/sum(valrelat))) %>%
as.data.frame()
lorenz <- lorenz[-1, ]
ggplot(lorenz, aes(acumulado.pop, acumulado.val, color = simulation)) +
geom_line() +
geom_abline(color = "red") +
scale_color_continuous(trans= "reverse",
breaks = seq(0, 100, 25)) +
labs(x="Población relativa acumulada",
y="Recurso relativo acumulado",
color ="Simulación")
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment