パッケージの読み込み

library(dplyr)
library(readr)
library(purrr)
library(ggplot2)
library(rlang)
library(patchwork)

データの読み込み

df_original <- read.csv(here::here("data\\cleaned\\20251101_gdp_office_land.csv"))

データの加工

コンマ外す

df_all <- df_original |>
  mutate(
    gdp_jpn = parse_number(as.character(gdp_jpn)),
    gdp_tokyo = parse_number(as.character(gdp_tokyo)),
    office_tokubu = parse_number(as.character(office_tokubu)),
    land_tokyo = parse_number(as.character(land_tokyo)),
    land_tokubu = parse_number(as.character(land_tokubu))
      )

logをとる

df_all_log <- df_all |>
  mutate(
    log_gdp_jpn = log(gdp_jpn),
    log_gdp_tokyo = log(gdp_tokyo),
    log_office_tokubu = log(office_tokubu),
    log_land_tokyo = log(land_tokyo),
    log_land_tokubu = log(land_tokubu),
  )

ダミー変数を作る

df_all_log_dummy <- df_all_log |>
  mutate(
    I_1980 = ifelse(year == 1980, 1, 0),
    I_1981 = ifelse(year == 1981, 1, 0),
    I_1982 = ifelse(year == 1982, 1, 0),
    I_1983 = ifelse(year == 1983, 1, 0),
    I_1984 = ifelse(year == 1984, 1, 0),
    I_1985 = ifelse(year == 1985, 1, 0),
    I_1986 = ifelse(year == 1986, 1, 0),
    I_1987 = ifelse(year == 1987, 1, 0),
    I_1988 = ifelse(year == 1988, 1, 0),
    I_1989 = ifelse(year == 1989, 1, 0),
    I_1990 = ifelse(year == 1990, 1, 0),
    I_1991 = ifelse(year == 1991, 1, 0),
    I_1992 = ifelse(year == 1992, 1, 0),
    I_1993 = ifelse(year == 1993, 1, 0),
    I_1994 = ifelse(year == 1994, 1, 0),
    I_1995 = ifelse(year == 1995, 1, 0),
    I_1996 = ifelse(year == 1996, 1, 0),
    I_1997 = ifelse(year == 1997, 1, 0),
    I_1998 = ifelse(year == 1998, 1, 0),
    I_1999 = ifelse(year == 1999, 1, 0),
    I_2000 = ifelse(year == 2000, 1, 0),
    I_2001 = ifelse(year == 2001, 1, 0),
    I_2002 = ifelse(year == 2002, 1, 0),
    I_2003 = ifelse(year == 2003, 1, 0),
    I_2004 = ifelse(year == 2004, 1, 0),
    I_2005 = ifelse(year == 2005, 1, 0),
    I_2006 = ifelse(year == 2006, 1, 0),
    I_2007 = ifelse(year == 2007, 1, 0),
    I_2008 = ifelse(year == 2008, 1, 0),
    I_2009 = ifelse(year == 2009, 1, 0),
    I_2010 = ifelse(year == 2010, 1, 0)
  )

回帰

モデルを作る

model_make <- function(df, explained_var, start_year, end_year){
  years_dummy <- start_year:end_year
  dummy_vars <- paste0("I_", years_dummy)
  model <- reformulate(c("year", dummy_vars), response = explained_var)
  lm(model, data = df)
}

explained_vars = c(
  "log_gdp_jpn",
  "log_gdp_tokyo",
  "log_office_tokubu",
  "log_land_tokyo",
  "log_land_tokubu"
  )

lm_names = c(
  "lm_gdp_jpn_",
  "lm_gdp_tokyo_",
  "lm_office_tokubu_",
  "lm_land_tokyo_",
  "lm_land_tokubu_"
  )

name_make_lm <- function(lm_names, start_year, end_year){
  paste0(lm_names, start_year, end_year)
}

# モデルを作る
#models_2010 <- purrr::map(explained_vars, ~ model_make(df_all_log_dummy, .x, 1985, 2010))

# モデルの名前を付け直す
#model_names_2010 <- name_make_lm(lm_names, 2010)
#names(models_2010) <- model_names_2010

# データフレームに予測値を追加
## 変数名の前半を作成
trend_names = c(
  "trend_gdp_jpn_",
  "trend_gdp_tokyo_",
  "trend_office_tokubu_",
  "trend_land_tokyo_",
  "trend_land_tokubu_"
  )

## 変数名の全体を作成する変数を作成
name_make_trend <- function(trend_names, start_year, end_year){
  paste0(trend_names, start_year, end_year)
}

## 変数名の全体を作成
#trend_names_2010 <- name_make_trend(trend_names, 2010)

## 予測値を追加
#test4 <- df_all_log_dummy |>
  #mutate(
    #!!!setNames(lapply(models_2010, predict, newdata = df_all_log_dummy), trend_names_2010)
  #)

1980年スタート

1980年~2010年

# モデルを作る
models_19802010 <- purrr::map(explained_vars, ~ model_make(df_all_log_dummy, .x, 1980, 2010))

# モデルの名前を付け直す
model_names_19802010 <- name_make_lm(lm_names, 1980, 2010)
names(models_19802010) <- model_names_19802010

## 変数名の全体を作成
trend_names_19802010 <- name_make_trend(trend_names, 1980, 2010)

## 予測値を追加
df_all_trend <- df_all_log_dummy |>
  mutate(
    !!!setNames(lapply(models_19802010, predict, newdata = df_all_log_dummy), trend_names_19802010)
  )

1985年スタート

1985年~1990年

# モデルを作る
models_19851990 <- purrr::map(explained_vars, ~ model_make(df_all_log_dummy, .x, 1985, 1990))

# モデルの名前を付け直す
model_names_19851990 <- name_make_lm(lm_names, 1985, 1990)
names(models_19851990) <- model_names_19851990

## 変数名の全体を作成
trend_names_19851990 <- name_make_trend(trend_names, 1985, 1990)

## 予測値を追加
df_all_trend <- df_all_trend |>
  mutate(
    !!!setNames(lapply(models_19851990, predict, newdata = df_all_log_dummy), trend_names_19851990)
  )

1985年~1995年

# モデルを作る
models_19851995 <- purrr::map(explained_vars, ~ model_make(df_all_log_dummy, .x, 1985, 1995))

# モデルの名前を付け直す
model_names_19851995 <- name_make_lm(lm_names, 1985, 1995)
names(models_19851995) <- model_names_19851995

## 変数名の全体を作成
trend_names_19851995 <- name_make_trend(trend_names, 1985, 1995)

## 予測値を追加
df_all_trend <- df_all_trend |>
  mutate(
    !!!setNames(lapply(models_19851995, predict, newdata = df_all_log_dummy), trend_names_19851995)
  )

1985年~2000年

# モデルを作る
models_19852000 <- purrr::map(explained_vars, ~ model_make(df_all_log_dummy, .x, 1985, 2000))

# モデルの名前を付け直す
model_names_19852000 <- name_make_lm(lm_names, 1985, 2000)
names(models_19852000) <- model_names_19852000

## 変数名の全体を作成
trend_names_19852000 <- name_make_trend(trend_names, 1985, 2000)

## 予測値を追加
df_all_trend <- df_all_trend |>
  mutate(
    !!!setNames(lapply(models_19852000, predict, newdata = df_all_log_dummy), trend_names_19852000)
  )

1985年~2005年

# モデルを作る
models_19852005 <- purrr::map(explained_vars, ~ model_make(df_all_log_dummy, .x, 1985, 2005))

# モデルの名前を付け直す
model_names_19852005 <- name_make_lm(lm_names, 1985, 2005)
names(models_19852005) <- model_names_19852005

## 変数名の全体を作成
trend_names_19852005 <- name_make_trend(trend_names, 1985, 2005)

## 予測値を追加
df_all_trend <- df_all_trend |>
  mutate(
    !!!setNames(lapply(models_19852005, predict, newdata = df_all_log_dummy), trend_names_19852005)
  )

1985年~2010年

# モデルを作る
models_19852010 <- purrr::map(explained_vars, ~ model_make(df_all_log_dummy, .x, 1985, 2010))

# モデルの名前を付け直す
model_names_19852010 <- name_make_lm(lm_names, 1985, 2010)
names(models_19852010) <- model_names_19852010

## 変数名の全体を作成
trend_names_19852010 <- name_make_trend(trend_names, 1985, 2010)

## 予測値を追加
df_all_trend <- df_all_trend |>
  mutate(
    !!!setNames(lapply(models_19852010, predict, newdata = df_all_log_dummy), trend_names_19852010)
  )

グラフ

1980年スタート

1980年~2010年

graph_gdp_jpn_19802010 <- ggplot(data = df_all_trend) +
  geom_line(aes(x = year, y = log_gdp_jpn, color = "Realized")) +
  geom_line(aes(x = year, y = trend_gdp_jpn_19802010, color = "Trend"), linetype = "dashed") +
  labs(title = "Japan Actual GDP")

graph_gdp_tokyo_19802010 <- ggplot(data = df_all_trend) +
  geom_line(aes(x = year, y = log_gdp_tokyo, color = "Realized")) +
  geom_line(aes(x = year, y = trend_gdp_tokyo_19802010, color = "Trend"), linetype = "dashed") +
  labs(title = "Tokyo Actual GDP")

graph_office_tokubu_19802010 <- ggplot(data = df_all_trend) +
  geom_line(aes(x = year, y = log_office_tokubu, color = "Realized")) +
  geom_line(aes(x = year, y = trend_office_tokubu_19802010, color = "Trend"), linetype = "dashed") +
  labs(title = "Tokubu Office")

graph_land_tokyo_19802010 <- ggplot(data = df_all_trend) +
  geom_line(aes(x = year, y = log_land_tokyo, color = "Realized")) +
  geom_line(aes(x = year, y = trend_land_tokyo_19802010, color = "Trend"), linetype = "dashed") +
  labs(title = "Tokyo Land price")

graph_land_tokubu_19802010 <- ggplot(data = df_all_trend) +
  geom_line(aes(x = year, y = log_land_tokubu, color = "Realized")) +
  geom_line(aes(x = year, y = trend_land_tokubu_19802010, color = "Trend"), linetype = "dashed") +
  labs(title = "Tokubu Land Price")

wrap_plots(graph_gdp_jpn_19802010, graph_gdp_tokyo_19802010, graph_land_tokyo_19802010, graph_land_tokubu_19802010, graph_office_tokubu_19802010, ncol = 2) +
  plot_annotation(title = "Dummy:1980 ~ 2010")

1985年スタート

1985~1990

graph_gdp_jpn_19851990 <- ggplot(data = df_all_trend) +
  geom_line(aes(x = year, y = log_gdp_jpn, color = "Realized")) +
  geom_line(aes(x = year, y = trend_gdp_jpn_19851990, color = "Trend"), linetype = "dashed") +
  labs(title = "Japan Actual GDP")

graph_gdp_tokyo_19851990 <- ggplot(data = df_all_trend) +
  geom_line(aes(x = year, y = log_gdp_tokyo, color = "Realized")) +
  geom_line(aes(x = year, y = trend_gdp_tokyo_19851990, color = "Trend"), linetype = "dashed") +
  labs(title = "Tokyo Actual GDP")

graph_office_tokubu_19851990 <- ggplot(data = df_all_trend) +
  geom_line(aes(x = year, y = log_office_tokubu, color = "Realized")) +
  geom_line(aes(x = year, y = trend_office_tokubu_19851990, color = "Trend"), linetype = "dashed") +
  labs(title = "Tokubu Office")

graph_land_tokyo_19851990 <- ggplot(data = df_all_trend) +
  geom_line(aes(x = year, y = log_land_tokyo, color = "Realized")) +
  geom_line(aes(x = year, y = trend_land_tokyo_19851990, color = "Trend"), linetype = "dashed") +
  labs(title = "Tokyo Land price")

graph_land_tokubu_19851990 <- ggplot(data = df_all_trend) +
  geom_line(aes(x = year, y = log_land_tokubu, color = "Realized")) +
  geom_line(aes(x = year, y = trend_land_tokubu_19851990, color = "Trend"), linetype = "dashed") +
  labs(title = "Tokubu Land Price")

wrap_plots(graph_gdp_jpn_19851990, graph_gdp_tokyo_19851990, graph_land_tokyo_19851990, graph_land_tokubu_19851990, graph_office_tokubu_19851990, ncol = 2) +
  plot_annotation(title = "Dummy:1985 ~ 1990")

1985~1995

graph_gdp_jpn_19851995 <- ggplot(data = df_all_trend) +
  geom_line(aes(x = year, y = log_gdp_jpn, color = "Realized")) +
  geom_line(aes(x = year, y = trend_gdp_jpn_19851995, color = "Trend"), linetype = "dashed") +
  labs(title = "Japan Actual GDP")

graph_gdp_tokyo_19851995 <- ggplot(data = df_all_trend) +
  geom_line(aes(x = year, y = log_gdp_tokyo, color = "Realized")) +
  geom_line(aes(x = year, y = trend_gdp_tokyo_19851995, color = "Trend"), linetype = "dashed") +
  labs(title = "Tokyo Actual GDP")

graph_office_tokubu_19851995 <- ggplot(data = df_all_trend) +
  geom_line(aes(x = year, y = log_office_tokubu, color = "Realized")) +
  geom_line(aes(x = year, y = trend_office_tokubu_19851995, color = "Trend"), linetype = "dashed") +
  labs(title = "Tokubu Office")

graph_land_tokyo_19851995 <- ggplot(data = df_all_trend) +
  geom_line(aes(x = year, y = log_land_tokyo, color = "Realized")) +
  geom_line(aes(x = year, y = trend_land_tokyo_19851995, color = "Trend"), linetype = "dashed") +
  labs(title = "Tokyo Land price")

graph_land_tokubu_19851995 <- ggplot(data = df_all_trend) +
  geom_line(aes(x = year, y = log_land_tokubu, color = "Realized")) +
  geom_line(aes(x = year, y = trend_land_tokubu_19851995, color = "Trend"), linetype = "dashed") +
  labs(title = "Tokubu Land Price")

wrap_plots(graph_gdp_jpn_19851995, graph_gdp_tokyo_19851995, graph_land_tokyo_19851995, graph_land_tokubu_19851995, graph_office_tokubu_19851995, ncol = 2) +
  plot_annotation(title = "Dummy:1985 ~ 1995")

1985~2000

graph_gdp_jpn_19852000 <- ggplot(data = df_all_trend) +
  geom_line(aes(x = year, y = log_gdp_jpn, color = "Realized")) +
  geom_line(aes(x = year, y = trend_gdp_jpn_19852000, color = "Trend"), linetype = "dashed") +
  labs(title = "Japan Actual GDP")

graph_gdp_tokyo_19852000 <- ggplot(data = df_all_trend) +
  geom_line(aes(x = year, y = log_gdp_tokyo, color = "Realized")) +
  geom_line(aes(x = year, y = trend_gdp_tokyo_19852000, color = "Trend"), linetype = "dashed") +
  labs(title = "Tokyo Actual GDP")

graph_office_tokubu_19852000 <- ggplot(data = df_all_trend) +
  geom_line(aes(x = year, y = log_office_tokubu, color = "Realized")) +
  geom_line(aes(x = year, y = trend_office_tokubu_19852000, color = "Trend"), linetype = "dashed") +
  labs(title = "Tokubu Office")

graph_land_tokyo_19852000 <- ggplot(data = df_all_trend) +
  geom_line(aes(x = year, y = log_land_tokyo, color = "Realized")) +
  geom_line(aes(x = year, y = trend_land_tokyo_19852000, color = "Trend"), linetype = "dashed") +
  labs(title = "Tokyo Land price")

graph_land_tokubu_19852000 <- ggplot(data = df_all_trend) +
  geom_line(aes(x = year, y = log_land_tokubu, color = "Realized")) +
  geom_line(aes(x = year, y = trend_land_tokubu_19852000, color = "Trend"), linetype = "dashed") +
  labs(title = "Tokubu Land Price")

wrap_plots(graph_gdp_jpn_19852000, graph_gdp_tokyo_19852000, graph_land_tokyo_19852000, graph_land_tokubu_19852000, graph_office_tokubu_19852000, ncol = 2) +
  plot_annotation(title = "Dummy:1985 ~ 2000")

1985~2005

graph_gdp_jpn_19852005 <- ggplot(data = df_all_trend) +
  geom_line(aes(x = year, y = log_gdp_jpn, color = "Realized")) +
  geom_line(aes(x = year, y = trend_gdp_jpn_19852005, color = "Trend"), linetype = "dashed") +
  labs(title = "Japan Actual GDP")

graph_gdp_tokyo_19852005 <- ggplot(data = df_all_trend) +
  geom_line(aes(x = year, y = log_gdp_tokyo, color = "Realized")) +
  geom_line(aes(x = year, y = trend_gdp_tokyo_19852005, color = "Trend"), linetype = "dashed") +
  labs(title = "Tokyo Actual GDP")

graph_office_tokubu_19852005 <- ggplot(data = df_all_trend) +
  geom_line(aes(x = year, y = log_office_tokubu, color = "Realized")) +
  geom_line(aes(x = year, y = trend_office_tokubu_19852005, color = "Trend"), linetype = "dashed") +
  labs(title = "Tokubu Office")

graph_land_tokyo_19852005 <- ggplot(data = df_all_trend) +
  geom_line(aes(x = year, y = log_land_tokyo, color = "Realized")) +
  geom_line(aes(x = year, y = trend_land_tokyo_19852005, color = "Trend"), linetype = "dashed") +
  labs(title = "Tokyo Land price")

graph_land_tokubu_19852005 <- ggplot(data = df_all_trend) +
  geom_line(aes(x = year, y = log_land_tokubu, color = "Realized")) +
  geom_line(aes(x = year, y = trend_land_tokubu_19852005, color = "Trend"), linetype = "dashed") +
  labs(title = "Tokubu Land Price")

wrap_plots(graph_gdp_jpn_19852005, graph_gdp_tokyo_19852005, graph_land_tokyo_19852005, graph_land_tokubu_19852005, graph_office_tokubu_19852005, ncol = 2) +
  plot_annotation(title = "Dummy:1985 ~ 2005")

1985~2010

graph_gdp_jpn_19852010 <- ggplot(data = df_all_trend) +
  geom_line(aes(x = year, y = log_gdp_jpn, color = "Realized")) +
  geom_line(aes(x = year, y = trend_gdp_jpn_19852010, color = "Trend"), linetype = "dashed") +
  labs(title = "Japan Actual GDP")

graph_gdp_tokyo_19852010 <- ggplot(data = df_all_trend) +
  geom_line(aes(x = year, y = log_gdp_tokyo, color = "Realized")) +
  geom_line(aes(x = year, y = trend_gdp_tokyo_19852010, color = "Trend"), linetype = "dashed") +
  labs(title = "Tokyo Actual GDP")

graph_office_tokubu_19852010 <- ggplot(data = df_all_trend) +
  geom_line(aes(x = year, y = log_office_tokubu, color = "Realized")) +
  geom_line(aes(x = year, y = trend_office_tokubu_19852010, color = "Trend"), linetype = "dashed") +
  labs(title = "Tokubu Office")

graph_land_tokyo_19852010 <- ggplot(data = df_all_trend) +
  geom_line(aes(x = year, y = log_land_tokyo, color = "Realized")) +
  geom_line(aes(x = year, y = trend_land_tokyo_19852010, color = "Trend"), linetype = "dashed") +
  labs(title = "Tokyo Land price")

graph_land_tokubu_19852010 <- ggplot(data = df_all_trend) +
  geom_line(aes(x = year, y = log_land_tokubu, color = "Realized")) +
  geom_line(aes(x = year, y = trend_land_tokubu_19852010, color = "Trend"), linetype = "dashed") +
  labs(title = "Tokubu Land Price")

wrap_plots(graph_gdp_jpn_19852010, graph_gdp_tokyo_19852010, graph_land_tokyo_19852010, graph_land_tokubu_19852010, graph_office_tokubu_19852010, ncol = 2) +
  plot_annotation(title = "Dummy:1985 ~ 2010")