Global Inflation Analysis using R

This R notebook explores the Global Inflation dataset downloaded from Kaggle.com.

https://www.kaggle.com/datasets/sazidthe1/global-inflation-data

library(tidyverse)
## ── Attaching core tidyverse packages ──────────────────────── tidyverse 2.0.0 ──
## ✔ dplyr     1.1.4     ✔ readr     2.1.6
## ✔ forcats   1.0.1     ✔ stringr   1.6.0
## ✔ ggplot2   4.0.1     ✔ tibble    3.3.0
## ✔ lubridate 1.9.4     ✔ tidyr     1.3.2
## ✔ purrr     1.2.0     
## ── Conflicts ────────────────────────────────────────── tidyverse_conflicts() ──
## ✖ dplyr::filter() masks stats::filter()
## ✖ dplyr::lag()    masks stats::lag()
## ℹ Use the conflicted package (<http://conflicted.r-lib.org/>) to force all conflicts to become errors
library(stringr)
library(ggplot2)
library(tmap)
library(tmaptools)
library(sf)
## Linking to GEOS 3.13.1, GDAL 3.11.4, PROJ 9.7.0; sf_use_s2() is TRUE
library(geojsonsf)
library(rjson)
library(sp)
library(reshape2)
## 
## Attaching package: 'reshape2'
## 
## The following object is masked from 'package:tidyr':
## 
##     smiths
global.data <- read.csv(file = "global_inflation_data.csv", header = TRUE)
head(global.data, 10)
##           country_name                                  indicator_name X1980
## 1          Afghanistan Annual average inflation (consumer prices) rate  13.4
## 2              Albania Annual average inflation (consumer prices) rate    NA
## 3              Algeria Annual average inflation (consumer prices) rate   9.7
## 4              Andorra Annual average inflation (consumer prices) rate    NA
## 5               Angola Annual average inflation (consumer prices) rate  46.7
## 6  Antigua and Barbuda Annual average inflation (consumer prices) rate  19.0
## 7            Argentina Annual average inflation (consumer prices) rate    NA
## 8              Armenia Annual average inflation (consumer prices) rate    NA
## 9                Aruba Annual average inflation (consumer prices) rate    NA
## 10           Australia Annual average inflation (consumer prices) rate  10.1
##    X1981 X1982 X1983 X1984 X1985 X1986 X1987 X1988 X1989 X1990 X1991  X1992
## 1   22.2  18.2  15.9  20.4   8.7  -2.1  18.4  27.5  71.5  47.4  43.8  58.19
## 2     NA    NA    NA    NA    NA    NA    NA    NA    NA  -0.2  35.7 226.00
## 3   14.6   6.6   7.8   6.3  10.4  14.0   5.9   5.9   9.2   9.3  25.9  31.70
## 4     NA    NA    NA    NA    NA    NA    NA    NA    NA    NA    NA     NA
## 5    1.4   1.8   1.8   1.8   1.8   1.8   1.8   1.8   1.8   1.8  85.3 299.10
## 6   11.5   4.2   2.3   3.8   1.0   0.5   3.6   6.8   4.4   6.6   4.5   3.00
## 7     NA    NA    NA    NA    NA    NA    NA    NA    NA    NA    NA     NA
## 8     NA    NA    NA    NA    NA    NA    NA    NA    NA    NA    NA     NA
## 9     NA    NA    NA    NA    NA    NA   3.6   3.1   4.0   5.8   5.6   3.90
## 10   9.5  11.4  10.0   4.0   6.7   9.1   8.5   7.3   7.6   7.2   3.3   1.00
##      X1993   X1994  X1995   X1996  X1997  X1998  X1999 X2000 X2001  X2002 X2003
## 1    33.99   20.01   14.0   14.01  14.01  14.01  14.01   0.0 -43.4  51.93 35.66
## 2    85.00   22.60    7.8   12.70  33.20  20.60   0.40   0.0   3.1   5.20  2.40
## 3    20.50   29.00   29.8   18.70   5.70   5.00   2.60   0.3   4.2   1.40  4.30
## 4       NA      NA     NA      NA     NA     NA     NA    NA    NA   3.10  3.10
## 5  1379.50  949.80 2672.2 4146.00 221.50 107.40 248.20 325.0 152.6 108.90 98.20
## 6     3.10    6.50    2.7    3.00   0.40   3.30   1.10  -0.2   1.9   2.40  2.00
## 7       NA      NA     NA      NA     NA   0.90  -1.20  -0.9  -1.1  25.90 13.40
## 8  3731.80 5273.40  176.7   18.70  14.00   8.70   0.70  -0.8   3.2   1.00  4.70
## 9     5.20    6.30    3.4    3.20   3.00   1.90   2.30   4.1   2.9   3.30  3.70
## 10    1.80    1.90    4.6    2.70   0.20   0.90   1.40   4.5   4.4   3.00  2.70
##    X2004 X2005 X2006 X2007 X2008 X2009 X2010 X2011 X2012 X2013 X2014 X2015
## 1  16.36 10.57  6.78  8.68 26.42 -6.81  2.18  11.8  6.44  7.39  4.67 -0.66
## 2   2.90  2.40  2.40  3.00  3.30  2.20  3.60   3.4  2.00  1.90  1.60  1.90
## 3   4.00  1.40  2.30  3.70  4.90  5.70  3.90   4.5  8.90  3.30  2.90  4.80
## 4   2.90  3.50  3.70  2.70  4.30 -1.20  1.70   2.6  1.50  0.50 -0.10 -1.10
## 5  43.50 23.00 13.30 12.20 12.50 13.70 14.50  13.5 10.30  8.80  7.30  9.20
## 6   2.00  2.10  1.80  1.40  5.30 -0.60  3.40   3.5  3.40  1.10  1.10  1.00
## 7   4.40  9.60 10.90  8.80  8.60  6.30 10.50   9.8 10.00 10.60    NA    NA
## 8   6.90  0.60  2.90  4.40  9.10  3.50  8.20   7.5  2.50  5.70  3.00  3.70
## 9   2.50  3.40  3.60  5.40  9.00 -2.10  2.10   4.4  0.60 -2.40  0.40  0.50
## 10  2.30  2.70  3.60  2.40  4.30  1.80  2.90   3.4  1.70  2.50  2.50  1.50
##    X2016 X2017 X2018 X2019 X2020 X2021 X2022 X2023 X2024
## 1   4.38  4.98  0.63   2.3  5.44  5.06 13.71   9.1    NA
## 2   1.30  2.00  2.00   1.4  1.60  2.00  6.70   4.8   4.0
## 3   6.40  5.60  4.30   2.0  2.40  7.20  9.30   9.0   6.8
## 4  -0.40  2.60  1.00   0.5  0.10  1.70  6.20   5.2   3.5
## 5  30.70 29.80 19.60  17.1 22.30 25.80 21.40  13.1  22.3
## 6  -0.50  2.40  1.20   1.4  1.10  1.60  7.50   5.0   2.9
## 7     NA 25.70 34.30  53.5 42.00 48.40 72.40 121.7  93.7
## 8  -1.40  1.20  2.50   1.4  1.20  7.20  8.60   3.5   4.0
## 9  -0.90 -1.00  3.60   3.9 -1.30  0.70  5.50   4.5   2.3
## 10  1.30  2.00  1.90   1.6  0.90  2.80  6.60   5.8   4.0
count(global.data)
##     n
## 1 196
countries <- fromJSON(file = "world-ash-ms.geojson")

countries <- geojson_sf(toJSON(countries))

countries <- subset(countries, st_is_valid(geometry) & name != "Antarctica")
countries[countries$continent == "Seven seas (open ocean)", c("continent")] <-
    "Seven Seas"
head(countries[, c("continent", "admin", "adm0_a3", "geometry")])
## Simple feature collection with 6 features and 3 fields
## Geometry type: GEOMETRY
## Dimension:     XY
## Bounding box:  xmin: -87.67017 ymin: 8.070654 xmax: -63.00942 ymax: 20.09365
## Geodetic CRS:  WGS 84
##       continent              admin adm0_a3                       geometry
## 1 North America         Costa Rica     CRI POLYGON ((-82.56357 9.57666...
## 2 North America          Nicaragua     NIC POLYGON ((-83.15752 14.9930...
## 3 North America       Saint Martin     MAF POLYGON ((-63.01118 18.0689...
## 4 North America       Sint Maarten     SXM POLYGON ((-63.12305 18.0689...
## 5 North America              Haiti     HTI MULTIPOLYGON (((-71.77925 1...
## 6 North America Dominican Republic     DOM POLYGON ((-71.76831 18.0391...

Prepare Data

# Lookup ISO code for country name
iso_code_lookup <- function(d) {
    d <- str_trim(d)
    c <- as.data.frame(
        subset(countries, 
                (iso_a3 != "-9999") & ((admin == d) | (name_long == d) | 
                (name_en == d) | (formal_en == d)) # | (sovereignt == d)))
        ) 
    )
    c[1, "adm0_a3"]
}

# Normalize some country names to match with polygon boundaries
global.data$country_name[global.data$country_name=="China, People's Republic of"] <-
    "China"
global.data$country_name[global.data$country_name=="Congo, Dem. Rep. of the"] <-
    "Democratic Republic of the Congo"
global.data$country_name[global.data$country_name=="Congo, Republic of "] <-
    "Republic of the Congo"
global.data$country_name[global.data$country_name=="Korea, Republic of"] <-
    "Republic of Korea"
global.data$country_name[global.data$country_name=="Gambia, The"] <-
    "Gambia"
global.data$country_name[global.data$country_name=="Kyrgyz Republic"] <-
    "Kyrgyzstan"
global.data$country_name[global.data$country_name=="South Sudan, Republic of"] <-
    "South Sudan"
global.data$country_name[global.data$country_name=="Hong Kong SAR"] <-
    "Hong Kong S.A.R."
global.data$country_name[global.data$country_name=="Macao SAR"] <-
    "Macao S.A.R"
global.data$country_name[global.data$country_name=="Türkiye, Republic of"] <-
    "Turkey"
global.data$country_name[global.data$country_name=="Taiwan Province of China"] <-
    "Taiwan"
global.data$country_name[global.data$country_name=="Bahamas, The"] <-
    "The Bahamas"
global.data$country_name[global.data$country_name=="Lao P.D.R."] <-
    "Laos"
global.data$country_name[global.data$country_name=="Micronesia, Fed. States of"] <-
    "Federated States of Micronesia"


global.data$iso_a3 <- sapply(global.data$country_name, FUN = iso_code_lookup)

global.data[, c("country_name", "iso_a3")] %>%
    subset(!is.na(iso_a3) & iso_a3 != "-99")
##                         country_name iso_a3
## 1                        Afghanistan    AFG
## 2                            Albania    ALB
## 3                            Algeria    DZA
## 4                            Andorra    AND
## 5                             Angola    AGO
## 6                Antigua and Barbuda    ATG
## 7                          Argentina    ARG
## 8                            Armenia    ARM
## 9                              Aruba    ABW
## 10                         Australia    AUS
## 11                           Austria    AUT
## 12                        Azerbaijan    AZE
## 13                       The Bahamas    BHS
## 14                           Bahrain    BHR
## 15                        Bangladesh    BGD
## 16                          Barbados    BRB
## 17                           Belarus    BLR
## 18                           Belgium    BEL
## 19                            Belize    BLZ
## 20                             Benin    BEN
## 21                            Bhutan    BTN
## 22                           Bolivia    BOL
## 23            Bosnia and Herzegovina    BIH
## 24                          Botswana    BWA
## 25                            Brazil    BRA
## 26                 Brunei Darussalam    BRN
## 27                          Bulgaria    BGR
## 28                      Burkina Faso    BFA
## 29                           Burundi    BDI
## 30                        Cabo Verde    CPV
## 31                          Cambodia    KHM
## 32                          Cameroon    CMR
## 33                            Canada    CAN
## 34          Central African Republic    CAF
## 35                              Chad    TCD
## 36                             Chile    CHL
## 37                             China    CHN
## 38                          Colombia    COL
## 39                           Comoros    COM
## 40  Democratic Republic of the Congo    COD
## 41             Republic of the Congo    COG
## 42                        Costa Rica    CRI
## 43                           Croatia    HRV
## 44                            Cyprus    CYP
## 45                    Czech Republic    CZE
## 46                     Côte d'Ivoire    CIV
## 47                           Denmark    DNK
## 48                          Djibouti    DJI
## 49                          Dominica    DMA
## 50                Dominican Republic    DOM
## 51                           Ecuador    ECU
## 52                             Egypt    EGY
## 53                       El Salvador    SLV
## 54                 Equatorial Guinea    GNQ
## 55                           Eritrea    ERI
## 56                           Estonia    EST
## 57                          Eswatini    SWZ
## 58                          Ethiopia    ETH
## 59                              Fiji    FJI
## 60                           Finland    FIN
## 61                            France    FRA
## 62                             Gabon    GAB
## 63                            Gambia    GMB
## 64                           Georgia    GEO
## 65                           Germany    DEU
## 66                             Ghana    GHA
## 67                            Greece    GRC
## 68                           Grenada    GRD
## 69                         Guatemala    GTM
## 70                            Guinea    GIN
## 71                     Guinea-Bissau    GNB
## 72                            Guyana    GUY
## 73                             Haiti    HTI
## 74                          Honduras    HND
## 75                  Hong Kong S.A.R.    HKG
## 76                           Hungary    HUN
## 77                           Iceland    ISL
## 78                             India    IND
## 79                         Indonesia    IDN
## 80                              Iran    IRN
## 81                              Iraq    IRQ
## 82                           Ireland    IRL
## 83                            Israel    ISR
## 84                             Italy    ITA
## 85                           Jamaica    JAM
## 86                             Japan    JPN
## 87                            Jordan    JOR
## 88                        Kazakhstan    KAZ
## 89                             Kenya    KEN
## 90                          Kiribati    KIR
## 91                 Republic of Korea    KOR
## 92                            Kosovo    KOS
## 93                            Kuwait    KWT
## 94                        Kyrgyzstan    KGZ
## 95                              Laos    LAO
## 96                            Latvia    LVA
## 97                           Lebanon    LBN
## 98                           Lesotho    LSO
## 99                           Liberia    LBR
## 100                            Libya    LBY
## 101                        Lithuania    LTU
## 102                       Luxembourg    LUX
## 103                      Macao S.A.R    MAC
## 104                       Madagascar    MDG
## 105                           Malawi    MWI
## 106                         Malaysia    MYS
## 107                         Maldives    MDV
## 108                             Mali    MLI
## 109                            Malta    MLT
## 110                 Marshall Islands    MHL
## 111                       Mauritania    MRT
## 112                        Mauritius    MUS
## 113                           Mexico    MEX
## 114   Federated States of Micronesia    FSM
## 115                          Moldova    MDA
## 116                         Mongolia    MNG
## 117                       Montenegro    MNE
## 118                          Morocco    MAR
## 119                       Mozambique    MOZ
## 120                          Myanmar    MMR
## 121                          Namibia    NAM
## 122                            Nauru    NRU
## 123                            Nepal    NPL
## 124                      Netherlands    NLD
## 125                      New Zealand    NZL
## 126                        Nicaragua    NIC
## 127                            Niger    NER
## 128                          Nigeria    NGA
## 129                 North Macedonia     MKD
## 130                           Norway    NOR
## 131                             Oman    OMN
## 132                         Pakistan    PAK
## 133                            Palau    PLW
## 134                           Panama    PAN
## 135                 Papua New Guinea    PNG
## 136                         Paraguay    PRY
## 137                             Peru    PER
## 138                      Philippines    PHL
## 139                           Poland    POL
## 140                         Portugal    PRT
## 141                      Puerto Rico    PRI
## 142                            Qatar    QAT
## 143                          Romania    ROU
## 144               Russian Federation    RUS
## 145                           Rwanda    RWA
## 146            Saint Kitts and Nevis    KNA
## 147                      Saint Lucia    LCA
## 148 Saint Vincent and the Grenadines    VCT
## 149                            Samoa    WSM
## 150                       San Marino    SMR
## 151                     Saudi Arabia    SAU
## 152                          Senegal    SEN
## 153                           Serbia    SRB
## 154                       Seychelles    SYC
## 155                     Sierra Leone    SLE
## 156                        Singapore    SGP
## 157                  Slovak Republic    SVK
## 158                         Slovenia    SVN
## 159                  Solomon Islands    SLB
## 160                          Somalia    SOM
## 161                     South Africa    ZAF
## 162                      South Sudan    SDS
## 163                            Spain    ESP
## 164                        Sri Lanka    LKA
## 165                            Sudan    SDN
## 166                         Suriname    SUR
## 167                           Sweden    SWE
## 168                      Switzerland    CHE
## 169                            Syria    SYR
## 170            São Tomé and Príncipe    STP
## 171                           Taiwan    TWN
## 172                       Tajikistan    TJK
## 173                         Tanzania    TZA
## 174                         Thailand    THA
## 175                      Timor-Leste    TLS
## 176                             Togo    TGO
## 177                            Tonga    TON
## 178              Trinidad and Tobago    TTO
## 179                          Tunisia    TUN
## 180                     Turkmenistan    TKM
## 181                           Tuvalu    TUV
## 182                           Turkey    TUR
## 183                           Uganda    UGA
## 184                          Ukraine    UKR
## 185             United Arab Emirates    ARE
## 186                   United Kingdom    GBR
## 187                    United States    USA
## 188                          Uruguay    URY
## 189                       Uzbekistan    UZB
## 190                          Vanuatu    VUT
## 191                        Venezuela    VEN
## 192                          Vietnam    VNM
## 193               West Bank and Gaza    PSX
## 194                            Yemen    YEM
## 195                           Zambia    ZMB
## 196                         Zimbabwe    ZWE
melted.data <- 
    melt(
        global.data, 
        id.vars = c("iso_a3", "country_name", "indicator_name"), 
        variable.name = "year",
        value.name = "inflation_rate"
    ) %>%
    #-- convert year to integer data type
    mutate(
        year = as.integer(str_replace(year, "X", ""))
    ) %>%
    #-- remove indicator_name column
    .[, -c(3)]

head(melted.data, 10)
##    iso_a3        country_name year inflation_rate
## 1     AFG         Afghanistan 1980           13.4
## 2     ALB             Albania 1980             NA
## 3     DZA             Algeria 1980            9.7
## 4     AND             Andorra 1980             NA
## 5     AGO              Angola 1980           46.7
## 6     ATG Antigua and Barbuda 1980           19.0
## 7     ARG           Argentina 1980             NA
## 8     ARM             Armenia 1980             NA
## 9     ABW               Aruba 1980             NA
## 10    AUS           Australia 1980           10.1

Exploratory Data Analysis

chart.data <- subset(melted.data, inflation_rate > 0) %>%
    group_by(country_name) %>%
    summarise(
        avg_inflation = round(mean(inflation_rate), 1)
    )
head(chart.data)
## # A tibble: 6 × 2
##   country_name        avg_inflation
##   <chr>                       <dbl>
## 1 Afghanistan                  18.6
## 2 Albania                      15.5
## 3 Algeria                       8.5
## 4 Andorra                       2.7
## 5 Angola                      250. 
## 6 Antigua and Barbuda           3.5
chart.data <- subset(melted.data, inflation_rate > 0) %>%
    group_by(country_name) %>%
    summarise(
        avg_inflation = round(mean(inflation_rate), 1)
    )

hist(
    chart.data$avg_inflation, col = "royalblue",
    main = "Histogram of Average Inflation Rate by Country",
    xlim = c(0, max(chart.data$avg_inflation) * 1.2),
    xlab = "Average Inflation Rate",
    ylim = c(0, 200)
)
grid(nx = NULL, ny = NULL)

chart.data <- subset(melted.data, inflation_rate > 0)
chart.data$inflation_rate <- ifelse(chart.data$inflation_rate > 100, 100, 
                                    chart.data$inflation_rate)

plot(
    chart.data$inflation_rate ~ as.factor(chart.data$iso_a3),
    las = 2,
    cex.axis = 0.7,
    main = "Distribution of Yearly Inflation per Country",
    col = "royalblue", xlab = "", ylab = "Inflation Rate"
)

# Average inflation by year
chart.data <- subset(melted.data, inflation_rate > 0) %>%
    group_by(year) %>%
    summarise(avg_inflation = mean(inflation_rate))

plot(
    x = chart.data$year, y = chart.data$avg_inflation, type = "l",
    col = "royalblue", lwd = 2,
    main = "Average Global Inflation per Year",
    ylab = "Inflation Rate", xlab = ""
)
grid(nx = NULL, ny = NULL)

# Maximum inflation by year
chart.data <- subset(melted.data, inflation_rate > 0) %>%
    group_by(year) %>%
    summarise(max_inflation = max(inflation_rate))

plot(
    x = chart.data$year, y = chart.data$max_inflation, type = "l",
    col = "royalblue", lwd = 2,
    main = "Maximum Global Inflation per Year",
    ylab = "Inflation Rate", xlab = ""
)
grid(nx = NULL, ny = NULL)

# Countries with hyper-inflation rates
subset(
       melted.data[, c("country_name", "year", "inflation_rate")], 
       inflation_rate > 250
    ) %>%
    group_by(country_name) %>%
    summarise(inflation_rate = max(inflation_rate)) %>%
    arrange(desc(inflation_rate)) %>%
    head(20)
## # A tibble: 20 × 2
##    country_name                     inflation_rate
##    <chr>                                     <dbl>
##  1 Venezuela                                65374.
##  2 Democratic Republic of the Congo         23773.
##  3 Nicaragua                                13110.
##  4 Bolivia                                  11750.
##  5 Peru                                      7482.
##  6 Armenia                                   5273.
##  7 Ukraine                                   4735.
##  8 Angola                                    4146 
##  9 Turkmenistan                              3102.
## 10 Brazil                                    2948.
## 11 Tajikistan                                2601.
## 12 Belarus                                   2221.
## 13 Azerbaijan                                1664 
## 14 Kazakhstan                                1662.
## 15 Russian Federation                        1569.
## 16 Uzbekistan                                1568.
## 17 Croatia                                   1518.
## 18 Kyrgyzstan                                1086.
## 19 Bulgaria                                  1061.
## 20 Moldova                                    788.
chart.data <- 
    subset(
        melted.data[, c("country_name", "year", "inflation_rate")], 
        inflation_rate > 250
    ) %>%
    group_by(country_name) %>%
    summarise(inflation_rate = max(inflation_rate)) %>%
    arrange(desc(inflation_rate)) %>%
    head(20)

barplot(
    chart.data$inflation_rate, col = "maroon",
    names.arg = chart.data$country_name,
    main = "Top 20 Countries with Hyper Inflation Rates",
    las = 2,
    ylim = c(0, 70000), #max(chart.data$inflation_rate) * 1.25),
    cex.names = 0.7,
    cex.axis = 0.8
)
grid(nx = NULL, ny = NULL)

# Maximum hyper-inflation rates per year
chart.data <- 
    subset(
        melted.data[, c("country_name", "year", "inflation_rate")], 
        inflation_rate > 250
    ) %>%
    group_by(year) %>%
    summarise(inflation_rate = max(inflation_rate)) %>%
    arrange(desc(inflation_rate)) %>%
    head(20)

barplot(
    chart.data$inflation_rate, col = "maroon",
    names.arg = chart.data$year,
    main = "Top 20 Years with Hyper Inflation Rates",
    las = 2,
    ylim = c(0, 70000),
    cex.names = 0.7,
    cex.axis = 0.8
)
grid(nx = NULL, ny = NULL)

Spatial Analysis

map.data <- merge(
    countries[, c("adm0_a3", "continent", "geometry")],
    melted.data,
    by.x = "adm0_a3", by.y = "iso_a3"
)

head(map.data, 5)
## Simple feature collection with 5 features and 5 fields
## Geometry type: POLYGON
## Dimension:     XY
## Bounding box:  xmin: -70.06611 ymin: 12.423 xmax: -69.8957 ymax: 12.61411
## Geodetic CRS:  WGS 84
##   adm0_a3     continent country_name year inflation_rate
## 1     ABW North America        Aruba 2003            3.7
## 2     ABW North America        Aruba 2005            3.4
## 3     ABW North America        Aruba 1994            6.3
## 4     ABW North America        Aruba 2024            2.3
## 5     ABW North America        Aruba 2010            2.1
##                         geometry
## 1 POLYGON ((-69.89912 12.452,...
## 2 POLYGON ((-69.89912 12.452,...
## 3 POLYGON ((-69.89912 12.452,...
## 4 POLYGON ((-69.89912 12.452,...
## 5 POLYGON ((-69.89912 12.452,...
chart.data <- merge(
    countries[, c("adm0_a3", "continent", "geometry")],
    subset(melted.data, inflation_rate > 0),
    by.x = "adm0_a3", by.y = "iso_a3"
)

plot(
    inflation_rate ~ as.factor(continent), data = chart.data,
    ylab = "Inflation Rate", xlab = "",
    col = "royalblue",
    main = "Inflation Distribution by Continent",
    ylim = c(0, 100),
    cex.axis = 0.7
)

# Range of Inflation Rate per country
calc_range <- function(x) {
    d <- subset(melted.data, iso_a3 == x["adm0_a3"])
    return(max(d$inflation_rate, na.rm = TRUE) - 
                    min(d$inflation_rate, na.rm = TRUE))
}

# Standard deviation of inflation rate per country
calc_stdev <- function(x) {
    d <- subset(melted.data, iso_a3 == x["adm0_a3"])
    return(sd(d$inflation_rate, na.rm = TRUE))
}

# Variance of inflation rate per country
calc_var <- function(x) {
    d <- subset(melted.data, iso_a3 == x["adm0_a3"])
    return(var(d$inflation_rate, na.rm = TRUE))
}

map.data <- melted.data[, c("iso_a3", "inflation_rate")] %>%
    group_by(iso_a3) %>%
    summarise(
        avg_inflation = round(mean(inflation_rate, na.rm = TRUE), 1)
    ) %>%
    merge(
        countries[, c("adm0_a3", "geometry")], ., 
        by.x = "adm0_a3", by.y = "iso_a3"
    ) %>%
    mutate(
        range_inflation = apply(., FUN = calc_range, MARGIN = 1),
        stdev_inflation = apply(., FUN = calc_stdev, MARGIN = 1),
        var_inflation = apply(., FUN = calc_var, MARGIN = 1)
    )

as.data.frame(map.data)[, c("adm0_a3", "avg_inflation", "range_inflation",
                            "stdev_inflation", "var_inflation")] %>%
    head(5)
##   adm0_a3 avg_inflation range_inflation stdev_inflation var_inflation
## 1     ABW           2.9            11.4        2.439423  5.950782e+00
## 2     AFG          15.3           114.9       19.013967  3.615309e+02
## 3     AGO         249.8          4144.6      749.345515  5.615187e+05
## 4     ALB          14.6           226.2       40.075300  1.606030e+03
## 5     AND           2.1             7.4        1.959229  3.838577e+00
tm_shape(countries) +
    tm_polygons(fill = "lightgray") +
    tm_shape(map.data) +
    tm_polygons(
        fill = "avg_inflation", 
        fill.scale = tm_scale_continuous(
            values = "viridis",
            labels = c(0, 50, 100, 250, 1000, 5000)
        ), 
        fill.legend = tm_legend(
            title = "Avg Rate",
            position = tm_pos_out("right", "center")
        )
    ) +
    tm_title(
        text = "Average Inflation Rate by Country",
        position = tm_pos_out("center", "top")
    )
## [tip] Consider a suitable map projection, e.g. by adding `+ tm_crs("auto")`.
## This message is displayed once per session.

# Chart showing highest average inflation
chart.data <- map.data %>%
    arrange(desc(avg_inflation)) %>%
    head(20)

barplot(
    chart.data$avg_inflation, col = "orange",
    names.arg = chart.data$adm0_a3,
    las = 2,
    cex.axis = 0.8,
    cex.names = 0.8,
    main = "Top 20 Average Inflation Rate Countries",
    ylim = c(0, 2500)
)
grid(nx = NULL, ny = NULL)

tm_shape(countries) +
    tm_polygons(col = "lightgray") +
    tm_shape(map.data) +
    tm_polygons(
        fill = "range_inflation", 
        fill.scale = tm_scale_continuous(
            values = "viridis", 
            labels = c(0, 50, 100, 250, 1000, 5000, 70000)
        ),
        fill.legend = tm_legend(
            title = "Rate Range",
            position = tm_pos_out("right", "center")
        ) 
    ) +
    tm_title("Inflation Rate Range by Country")
## [plot mode] fit legend/component: Some legend items or map compoments do not
## fit well, and are therefore rescaled.
## ℹ Set the tmap option `component.autoscale = FALSE` to disable rescaling.

# Chart showing highest inflation rate range
chart.data <- map.data %>%
    arrange(desc(range_inflation)) %>%
    head(20)

barplot(
    chart.data$range_inflation, col = "orange",
    names.arg = chart.data$adm0_a3,
    las = 2,
    cex.axis = 0.8,
    cex.names = 0.8,
    main = "Top 20 Inflation Rate Range Countries",
    ylim = c(0, 70000)
)
grid(nx = NULL, ny = NULL)

tm_shape(countries) +
    tm_polygons(fill = "lightgray") +
    tm_shape(map.data) +
    tm_polygons(
        fill = "stdev_inflation", 
        fill.scale = tm_scale_continuous(
            values = "viridis", 
            labels = c(0, 25, 50, 75, 100, 250, 12000)
        ),
        fill.legend = tm_legend(
            title = "St Dev"
        )
    ) +
    tm_title(text = "Inflation Rate Standard Deviation by Country")
## [plot mode] fit legend/component: Some legend items or map compoments do not
## fit well, and are therefore rescaled.
## ℹ Set the tmap option `component.autoscale = FALSE` to disable rescaling.

# Chart showing highest inflation rate standard deviation
chart.data <- map.data %>%
    arrange(desc(stdev_inflation)) %>%
    head(20)

barplot(
    chart.data$stdev_inflation, col = "orange",
    names.arg = chart.data$adm0_a3,
    las = 2,
    cex.axis = 0.8,
    cex.names = 0.8,
    main = "Top 20 Inflation Rate Standard Deviation Countries",
    ylim = c(0, 12000)
)
grid(nx = NULL, ny = NULL)

chart.data <- subset(melted.data, inflation_rate > 0 & iso_a3 == "VEN")
chart.data
##      iso_a3 country_name year inflation_rate
## 191     VEN    Venezuela 1980           21.4
## 387     VEN    Venezuela 1981           16.2
## 583     VEN    Venezuela 1982            9.6
## 779     VEN    Venezuela 1983            6.2
## 975     VEN    Venezuela 1984           12.2
## 1171    VEN    Venezuela 1985           11.4
## 1367    VEN    Venezuela 1986           11.5
## 1563    VEN    Venezuela 1987           28.1
## 1759    VEN    Venezuela 1988           29.5
## 1955    VEN    Venezuela 1989           84.5
## 2151    VEN    Venezuela 1990           40.7
## 2347    VEN    Venezuela 1991           34.2
## 2543    VEN    Venezuela 1992           31.4
## 2739    VEN    Venezuela 1993           38.1
## 2935    VEN    Venezuela 1994           60.8
## 3131    VEN    Venezuela 1995           59.9
## 3327    VEN    Venezuela 1996           99.9
## 3523    VEN    Venezuela 1997           50.0
## 3719    VEN    Venezuela 1998           35.8
## 3915    VEN    Venezuela 1999           23.6
## 4111    VEN    Venezuela 2000           16.2
## 4307    VEN    Venezuela 2001           12.5
## 4503    VEN    Venezuela 2002           22.4
## 4699    VEN    Venezuela 2003           31.1
## 4895    VEN    Venezuela 2004           21.7
## 5091    VEN    Venezuela 2005           16.0
## 5287    VEN    Venezuela 2006           13.7
## 5483    VEN    Venezuela 2007           18.7
## 5679    VEN    Venezuela 2008           31.4
## 5875    VEN    Venezuela 2009           26.0
## 6071    VEN    Venezuela 2010           28.2
## 6267    VEN    Venezuela 2011           26.1
## 6463    VEN    Venezuela 2012           21.1
## 6659    VEN    Venezuela 2013           40.6
## 6855    VEN    Venezuela 2014           62.2
## 7051    VEN    Venezuela 2015          121.7
## 7247    VEN    Venezuela 2016          254.9
## 7443    VEN    Venezuela 2017          438.1
## 7639    VEN    Venezuela 2018        65374.1
## 7835    VEN    Venezuela 2019        19906.0
## 8031    VEN    Venezuela 2020         2355.1
## 8227    VEN    Venezuela 2021         1588.5
## 8423    VEN    Venezuela 2022          186.5
## 8619    VEN    Venezuela 2023          360.0
## 8815    VEN    Venezuela 2024          200.0
plot(
    inflation_rate ~ as.factor(country_name),
    data = chart.data,
    main = "Venezuela Yearly Inflation Rate Distribution",
    xlab = "", ylab = "Inflation Rate",
    col = "maroon",
    ylim = c(0, 500)
)
grid(nx = NULL, ny = NULL)

plot(
    chart.data$year, chart.data$inflation_rate, type = "l", col = "maroon",
    lwd = 2,
    main = "Venezuela Inflation Rate per Year",
    xlab = "", ylab = "Inflation Rate",
    ylim = c(0, 500)
)
grid(nx = NULL, ny = NULL)

# Compare Venezuela with average of other South American countries 
# and other countries globally

# Venezuela only
chart.data1 <- 
    subset(
        melted.data, 
        inflation_rate > 0 & iso_a3 == "VEN"
    ) %>%
    arrange(year)

# All Global Countries except Venezuela
chart.data2 <- 
    subset(
        melted.data,
        inflation_rate > 0 & iso_a3 != "VEN"
    ) %>%
    group_by(year) %>%
    summarise(
        inflation_rate = mean(inflation_rate)
    ) %>%
    arrange(year)

# All South American Countries except Venezuela
chart.data3 <- 
    subset(
        melted.data,
        inflation_rate > 0 & iso_a3 != "VEN"
    ) %>%
    merge(
        ., 
        as.data.frame(countries)[, c("adm0_a3", "continent")],
        by.x = "iso_a3", by.y = "adm0_a3"
    ) %>%
    subset(
        ., 
        continent == "South America"
    ) %>%
    group_by(year) %>%
    summarise(
        inflation_rate = mean(inflation_rate)
    ) %>%
    arrange(year)

plot(
    chart.data1$year, chart.data1$inflation_rate, 
    type = "l", col = "maroon",
    lwd = 2,
    main = "Venezuela vs Average Inflation Rate per Year",
    xlab = "", ylab = "Inflation Rate",
    ylim = c(0, 500)
)

lines(chart.data2$year, chart.data2$inflation_rate, col = "green", lwd = 2)
lines(chart.data3$year, chart.data3$inflation_rate, col = "blue", lwd = 2)

legend(
    1995, 500, c("Global", "Venezuela", "South America"), 
    lwd = 2, 
    col = c("green", "maroon", "blue"),
    cex = 0.75
)
grid(nx = NULL, ny = NULL)


Research Challenges

C1: Which countries have had consecutive years of inflation rates > 50%?

sum.data <- melted.data %>%
    mutate(inflation_rate = coalesce(inflation_rate, 0)) %>%
    arrange(iso_a3, year) %>%
    mutate(
        high_index = (inflation_rate > 50),
        group_index = 
               cumsum(high_index != coalesce(lag(high_index), FALSE) | 
                          (iso_a3 != coalesce(lag(iso_a3), "")))
    ) %>%
    group_by(iso_a3, group_index) %>%
    summarise(country_name = first(country_name),
              min_inflation = min(inflation_rate),
              max_inflation = max(inflation_rate),
              high_index = first(high_index),
              avg_inflation = round(mean(inflation_rate), 1),
              min_year = min(year),
              max_year = max(year),
              years = n(),
              .groups = "keep") %>%
    subset(., (years > 1) & (high_index == TRUE), select = -c(2, 6))

sum.data
## # A tibble: 67 × 8
## # Groups:   iso_a3 [50]
##    iso_a3 country_name min_inflation max_inflation avg_inflation min_year
##    <chr>  <chr>                <dbl>         <dbl>         <dbl>    <int>
##  1 AGO    Angola                85.3        4146           830.      1991
##  2 ALB    Albania               85           226           156.      1992
##  3 ARG    Argentina             72.4         122.           95.9     2022
##  4 ARM    Armenia              177.         5273.         3061.      1993
##  5 AZE    Azerbaijan           412.         1664          1068.      1993
##  6 BGR    Bulgaria              62.1        1061.          262.      1991
##  7 BLR    Belarus               52.7        2221.          535.      1991
##  8 BLR    Belarus               53.2          59.2          56.2     2011
##  9 BOL    Bolivia              124.        11750.         2741.      1982
## 10 BRA    Brazil                66          2948.          730.      1980
## # ℹ 57 more rows
## # ℹ 2 more variables: max_year <int>, years <int>

C2: What is the most number of consecutive years of high inflation per country?

sum.data %>%
    arrange(iso_a3, desc(years)) %>%
    group_by(iso_a3) %>%
    summarise(country_name = first(country_name),
              years = first(years),
              min_inflation = first(min_inflation),
              avg_inflation = first(avg_inflation),
              max_inflation = first(max_inflation),
              .groups = "keep") %>%
    arrange(desc(years), avg_inflation)
## # A tibble: 50 × 6
## # Groups:   iso_a3 [50]
##    iso_a3 country_name           years min_inflation avg_inflation max_inflation
##    <chr>  <chr>                  <int>         <dbl>         <dbl>         <dbl>
##  1 BRA    Brazil                    16          66           730.          2948.
##  2 TUR    Turkey                    14          54.2          72.7          104.
##  3 AGO    Angola                    13          85.3         830.          4146 
##  4 PER    Peru                      13          59.1         983.          7482.
##  5 BLR    Belarus                   11          52.7         535.          2221.
##  6 COD    Democratic Republic o…    11          81.3        3069.         23773.
##  7 VEN    Venezuela                 11          62.2        8259.         65374.
##  8 URY    Uruguay                   10          54.1          74.7          112.
##  9 LBN    Lebanon                    8          50.1         137.           487.
## 10 NIC    Nicaragua                  8         117.         3754          13110.
## # ℹ 40 more rows

C3: Create a map showing maximum number of years of consecutive high inflation per country.

map.data <- sum.data %>%
    arrange(iso_a3, desc(years)) %>%
    group_by(iso_a3) %>%
    summarise(
        country_name = first(country_name),
        years = first(years),
        min_inflation = first(min_inflation),
        avg_inflation = first(avg_inflation),
        max_inflation = first(max_inflation),
        .groups = "keep"
    ) %>%
    arrange(desc(years), avg_inflation) %>%
    merge(
        countries, ., 
        by.x = "adm0_a3", 
        by.y = "iso_a3"
    )

tm_shape(countries) +
    tm_polygons(fill = "lightgray") +
    tm_shape(map.data) +
    tm_polygons(
        fill = "years", 
        fill.scale = tm_scale_continuous(
            values = "viridis"
        ),
        fill.legend = tm_legend(
            title = "Years"
        )
    ) +
    tm_title(
        text = "Maximum Consecutive High Inflation Rate Years by Country",
        position = tm_pos_out("center", "top")
    )
## [plot mode] fit legend/component: Some legend items or map compoments do not
## fit well, and are therefore rescaled.
## ℹ Set the tmap option `component.autoscale = FALSE` to disable rescaling.