The Importance Of Trade In Sub-Saharan Africa

#load packages
library(readxl)
library(tidyverse)
library(rstatix)
library(plm)

#load dataset

P_Popular_Indicators <- read_excel("P_Popular Indicators.xlsx")
View(P_Popular_Indicators)


sub_saharan_trade <- P_Popular_Indicators
#Go through the data

head(sub_saharan_trade, 5)
## # A tibble: 5 × 55
##   `Series Name`      `Series Code` `Country Name` `Country Code` `1975 [YR1975]`
##   <chr>              <chr>         <chr>          <chr>          <chr>          
## 1 Population growth… SP.POP.GROW   Angola         AGO            3.4125582897881
## 2 Population growth… SP.POP.GROW   Benin          BEN            2.431774167343…
## 3 Population growth… SP.POP.GROW   Botswana       BWA            4.970621147575…
## 4 Population growth… SP.POP.GROW   Burkina Faso   BFA            2.015517348022…
## 5 Population growth… SP.POP.GROW   Burundi        BDI            2.480647057955…
## # ℹ 50 more variables: `1976 [YR1976]` <chr>, `1977 [YR1977]` <chr>,
## #   `1978 [YR1978]` <chr>, `1979 [YR1979]` <chr>, `1980 [YR1980]` <chr>,
## #   `1981 [YR1981]` <chr>, `1982 [YR1982]` <chr>, `1983 [YR1983]` <chr>,
## #   `1984 [YR1984]` <chr>, `1985 [YR1985]` <chr>, `1986 [YR1986]` <chr>,
## #   `1987 [YR1987]` <chr>, `1988 [YR1988]` <chr>, `1989 [YR1989]` <chr>,
## #   `1990 [YR1990]` <chr>, `1991 [YR1991]` <chr>, `1992 [YR1992]` <chr>,
## #   `1993 [YR1993]` <chr>, `1994 [YR1994]` <chr>, `1995 [YR1995]` <chr>, …
str(sub_saharan_trade, 5)
## tibble [1,349 × 55] (S3: tbl_df/tbl/data.frame)
##  $ Series Name  : chr [1:1349] "Population growth (annual %)" "Population growth (annual %)" "Population growth (annual %)" "Population growth (annual %)" ...
##  $ Series Code  : chr [1:1349] "SP.POP.GROW" "SP.POP.GROW" "SP.POP.GROW" "SP.POP.GROW" ...
##  $ Country Name : chr [1:1349] "Angola" "Benin" "Botswana" "Burkina Faso" ...
##  $ Country Code : chr [1:1349] "AGO" "BEN" "BWA" "BFA" ...
##  $ 1975 [YR1975]: chr [1:1349] "3.4125582897881" "2.43177416734328" "4.97062114757515" "2.01551734802293" ...
##  $ 1976 [YR1976]: chr [1:1349] "3.33014644361355" "2.48777341182847" "5.15744953032927" "2.1414370897999" ...
##  $ 1977 [YR1977]: chr [1:1349] "3.37937480483443" "2.54383914171627" "5.27257399739864" "2.33065392660895" ...
##  $ 1978 [YR1978]: chr [1:1349] "3.4768536806595" "2.59097540280167" "6.02615685121168" "2.49250252307762" ...
##  $ 1979 [YR1979]: chr [1:1349] "3.50754362150738" "2.65970887557255" "5.99877937542467" "2.5897435791745" ...
##  $ 1980 [YR1980]: chr [1:1349] "3.58793986406709" "2.75652901926142" "3.887230961075" "2.68393653376526" ...
##  $ 1981 [YR1981]: chr [1:1349] "3.64246045254538" "2.83952465028822" "3.40911900610212" "2.73112751689755" ...
##  $ 1982 [YR1982]: chr [1:1349] "3.67803500207566" "2.88709253402082" "3.96896376390478" "2.76149506900293" ...
##  $ 1983 [YR1983]: chr [1:1349] "3.71606373976317" "2.90177259558927" "3.58780257751429" "2.83314070020022" ...
##  $ 1984 [YR1984]: chr [1:1349] "3.70603601409782" "2.92292209235418" "3.52398684718508" "2.88633812219102" ...
##  $ 1985 [YR1985]: chr [1:1349] "3.67864774172346" "2.95456137340361" "3.47195856096711" "2.88729469195998" ...
##  $ 1986 [YR1986]: chr [1:1349] "3.61842556125761" "2.98299675866442" "3.39164067201576" "2.8212491022274" ...
##  $ 1987 [YR1987]: chr [1:1349] "3.47374802918502" "3.01407725589672" "3.26469560707317" "2.73614817791528" ...
##  $ 1988 [YR1988]: chr [1:1349] "3.40339476992955" "3.04249160427487" "2.982917184193" "2.68703394002769" ...
##  $ 1989 [YR1989]: chr [1:1349] "3.41457162860406" "3.0593332626888" "2.77763043077559" "2.65499355256176" ...
##  $ 1990 [YR1990]: chr [1:1349] "3.39240335400398" "3.07281526239198" "2.75036067045791" "2.6348206163474" ...
##  $ 1991 [YR1991]: chr [1:1349] "3.35905461487339" "3.0846342257467" "2.72429719004269" "2.53625917894951" ...
##  $ 1992 [YR1992]: chr [1:1349] "3.27414246108306" "3.08954460200877" "2.81282080899369" "2.46694750828756" ...
##  $ 1993 [YR1993]: chr [1:1349] "3.19559442992564" "4.43675552850148" "2.86304423228008" "2.48654572072589" ...
##  $ 1994 [YR1994]: chr [1:1349] "3.24168920160086" "3.74003044963406" "2.77143347713386" "2.52154952786592" ...
##  $ 1995 [YR1995]: chr [1:1349] "3.33998873703717" "2.11424008856257" "2.62366178434921" "2.56393697670826" ...
##  $ 1996 [YR1996]: chr [1:1349] "3.3816089218138" "2.6166771873555" "2.44368367103611" "2.55894111772828" ...
##  $ 1997 [YR1997]: chr [1:1349] "3.39551504166387" "2.95210198459104" "2.30264662254376" "2.57799245224128" ...
##  $ 1998 [YR1998]: chr [1:1349] "3.34679546644871" "3.0829668212606" "2.21541249490062" "2.76403220911076" ...
##  $ 1999 [YR1999]: chr [1:1349] "3.29527663572252" "3.09369584671073" "2.16069492392568" "2.9303462196778" ...
##  $ 2000 [YR2000]: chr [1:1349] "3.31228746945312" "3.07665265205002" "2.13210606291764" "2.98414422391968" ...
##  $ 2001 [YR2001]: chr [1:1349] "3.35370941359163" "3.05435506649292" "2.05763865830609" "3.05099351122556" ...
##  $ 2002 [YR2002]: chr [1:1349] "3.40747615681454" "3.04010801597376" "1.88080986109573" "3.09039308299234" ...
##  $ 2003 [YR2003]: chr [1:1349] "3.49334277776736" "3.04920259714654" "1.75962550604573" "3.11812174449527" ...
##  $ 2004 [YR2004]: chr [1:1349] "3.59445752400652" "3.06173106096489" "1.75338203751141" "3.14512608695879" ...
##  $ 2005 [YR2005]: chr [1:1349] "3.64626893356444" "3.22061199704122" "1.79499163132533" "3.14680149143287" ...
##  $ 2006 [YR2006]: chr [1:1349] "3.68489201610335" "3.12066888297415" "1.86517712065057" "3.12034537509581" ...
##  $ 2007 [YR2007]: chr [1:1349] "3.74258021023639" "2.95509554727715" "1.9381347493906" "3.03277637197036" ...
##  $ 2008 [YR2008]: chr [1:1349] "3.77828976645524" "3.01959398464251" "2.0082147581123" "2.93816522811728" ...
##  $ 2009 [YR2009]: chr [1:1349] "3.80156187494836" "2.99903642214246" "2.06587846755776" "2.91916025918168" ...
##  $ 2010 [YR2010]: chr [1:1349] "3.85109824897149" "2.98424162798508" "2.10943597528371" "2.92577947499021" ...
##  $ 2011 [YR2011]: chr [1:1349] "3.8879459971363" "2.97782202797765" "1.96877479635498" "2.95657078370668" ...
##  $ 2012 [YR2012]: chr [1:1349] "3.88358355528674" "2.96766540040992" "1.67774021157009" "3.0171707485343" ...
##  $ 2013 [YR2013]: chr [1:1349] "3.84998068971631" "2.94885160135819" "1.50921875015756" "3.00083639274971" ...
##  $ 2014 [YR2014]: chr [1:1349] "3.73272806660513" "2.9550187594558" "1.45441898422222" "2.9733794200807" ...
##  $ 2015 [YR2015]: chr [1:1349] "3.60507162233632" "2.95391627666724" "1.42753223737773" "2.96196306547686" ...
##  $ 2016 [YR2016]: chr [1:1349] "3.57644143982026" "2.92460216679542" "1.41970172619868" "2.92508251378454" ...
##  $ 2017 [YR2017]: chr [1:1349] "3.54061221486272" "2.88117869411855" "1.42047647332447" "2.85291595529266" ...
##  $ 2018 [YR2018]: chr [1:1349] "3.45323300897802" "2.8136418840483" "1.41898108514606" "2.69713675384815" ...
##  $ 2019 [YR2019]: chr [1:1349] "3.38788422184303" "2.73538842831436" "1.42262847077389" "2.52989803984158" ...
##  $ 2020 [YR2020]: chr [1:1349] "3.26795864914978" "2.66259875153288" "1.4394103578493" "2.43522959287393" ...
##  $ 2021 [YR2021]: chr [1:1349] "3.18132259995095" "2.59230167510348" "1.49130106425067" "2.37649218888363" ...
##  $ 2022 [YR2022]: chr [1:1349] "3.14302612044601" "2.54740926342667" "1.58848021878269" "2.30907147387783" ...
##  $ 2023 [YR2023]: chr [1:1349] "3.08065530722915" "2.52274772277722" "1.64031664035479" "2.26973666268599" ...
##  $ 2024 [YR2024]: chr [1:1349] "3.04419972245653" "2.46175361912328" "1.63538411022867" "2.24597728439097" ...
##  $ 2025 [YR2025]: chr [1:1349] ".." ".." ".." ".." ...

Clean dataset

#Clean the colomn and pivot the dataset into longer format

sub_saharan_trade <- sub_saharan_trade %>%
   pivot_longer(
     cols = matches("^197\\d|^198\\d|^199\\d|^200\\d|^201\\d|^202\\d"),
     names_to = "Years",
     values_to = "Values") %>%
  mutate(Years = sub("\\s*\\[YR\\d+\\]", "", Years))


# year and value should be numeric
sub_saharan_trade <- sub_saharan_trade %>%
  mutate(Values = as.numeric(Values),
         Years = as.numeric(Years)) %>% # remove null values
  drop_na()
#rename some colomns 


sub_saharan_trade <- sub_saharan_trade %>%
  rename("Indicators" = `Series Name`,
         "Country" = `Country Name`,
         )
#filter the variables that are useful for trading in the dataset



Trade_African_Sub <- sub_saharan_trade %>%
  filter(Indicators %in% c("GDP growth (annual %)",
                           "Trade (% of GDP)",
                           "Foreign direct investment, net inflows (% of GDP)",
                           "Industry (including construction), value added (% of GDP)",
                           "Labor force, total",
                           "Inflation, consumer prices (annual %)"
                           
                           
                          
                           
  ))  %>% # select colomns that are needed in the dataset
  select(Indicators, `Series Code`, Country, Years, Values) %>% #remove null values
  drop_na()
# pivot the dataset into wider format so itll be easy for analyses




Trade_African_Sub <- Trade_African_Sub %>%
  pivot_wider(id_cols = c(Country, Years),
              names_from = Indicators,
              values_from = Values) %>% # remove the null values
  drop_na()
#skim the dataset

head(Trade_African_Sub, 10)
## # A tibble: 10 × 8
##    Country Years `GDP growth (annual %)` Industry (including construction), va…¹
##    <chr>   <dbl>                   <dbl>                                   <dbl>
##  1 Angola   2002                   13.7                                     51.9
##  2 Angola   2003                    3.49                                    48.9
##  3 Angola   2004                   11.4                                     50.3
##  4 Angola   2005                   14.2                                     56.1
##  5 Angola   2006                   11.8                                     54.2
##  6 Angola   2007                   13.0                                     57.9
##  7 Angola   2008                   10.8                                     56.2
##  8 Angola   2009                    2.00                                    41.0
##  9 Angola   2010                    5.29                                    48.6
## 10 Angola   2011                    3.59                                    52.2
## # ℹ abbreviated name:
## #   ¹​`Industry (including construction), value added (% of GDP)`
## # ℹ 4 more variables:
## #   `Foreign direct investment, net inflows (% of GDP)` <dbl>,
## #   `Inflation, consumer prices (annual %)` <dbl>, `Labor force, total` <dbl>,
## #   `Trade (% of GDP)` <dbl>
tail(Trade_African_Sub, 10)
## # A tibble: 10 × 8
##    Country  Years `GDP growth (annual %)` Industry (including construction), v…¹
##    <chr>    <dbl>                   <dbl>                                  <dbl>
##  1 Tanzania  2015                    6.16                                   24.5
##  2 Tanzania  2016                    6.87                                   24.9
##  3 Tanzania  2017                    6.73                                   25.0
##  4 Tanzania  2018                    5.50                                   27.0
##  5 Tanzania  2019                    5.80                                   28.7
##  6 Tanzania  2020                    1.99                                   27.1
##  7 Tanzania  2021                    4.32                                   27.3
##  8 Tanzania  2022                    4.57                                   27.7
##  9 Tanzania  2023                    5.07                                   28.0
## 10 Tanzania  2024                    5.53                                   28.6
## # ℹ abbreviated name:
## #   ¹​`Industry (including construction), value added (% of GDP)`
## # ℹ 4 more variables:
## #   `Foreign direct investment, net inflows (% of GDP)` <dbl>,
## #   `Inflation, consumer prices (annual %)` <dbl>, `Labor force, total` <dbl>,
## #   `Trade (% of GDP)` <dbl>
# rename the colomns 



  Trade_African_Sub <- Trade_African_Sub %>%
  rename(
    gdp_growth = `GDP growth (annual %)`,
    industry_value = `Industry (including construction), value added (% of GDP)`,
    fdi_inflows = `Foreign direct investment, net inflows (% of GDP)`,
    inflation = `Inflation, consumer prices (annual %)`,
    labor_force = `Labor force, total`,
    trade = `Trade (% of GDP)`
  )
#remove duplicate


Trade_African_Sub %>%
  distinct()
## # A tibble: 1,123 × 8
##    Country Years gdp_growth industry_value fdi_inflows inflation labor_force
##    <chr>   <dbl>      <dbl>          <dbl>       <dbl>     <dbl>       <dbl>
##  1 Angola   2002      13.7            51.9     10.1        109.      7311174
##  2 Angola   2003       3.49           48.9     17.6         98.2     7582160
##  3 Angola   2004      11.4            50.3      8.14        43.5     7870095
##  4 Angola   2005      14.2            56.1     -3.15        23.0     8170696
##  5 Angola   2006      11.8            54.2     -0.0643      13.3     8483347
##  6 Angola   2007      13.0            57.9     -1.22        12.3     8810729
##  7 Angola   2008      10.8            56.2      1.70        12.5     9150465
##  8 Angola   2009       2.00           41.0      2.70        13.7     9502781
##  9 Angola   2010       5.29           48.6     -3.38        14.5     9873442
## 10 Angola   2011       3.59           52.2     -2.41        13.5    10270426
## # ℹ 1,113 more rows
## # ℹ 1 more variable: trade <dbl>

Data Analysis

#Summary statistics 

summary_stat <- Trade_African_Sub %>%
  group_by(Country) %>%
  get_summary_stats(trade, gdp_growth, type = "mean_sd")
print(summary_stat, n = Inf)
## # A tibble: 82 × 5
##    Country                  variable       n    mean    sd
##    <chr>                    <fct>      <dbl>   <dbl> <dbl>
##  1 Angola                   trade         23  74.4   19.9 
##  2 Angola                   gdp_growth    23   4.93   5.32
##  3 Benin                    trade         32  51.7    8.30
##  4 Benin                    gdp_growth    32   4.83   1.71
##  5 Botswana                 trade         35  93.7   12.2 
##  6 Botswana                 gdp_growth    35   3.82   5.31
##  7 Burkina Faso             trade         35  45.8   13.1 
##  8 Burkina Faso             gdp_growth    35   5.22   2.59
##  9 Cabo Verde               trade         35  89.7   11.3 
## 10 Cabo Verde               gdp_growth    35   6.16   6.98
## 11 Cameroon                 trade         35  42.9    6.66
## 12 Cameroon                 gdp_growth    35   2.85   3.25
## 13 Central African Republic trade         16  44.6    8.05
## 14 Central African Republic gdp_growth    16   0.7   10.2 
## 15 Chad                     trade         35  56.4   19.0 
## 16 Chad                     gdp_growth    35   4.73   7.93
## 17 Comoros                  trade         24  39.1    4.09
## 18 Comoros                  gdp_growth    24   2.88   1.71
## 19 Congo, Dem. Rep.         trade         23  55.9   17.3 
## 20 Congo, Dem. Rep.         gdp_growth    23   2.87   5.18
## 21 Congo, Rep.              trade         33 110.    25.3 
## 22 Congo, Rep.              gdp_growth    33   2.01   5.04
## 23 Cote d'Ivoire            trade         35  56.6    7.91
## 24 Cote d'Ivoire            gdp_growth    35   3.58   4.21
## 25 Djibouti                 trade         11 275.    35.7 
## 26 Djibouti                 gdp_growth    11   5.63   1.81
## 27 Equatorial Guinea        trade         19 102.    23.3 
## 28 Equatorial Guinea        gdp_growth    19   0.217  7.96
## 29 Eswatini                 trade          7  87.6    2.28
## 30 Eswatini                 gdp_growth     7   2.98   2.02
## 31 Ethiopia                 trade         14  32.5    9.53
## 32 Ethiopia                 gdp_growth    14   8.32   1.98
## 33 Gabon                    trade         35  85.8    8.76
## 34 Gabon                    gdp_growth    35   2.27   3.46
## 35 Gambia, The              trade         35  53.2   16.1 
## 36 Gambia, The              gdp_growth    35   3.36   3.42
## 37 Ghana                    trade         35  74.0   17.6 
## 38 Ghana                    gdp_growth    35   5.20   2.42
## 39 Guinea                   trade         19  85.5   18.2 
## 40 Guinea                   gdp_growth    19   5.01   2.70
## 41 Guinea-Bissau            trade         35  47.2    6.27
## 42 Guinea-Bissau            gdp_growth    35   3.68   5.61
## 43 Kenya                    trade         35  50.4   11.1 
## 44 Kenya                    gdp_growth    35   3.75   2.28
## 45 Lesotho                  trade         17 144.    10.6 
## 46 Lesotho                  gdp_growth    17   1.6    3.70
## 47 Madagascar               trade         17  61.3    6.47
## 48 Madagascar               gdp_growth    17   2.58   3.42
## 49 Mali                     trade         35  47.4    6.51
## 50 Mali                     gdp_growth    35   4.04   4.06
## 51 Mauritania               trade         35  77.2   18.0 
## 52 Mauritania               gdp_growth    35   3.33   4.37
## 53 Mauritius                trade         35 125.    10.6 
## 54 Mauritius                gdp_growth    35   4.03   3.59
## 55 Mozambique               trade         20  96.1   21.1 
## 56 Mozambique               gdp_growth    20   5.32   2.68
## 57 Namibia                  trade         22  96.5   13.3 
## 58 Namibia                  gdp_growth    22   3.40   3.94
## 59 Niger                    trade         35  38.6    6.98
## 60 Niger                    gdp_growth    35   4.18   3.61
## 61 Rwanda                   trade         33  39.8   13.1 
## 62 Rwanda                   gdp_growth    33   6.68   4.84
## 63 Senegal                  trade         35  55.4    8.81
## 64 Senegal                  gdp_growth    35   3.77   2.13
## 65 Sierra Leone             trade         18  39.2   10.8 
## 66 Sierra Leone             gdp_growth    18   4.14   5.09
## 67 South Africa             trade         35  50.2    8.90
## 68 South Africa             gdp_growth    35   1.98   2.40
## 69 South Sudan              trade          4  63.2    8.79
## 70 South Sudan              gdp_growth     4 -10.1   25.9 
## 71 Sudan                    trade         33  22.0    8.95
## 72 Sudan                    gdp_growth    33   2.61   5.76
## 73 Tanzania                 trade         35  37.6    8.59
## 74 Tanzania                 gdp_growth    35   5.17   1.91
## 75 Togo                     trade         33  59.7    8.50
## 76 Togo                     gdp_growth    33   3.29   5.49
## 77 Uganda                   trade         31  38.0    5.71
## 78 Uganda                   gdp_growth    31   6.20   2.24
## 79 Zambia                   trade         31  68.2    8.03
## 80 Zambia                   gdp_growth    31   4.63   3.64
## 81 Zimbabwe                 trade         13  58.7   14.8 
## 82 Zimbabwe                 gdp_growth    13   5.20   8.09
# trade trend over time
  
trade_trends <- Trade_African_Sub %>%
  group_by(Years) %>%
  summarise(avg_trend = mean(trade, na.rm = TRUE))




ggplot(trade_trends, aes(x = Years, y = avg_trend))+
  geom_line()+
  labs(title = "Trade Openness in Sub-Saharan Africa",
       x = "Year",
       y = "Trade (% of GDP)")

#The year where trade was the highest  

trade_trends %>%
  filter(avg_trend == max(avg_trend, na.rm = TRUE))
## # A tibble: 1 × 2
##   Years avg_trend
##   <dbl>     <dbl>
## 1  2014      74.0
# The year where trade was the lowest

trade_trends %>% 
  filter(avg_trend == min(avg_trend, na.rm = TRUE))
## # A tibble: 1 × 2
##   Years avg_trend
##   <dbl>     <dbl>
## 1  1993      51.2
# correlation analysis to see which variables are correlated

cor_matrix <- Trade_African_Sub %>%
  select(gdp_growth, fdi_inflows,trade, inflation, industry_value)%>%
  cor(use = "complete.obs")

print(cor_matrix)
##                 gdp_growth fdi_inflows       trade   inflation industry_value
## gdp_growth      1.00000000  0.10958227  0.04182410 -0.05512160    -0.05254132
## fdi_inflows     0.10958227  1.00000000  0.26464441 -0.02276895     0.09012893
## trade           0.04182410  0.26464441  1.00000000 -0.02555055     0.35597147
## inflation      -0.05512160 -0.02276895 -0.02555055  1.00000000    -0.02546744
## industry_value -0.05254132  0.09012893  0.35597147 -0.02546744     1.00000000

Panel data model

panel_data <- pdata.frame(Trade_African_Sub, index = c("Country", "Years"))

trade_panel_fe <- plm(gdp_growth ~ fdi_inflows + trade +inflation +industry_value, 
                      data = panel_data, model = "within")

summary(trade_panel_fe)
## Oneway (individual) effect Within Model
## 
## Call:
## plm(formula = gdp_growth ~ fdi_inflows + trade + inflation + 
##     industry_value, data = panel_data, model = "within")
## 
## Unbalanced Panel: n = 41, T = 4-35, N = 1123
## 
## Residuals:
##      Min.   1st Qu.    Median   3rd Qu.      Max. 
## -37.81696  -1.85530   0.29444   2.20740  25.66718 
## 
## Coefficients:
##                   Estimate  Std. Error t-value  Pr(>|t|)    
## fdi_inflows     0.06801791  0.03506412  1.9398  0.052663 .  
## trade           0.05373613  0.01156471  4.6466 3.792e-06 ***
## inflation      -0.00023578  0.00019560 -1.2055  0.228286    
## industry_value  0.09612132  0.02665500  3.6061  0.000325 ***
## ---
## Signif. codes:  0 '***' 0.001 '**' 0.01 '*' 0.05 '.' 0.1 ' ' 1
## 
## Total Sum of Squares:    23308
## Residual Sum of Squares: 21946
## R-Squared:      0.058448
## Adj. R-Squared: 0.020017
## F-statistic: 16.7294 on 4 and 1078 DF, p-value: 2.5948e-13

hausman test

trade_panel_re <-  plm(gdp_growth ~ fdi_inflows + trade + inflation + industry_value,
                       data = panel_data, model = "random")

phtest(trade_panel_fe, trade_panel_re)
## 
##  Hausman Test
## 
## data:  gdp_growth ~ fdi_inflows + trade + inflation + industry_value
## chisq = 54.721, df = 4, p-value = 3.717e-11
## alternative hypothesis: one model is inconsistent