library(dplyr)
##
## 次のパッケージを付け加えます: 'dplyr'
## 以下のオブジェクトは 'package:stats' からマスクされています:
##
## filter, lag
## 以下のオブジェクトは 'package:base' からマスクされています:
##
## intersect, setdiff, setequal, union
library(ggplot2)
library(tidyr)
library(directlabels)
## Warning: パッケージ 'directlabels' はバージョン 4.5.3 の R の下で造られました
library(tidyverse)
## ── Attaching core tidyverse packages ──────────────────────── tidyverse 2.0.0 ──
## ✔ forcats 1.0.1 ✔ readr 2.1.5
## ✔ lubridate 1.9.4 ✔ stringr 1.5.2
## ✔ purrr 1.0.4 ✔ tibble 3.3.0
## ── Conflicts ────────────────────────────────────────── tidyverse_conflicts() ──
## ✖ dplyr::filter() masks stats::filter()
## ✖ dplyr::lag() masks stats::lag()
## ℹ Use the conflicted package (<http://conflicted.r-lib.org/>) to force all conflicts to become errors
library(patchwork)
## Warning: パッケージ 'patchwork' はバージョン 4.5.2 の R の下で造られました
df_ind <- read.csv(
here::here("data/cleaned/NEEDS/20260913_industry_average.csv"),
fileEncoding = "CP932"
)
df_ind <- df_ind |>
mutate(
month = seq(
from = as.Date("1980-01-01"),
by = "month",
length.out = n()
)
) |>
relocate(month)
df_ind_long <- df_ind |>
pivot_longer(
cols = -month,
names_to = "industry",
values_to = "value"
) |>
group_by(industry) |>
mutate(
change_rate = value / lag(value) -1
)
f_index_base <- function(data, base_date){
data |>
mutate(
index = value / value[month == as.Date(base_date)]
)
}
df_ind_index <- df_ind_long |>
group_by(industry) |>
f_index_base(
base_date = "1980-01-01"
)
#####関数づくり
f_filter_ind <- function(data, threshold){
target_industry <- data |>
group_by(industry) |>
summarise(
max_index = max(index, na.rm = TRUE)
) |>
filter(max_index >= threshold) |>
pull(industry)
data |>
filter(industry %in% target_industry)
}
df_ind_index_10 <- df_ind_index |>
group_by(industry) |>
f_filter_ind(10)
df_ind_index |>
filter(
month >= "1975-01-01",
month <= "2000-01-01"
) |>
group_by(industry) |>
summarise(
max_index = max(index, na.rm = TRUE)
) |>
arrange(desc(max_index)) |>
pull(industry)
## [1] "通信" "証券" "銀行"
## [4] "倉庫.運輸関連" "サービス業" "鉄道.バス"
## [7] "ガス" "保険" "水産"
## [10] "商社" "不動産" "造船"
## [13] "小売業" "精密機器" "空運"
## [16] "その他金融業" "電力" "電気機器"
## [19] "建設" "非鉄金属および金属製品" "陸運"
## [22] "窯業" "繊維" "医薬品"
## [25] "パルプ.紙" "ゴム" "その他製造業"
## [28] "鉄鋼業" "食品" "海運"
## [31] "化学工業" "自動車.自動車部品" "機械"
## [34] "その他輸送機器" "石油" "鉱業"
df_ind_long |> ggplot(
aes(
x = month,
y = value,
color = industry
)
) +
geom_line()
df_ind_long |> ggplot(
aes(
x = month,
y = change_rate,
color = industry
)
) +
geom_line()
## Warning: Removed 36 rows containing missing values or values outside the scale range
## (`geom_line()`).
g_shoken <- ggplot(
data = df_ind_long |> filter(
industry == "証券"
),
aes(
x = month,
y = change_rate,
color = industry
)
) +
geom_line()
g_ginko <- ggplot(
data = df_ind_long |> filter(
industry == "銀行"
),
aes(
x = month,
y = change_rate,
color = industry
)
) +
geom_line()
g_sokounnyu <- ggplot(
data = df_ind_long |> filter(
industry == "倉庫.運輸関連"
),
aes(
x = month,
y = change_rate,
color = industry
)
) +
geom_line()
(g_shoken / g_ginko / g_sokounnyu) +
plot_annotation(
title = "株価の前年比"
)
## Warning: Removed 1 row containing missing values or values outside the scale range
## (`geom_line()`).
## Warning: Removed 1 row containing missing values or values outside the scale range
## (`geom_line()`).
## Removed 1 row containing missing values or values outside the scale range
## (`geom_line()`).
tbl_max_changerate <- df_ind_long |>
group_by(industry) |>
slice_max(change_rate, n = 1) |>
ungroup() |>
arrange(desc(change_rate)) |>
select(industry, month, change_rate)
knitr::kable(
tbl_max_changerate,
caption = "業種別のchange_rate最大値とその発生月"
)
| industry | month | change_rate |
|---|---|---|
| ガス | 1986-07-01 | 0.6144149 |
| 証券 | 1986-03-01 | 0.4848288 |
| 不動産 | 1986-03-01 | 0.4768526 |
| 電力 | 1986-07-01 | 0.4678353 |
| 銀行 | 1987-01-01 | 0.4642825 |
| 商社 | 1999-11-01 | 0.4226380 |
| 保険 | 1984-03-01 | 0.3837566 |
| 倉庫.運輸関連 | 1986-03-01 | 0.3800809 |
| 電気機器 | 1999-12-01 | 0.3707116 |
| 海運 | 1991-02-01 | 0.3678103 |
| 建設 | 1990-10-01 | 0.3607528 |
| その他輸送機器 | 1990-10-01 | 0.3552856 |
| 通信 | 1999-10-01 | 0.3430579 |
| 造船 | 1990-10-01 | 0.3312723 |
| 石油 | 1991-02-01 | 0.3309966 |
| サービス業 | 1999-11-01 | 0.3185264 |
| 機械 | 1990-10-01 | 0.2962858 |
| 水産 | 1989-11-01 | 0.2822837 |
| 空運 | 1986-01-01 | 0.2817474 |
| 鉄道.バス | 1986-03-01 | 0.2784366 |
| 医薬品 | 1984-10-01 | 0.2740549 |
| 食品 | 1990-10-01 | 0.2680965 |
| ゴム | 1990-10-01 | 0.2645909 |
| その他金融業 | 1991-02-01 | 0.2615077 |
| 鉄鋼業 | 1998-01-01 | 0.2603152 |
| 精密機器 | 1991-02-01 | 0.2595032 |
| 窯業 | 1990-10-01 | 0.2559478 |
| 非鉄金属および金属製品 | 1990-10-01 | 0.2555259 |
| 鉱業 | 1998-01-01 | 0.2531925 |
| パルプ.紙 | 1990-05-01 | 0.2496986 |
| 化学工業 | 1991-02-01 | 0.2491120 |
| 繊維 | 1991-02-01 | 0.2441130 |
| 陸運 | 1990-10-01 | 0.2347263 |
| その他製造業 | 1987-05-01 | 0.2200647 |
| 小売業 | 1983-12-01 | 0.1896625 |
| 自動車.自動車部品 | 1990-10-01 | 0.1548968 |
df_ind_index |>
ggplot(
aes(
x = month,
y = index,
color = industry
)
) +
geom_line()
df_ind_index_10 |>
filter(
month >= "1975-01-01",
month <= "2000-01-01"
) |>
ggplot(
aes(
x = month,
y = index,
color = industry
)
) +
geom_line()