Our world in data

library(readxl)
covid2022G7 <- read_excel("covid2022G7.xlsx")
barplot(covid2022G7$total_deaths,names.arg = covid2022G7$iso_code)

library(readxl)
covid2022 <- read_excel("covid2022.xlsx")
hist(covid2022$total_deaths_per_million)

hist(covid2022$gdp_per_capita)

hist(covid2022$total_cases)

#回帰分析
ols<-lm(covid2022$total_deaths_per_million~covid2022$aged_65_older)
summary(ols)
## 
## Call:
## lm(formula = covid2022$total_deaths_per_million ~ covid2022$aged_65_older)
## 
## Residuals:
##     Min      1Q  Median      3Q     Max 
## -3118.5  -494.6  -293.0   483.0  5222.0 
## 
## Coefficients:
##                         Estimate Std. Error t value Pr(>|t|)    
## (Intercept)                82.03     126.53   0.648    0.518    
## covid2022$aged_65_older   124.98      11.94  10.466   <2e-16 ***
## ---
## Signif. codes:  0 '***' 0.001 '**' 0.01 '*' 0.05 '.' 0.1 ' ' 1
## 
## Residual standard error: 997.9 on 184 degrees of freedom
##   (42 observations deleted due to missingness)
## Multiple R-squared:  0.3731, Adjusted R-squared:  0.3697 
## F-statistic: 109.5 on 1 and 184 DF,  p-value: < 2.2e-16
#散布図
plot(covid2022$aged_65_older,covid2022$total_deaths_per_million)
#散布図に回帰直線加える
abline(ols)

厚労省オープンデータ

library(readr)
deaths_cumulative_daily <- read_csv("deaths_cumulative_daily.csv")
## Rows: 943 Columns: 49
## ── Column specification ────────────────────────────────────────────────────────
## Delimiter: ","
## chr  (1): Date
## dbl (48): ALL, Hokkaido, Aomori, Iwate, Miyagi, Akita, Yamagata, Fukushima, ...
## 
## ℹ 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.
head(deaths_cumulative_daily)
## # A tibble: 6 × 49
##   Date        ALL Hokkaido Aomori Iwate Miyagi Akita Yamagata Fukushima Ibaraki
##   <chr>     <dbl>    <dbl>  <dbl> <dbl>  <dbl> <dbl>    <dbl>     <dbl>   <dbl>
## 1 2020/5/9    613       48      0     0      1     0        0         0       9
## 2 2020/5/10   621       51      0     0      1     0        0         0       9
## 3 2020/5/11   643       56      0     0      1     0        0         0       9
## 4 2020/5/12   668       62      0     0      1     0        0         0       9
## 5 2020/5/13   687       68      0     0      1     0        0         0       9
## 6 2020/5/14   710       70      0     0      1     0        0         0       9
## # ℹ 39 more variables: Tochigi <dbl>, Gunma <dbl>, Saitama <dbl>, Chiba <dbl>,
## #   Tokyo <dbl>, Kanagawa <dbl>, Niigata <dbl>, Toyama <dbl>, Ishikawa <dbl>,
## #   Fukui <dbl>, Yamanashi <dbl>, Nagano <dbl>, Gifu <dbl>, Shizuoka <dbl>,
## #   Aichi <dbl>, Mie <dbl>, Shiga <dbl>, Kyoto <dbl>, Osaka <dbl>, Hyogo <dbl>,
## #   Nara <dbl>, Wakayama <dbl>, Tottori <dbl>, Shimane <dbl>, Okayama <dbl>,
## #   Hiroshima <dbl>, Yamaguchi <dbl>, Tokushima <dbl>, Kagawa <dbl>,
## #   Ehime <dbl>, Kochi <dbl>, Fukuoka <dbl>, Saga <dbl>, Nagasaki <dbl>, …
#データの観測数
dim(deaths_cumulative_daily)
## [1] 943  49
nrow(deaths_cumulative_daily)
## [1] 943
death<-subset(deaths_cumulative_daily,deaths_cumulative_daily$Date=="2022/12/7"|deaths_cumulative_daily$Date=="2021/12/31"|deaths_cumulative_daily$Date=="2020/12/31")

head(death)
## # A tibble: 3 × 49
##   Date         ALL Hokkaido Aomori Iwate Miyagi Akita Yamagata Fukushima Ibaraki
##   <chr>      <dbl>    <dbl>  <dbl> <dbl>  <dbl> <dbl>    <dbl>     <dbl>   <dbl>
## 1 2020/12/31  3459      453      8    24     16     1        6        20      36
## 2 2021/12/31 18385     1475     38    53    118    27       56       176     220
## 3 2022/12/7  50819     3476    423   291    554   337      268       365     817
## # ℹ 39 more variables: Tochigi <dbl>, Gunma <dbl>, Saitama <dbl>, Chiba <dbl>,
## #   Tokyo <dbl>, Kanagawa <dbl>, Niigata <dbl>, Toyama <dbl>, Ishikawa <dbl>,
## #   Fukui <dbl>, Yamanashi <dbl>, Nagano <dbl>, Gifu <dbl>, Shizuoka <dbl>,
## #   Aichi <dbl>, Mie <dbl>, Shiga <dbl>, Kyoto <dbl>, Osaka <dbl>, Hyogo <dbl>,
## #   Nara <dbl>, Wakayama <dbl>, Tottori <dbl>, Shimane <dbl>, Okayama <dbl>,
## #   Hiroshima <dbl>, Yamaguchi <dbl>, Tokushima <dbl>, Kagawa <dbl>,
## #   Ehime <dbl>, Kochi <dbl>, Fukuoka <dbl>, Saga <dbl>, Nagasaki <dbl>, …
library(tidyr)
names(death) <- paste0("pref_",names(death))
death$date<-death$pref_Date
death$pref_Date<-NULL
deathlong<-pivot_longer(death, starts_with("pref_"), names_to = "pref", values_to = "death")
deathlong<-subset(deathlong,pref!="pref_ALL")

library(stringr)
deathlong$pref<-str_sub(deathlong$pref,6,20)

summary(deathlong)
##      date               pref               death       
##  Length:141         Length:141         Min.   :   0.0  
##  Class :character   Class :character   1st Qu.:  30.0  
##  Mode  :character   Mode  :character   Median : 136.0  
##                                        Mean   : 515.3  
##                                        3rd Qu.: 453.0  
##                                        Max.   :6793.0
head(deathlong)
## # A tibble: 6 × 3
##   date       pref     death
##   <chr>      <chr>    <dbl>
## 1 2020/12/31 Hokkaido   453
## 2 2020/12/31 Aomori       8
## 3 2020/12/31 Iwate       24
## 4 2020/12/31 Miyagi      16
## 5 2020/12/31 Akita        1
## 6 2020/12/31 Yamagata     6