1 Introduction

2 Problem

2.1 Calculating contribution to annual growth rate of GDP

\[ \tiny C_t= \left( \frac{C_{q_{t}}^{clv}-C_{q_{t-4}}^{clv}} {GDP_{q_{t-4}}^{clv}} \times \frac{\frac{\sum_{i=-1}^{-4} C_{q_{i}}^{nom}}{\sum_{i=-1}^{-4} C_{q_{i}}^{clv}}} {\frac{\sum_{i=-1}^{-4} C_{q_{i}}^{clv}}{\sum_{i=-1}^{-4} GDP_{q_{i}}^{clv}}} \right) + \left( \frac {\frac{\sum_{i=-1}^{-4} C_{q_{i}}^{nom}}{\sum_{i=-1}^{-4} C_{q_{i}}^{clv}}} {\frac{\sum_{i=-1}^{-4} C_{q_{i}}^{clv}}{\sum_{i=-1}^{-4} GDP_{q_{i}}^{clv}}} - \frac {\frac{\sum_{i=-5}^{-8} C_{q_{i}}^{nom}}{\sum_{i=-5}^{-8} C_{q_{i}}^{clv}}} {\frac{\sum_{i=-5}^{-8} C_{q_{i}}^{clv}}{\sum_{i=-5}^{-8} GDP_{q_{i}}^{clv}}} \right) \times \left( \frac{C_{q_{t-4}}^{clv}}{GDP_{q_{t-4}}^{clv}}- \frac {\frac{\sum_{i=-1}^{-4}C_{q_{i}}^{clv}}{\sum_{i=-1}^{-4}GDP_{q_{i}}^{clv}}} {\frac{\sum_{i=-1}^{-4}GDP_{q_{i}}^{clv}}{\sum_{i=-1}^{-4}GDP_{q_{i}}^{clv}}} \right) \]

\[\tiny C_t= \left( \frac {C_{q_{t}}^{clv}-C_{q_{t-4}}^{clv}} {GDP_{q_{t-4}}^{clv}} \times \frac {\sum_{i=-1}^{-4} C_{q_{i}}^{nom} \times \sum_{i=-1}^{-4} GDP_{q_{i}}^{clv}} {(\sum_{i=-1}^{-4} C_{q_{i}}^{clv})^2} \right) + \left( \frac {\sum_{i=-1}^{-4} C_{q_{i}}^{nom} \times \sum_{i=-1}^{-4} GDP_{q_{i}}^{clv}} {(\sum_{i=-1}^{-4} C_{q_{i}}^{clv})^2} - \frac {\sum_{i=-5}^{-8} C_{q_{i}}^{nom} \times \sum_{i=-5}^{-8} GDP_{q_{i}}^{clv}} {(\sum_{i=-5}^{-8} C_{q_{i}}^{clv})^2} \right) \times \left( \frac{C_{q_{t-4}}^{clv}}{GDP_{q_{t-4}}^{clv}}- \frac{\sum_{i=-1}^{-4}C_{q_{i}}^{clv}}{\sum_{i=-1}^{-4}GDP_{q_{i}}^{clv}} \right) \]

3 R code

The following libraries are needed to reproduce the code. All data will be imported from eurostat (~8mb) and stored locally in your working directory

library(tidyverse) # brings dplyr, tidyr, ggplot
library(eurostat) # allows data import from eurostat
library(lubridate) # for date conversions
library(zoo) # for date conversions
library(knitr)

In this step, data is downaloaded from the eurostat

#downlaoding GDP data from eurostat
df <- get_eurostat("namq_10_gdp")

The following country abbreviations are availble from the dataset

unique(as.character(df$geo))
##  [1] "AT"   "CH"   "CY"   "CZ"   "FI"   "FR"   "HU"   "IT"   "LT"   "LV"  
## [11] "NL"   "LU"   "BE"   "BG"   "DE"   "DK"   "EA"   "EA12" "EA18" "EA19"
## [21] "EE"   "EL"   "ES"   "EU15" "EU28" "HR"   "IE"   "MK"   "NO"   "PL"  
## [31] "PT"   "RO"   "RS"   "SE"   "SI"   "SK"   "TR"   "UK"   "IS"   "MT"  
## [41] "AL"   "BA"   "ME"   "XK"

So one has to choose which country chould be used in the subsequent steps

country="LT" #<- type here the country code

Selected country code is used to reduce the working dataframe. “Not seasionally adjusted” data is selected, as well as items GDP (B1GQ), Consumption (P3), Gross fixed capital investment (P51G), gross investment (P5G), Exports(P6) and Imports (P7). Units selected are chain-linked volumes (2010) and current year prices

df <- df %>%
        filter(geo==country, 
               s_adj=="NSA", 
               na_item %in% c("B1GQ","P3","P51G","P5G","P6","P7"), 
               unit %in% c("CLV10_MEUR", "CP_MEUR")) %>%
        mutate(time=ymd(time)) %>% 
        select(-s_adj) %>% 
        mutate(year=year(time))

First calculating the proportion of nominal values to real values for every year

NOM_REAL <- df %>% group_by(unit, na_item, year) %>% 
                        summarise(sum=sum(values)) %>% 
                        spread(unit, sum) %>% 
                        mutate(part=CP_MEUR/CLV10_MEUR)%>%
                        select(year, na_item, part)%>%
                        spread(na_item, part) %>%
                        rename(B1GQ_n_r=B1GQ, P3_n_r=P3, P51G_n_r=P51G, P5G_n_r=P5G, P6_n_r=P6, P7_n_r=P7)

Second calculating the proportion of real values to real GDP for every year

REAL_REAL <- df %>% filter(unit=="CLV10_MEUR")  %>%
                                group_by(na_item, year) %>%
                                summarise(sum=sum(values)) %>%
                                spread(na_item, sum) %>%
                                mutate(B1GQ_r_r=B1GQ/B1GQ) %>%
                                mutate(P3_r_r=P3/B1GQ) %>%
                                mutate(P51G_r_r=P51G/B1GQ) %>%
                                mutate(P5G_r_r=P5G/B1GQ) %>%
                                mutate(P6_r_r=P6/B1GQ) %>%
                                mutate(P7_r_r=P7/B1GQ) %>%
                                select(-c(2:6))

In the enxt one dataframe is being build consisinting of quarterly entries originating from the df dataframe, combined with values from NOM_REAL and REAL_REAL dataframes

DF_TOTAL <- data.frame(time=unique(df$time)) %>% 
        arrange(time) %>%
        mutate(year=year(time)) 

DF_TOTAL <- merge(DF_TOTAL, NOM_REAL, by.x = "year")

DF_TOTAL <- merge(DF_TOTAL, REAL_REAL, by.x="year")

DF_TOTAL <- merge(DF_TOTAL, (df %>% 
                                     filter(unit=="CLV10_MEUR") %>%
                                     spread(na_item, values) %>% 
                                     rename(B1GQ_clv=B1GQ, 
                                            P3_clv=P3, 
                                            P51G_clv=P51G, 
                                            P5G_clv=P5G, 
                                            P6_clv=P6, 
                                            P7_clv=P7) %>%
                                     select(-c(year,geo,unit))), by.x="time")

DF_TOTAL <- merge(DF_TOTAL, (df %>% 
                                     filter(unit=="CP_MEUR") %>%
                                     spread(na_item, values) %>%
                                     rename(B1GQ_cp=B1GQ, 
                                            P3_cp=P3, 
                                            P51G_cp=P51G, 
                                            P5G_cp=P5G, 
                                            P6_cp=P6, 
                                            P7_cp=P7) %>%
                                     select(-c(year,geo,unit))), by.x="time")

In the next step, the contribution to GDP is being calculated using the FORMULA

# Contribution of GDP to GDP
DF_TOTAL <- DF_TOTAL %>% mutate(B1GQ_cont = ((B1GQ_clv - lag(B1GQ_clv, 4))/lag(B1GQ_clv, 
    4)) * (lag(B1GQ_n_r, 4)/lag(B1GQ_n_r, 4)) + ((lag(B1GQ_n_r, 4)/lag(B1GQ_n_r, 
    4) - (lag(B1GQ_n_r, 8)/lag(B1GQ_n_r, 8))) * (lag(B1GQ_clv, 4)/lag(B1GQ_clv, 
    4) - lag(B1GQ_r_r, 4)/lag(B1GQ_r_r, 4))))

# Contribution of Consumption to GDP
DF_TOTAL <- DF_TOTAL %>% mutate(P3_cont = ((P3_clv - lag(P3_clv, 4))/lag(B1GQ_clv, 
    4)) * (lag(P3_n_r, 4)/lag(B1GQ_n_r, 4)) + ((lag(P3_n_r, 4)/lag(B1GQ_n_r, 
    4) - (lag(P3_n_r, 8)/lag(B1GQ_n_r, 8))) * (lag(P3_clv, 4)/lag(B1GQ_clv, 
    4) - lag(P3_r_r, 4)/lag(B1GQ_r_r, 4))))

# Contribution of Gross Fixed Capital Formation to GDP
DF_TOTAL <- DF_TOTAL %>% mutate(P51G_cont = ((P51G_clv - lag(P51G_clv, 4))/lag(B1GQ_clv, 
    4)) * (lag(P51G_n_r, 4)/lag(B1GQ_n_r, 4)) + ((lag(P51G_n_r, 4)/lag(B1GQ_n_r, 
    4) - (lag(P51G_n_r, 8)/lag(B1GQ_n_r, 8))) * (lag(P51G_clv, 4)/lag(B1GQ_clv, 
    4) - lag(P51G_r_r, 4)/lag(B1GQ_r_r, 4))))

# Contribution of Gorss Investment to GDP
DF_TOTAL <- DF_TOTAL %>% mutate(P5G_cont = ((P5G_clv - lag(P5G_clv, 4))/lag(B1GQ_clv, 
    4)) * (lag(P5G_n_r, 4)/lag(B1GQ_n_r, 4)) + ((lag(P5G_n_r, 4)/lag(B1GQ_n_r, 
    4) - (lag(P5G_n_r, 8)/lag(B1GQ_n_r, 8))) * (lag(P5G_clv, 4)/lag(B1GQ_clv, 
    4) - lag(P5G_r_r, 4)/lag(B1GQ_r_r, 4))))

# Contribution of Exports to GDP
DF_TOTAL <- DF_TOTAL %>% mutate(P6_cont = ((P6_clv - lag(P6_clv, 4))/lag(B1GQ_clv, 
    4)) * (lag(P6_n_r, 4)/lag(B1GQ_n_r, 4)) + ((lag(P6_n_r, 4)/lag(B1GQ_n_r, 
    4) - (lag(P6_n_r, 8)/lag(B1GQ_n_r, 8))) * (lag(P6_clv, 4)/lag(B1GQ_clv, 
    4) - lag(P6_r_r, 4)/lag(B1GQ_r_r, 4))))

# Contribution of Imports to GDP
DF_TOTAL <- DF_TOTAL %>% mutate(P7_cont = ((P7_clv - lag(P7_clv, 4))/lag(B1GQ_clv, 
    4)) * (lag(P7_n_r, 4)/lag(B1GQ_n_r, 4)) + ((lag(P7_n_r, 4)/lag(B1GQ_n_r, 
    4) - (lag(P7_n_r, 8)/lag(B1GQ_n_r, 8))) * (lag(P7_clv, 4)/lag(B1GQ_clv, 
    4) - lag(P7_r_r, 4)/lag(B1GQ_r_r, 4))))

In this step, Inventories are calculated as \(Inventories=\text{Gross investemnt}-\text{Gross fixed capital formation}\) and \(NX=\text{Export}-\text{Import}\)

DF_TOTAL <- DF_TOTAL %>% mutate(INV_cont = P5G_cont-P51G_cont)
DF_TOTAL <- DF_TOTAL %>% mutate(NX_cont =P6_cont-P7_cont)

In the next step, the data is only processed to make it more usable for the GGPLOT package

DF_TOTAL_p1 <- DF_TOTAL %>% 
                        gather(na_item, values, 3:35) %>%
                        filter(na_item %in% c("P3_cont", "P51G_cont","INV_cont", "NX_cont")) %>%
                        arrange(time) %>% 
                        na.omit() %>% 
                        mutate(values=values*100)%>%
                        mutate(time=as.yearqtr(time))

DF_TOTAL_p2 <- DF_TOTAL %>% 
                        gather(na_item, values, 3:35) %>%
                        filter(na_item =="B1GQ_cont") %>%
                        arrange(time)%>% na.omit() %>%
                        mutate(values=values*100)%>%
                        mutate(time=as.yearqtr(time))

And a plot is being drawn

ggplot(data=DF_TOTAL_p1, aes(x=time, y=values))+
        geom_bar(aes(fill=na_item),stat="identity")+
        geom_line(data=DF_TOTAL_p2, aes(x=time, y=values, group=1), colour="blue")+
        labs(title=paste(country, "Contribution to annual GDP growth, NSA"), x="Date", y="Percent")+
        scale_fill_discrete(name="Variables", labels=c("Inventories", "Net Exports", "Consumption", "Investment"))+
        theme(axis.text.x = element_text(angle=45, hjust = 1, vjust = 1),legend.position="bottom")+
        scale_x_yearqtr(n=30, format="%Y-Q%q")