R Markdown

This is an R Markdown document. Markdown is a simple formatting syntax for authoring HTML, PDF, and MS Word documents. For more details on using R Markdown see http://rmarkdown.rstudio.com.

When you click the Knit button a document will be generated that includes both content as well as the output of any embedded R code chunks within the document. You can embed an R code chunk like this:

library(readxl)
library(tidyverse)
library(ineq)
library(reshape2)
## Warning: пакет 'reshape2' был собран под R версии 4.3.3
library(ggplot2)
library(knitr)

Lorenz curve

decile_data <- read_excel("C:/Users/astsl/Desktop/GCIPrawdata.xlsx", skip = 2) 

head(decile_data)
## # A tibble: 6 × 14
##   Country      Year `Decile 1 Income` `Decile 2 Income` `Decile 3 Income`
##   <chr>       <dbl>             <dbl>             <dbl>             <dbl>
## 1 Afghanistan  1980               206               350               455
## 2 Afghanistan  1981               212               361               469
## 3 Afghanistan  1982               221               377               490
## 4 Afghanistan  1983               238               405               527
## 5 Afghanistan  1984               249               424               551
## 6 Afghanistan  1985               256               435               566
## # ℹ 9 more variables: `Decile 4 Income` <dbl>, `Decile 5 Income` <dbl>,
## #   `Decile 6 Income` <dbl>, `Decile 7 Income` <dbl>, `Decile 8 Income` <dbl>,
## #   `Decile 9 Income` <dbl>, `Decile 10 Income` <dbl>, `Mean Income` <dbl>,
## #   Population <dbl>
sel_Year1 <- c(1980, 2014)
sel_Country1 <- c("Germany", "Finland")

temp1 <- decile_data %>% filter(Country %in% sel_Country1 & Year %in% sel_Year1)
temp1
## # A tibble: 4 × 14
##   Country  Year `Decile 1 Income` `Decile 2 Income` `Decile 3 Income`
##   <chr>   <dbl>             <dbl>             <dbl>             <dbl>
## 1 Finland  1980              7450              9900             11775
## 2 Finland  2014              5741              7580              9067
## 3 Germany  1980              5077              6461              7689
## 4 Germany  2014              5222              7305              9045
## # ℹ 9 more variables: `Decile 4 Income` <dbl>, `Decile 5 Income` <dbl>,
## #   `Decile 6 Income` <dbl>, `Decile 7 Income` <dbl>, `Decile 8 Income` <dbl>,
## #   `Decile 9 Income` <dbl>, `Decile 10 Income` <dbl>, `Mean Income` <dbl>,
## #   Population <dbl>
total_income1 <- temp1[, "Mean Income"] * temp1[, "Population"]  
options(scipen = 999)
total_income1
##     Mean Income
## 1   81347685700
## 2   76845477740
## 3  764805832000
## 4 1239206436000
# Germany, 1980
decs_a80 <- unlist(temp1[1, 3:12]) * 2

# Total income for Germany, 1980, assuming a population of 20
total_inc_a80 <- 20 * unlist(temp1[1, "Mean Income"])

cum_inc_share_a80 <- cumsum(decs_a80) / total_inc_a80

# For Germany, 2014 
decs_a14 <- unlist(temp1[2, 3:12]) * 2

# Total income for Germany, 2014, assuming a population of 20
total_inc_a14 <- 20 * unlist(temp1[2, "Mean Income"]) 

cum_inc_share_a14 <- cumsum(decs_a14) / total_inc_a14

# For Finland, 1980  
decs_j80 <- unlist(temp1[3, 3:12]) * 2

# Total income for Finland, 1980, assuming a population of 20
total_inc_j80 <- 20 * unlist(temp1[3, "Mean Income"])

cum_inc_share_j80 <- cumsum(decs_j80) / total_inc_j80

# For Finland, 2014  
decs_j14 <- unlist(temp1[4, 3:12]) * 2   

# Total income for the Finland, 2014, assuming a population of 20 
total_inc_j14 <- 20 * unlist(temp1[4, "Mean Income"])

cum_inc_share_j14 <- cumsum(decs_j14) / total_inc_j14

# Plot of the cumulative income share for each country and year with perfect equality line 
par(mar = c(5, 5, 4, 2) + 0.1)
plot(cum_inc_share_a80, type = "l", col = "#491d8b", 
     lty = 2, lwd = 2, xlab = "Deciles", 
     ylab = "Cumulative income share")  
abline(a = 0, b = 0.1, col = "black", lwd = 2)  
lines(cum_inc_share_a14, col = "#a56eff", lty = 1, lwd = 2)
lines(cum_inc_share_j80, col = "#005d5d", lty = 2, lwd = 2)
lines(cum_inc_share_j14, col = "#3ddbd9", lty = 1, lwd = 2)  
title("Lorenz curves, Germany, Finland(1980 and 2014)")  

legend("topleft", lty = 2:1, lwd = 2, cex = 0.3, legend = 
         c("Germany, 1980", "Germany, 2014",
           "Finland, 1980", "Finland, 2014"),  
       col = c("#491d8b", "#a56eff", "#005d5d", "#3ddbd9"))

Gini dynamics

g_g80 <- Gini(decs_a80)
g_g14 <- Gini(decs_a14)
g_f80 <- Gini(decs_j80)
g_f14 <- Gini(decs_j14)
paste("Gini coefficients")
## [1] "Gini coefficients"
paste("Germany - 1980: ", round(g_g80, 2), ", 2014: ", round(g_g14, 2))
## [1] "Germany - 1980:  0.23 , 2014:  0.26"
paste("Finland - 1980: ", round(g_f80, 2), ", 2014: ", round(g_f14, 2))
## [1] "Finland - 1980:  0.28 , 2014:  0.31"
plot(cum_inc_share_a80, type = "l", col = "#491d8b", lty = 2, 
  lwd = 2, xlab = "Deciles", 
  ylab = "Cumulative income share")   

abline(a = 0, b = 0.1, col = "black", lwd = 2)

lines(cum_inc_share_a14, col = "#a56eff", lty = 1, lwd = 2)

lines(cum_inc_share_j80, col = "#005d5d", lty = 2, lwd = 2)

lines(cum_inc_share_j14, col = "#3ddbd9", lty = 1, lwd = 2)

title("Lorenz curves, Germany and Finland (1980 and 2014)")

legend("topleft", lty = 2.5:1, lwd = 2, cex = 0.3, legend = 
  c("Germany, 1980", "Germany, 2014", 
    "Finland, 1980", "Finland, 2014"),
  col = c("#491d8b", "#a56eff", "#005d5d", "#3ddbd9"))

text(8.4, 0.78, round(g_g80, digits = 3), col = '#491d8b')
text(9.4, 0.6, round(g_g14, digits = 3), col = '#a56eff')
text(5.2, 0.37, round(g_f80, digits = 3), col = '#005d5d')
text(6.4, 0.31, round(g_f14, digits = 3), col = '#3ddbd9')

###OBSERVATIONS # The curve for Germany in 1980 is further from the line of perfect equality, with a Gini coefficient of 0.276. This indicates a higher level of income inequality. During this period, West Germany faced significant economic disparities due to post-WWII economic policies and social stratification. The curve for Germany in 2014 (purple solid line) is closer to the line of perfect equality, with a Gini coefficient of 0.265, indicating reduced income inequality. This improvement can be attributed to progressive taxation, social welfare programs, and economic policies aimed at reducing inequality (OECD, 2015).

#The curve for Finland in 1980 indicates notable income inequality, with a Gini coefficient of 0.306. Finland had a relatively balanced economy but still experienced income disparities due to social class differences (Jäntti et al., 2006). The curve for Finland in 2014 is much closer to the line of perfect equality, with a Gini coefficient of 0.23, showing a significant reduction in income inequality. This can be credited to effective social reforms, including investments in education and healthcare, which promoted a more equitable distribution of income (OECD, 2015).

#Sources used: #OECD (2015): “In It Together: Why Less Inequality Benefits All”. OECD Publishing. #Jäntti, M., Riihelä, M., Sullström, R., & Tuomala, M. (2006): “Trends in income inequality: The case of Finland 1971-2002”. Finnish Economic Papers.

LAB 2

#Countries: Greece, Spain, Italy, Portugal

data_lab2 <- read_excel("C:/Users/astsl/Desktop/GCIPrawdata.xlsx", skip = 2)

data_lab2$gini <- 0

noc <- nrow(data_lab2)

for (i in seq(1, noc)){
  decs_i <- unlist(data_lab2[i, 3:12]) 
  data_lab2$gini[i] <- Gini(decs_i)
}


summary(data_lab2$gini)
##    Min. 1st Qu.  Median    Mean 3rd Qu.    Max. 
##  0.1791  0.3470  0.4814  0.4617  0.5700  0.7386
data_lab2 %>% filter(gini < 0.2) %>% select (Country, Year, gini)
## # A tibble: 17 × 3
##    Country          Year  gini
##    <chr>           <dbl> <dbl>
##  1 Bulgaria         1987 0.191
##  2 Czech Republic   1985 0.195
##  3 Czech Republic   1986 0.194
##  4 Czech Republic   1987 0.192
##  5 Czech Republic   1988 0.191
##  6 Czech Republic   1989 0.194
##  7 Czech Republic   1990 0.196
##  8 Czech Republic   1991 0.199
##  9 Slovak Republic  1985 0.195
## 10 Slovak Republic  1986 0.194
## 11 Slovak Republic  1987 0.193
## 12 Slovak Republic  1988 0.192
## 13 Slovak Republic  1989 0.193
## 14 Slovak Republic  1990 0.194
## 15 Slovak Republic  1991 0.195
## 16 Slovak Republic  1992 0.196
## 17 Slovak Republic  1993 0.179
data_lab2 %>% filter(gini > 0.73) %>% select (Country, Year, gini)
## # A tibble: 27 × 3
##    Country       Year  gini
##    <chr>        <dbl> <dbl>
##  1 Burkina Faso  1980 0.738
##  2 Burkina Faso  1981 0.738
##  3 Burkina Faso  1982 0.738
##  4 Burkina Faso  1983 0.738
##  5 Burkina Faso  1984 0.738
##  6 Burkina Faso  1985 0.738
##  7 Burkina Faso  1986 0.738
##  8 Burkina Faso  1987 0.738
##  9 Burkina Faso  1988 0.738
## 10 Burkina Faso  1989 0.739
## # ℹ 17 more rows
gini_s <- data_lab2 %>% filter(Country %in% c("Greece", "Spain", "Italy", "Portugal"))
ggplot(gini_s, 
       aes(x = Year, y = gini, color = Country)) +
  geom_line(size = 1) +
  theme_bw() +
  ylab("Gini") +
  ggtitle("Gini coefficients for the Southern Europe countries")
## Warning: Using `size` aesthetic for lines was deprecated in ggplot2 3.4.0.
## ℹ Please use `linewidth` instead.
## This warning is displayed once every 8 hours.
## Call `lifecycle::last_lifecycle_warnings()` to see where this warning was
## generated.

###Interpretation #The Gini coefficient for Greece (red line) shows relatively stable low inequality over the years compared to other countries. This suggests consistent income distribution policies. #Italy (green line) exhibits fluctuations with notable peaks and troughs, indicating varying degrees of income inequality likely influenced by economic and policy changes. #Portugal (blue line) shows a significant increase in inequality over time, peaking in certain periods, possibly reflecting economic challenges and structural adjustments. #Spain (purple line) displays erratic changes in inequality, highlighting periods of economic instability and policy shifts.

###Graph of Interdecile P90/P10 ratio dynamics

data_lab2$ratio90_10 <- data_lab2$`Decile 10 Income`/data_lab2$`Decile 1 Income`

ratios_selected <- data_lab2 %>% filter(Country %in% c("Greece", "Spain", "Italy", "Portugal"))

ggplot(ratios_selected, 
       aes(x = Year, y = ratio90_10, color = Country)) +
  geom_line(size = 1) +
  theme_bw() +
  ylab("Interdecile P90/P10") +
  ggtitle("Interdecile P90/P10 for the Southern Europe countries")

###Interpretation #The red line for Greece shows relatively low and stable inequality initially but a sharp increase in recent years, indicating rising disparities between high and low-income earners. #The green line for Italy shows significant fluctuations with sharp peaks, indicating periods of increased inequality followed by improvements. #The blue line for Portugal displays a marked increase in inequality, peaking in recent years, reflecting widening income gaps. #The purple line for Spain shows variability but an overall increasing trend in inequality, indicating a growing disparity between the top and bottom income deciles.

###Once more interdecile (90/50 or 50/10) and compare the two Interdeciles

data_lab2$ratio50_10 <- data_lab2$`Decile 5 Income`/data_lab2$`Decile 1 Income`

ratios_selected <- data_lab2 %>% filter(Country %in% c("Greece", "Spain", "Italy", "Portugal"))

ggplot(ratios_selected, 
       aes(x = Year, y = ratio50_10, color = Country)) +
  geom_line(size = 1) +
  theme_bw() +
  ylab("Interdecile P50/10") +
  ggtitle("Interdecile 50/10 for the Southern Europe countries")

###Comparison of interdeciles #Both plots indicate increasing inequality in recent years for all countries but highlight different periods and degrees of fluctuation, especially noticeable in Italy and Spain

#Find the top-5 least unequal and most unequal countries in terms of Gini as of 2014. Are they the same countries with the least and most inequality if we measure the inequality by the interdeciles? Show the tables with the top-5 countries compared and interpret the differences.

dat2014 <- data_lab2 %>% filter(Year == 2014)

top_5_least_unequal_gini <- dat2014 %>% arrange(gini) %>% head(5)
top_5_most_unequal_gini <- dat2014 %>% arrange(desc(gini)) %>% head(5)

top_5_least_unequal_ratio90_10 <- dat2014 %>% arrange(ratio90_10) %>% head(5)
top_5_most_unequal_ratio90_10 <- dat2014 %>% arrange(desc(ratio90_10)) %>% head(5)

print("Top-5 Least Unequal Countries (Gini Coefficient):")
## [1] "Top-5 Least Unequal Countries (Gini Coefficient):"
print(top_5_least_unequal_gini)
## # A tibble: 5 × 17
##   Country          Year `Decile 1 Income` `Decile 2 Income` `Decile 3 Income`
##   <chr>           <dbl>             <dbl>             <dbl>             <dbl>
## 1 Slovenia         2014              3983              5754              6979
## 2 Norway           2014              8325             11839             14417
## 3 Slovak Republic  2014              1939              3241              4072
## 4 Czech Republic   2014              3211              4627              5563
## 5 Finland          2014              5741              7580              9067
## # ℹ 12 more variables: `Decile 4 Income` <dbl>, `Decile 5 Income` <dbl>,
## #   `Decile 6 Income` <dbl>, `Decile 7 Income` <dbl>, `Decile 8 Income` <dbl>,
## #   `Decile 9 Income` <dbl>, `Decile 10 Income` <dbl>, `Mean Income` <dbl>,
## #   Population <dbl>, gini <dbl>, ratio90_10 <dbl>, ratio50_10 <dbl>
print("Top-5 Most Unequal Countries (Gini Coefficient):")
## [1] "Top-5 Most Unequal Countries (Gini Coefficient):"
print(top_5_most_unequal_gini)
## # A tibble: 5 × 17
##   Country             Year `Decile 1 Income` `Decile 2 Income` `Decile 3 Income`
##   <chr>              <dbl>             <dbl>             <dbl>             <dbl>
## 1 Botswana            2014               169               465               778
## 2 South Africa        2014               253               401               584
## 3 Zambia              2014                40               100               158
## 4 Comoros             2014                91               211               334
## 5 Central African R…  2014                21                56                92
## # ℹ 12 more variables: `Decile 4 Income` <dbl>, `Decile 5 Income` <dbl>,
## #   `Decile 6 Income` <dbl>, `Decile 7 Income` <dbl>, `Decile 8 Income` <dbl>,
## #   `Decile 9 Income` <dbl>, `Decile 10 Income` <dbl>, `Mean Income` <dbl>,
## #   Population <dbl>, gini <dbl>, ratio90_10 <dbl>, ratio50_10 <dbl>
print("Top-5 Least Unequal Countries (P90/P10 Ratio):")
## [1] "Top-5 Least Unequal Countries (P90/P10 Ratio):"
print(top_5_least_unequal_ratio90_10)
## # A tibble: 5 × 17
##   Country         Year `Decile 1 Income` `Decile 2 Income` `Decile 3 Income`
##   <chr>          <dbl>             <dbl>             <dbl>             <dbl>
## 1 Norway          2014              8325             11839             14417
## 2 Slovenia        2014              3983              5754              6979
## 3 Finland         2014              5741              7580              9067
## 4 Czech Republic  2014              3211              4627              5563
## 5 Netherlands     2014              5439              7790              9537
## # ℹ 12 more variables: `Decile 4 Income` <dbl>, `Decile 5 Income` <dbl>,
## #   `Decile 6 Income` <dbl>, `Decile 7 Income` <dbl>, `Decile 8 Income` <dbl>,
## #   `Decile 9 Income` <dbl>, `Decile 10 Income` <dbl>, `Mean Income` <dbl>,
## #   Population <dbl>, gini <dbl>, ratio90_10 <dbl>, ratio50_10 <dbl>
print("Top-5 Most Unequal Countries (P90/P10 Ratio):")
## [1] "Top-5 Most Unequal Countries (P90/P10 Ratio):"
print(top_5_most_unequal_ratio90_10)
## # A tibble: 5 × 17
##   Country             Year `Decile 1 Income` `Decile 2 Income` `Decile 3 Income`
##   <chr>              <dbl>             <dbl>             <dbl>             <dbl>
## 1 Botswana            2014               169               465               778
## 2 Lesotho             2014                19                56                97
## 3 Central African R…  2014                21                56                92
## 4 Zambia              2014                40               100               158
## 5 Swaziland           2014                38                96               154
## # ℹ 12 more variables: `Decile 4 Income` <dbl>, `Decile 5 Income` <dbl>,
## #   `Decile 6 Income` <dbl>, `Decile 7 Income` <dbl>, `Decile 8 Income` <dbl>,
## #   `Decile 9 Income` <dbl>, `Decile 10 Income` <dbl>, `Mean Income` <dbl>,
## #   Population <dbl>, gini <dbl>, ratio90_10 <dbl>, ratio50_10 <dbl>

###Comments #Four out of five countries are consistent across both Gini coefficient and P90/P10 interdecile ratio, indicating similar results. P90/P10 shows a wider range (5.44-6.23) compared to the Gini coefficient (0.25-0.26). #Three out of five countries are the same, showing more variability in P90/P10 (103.4-145.1) than in the Gini coefficient (0.624-0.639). #The P90/P10 ratio exhibits more drastic changes in inequality compared to the Gini coefficient, due to its larger variance in values.

###LAB3

data <- read.csv("C:/Users/astsl/Desktop/gini-coefficient-of-lifespan-inequality-in-females.csv")
data<- data %>% filter(Entity %in% c("Italy", "Japan", "Armenia", "Spain", "Sudan", "Nigeria", "Liberia", "Canada", "China", "USA"))

summary(data)
##     Entity              Code                Year     
##  Length:800         Length:800         Min.   :1872  
##  Class :character   Class :character   1st Qu.:1955  
##  Mode  :character   Mode  :character   Median :1977  
##                                        Mean   :1973  
##                                        3rd Qu.:1999  
##                                        Max.   :2021  
##  Gini.coefficient.of.lifespan.inequality...Sex..female
##  Min.   :0.06931                                      
##  1st Qu.:0.10041                                      
##  Median :0.19007                                      
##  Mean   :0.22429                                      
##  3rd Qu.:0.34217                                      
##  Max.   :0.55993
filtered_data <- data %>% filter(Year >= 1952 & Year <= 2002)

Line chart

ggplot(filtered_data, aes(x = Year, y = `Gini.coefficient.of.lifespan.inequality...Sex..female`, color = Entity)) +
  geom_line(linewidth = 1) +
  theme_bw() +
  ylab("Gini") +
  ggtitle("Mortality Gini Coefficient") +
  theme(legend.position = "bottom")