rm(list  =  ls())

if(!require("pacman")) install.packages("pacman")
##  要求されたパッケージ pacman をロード中です
pacman::p_load("tidyverse",  
               "skimr",    
               "gt")     
 options(scipen = 999)
dat_temp <- read_csv("QFR2023TermPaper.csv")
## New names:
## Rows: 350 Columns: 5
## ── Column specification
## ────────────────────────────────────────────────────────
## Delimiter: "," chr (1): Fish_Market dbl (4): ...1, Year, Month, Total
## ℹ Use `spec()` to retrieve the full column specification for this data. ℹ
## Specify the column types or set `show_col_types = FALSE` to quiet this message.
## • `` -> `...1`
skimr::skim(dat_temp)
Data summary
Name dat_temp
Number of rows 350
Number of columns 5
_______________________
Column type frequency:
character 1
numeric 4
________________________
Group variables None

Variable type: character

skim_variable n_missing complete_rate min max empty n_unique whitespace
Fish_Market 0 1 8 8 0 2 0

Variable type: numeric

skim_variable n_missing complete_rate mean sd p0 p25 p50 p75 p100 hist
…1 0 1 175.50 101.18 1 88.25 175.5 262.75 350 ▇▇▇▇▇
Year 0 1 2005.13 6.92 1994 1999.00 2005.0 2011.00 2019 ▇▇▇▆▃
Month 0 1 8.33 2.91 1 7.00 9.0 11.00 12 ▂▁▅▆▇
Total 0 1 2317.37 3038.65 0 1.00 640.0 4176.50 13095 ▇▂▂▁▁
summary_1 <- dat_temp |> group_by(Year, Month) |> summarize(total_landing = sum(Total))
## `summarise()` has grouped output by 'Year'. You can override using the
## `.groups` argument.
fig_1 <- ggplot(summary_1, aes(x = Month, y = total_landing, fill = Year)) + geom_bar(stat = "identity", position = "dodge")

fig_1

summary_1 <- dat_temp |> group_by(Year, Month) |> 
  summarize(total_landing = sum(Total)) |>
  mutate(Month = as.factor(Month), Year = as.factor(Year)) |> ungroup()
## `summarise()` has grouped output by 'Year'. You can override using the
## `.groups` argument.
fig_2 <-  ggplot(summary_1, aes(x = Month, y = total_landing, fill = Year)) + 
            geom_bar(stat = "identity", position = "dodge")

fig_2

#上のグラフはマーケットA、Bでの水揚げ量を合計した数値をもとに作成したものである。この魚種は8月から12月までの期間にのみ漁獲されそれ以外の期間には全く漁獲が行われていない。漁獲量の最も多くなる月は10月である。また、年別に漁獲量を見ると各月ともに2000年前後までは増加しておりそれ以降は減少傾向であることが分かる。このことから、この種の資源量は2000年前後までは増加傾向であり、それ以降は減少が続いていると考えられる。
table(dat_temp$Fish_Market)
## 
## Market_A Market_B 
##      183      167
dat_temp <- dat_temp|> filter(Fish_Market %in% c("Market_A")) 
summary_1 <- dat_temp |> group_by(Year, Month) |> 
  summarize(total_landing = sum(Total)) |>
  mutate(Month = as.factor(Month), Year = as.factor(Year)) |> ungroup()
## `summarise()` has grouped output by 'Year'. You can override using the
## `.groups` argument.
fig_3 <-  ggplot(summary_1, aes(x = Month, y = total_landing, fill = Year)) + 
            geom_bar(stat = "identity", position = "dodge")
  
   
fig_3

#マーケットAに限定してみると10月に漁獲量が最大となる傾向が顕著に表れていることが分かる。年別に見ると年々漁獲量が減少しているが減少の幅は大きくない。
dat_temp2 <- read_csv("QFR2023TermPaper.csv")
## New names:
## Rows: 350 Columns: 5
## ── Column specification
## ────────────────────────────────────────────────────────
## Delimiter: "," chr (1): Fish_Market dbl (4): ...1, Year, Month, Total
## ℹ Use `spec()` to retrieve the full column specification for this data. ℹ
## Specify the column types or set `show_col_types = FALSE` to quiet this message.
## • `` -> `...1`
dat_temp2 <- dat_temp2|> filter(Fish_Market %in% c("Market_B")) 
summary_2 <- dat_temp2 |> group_by(Year, Month) |> 
  summarize(total_landing = sum(Total)) |>
  mutate(Month = as.factor(Month), Year = as.factor(Year)) |> ungroup()
## `summarise()` has grouped output by 'Year'. You can override using the
## `.groups` argument.
fig_4 <-  ggplot(summary_2, aes(x = Month, y = total_landing, fill = Year)) + 
            geom_bar(stat = "identity", position = "dodge")

fig_4

#マーケットBに限定してみると9月、10月に漁獲量が増大している。年別に見ると各月ともにマーケットAに比べて漁獲量の減少幅が大きい。

#このように、この魚種は8~12月にのみ漁獲され、10月頃に漁獲量が最大となり、年別の漁獲量は減少傾向にあるという漁獲量の特徴があることが分かる。秋に漁獲される魚にはシロザケ、サンマや近年増加しているタチウオ等が存在する。いわて大漁ナビ(URL: https://www.suigi.pref.iwate.jp/shikyosearch)の市況検索を用いてこられの魚種の水揚げ量について調べるとシロザケは秋に漁獲量が増大するものの春、夏にもごく少量ではあるが漁獲されており作成したグラフの特徴とは合致しない。タチウオについてはデータ上では分類がなく確認ができなかった。一方、サンマの漁獲量は作成したグラフの特徴に非常に似ていた。1994年から2023年までの岩手県内の全ての市場でのサンマの漁獲量のグラフは次のようになる。
dat_temp3 <- read_csv("kadai.csv")
## New names:
## Rows: 140 Columns: 6
## ── Column specification
## ────────────────────────────────────────────────────────
## Delimiter: "," chr (1): Date num (1): Landing_Amount lgl (4): ...3, ...4, ...5,
## ...6
## ℹ Use `spec()` to retrieve the full column specification for this data. ℹ
## Specify the column types or set `show_col_types = FALSE` to quiet this message.
## • `` -> `...3`
## • `` -> `...4`
## • `` -> `...5`
## • `` -> `...6`
#いわて大漁ナビの市況検索で1994年9月から2023年9月までの岩手県内の市場のサンマの水揚げ量のデータを取得した。
skimr::skim(dat_temp3)
Data summary
Name dat_temp3
Number of rows 140
Number of columns 6
_______________________
Column type frequency:
character 1
logical 4
numeric 1
________________________
Group variables None

Variable type: character

skim_variable n_missing complete_rate min max empty n_unique whitespace
Date 1 0.99 8 9 0 139 0

Variable type: logical

skim_variable n_missing complete_rate mean count
…3 140 0 NaN :
…4 140 0 NaN :
…5 140 0 NaN :
…6 140 0 NaN :

Variable type: numeric

skim_variable n_missing complete_rate mean sd p0 p25 p50 p75 p100 hist
Landing_Amount 1 0.99 6772625 6693217 4094 862112.5 4643085 12171277 22943560 ▇▂▂▂▁
dat_temp3 <- dat_temp3 |> mutate(Date = lubridate::ymd(Date)) 
dat_temp3 <- dat_temp3 |> 
  mutate(YEAR = lubridate::year(Date), 
         MONTH = lubridate::month(Date), 
         DAY = lubridate::day(Date))
skimr::skim(dat_temp3)
Data summary
Name dat_temp3
Number of rows 140
Number of columns 9
_______________________
Column type frequency:
Date 1
logical 4
numeric 4
________________________
Group variables None

Variable type: Date

skim_variable n_missing complete_rate min max median n_unique
Date 1 0.99 1994-09-01 2023-09-01 2009-11-01 139

Variable type: logical

skim_variable n_missing complete_rate mean count
…3 140 0 NaN :
…4 140 0 NaN :
…5 140 0 NaN :
…6 140 0 NaN :

Variable type: numeric

skim_variable n_missing complete_rate mean sd p0 p25 p50 p75 p100 hist
Landing_Amount 1 0.99 6772624.97 6693217.36 4094 862112.5 4643085 12171277 22943560 ▇▂▂▂▁
YEAR 1 0.99 2008.81 8.42 1994 2001.5 2009 2016 2023 ▇▇▇▇▇
MONTH 1 0.99 9.81 1.41 6 9.0 10 11 12 ▁▅▅▅▇
DAY 1 0.99 1.00 0.00 1 1.0 1 1 1 ▁▁▇▁▁
summary_3 <- dat_temp3 |> group_by(YEAR, MONTH) |> summarize(total_landing = sum(Landing_Amount))
## `summarise()` has grouped output by 'YEAR'. You can override using the
## `.groups` argument.
fig_5 <- ggplot(summary_3, aes(x = MONTH, y = total_landing, fill = YEAR)) + geom_bar(stat = "identity", position = "dodge")

fig_5
## Warning: Removed 1 rows containing missing values (`geom_bar()`).

summary_3 <- dat_temp3 |> group_by(YEAR, MONTH) |> 
  summarize(total_landing = sum(Landing_Amount)) |>
  mutate(MONTH = as.factor(MONTH), YEAR = as.factor(YEAR)) |> ungroup()
## `summarise()` has grouped output by 'YEAR'. You can override using the
## `.groups` argument.
fig_6 <-  ggplot(summary_3, aes(x = MONTH, y = total_landing, fill = YEAR)) + 
            geom_bar(stat = "identity", position = "dodge")

fig_6
## Warning: Removed 1 rows containing missing values (`geom_bar()`).

#このグラフでは7~12月にサンマが漁獲されていることが分かり7月の漁獲は非常に少量であるためマーケットABのグラフと一致しているといえる。漁獲量も9,10,11月に増大しておりこの特徴も一致している。また、年別の漁獲量を見ても2000年前後にかけて増加してそれ以降は大きく減少しているという特徴も一致している。以上のことから与えられたデータの魚種はサンマであると推測できる。