library("tidyverse")
library("readr")
library("dplyr")
library("ggrepel")

データの読み込み

df_growth_forecast <- read.csv(here::here("data\\cleaned\\202511005_growth_forecast.csv"))
df_office <- read.csv(here::here("data\\cleaned\\20250922_office_forecast.csv"))
df_GDP <- read.csv(here::here("data\\cleaned\\20250920_GDP_forecast.csv"))
df_actualGDP_jpn_tokyo <- read.csv(here::here("data\\cleaned\\20250925_actualGDP_jpn_tokyo.csv"))
df_pop <- read.csv(here::here("data\\cleaned\\20251007_pop.csv"))

データの加工

df_office <- df_office |>
  mutate(
    office = parse_number(as.character(office)),
    forecast = parse_number(as.character(forecast)),
    after_forecast = parse_number(as.character(after_forecast))
  ) |>
  mutate(log_office = log(office), log_forecast = log(forecast), log_after_forecast = log(after_forecast)) |>
  mutate(growth_log_office = log_office - lag(log_office), growth_log_forecast = log_forecast - lag(log_forecast), growth_log_after_forecast = log_after_forecast - lag(log_after_forecast)) |>
  mutate(growth_log_combined = coalesce(growth_log_forecast, growth_log_after_forecast))

df_GDP <- df_GDP |>
  mutate(GDP = parse_number(as.character(GDP))) |>
  mutate(log_GDP = log(GDP), log_forecast = log(forecast), log_after_forecast = log(after_forecast)) |>
  mutate(growth_log_GDP = log_GDP - lag(log_GDP), growth_log_forecast = log_forecast -lag(log_forecast), growth_log_after_forecast = log_after_forecast - lag(log_after_forecast))

df_actualGDP_jpn_tokyo <- df_actualGDP_jpn_tokyo |>
  mutate(jpn = parse_number(as.character(jpn))) |>
  mutate(tokyo = parse_number(as.character(tokyo))) |>
  mutate(tokyo = tokyo/1000) |>
  mutate(log_jpn = log(jpn), log_tokyo = log(tokyo)) |>
  mutate(growth_log_jpn = log_jpn - lag(log_jpn), growth_log_tokyo = log_tokyo -lag(log_tokyo)) |>
  group_by(period = if_else(year <= 1992, "1980-1992", "1993-2022"))

df_pop <- df_pop |>
  mutate(pop_jpn = parse_number(as.character(pop_jpn)), wapop_jpn = parse_number(as.character(wapop_jpn)), pop_tokyo = parse_number(as.character(pop_tokyo)), wapop_tokyo = parse_number(as.character(wapop_tokyo))) |>
  mutate(pop_jpn = pop_jpn*1000, wapop_jpn = wapop_jpn*1000) |>
  mutate(log_growth_pop_jpn = log(pop_jpn/lag(pop_jpn, order_by = year, n = 1)), log_growth_pop_tokyo = log(pop_tokyo/lag(pop_tokyo))) |>
  mutate(log_pop_jpn = log(pop_jpn), log_wapop_jpn = log(wapop_jpn), log_pop_tokyo = log(pop_tokyo), log_wapop_tokyo = log(wapop_tokyo)) |>
  mutate(growth_log_pop_jpn = log_pop_jpn - lag(log_pop_jpn), growth_log_wapop_jpn = log_wapop_jpn - lag(log_wapop_jpn), growth_log_pop_tokyo = log_pop_tokyo - lag(log_pop_tokyo), growth_log_wapop_tokyo = log_wapop_tokyo - lag(log_wapop_tokyo))

グラフ

df_growth_forecast |>
  ggplot() +
  geom_line(aes(x = year, y = actualGDP_growth_rate, color = "actualGDP_growth_rate")) +
  geom_line(aes(x = year, y = gov, color = "gov")) +
  geom_line(aes(x = year, y = min, color = "min")) +
  geom_line(aes(x = year, y = 4, color = "4%forecast")) +
  scale_x_continuous(breaks = seq(1980, 2023, 5)) +
  scale_y_continuous(breaks = seq(0, 5, 1)) +
  labs(x = "年", y = "実質GDP成長率", title = "実質GDP成長率と予測") +
  scale_color_manual(values = c( "red", "black", "blue", "green")) +
  theme_classic() +
  theme(plot.title = element_text(hjust = 0.5), legend.title = element_blank())

df_office |>
  ggplot() +
  geom_line(aes(x = year, y = growth_log_office, color = "office面積")) +
  geom_line(aes(x = year, y = growth_log_forecast, color = "forecast")) +
  geom_line(aes(x = year, y = growth_log_after_forecast, color = "forecast"), linetype = "dashed") +
  scale_x_continuous(breaks = seq(1980, 2023, 5)) +
  scale_y_continuous(breaks = seq(0, 5, 1)) +
  labs(x = "年", y = "実質GDP成長率", title = "office面積成長率と予測") +
  scale_color_manual(values = c("black", "red")) +
  theme_classic() +
  theme(plot.title = element_text(hjust = 0.5), legend.title = element_blank())

df_actualGDP_jpn_tokyo |>
  ggplot() +
  geom_line(aes(x = year, y = growth_log_jpn, color ="jpn")) +
  geom_line(aes(x = year, y = growth_log_tokyo, color = "tokyo")) +
  geom_line(aes(x = year, y = 0)) +
  scale_x_continuous(breaks = seq(1980, 2022, 5)) +
  scale_y_continuous(breaks = seq(-0.1, 0.1, 0.01)) +
  labs(x = "年", y = "実質GDP成長率", title = "日本と東京都の実質GDP成長率") +
  theme_minimal() +
  theme(legend.title = element_blank())

df_pop |>
  ggplot() +
  geom_line(aes(x = year, y = log_growth_pop_jpn, color ="jpn")) +
  geom_line(aes(x = year, y = log_growth_pop_tokyo, color = "tokyo")) +
  geom_line(aes(x = year, y = 0)) +
  scale_x_continuous(breaks = seq(1980, 2022, 5)) +
  scale_y_continuous(limits = c(-0.015, 0.015, 0.01)) +
  labs(x = "年", y = "実質GDP成長率", title = "日本と東京都の人口成長率") +
  theme_minimal() +
  theme(legend.title = element_blank())

df_pop |>
  ggplot() +
  geom_line(aes(x = year, y = growth_log_wapop_jpn, color ="jpn")) +
  geom_line(aes(x = year, y = growth_log_wapop_tokyo, color = "tokyo")) +
  geom_line(aes(x = year, y = 0)) +
  ylim(-0.15, 0.15) +
  scale_x_continuous(breaks = seq(1980, 2022, 5)) +
  scale_y_continuous(breaks = seq(-0.15, 0.15, 0.01)) +
  labs(x = "年", y = "実質GDP成長率", title = "日本と東京都生産年齢人口成長率") +
  theme_minimal() +
  theme(legend.title = element_blank())

df_pop |>
  ggplot() +
  geom_line(aes(x = year, y = pop_jpn, color ="jpn")) +
  geom_line(aes(x = year, y = pop_tokyo, color = "tokyo")) +
  geom_line(aes(x = year, y = 0)) +
  scale_x_continuous(breaks = seq(1980, 2022, 5)) +
  scale_y_continuous(breaks = seq(0, 150000000, 10000000)) +
  labs(x = "年", y = "人口(人)", title = "日本と東京都の人口") +
  theme_minimal() +
  theme(legend.title = element_blank())

CAGR <- function(start, end, year){
  (end/start)^(1/year) - 1
}


actualGDP = CAGR(df_GDP$GDP[df_GDP$year == 1983], df_GDP$GDP[df_GDP$year == 2023], 2023-1990)

actualGDP_s = CAGR(df_GDP$GDP[df_GDP$year == 1983], df_GDP$GDP[df_GDP$year == 1990], 1990-1983)

office_l = CAGR(df_office$office[df_office$year == 1983], df_office$office[df_office$year == 2023], 2023-1983)

office_s = CAGR(df_office$office[df_office$year == 1983], df_office$office[df_office$year == 1990], 1990-1983)

pop_l = CAGR(df_pop$pop_jpn[df_pop$pop_jpn == 1980], df_office$office[df_office$year == 2020], 2020-1980)

pop_s = CAGR(df_pop$pop_jpn[df_pop$pop_jpn == 1980], df_office$office[df_office$year == 1990], 1990-1980)

df_growth_rate <- data.frame("期間" = c("1983-1990", "full sample"), GDP = c(actualGDP, actualGDP_s))

df_growth_rate <- df_growth_rate |>
  mutate(
    "office" = c(office_l, office_s)
  ) #|>
  #mutate(
    #"人口" = c(pop_l, pop_s))


print(df_growth_rate)
##          期間        GDP     office
## 1   1983-1990 0.01841021 0.02432754
## 2 full sample 0.04844745 0.04771538