Package ——————————————————————–

library("tidyverse")
## -- Attaching packages --------------------------------------- tidyverse 1.3.1 --
## v ggplot2 3.3.6     v purrr   0.3.4
## v tibble  3.1.6     v dplyr   1.0.9
## v tidyr   1.2.0     v stringr 1.4.0
## v readr   2.1.2     v forcats 0.5.1
## -- Conflicts ------------------------------------------ tidyverse_conflicts() --
## x dplyr::filter() masks stats::filter()
## x dplyr::lag()    masks stats::lag()
library("readxl")
library("openxlsx")

DATA ——————————————————————–

### Terms Category

## Import the correspondence table that relates term categories to term weight
terms_category <- read_excel("DATA/terms_all_counted_宮川作業後_島村点検済み.xlsx") %>%
  select(Term_cat. = 用語区分, Cat. = 宮川による区分設定案)

PSACs <- read_excel(
  "DATA/All_PSACs.xlsx"
) %>% 
  left_join(terms_category, by = "Term_cat.")

### soumu
# unique
soumu <- read_excel(
  "DATA/soumu.xlsx"
) %>% 
  mutate(Corresp = "soumu")
# general
general_soumu <- read_excel(
  "DATA/general_soumu.xlsx"
) %>% 
  mutate(Corresp = "general_soumu",
         Industry = "General")

### loose
# unique
loose <- read_excel("DATA/産業法令対応表_loose.xlsx") %>% 
  rename(Law = "根拠法令名", Industry = "産業") %>% 
  mutate(Corresp = "loose")
# general
general_loose <- read_excel("DATA/産業法令対応表(一般)_loose.xlsx") %>% 
  rename(Law = "根拠法令名") %>% 
  mutate(Corresp = "general_loose",
         Industry = "General")

### strict
# unique
strict <- read_excel("DATA/産業法令対応表_strict.xlsx") %>% 
  rename(Law = "根拠法令名", Industry = "産業") %>% 
  mutate(Corresp = "strict")
# general
general_strict <- read_excel("DATA/産業法令対応表(一般)_strict.xlsx") %>% 
  rename(Law = "根拠法令名") %>% 
  mutate(Corresp = "general_strict",
         Industry = "General")

Corresp <- bind_rows(
  soumu, loose, strict
)

Corresp_general <- bind_rows(
  general_soumu,
  general_loose,
  general_strict
)

Build functions ———————————————————

## RegIndex_generator() creates a panel data set that contains
## a series of regulation indices according to your inputs;
## Reg_Type can be specified as "Non.S", "S", "Common", or "Mean".
## Corresp_Type can be specified as 
## "soumu", "loose", "strict", 
## "general_soumu", "general_loose", or "general_strict".
## Weight_Type can be specified as "Original" or "Moderate".

RegIndex_generator <- function(Reg_Type, Corresp_Type, Weight_Type){
  switch(Reg_Type,
      "Non.S" = {
      Reg <- left_join(
      PSACs %>% filter(RegType == Reg_Type),
      switch(Corresp_Type,
      "soumu" = {Corresp},
      "loose" = {Corresp},
      "strict" = {Corresp},
      "general_soumu" = {Corresp_general},
      "general_loose" = {Corresp_general},
      "general_strict" = {Corresp_general}) %>% 
      filter(Corresp == Corresp_Type), 
      by = "Law") %>% 
      mutate(Law_weight = case_when(
        Law_cat. == "Act" ~ 4,
        Law_cat. == "Cabinet_Order" ~ 3,
        Law_cat. == "Imperial_Ordinance" ~ 3,
        Law_cat. == "Ministry_Ordinance" ~ 2,
        Law_cat. == "Rule" ~ 2,
        Law_cat. == "Notice" ~ 1),
        Term_weight = switch (Weight_Type,
        "Original" = case_when(
        Cat. == "A" ~ 1000,
        Cat. == "B" ~ 100,
        Cat. == "C" ~ 10,
        Cat. == "D" ~ 1),
        "Moderate" = case_when(
        Cat. == "A" ~ 4,
        Cat. == "B" ~ 3,
        Cat. == "C" ~ 2,
        Cat. == "D" ~ 1)
          )
        ) %>% 
      group_by(Year, Industry) %>% 
      summarise(Reg_Index = sum(Law_weight * Term_weight, na.rm = T)) 
      Reg_Non.S_1995 <- Reg %>% group_by(Year, Industry) %>% 
                         filter(Year == 1995)
      Reg_Non.S <- left_join(Reg, Reg_Non.S_1995, by = "Industry", 
                       suffix = c("", "_1995")) %>% 
        summarize(Industry, Reg_Index = Reg_Index/Reg_Index_1995) %>% 
        filter(!is.na(Industry))
      },
    "S" = {
      Reg <- left_join(
        PSACs %>% filter(RegType == Reg_Type), 
        switch(Corresp_Type,
        "soumu" = {Corresp},
        "loose" = {Corresp},
        "strict" = {Corresp},
        "general_soumu" = {Corresp_general},
        "general_loose" = {Corresp_general},
        "general_strict" = {Corresp_general}) %>% 
        filter(Corresp == Corresp_Type), 
        by = "Law") %>% 
        mutate(Law_weight = case_when(
          Law_cat. == "Act" ~ 4,
          Law_cat. == "Cabinet_Order" ~ 3,
          Law_cat. == "Imperial_Ordinance" ~ 3,
          Law_cat. == "Ministry_Ordinance" ~ 2,
          Law_cat. == "Rule" ~ 2,
          Law_cat. == "Notice" ~ 1),
          Term_weight = switch (Weight_Type,
          "Original" = case_when(
          Cat. == "S" ~ 10000,
          Cat. == "A" ~ 1000,
          Cat. == "B" ~ 100,
          Cat. == "C" ~ 10,
          Cat. == "D" ~ 1),
          "Moderate" = case_when(
          Cat. == "S" ~ 5,
          Cat. == "A" ~ 4,
          Cat. == "B" ~ 3,
          Cat. == "C" ~ 2,
          Cat. == "D" ~ 1)
          )
        ) %>% 
        group_by(Year, Industry) %>% 
        summarise(Reg_Index = sum(Law_weight * Term_weight, na.rm = T)) 
      Reg_S_1995 <- Reg %>% group_by(Year, Industry) %>% 
        filter(Year == 1995)
      Reg_S <- left_join(Reg, Reg_S_1995, by = "Industry", 
                             suffix = c("", "_1995")) %>% 
        summarize(Industry, Reg_Index = Reg_Index/Reg_Index_1995) %>% 
        filter(!is.na(Industry))
    },
    "Common" = {
      Reg <- left_join(
        PSACs %>% filter(RegType == Reg_Type), 
        switch(Corresp_Type,
        "soumu" = {Corresp},
        "loose" = {Corresp},
        "strict" = {Corresp},
        "general_soumu" = {Corresp_general},
        "general_loose" = {Corresp_general},
        "general_strict" = {Corresp_general}) %>% 
        filter(Corresp == Corresp_Type), 
        by = "Law") %>% 
        mutate(Law_weight = case_when(
          Law_cat. == "Act" ~ 4,
          Law_cat. == "Cabinet_Order" ~ 3,
          Law_cat. == "Imperial_Ordinance" ~ 3,
          Law_cat. == "Ministry_Ordinance" ~ 2,
          Law_cat. == "Rule" ~ 2,
          Law_cat. == "Notice" ~ 1),
          Term_weight = switch (Weight_Type,
          "Original" = case_when(
          Cat. == "A" ~ 1000,
          Cat. == "B" ~ 100,
          Cat. == "C" ~ 10,
          Cat. == "D" ~ 1),
          "Moderate" = case_when(
          Cat. == "A" ~ 4,
          Cat. == "B" ~ 3,
          Cat. == "C" ~ 2,
          Cat. == "D" ~ 1)
          )
        ) %>% 
        group_by(Year, Industry) %>% 
        summarise(Reg_Index = sum(Law_weight * Term_weight, na.rm = T)) 
      Reg_Common_1995 <- Reg %>% group_by(Year, Industry) %>% 
        filter(Year == 1995)
      Reg_Common <- left_join(Reg, Reg_Common_1995, by = "Industry", 
                              suffix = c("", "_1995")) %>% 
        summarize(Industry, Reg_Index = Reg_Index/Reg_Index_1995) %>% 
        filter(!is.na(Industry))
    },
    "Mean" = {
      Reg <- left_join(
        PSACs %>% filter(RegType == "Non.S"), 
        switch(Corresp_Type,
        "soumu" = {Corresp},
        "loose" = {Corresp},
        "strict" = {Corresp},
        "general_soumu" = {Corresp_general},
        "general_loose" = {Corresp_general},
        "general_strict" = {Corresp_general}) %>% 
        filter(Corresp == Corresp_Type), 
        by = "Law") %>% 
        mutate(Law_weight = case_when(
          Law_cat. == "Act" ~ 4,
          Law_cat. == "Cabinet_Order" ~ 3,
          Law_cat. == "Imperial_Ordinance" ~ 3,
          Law_cat. == "Ministry_Ordinance" ~ 2,
          Law_cat. == "Rule" ~ 2,
          Law_cat. == "Notice" ~ 1),
          Term_weight = switch (Weight_Type,
          "Original" = case_when(
          Cat. == "A" ~ 1000,
          Cat. == "B" ~ 100,
          Cat. == "C" ~ 10,
          Cat. == "D" ~ 1),
          "Moderate" = case_when(
          Cat. == "A" ~ 4,
          Cat. == "B" ~ 3,
          Cat. == "C" ~ 2,
          Cat. == "D" ~ 1)
          )
        ) %>% 
        group_by(Year, Industry) %>% 
        summarise(Reg_Index = sum(Law_weight * Term_weight, na.rm = T)) 
      Reg_Non.S_1995 <- Reg %>% group_by(Year, Industry) %>% 
        filter(Year == 1995)
      Reg_Non.S <- left_join(Reg, Reg_Non.S_1995, by = "Industry", 
                             suffix = c("", "_1995")) %>% 
        summarize(Industry, Reg_Index = Reg_Index/Reg_Index_1995)
      Reg_S <- left_join(
        PSACs %>% filter(RegType == "S"), 
        switch(Corresp_Type,
        "soumu" = {Corresp},
        "loose" = {Corresp},
        "strict" = {Corresp},
        "general_soumu" = {Corresp_general},
        "general_loose" = {Corresp_general},
        "general_strict" = {Corresp_general}) %>% 
        filter(Corresp == Corresp_Type), 
        by = "Law") %>% 
        mutate(Law_weight = case_when(
          Law_cat. == "Act" ~ 4,
          Law_cat. == "Cabinet_Order" ~ 3,
          Law_cat. == "Imperial_Ordinance" ~ 3,
          Law_cat. == "Ministry_Ordinance" ~ 2,
          Law_cat. == "Rule" ~ 2,
          Law_cat. == "Notice" ~ 1),
          Term_weight = switch (Weight_Type,
          "Original" = case_when(
          Cat. == "S" ~ 10000,
          Cat. == "A" ~ 1000,
          Cat. == "B" ~ 100,
          Cat. == "C" ~ 10,
          Cat. == "D" ~ 1),
          "Moderate" = case_when(
          Cat. == "S" ~ 5,
          Cat. == "A" ~ 4,
          Cat. == "B" ~ 3,
          Cat. == "C" ~ 2,
          Cat. == "D" ~ 1)
          )
        ) %>% 
        group_by(Year, Industry) %>% 
        summarise(Reg_Index = sum(Law_weight * Term_weight, na.rm = T)) 
      Reg_S_1995 <- Reg_S %>% group_by(Year, Industry) %>% 
        filter(Year == 1995)
      Reg_S <- left_join(Reg_S, Reg_S_1995, by = "Industry", 
                         suffix = c("", "_1995")) %>% 
        summarize(Industry, Reg_Index = Reg_Index/Reg_Index_1995)
      Reg_Mean <- left_join(Reg_Non.S, Reg_S, by = c("Year", "Industry"),
                            suffix = c("_non.s", "_s")) %>% 
        group_by(Year, Industry) %>% 
        summarise(Reg_Index = (Reg_Index_non.s + Reg_Index_s)/2) %>% 
        filter(!is.na(Industry))
    }
    )
}

An Example ————————————————————–

RegIndex_generator(
              Reg_Type = "Mean", 
              Corresp_Type = "general_soumu", 
              Weight_Type = "Original"
              ) %>% 
  ggplot(aes(Year, Reg_Index)) +
  geom_line() + geom_point()
## `summarise()` has grouped output by 'Year'. You can override using the
## `.groups` argument.
## `summarise()` has grouped output by 'Year'. You can override using the
## `.groups` argument.
## `summarise()` has grouped output by 'Year'. You can override using the
## `.groups` argument.
## `summarise()` has grouped output by 'Year'. You can override using the
## `.groups` argument.
## `summarise()` has grouped output by 'Year'. You can override using the
## `.groups` argument.