GDP Components Over Time and Among Countries
At the risk of oversimplifying things, the main components of gross domestic product, GDP are personal consumption (C), business investment (I), government spending (G) and net exports (exports - imports). You can read more about GDP and the different approaches in calculating at the Wikipedia GDP page.
The GDP data we will look at is from the United Nations’ National Accounts Main Aggregates Database, which contains estimates of total GDP and its components for all countries from 1970 to today. We will look at how GDP and its components have changed over time, and compare different countries and how much each component contributes to that country’s GDP. The file we will work with is GDP and its breakdown at constant 2010 prices in US Dollars and it has already been saved in the Data directory. Have a look at the Excel file to see how it is structured and organised
UN_GDP_data <- read_excel("Download-GDPconstant-USD-countries.xls", # Excel filename
sheet="Download-GDPconstant-USD-countr", # Sheet name
skip=2) # Number of rows to skip
The first thing you need to do is to tidy the data, as it is in wide format and you must make it into long, tidy format. Please express all figures in billions (divide values by 1e9, or \(10^9\)), and you want to rename the indicators into something shorter.
make sure you remove
eval=FALSEfrom the next chunk of R code– I have it there so I could knit the document
tidy_GDP_data <- UN_GDP_data %>%
pivot_longer(cols=4:51,
names_to = "Year",
values_to = "value")
glimpse(tidy_GDP_data)
## Rows: 176,880
## Columns: 5
## $ CountryID <dbl> 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4,…
## $ Country <chr> "Afghanistan", "Afghanistan", "Afghanistan", "Afghanista…
## $ IndicatorName <chr> "Final consumption expenditure", "Final consumption expe…
## $ Year <chr> "1970", "1971", "1972", "1973", "1974", "1975", "1976", …
## $ value <dbl> 5.56e+09, 5.33e+09, 5.20e+09, 5.75e+09, 6.15e+09, 6.32e+…
# Let us compare GDP components for these 3 countries
country_list <- c("United States","India", "Germany")
tidy_GDP_data<-tidy_GDP_data %>%
filter(Country==country_list) %>%
mutate(value=value/10^9)
First, can you produce this plot?
# knitr::include_graphics(here::here("images", "gdp1.png"), error = FALSE)
tidy_GDP_for_graph1 <- tidy_GDP_data %>%
mutate(components_of_GDP=case_when(
grepl("Exports",IndicatorName) ~ "Exports",
grepl("Gross capital formation",IndicatorName) ~ "Gross capital formation",
grepl("Government",IndicatorName) ~ "Government expenditure",
grepl("government",IndicatorName) ~ "Government expenditure",
grepl("Household",IndicatorName) ~ "Household expenditure",
grepl("household",IndicatorName) ~ "Household expenditure",
grepl("Imports",IndicatorName)~"Imports"
)) %>%
mutate(Year = as.integer(Year)) %>%
na.omit(components_of_GDP)
ggplot(tidy_GDP_for_graph1,
aes(x=Year,y=value,color=components_of_GDP))+
geom_line(aes(group=components_of_GDP))+
# geom_point()+
facet_wrap(~Country)+
labs(
title = "Evolution of the components of GDP by country",
subtitle = "In Constant 2010 USD",
caption = "Source: United Nations' National Accounts Main Aggregates Database",
color = "GDP Components",
x = "Year",
y = "Value in $M"
)

Secondly, recall that GDP is the sum of Household Expenditure (Consumption C), Gross Capital Formation (business investment I), Government Expenditure (G) and Net Exports (exports - imports). Even though there is an indicator Gross Domestic Product (GDP) in your dataframe, I would like you to calculate it given its components discussed above.
What is the % difference between what you calculated as GDP and the GDP figure included in the dataframe?
tidy_GDP_for_graph1 %>%
select(Country, components_of_GDP, Year, value) %>%
pivot_wider(names_from = components_of_GDP, values_from = value) %>%
janitor::clean_names() %>%
mutate(
net_exports = exports - imports,
gdp = household_expenditure + government_expenditure + gross_capital_formation + net_exports
) -> wider_gdp
wider_gdp %>%
select(country, year, gdp)
## # A tibble: 48 × 3
## country year gdp
## <chr> <int> <dbl>
## 1 Germany 1972 1709.
## 2 Germany 1975 1780.
## 3 Germany 1978 1991.
## 4 Germany 1981 2091.
## 5 Germany 1984 2158.
## 6 Germany 1987 2303.
## 7 Germany 1990 2591.
## 8 Germany 1993 2748.
## 9 Germany 1996 2887.
## 10 Germany 1999 3053.
## # … with 38 more rows
tidy_GDP_data_for_join = tidy_GDP_data%>%
filter(IndicatorName=="Gross Domestic Product (GDP)") %>%
mutate(Year=as.integer(Year)) %>%
select(Country,Year,value)
tidy_GDP_data_comparison=left_join(wider_gdp,tidy_GDP_data_for_join,by=c("country"="Country","year"="Year"))
tidy_GDP_data_comparison %>%
mutate(percentage_change=(value-gdp)/value) %>%
select(country,year,gdp,value,percentage_change)
## # A tibble: 48 × 5
## country year gdp value percentage_change
## <chr> <int> <dbl> <dbl> <dbl>
## 1 Germany 1972 1709. 1650. -0.0356
## 2 Germany 1975 1780. 1729. -0.0293
## 3 Germany 1978 1991. 1932. -0.0305
## 4 Germany 1981 2091. 2051. -0.0192
## 5 Germany 1984 2158. 2134. -0.0114
## 6 Germany 1987 2303. 2265. -0.0168
## 7 Germany 1990 2591. 2569. -0.00877
## 8 Germany 1993 2748. 2725. -0.00811
## 9 Germany 1996 2887. 2864. -0.00787
## 10 Germany 1999 3053. 3034. -0.00613
## # … with 38 more rows
#knitr::include_graphics(here::here("images", "gdp2.png"), error = FALSE)
wider_gdp %>%
mutate(
percentage_GE = 100*government_expenditure/gdp,
percentage_GCF = 100*gross_capital_formation/gdp,
percentage_HE = 100*household_expenditure/gdp,
percentage_NE = 100*net_exports/gdp
) %>%
select(country, year, percentage_GE, percentage_GCF, percentage_HE, percentage_NE) %>%
pivot_longer(cols = 3:6, names_to = "component_of_gdp", values_to = "value") %>%
ggplot(aes(x=year, y=value, color=component_of_gdp)) +
geom_line() +
facet_wrap(~country) +
theme_minimal() +
labs(
title = "GDP and its breakdown at constant 2010 prices in US Dollars",
x = NULL,
y = "% of GDP",
caption = "Source: United Nations",
color = NULL
) +
scale_color_hue(
labels = c('Gross Capital Formation',
'Government Expenditure',
'Household Expenditure',
'Net Exports'
)
)

For all three countries household expenditure is the biggest contributor to the GDP, followed by gross capital formation, government expenditure, and finally Net Exports.
The graph clearly shows that Germany is an exporting country, whereas the US seems to import more than it exports, and India seems to fluctuate close to zero.
Between the three countries, India experiences the highest variability in GDP components, with Gross Capital Formation increasing greatly, and household expenditure decreasing greatly. In the United States, gross capital formation and government expenditure contributed roughly the same amount, with gross capital formation briefly increasing to more than government expenditure, before dipping back down to less than government expenditure.
If you want to, please change
country_list <- c("United States","India", "Germany")to include your own country and compare it with any two other countries you like:
country_list <- c("Poland","Spain", "Singapore", "China")
tidy_GDP_data <- UN_GDP_data %>%
pivot_longer(cols=4:51,
names_to = "Year",
values_to = "value")
tidy_GDP_data<-tidy_GDP_data %>%
filter(Country==country_list) %>%
mutate(value=value/10^9)
tidy_GDP_for_graph1 <- tidy_GDP_data %>%
mutate(components_of_GDP=case_when(
grepl("Exports",IndicatorName) ~ "Exports",
grepl("Gross capital formation",IndicatorName) ~ "Gross capital formation",
grepl("Government",IndicatorName) ~ "Government expenditure",
grepl("government",IndicatorName) ~ "Government expenditure",
grepl("Household",IndicatorName) ~ "Household expenditure",
grepl("household",IndicatorName) ~ "Household expenditure",
grepl("Imports",IndicatorName)~"Imports"
)) %>%
mutate(Year = as.integer(Year)) %>%
na.omit(components_of_GDP)
tidy_GDP_for_graph1 %>%
select(Country, components_of_GDP, Year, value) %>%
pivot_wider(names_from = components_of_GDP, values_from = value) %>%
janitor::clean_names() %>%
mutate(
net_exports = exports - imports,
gdp = household_expenditure + government_expenditure + gross_capital_formation + net_exports
) -> wider_gdp
wider_gdp %>%
mutate(
percentage_GE = 100*government_expenditure/gdp,
percentage_GCF = 100*gross_capital_formation/gdp,
percentage_HE = 100*household_expenditure/gdp,
percentage_NE = 100*net_exports/gdp
) %>%
select(country, year, percentage_GE, percentage_GCF, percentage_HE, percentage_NE) %>%
pivot_longer(cols = 3:6, names_to = "component_of_gdp", values_to = "value") %>%
ggplot(aes(x=year, y=value, color=component_of_gdp)) +
geom_line() +
facet_wrap(~country) +
theme_minimal() +
labs(
title = "GDP and its breakdown at constant 2010 prices in US Dollars",
x = NULL,
y = "% of GDP",
caption = "Source: United Nations",
color = NULL
) +
scale_color_hue(
labels = c('Gross Capital Formation',
'Government Expenditure',
'Household Expenditure',
'Net Exports'
)
)
