# Load required libraries
library(haven)    # For reading .dta files
library(dplyr)    # For data manipulation
## 
## Attaching package: 'dplyr'
## The following objects are masked from 'package:stats':
## 
##     filter, lag
## The following objects are masked from 'package:base':
## 
##     intersect, setdiff, setequal, union
library(tidyr)    # For data reshaping
library(ggplot2)  # For plotting
library(zoo)      # For rolling mean
## 
## Attaching package: 'zoo'
## The following objects are masked from 'package:base':
## 
##     as.Date, as.Date.numeric
# Load the dataset
pwt1001 <- read_dta("pwt1001.dta")

# Define analysis parameters
countries <- c("United States", "United Kingdom", "Germany", "France", "Canada", "Italy")
start_year <- 1960
n_years <- 10  # Number of years for annualized growth rate calculations

# Data Preprocessing
# Filter data for selected countries and variables, and arrange it by country and year
selected_countries <- pwt1001 %>%
  filter(country %in% countries) %>%
  select(year, country, rgdpna, rkna, emp, labsh, hc) %>%
  mutate(year = as.numeric(year)) %>%
  filter(year >= start_year) %>%
  arrange(country, year)

# Calculate Growth Rates
# Compute 10-year annualized growth rates for GDP per capita
data <- selected_countries %>%
  group_by(country) %>%
  mutate(
    y_pc = rgdpna / emp,  # GDP per capita
    lagged_y_pc = lag(y_pc, n_years),
    lagged_GDP_growth = (((y_pc / lagged_y_pc)^(1 / n_years)) - 1) * 100
  )

# PLOT 1: Labour Productivity Growth
ggplot(data[data$year >= 1985, ], aes(x = year, y = lagged_GDP_growth, color = country)) +
  geom_line() +
  geom_vline(xintercept = 2007, linetype = "dashed", color = "black", size = 0.5) +
  labs(
    title = paste(n_years, "Year Annualized Labour Productivity Growth Rate"),
    x = "Year",
    y = "Labour Productivity Growth (%)"
  ) +
  facet_wrap(~ country, scales = "free_y") +  # Separate plots for each country
  theme_minimal() + 
  theme(legend.position = "none")  # Hide the legend
## 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.

# Calculate Contributions to Growth
annualized_data <- selected_countries %>%
  group_by(country) %>%
  mutate(
    y_pc = rgdpna / emp,  # GDP per capita
    k_pc = rkna / emp,    # Capital per capita
    lagged_y_pc = lag(y_pc, n_years),
    lagged_k_pc = lag(k_pc, n_years),
    lagged_hc = lag(hc, n_years),
    lagged_labsch = rollmean(labsh, k = n_years, align = "right", na.pad = TRUE),
    lagged_GDP_growth = (((y_pc / lagged_y_pc)^(1 / n_years)) - 1)*100,
    lagged_capital_growth = (((k_pc / lagged_k_pc)^(1 / n_years)) - 1)*100,
    lagged_human_capital_growth = (((hc / lagged_hc)^(1 / n_years)) - 1)*100,
    physical_capital_deepening = (1 - lagged_labsch) * lagged_capital_growth,
    human_capital_deepening = lagged_labsch * lagged_human_capital_growth,
    lagged_TFP_growth = lagged_GDP_growth - physical_capital_deepening - human_capital_deepening
  ) %>%
  na.omit()

# Analyze Growth Slowdown
growth_analysis <- annualized_data %>%
  mutate(year = as.numeric(year)) %>%
  mutate(period = ifelse(year <= 2007, "1985_2007", "2007_2020")) %>%
  group_by(country, period) %>%
  summarize(
    avg_growth_rate = mean(lagged_GDP_growth, na.rm = TRUE),
    avg_capital_growth = mean(lagged_capital_growth, na.rm = TRUE),
    avg_hc_growth = mean(lagged_human_capital_growth, na.rm = TRUE),
    avg_TFP_growth = mean(lagged_TFP_growth, na.rm = TRUE),
    avg_human_capital_contribution = mean(human_capital_deepening, na.rm = TRUE),
    avg_physical_capital_contribution = mean(physical_capital_deepening, na.rm = TRUE),
    avg_tfp_contribution = mean(lagged_TFP_growth, na.rm = TRUE),
    mean_labsch = mean(lagged_labsch, na.rm = TRUE)
  ) %>%
  pivot_wider(
    names_from = period,
    values_from = c(avg_growth_rate, avg_capital_growth, avg_hc_growth, avg_TFP_growth, avg_human_capital_contribution, avg_physical_capital_contribution, avg_tfp_contribution, mean_labsch)
  )
## `summarise()` has grouped output by 'country'. You can override using the
## `.groups` argument.
View(growth_analysis)

growth_analysis2 <- growth_analysis %>%
  mutate(
    productivity_slowdown = avg_growth_rate_2007_2020 - avg_growth_rate_1985_2007,
    human_capital_slowdown = avg_hc_growth_2007_2020 - avg_hc_growth_1985_2007,
    physical_capital_slowdown = avg_capital_growth_2007_2020 - avg_capital_growth_1985_2007,
    tfp_slowdown = avg_TFP_growth_2007_2020 - avg_TFP_growth_1985_2007,
    human_capital_contribution_slowdown = avg_human_capital_contribution_2007_2020 - avg_human_capital_contribution_1985_2007,
    physical_capital_contribution_slowdown = avg_physical_capital_contribution_2007_2020 - avg_physical_capital_contribution_1985_2007,
    tfp_contribution_slowdown = avg_tfp_contribution_2007_2020 - avg_tfp_contribution_1985_2007,
    hc_share = human_capital_contribution_slowdown / productivity_slowdown,
    pc_share = physical_capital_contribution_slowdown / productivity_slowdown,
    tfp_share = tfp_contribution_slowdown / productivity_slowdown
  )

View(growth_analysis2)


# Slowdown Table

library(gt)
library(dplyr)
# Preprocess the data to multiply shares by 100
growth_analysis2 <- growth_analysis2 %>%
  mutate(
    hc_share = hc_share * 100,
    pc_share = pc_share * 100,
    tfp_share = tfp_share * 100
  )

# Create the table
growth_analysis2 %>%
  ungroup() %>%  # Remove grouping by country
  arrange(desc(productivity_slowdown)) %>%  # Sort by highest productivity slowdown
  select(
    country,
    productivity_slowdown,
    human_capital_slowdown,
    physical_capital_slowdown,
    tfp_slowdown,
    hc_share,
    pc_share,
    tfp_share
  ) %>%
  gt() %>%
  tab_header(
    title = "Accounting for the Slowdown in Labour Productivity after the 2008 Financial Crisis",
    subtitle = "Changes in Growth Rates Between 1985-2007, and 2008-2019; and Percentage Contributions to the Slowdown"
  ) %>%
  fmt_number(
    columns = c(
      productivity_slowdown,
      human_capital_slowdown,
      physical_capital_slowdown,
      tfp_slowdown,
      hc_share,
      pc_share,
      tfp_share
    ),
    n_sigfig = 3
  ) %>%
  cols_label(
    country = "Country",
    productivity_slowdown = "Productivity Slowdown",
    human_capital_slowdown = "Human Capital Slowdown",
    physical_capital_slowdown = "Physical Capital Slowdown",
    tfp_slowdown = "TFP Slowdown",
    hc_share = "Human Capital Contribution (%)",
    pc_share = "Physical Capital Contribution (%)",
    tfp_share = "TFP Contribution (%)"
  ) %>%
  tab_style(
    style = cell_borders(
      sides = "right",
      color = "black",
      weight = px(2)
    ),
    locations = list(
      cells_body(columns = c(country)),
      cells_column_labels(columns = c(country))
    )
  ) %>% 
  tab_style(
    style = cell_borders(
      sides = "right",
      color = "black",
      weight = px(2)
    ),
    locations = list(
      cells_body(columns = c(productivity_slowdown)),
      cells_column_labels(columns = c(productivity_slowdown))
    )
  ) %>%
  tab_style(
    style = cell_borders(
      sides = "right",
      color = "black",
      weight = px(2)
    ),
    locations = list(
      cells_body(columns = c(tfp_slowdown)),
      cells_column_labels(columns = c(tfp_slowdown))
    )
  ) %>%
  tab_style(
    style = list(cell_text(weight = "bold")),
    locations = cells_column_labels(everything())
  )
Accounting for the Slowdown in Labour Productivity after the 2008 Financial Crisis
Changes in Growth Rates Between 1985-2007, and 2008-2019; and Percentage Contributions to the Slowdown
Country Productivity Slowdown Human Capital Slowdown Physical Capital Slowdown TFP Slowdown Human Capital Contribution (%) Physical Capital Contribution (%) TFP Contribution (%)
United States −0.387 −0.321 −0.178 −0.146 53.5 8.68 37.8
Canada −0.722 −0.352 −0.564 −0.416 37.8 4.54 57.6
United Kingdom −1.65 −0.364 −2.00 −0.530 11.3 56.5 32.1
France −1.78 −0.186 −1.85 −1.06 8.11 32.1 59.8
Germany −1.94 −0.519 −3.03 −0.616 18.2 50.1 31.7
Italy −3.06 −0.231 −2.85 −1.77 5.60 36.6 57.8
# Accounting Table

# Load necessary library
library(gt)
growth_analysis %>%
  select(
    country,
    starts_with("avg_growth_rate"),
    starts_with("avg_capital_growth"),
    starts_with("avg_hc_growth"),
    starts_with("avg_TFP_growth"),
    starts_with("mean_labsch"),
    starts_with("avg_human_capital_contribution"),
    starts_with("avg_physical_capital_contribution"),
    starts_with("avg_tfp_contribution")
  ) %>%
  pivot_longer(
    cols = -country,
    names_to = c(".value", "period"),
    names_pattern = "(.*)_(1985_2007|2007_2020)"
  ) %>%
  mutate(period = recode(period, 
                         "1985_2007" = "1985-2007", 
                         "2007_2020" = "2007-2020")) %>%
  gt() %>%
  tab_header(
    title = "Growth Analysis by Country and Period",
    subtitle = "Detailed Breakdown of Growth Metrics Across Periods"
  ) %>%
  fmt_number(
    columns = everything(),
    decimals = 2
  ) %>%
  cols_label(
    avg_growth_rate = "Growth Rate",
    avg_capital_growth = "Capital Growth",
    avg_hc_growth = "Human Capital Growth",
    avg_TFP_growth = "TFP Growth",
    mean_labsch = "Labour Share",
    avg_human_capital_contribution = "Human Capital Contribution",
    avg_physical_capital_contribution = "Physical Capital Contribution",
    avg_tfp_contribution = "TFP Contribution",
    period = ""  # Hide period column label
  ) %>%
  tab_spanner(
    label = "1985-2007",
    columns = contains("1985-2007")
  ) %>%
  tab_spanner(
    label = "2007-2020",
    columns = contains("2007-2020")
  ) %>%
  tab_style(
    style = list(cell_text(weight = "bold")),
    locations = cells_column_labels(everything())
  )
Growth Analysis by Country and Period
Detailed Breakdown of Growth Metrics Across Periods
Growth Rate Capital Growth Human Capital Growth TFP Growth Labour Share Human Capital Contribution Physical Capital Contribution TFP Contribution
Canada
1985-2007 1.30 2.50 0.70 0.09 0.71 0.50 0.71 0.09
2007-2020 0.58 1.93 0.34 −0.32 0.65 0.22 0.68 −0.32
France
1985-2007 2.46 3.50 0.76 0.76 0.65 0.50 1.20 0.76
2007-2020 0.68 1.65 0.57 −0.31 0.62 0.35 0.63 −0.31
Germany
1985-2007 2.48 3.90 0.70 0.71 0.66 0.46 1.31 0.71
2007-2020 0.54 0.87 0.18 0.10 0.62 0.11 0.33 0.10
Italy
1985-2007 2.70 4.02 0.94 0.47 0.57 0.54 1.69 0.47
2007-2020 −0.36 1.17 0.71 −1.30 0.52 0.37 0.57 −1.30
United Kingdom
1985-2007 2.24 3.22 0.79 0.37 0.56 0.44 1.43 0.37
2007-2020 0.59 1.23 0.42 −0.16 0.59 0.25 0.50 −0.16
United States
1985-2007 1.66 2.13 0.60 0.48 0.62 0.37 0.81 0.48
2007-2020 1.28 1.95 0.28 0.34 0.60 0.17 0.77 0.34