全コードは、必要に応じて右上のタブで表示されたい。

準備

# setup library
Packages <- c("tidyverse", "DT", "OECD")
lapply(Packages, library, character.only = TRUE)

# import data
df1_tidy <- readRDS("~/R/Musashi_University/2021_first_semester/Economic_English/Data/df1_tidy.RDS")

df2_tidy <- readRDS("~/R/Musashi_University/2021_first_semester/Economic_English/Data/df2_tidy.RDS")

データセット

DT::datatable(df1_tidy, 
              rownames = FALSE, 
              extensions = 'Buttons',
              options = list(autoWidth = TRUE,
                             pageLength = 5,
                             dom = 'Bfrtip',
                             buttons = list('copy', 'excel', "csv"),
                             scrollX = TRUE,
                             scrollCollapse = TRUE),
              class = 'cell-border stripe',
              caption = "source: ")
DT::datatable(df2_tidy, 
              rownames = FALSE, 
              extensions = 'Buttons',
              options = list(autoWidth = TRUE,
                             pageLength = 5,
                             dom = 'Bfrtip',
                             buttons = list('copy', 'excel', "csv"),
                             scrollX = TRUE,
                             scrollCollapse = TRUE),
              class = 'cell-border stripe',
              caption = "source: ")

g1 <- ggplot(df1_tidy, aes(t_mfp, t_gdphrs_v, colour = location)) + 
  geom_point() + 
  theme(legend.position = "none")

print(g1)

df1_tidy %>% 
  pivot_longer(cols = c(-"location", -"obs_time")) %>%
  ggplot(aes(obs_time, value, colour = name, group = name)) + 
  geom_point()

# tenure vs. age
df2_tidy %>% 
  ggplot(aes(tenure, fill = age)) + 
  geom_bar() +
  coord_flip()

重回帰分析

# liner regression --------------------------------------------------------

df1_model <- 
  df1_tidy %>% 
  filter(location %in% c("AUT", "JPN")) %>% 
  group_by(location) %>%
  nest() %>% 
  mutate(model = map(data, ~lm(t_mfp ~ t_gdphrs_v + t_hrspop + t_ulch, data = .)) %>% 
  setNames(c(location))
  )


library(gtsummary) #http://www.danieldsjoberg.com/gtsummary/articles/tbl_regression.html
tbl_regression(df1_model$model[[1]]) %>% 
  add_global_p() %>%
  bold_p(t = 0.05) %>% 
  modify_caption(location[1])

tbl_regression(df1_model$model[[2]]) %>% 
  add_global_p() %>%
  bold_p(t = 0.05) %>% 
  modify_caption(location[2])