1. Set up
    パッケージをアクティベート
knitr::opts_chunk$set(echo = TRUE, comment = "")
library(dplyr)
library(psych)
library(readr)
library(lubridate)
library(summarytools)
library(tidyverse)
library(here)
library(naniar)
library(mice)
  1. Import
    RDataをインポート。
load("hyogo_defined.RData")

hyogo_defined_RData の中には、df:KCHとamagasaki全てを統合したものが入っている

dfを下記のように分類
df_2019 兵庫こどもと尼崎合わせて、2019年1月1日から2023年12月31日までで16歳未満でPICUのみ df_amagasaki 上記で尼崎のみ
df_kch 上記で兵庫こども

#2019年1月1日〜2023年12月31日
df_2019 <- df %>%
  filter((date_icu_adm >= as.Date("2019-01-01") & 
          date_icu_adm <= as.Date("2023-12-31")) &
          age_adm_y <16 &
          unit_dic == "PICU") 

#尼崎のみ
df_amagasaki <- df_2019 %>%
  filter(Amagasaki_DB == "Amagasaki")

#兵庫こどものみ
df_kch <- df_2019 %>%
  filter(Amagasaki_DB == "KCH")

・PIM3(欠損値141)

df_2019 %>%
  group_by(Amagasaki_DB) %>%
  summarize(
    mean_PIM3 = mean(PIM3_death_rate, na.rm = TRUE),  # 欠損値を除く平均
    count_total = n(),                               # 全体のデータ数
    count_NA = sum(is.na(PIM3_death_rate))           # 欠損値の数
  )
# A tibble: 2 × 4
  Amagasaki_DB mean_PIM3 count_total count_NA
  <fct>            <dbl>       <int>    <int>
1 Amagasaki         3.40        1272      143
2 KCH               2.80        2830        0

・PIM3の欠損値年度毎評価

df_2019 %>%
  group_by(adm_year, Amagasaki_DB) %>%
  summarize(
    mean_PIM3 = mean(PIM3_death_rate, na.rm = TRUE),  # 欠損値を除く平均
    count_total = n(),                               # 全体のデータ数
    count_NA = sum(is.na(PIM3_death_rate))           # 欠損値の数
  ) %>%
  arrange(adm_year, Amagasaki_DB)  # adm_yearとAmagasaki_DBで並べ替え
`summarise()` has grouped output by 'adm_year'. You can override using the
`.groups` argument.
# A tibble: 10 × 5
# Groups:   adm_year [5]
   adm_year Amagasaki_DB mean_PIM3 count_total count_NA
      <dbl> <fct>            <dbl>       <int>    <int>
 1     2019 Amagasaki         2.43         195        0
 2     2019 KCH               2.17         612        0
 3     2020 Amagasaki         2.85         287       10
 4     2020 KCH               2.91         536        0
 5     2021 Amagasaki         2.33         250       17
 6     2021 KCH               2.27         565        0
 7     2022 Amagasaki         3.76         271       46
 8     2022 KCH               3.44         499        0
 9     2023 Amagasaki         5.97         269       70
10     2023 KCH               3.28         618        0
# PIM3の欠損値パターンを視覚化

# PIM3の変数を指定してデータを抽出
PIM3_variables <- c("PIM_cardiac", "PIM_CPB", "PIM_hr", 
                        "PIM_lr", "PIM_MV", "PIM_pupils", "PIM_sBP", 
                        "PIM_vhr", "BE", "FIO2_ped", "PaO2_ped", "PIM2_recovery")

# 指定した変数の欠損割合を棒グラフで可視化
gg_miss_var(df_amagasaki[PIM3_variables])

# 欠損値の個数と割合を計算
missing_summary_PIM3 <- df_amagasaki %>%
  summarise(across(all_of(PIM3_variables), list(
    count_NA = ~sum(is.na(.)),
    percent_NA = ~mean(is.na(.)) * 100
  ), .names = "{.col}_{.fn}"))

# 結果を表示
#missing_summary_PIM3


# 欠損パターンを確認
library(mice)
md.pattern(df_amagasaki[PIM3_variables], rotate.names = TRUE)

     PIM_lr PIM_hr PIM2_recovery PIM_CPB PIM_vhr PIM_cardiac PIM_pupils PIM_MV
1062      1      1             1       1       1           1          1      1
45        1      1             1       1       1           1          1      1
3         1      1             1       1       1           1          1      1
2         1      1             1       1       1           1          1      1
1         1      1             1       1       1           1          1      1
1         1      1             1       1       1           1          1      1
1         1      1             1       1       1           1          1      1
29        1      1             1       1       1           1          1      1
6         1      1             1       1       1           1          1      0
4         1      1             1       1       1           1          1      0
1         1      1             1       1       1           1          1      0
1         1      1             1       1       1           1          1      0
8         1      1             1       1       1           1          1      0
1         1      1             1       1       1           1          0      0
1         1      1             1       1       1           1          0      0
1         1      1             1       1       1           1          0      0
1         1      1             1       1       1           0          0      0
1         1      1             1       1       1           0          0      0
34        1      1             1       1       0           1          1      1
2         1      1             1       1       0           1          1      1
2         1      1             1       1       0           1          1      0
1         1      1             1       0       1           0          0      0
2         1      1             0       0       1           0          0      0
35        1      1             0       0       1           0          0      0
20        1      0             1       1       1           1          1      1
1         1      0             1       1       1           1          1      0
5         0      1             1       1       1           1          1      1
1         0      1             1       1       1           1          1      0
          6     21            37      38      38          40         43     67
     FIO2_ped PaO2_ped BE PIM_sBP    
1062        1        1  1       1   0
45          1        1  1       0   1
3           1        1  0       1   1
2           1        0  1       1   1
1           1        0  0       1   2
1           0        1  1       1   1
1           0        0  0       1   3
29          0        0  0       0   4
6           1        1  1       1   1
4           1        1  1       0   2
1           1        1  0       1   2
1           0        0  0       1   4
8           0        0  0       0   5
1           1        1  1       0   3
1           1        1  0       0   4
1           0        0  0       0   6
1           1        1  1       1   3
1           0        0  0       0   7
34          1        1  1       1   1
2           0        0  0       0   5
2           0        0  0       1   5
1           0        0  0       0   8
2           1        1  1       0   6
35          0        0  0       0   9
20          1        1  1       1   1
1           0        0  0       0   6
5           1        1  1       1   1
1           1        1  1       1   2
           83       85 88     131 677
library(knitr)
pattern <- md.pattern(df_amagasaki[PIM3_variables], plot = FALSE)
kable(pattern)
PIM_lr PIM_hr PIM2_recovery PIM_CPB PIM_vhr PIM_cardiac PIM_pupils PIM_MV FIO2_ped PaO2_ped BE PIM_sBP
1062 1 1 1 1 1 1 1 1 1 1 1 1 0
45 1 1 1 1 1 1 1 1 1 1 1 0 1
3 1 1 1 1 1 1 1 1 1 1 0 1 1
2 1 1 1 1 1 1 1 1 1 0 1 1 1
1 1 1 1 1 1 1 1 1 1 0 0 1 2
1 1 1 1 1 1 1 1 1 0 1 1 1 1
1 1 1 1 1 1 1 1 1 0 0 0 1 3
29 1 1 1 1 1 1 1 1 0 0 0 0 4
6 1 1 1 1 1 1 1 0 1 1 1 1 1
4 1 1 1 1 1 1 1 0 1 1 1 0 2
1 1 1 1 1 1 1 1 0 1 1 0 1 2
1 1 1 1 1 1 1 1 0 0 0 0 1 4
8 1 1 1 1 1 1 1 0 0 0 0 0 5
1 1 1 1 1 1 1 0 0 1 1 1 0 3
1 1 1 1 1 1 1 0 0 1 1 0 0 4
1 1 1 1 1 1 1 0 0 0 0 0 0 6
1 1 1 1 1 1 0 0 0 1 1 1 1 3
1 1 1 1 1 1 0 0 0 0 0 0 0 7
34 1 1 1 1 0 1 1 1 1 1 1 1 1
2 1 1 1 1 0 1 1 1 0 0 0 0 5
2 1 1 1 1 0 1 1 0 0 0 0 1 5
1 1 1 1 0 1 0 0 0 0 0 0 0 8
2 1 1 0 0 1 0 0 0 1 1 1 0 6
35 1 1 0 0 1 0 0 0 0 0 0 0 9
20 1 0 1 1 1 1 1 1 1 1 1 1 1
1 1 0 1 1 1 1 1 0 0 0 0 0 6
5 0 1 1 1 1 1 1 1 1 1 1 1 1
1 0 1 1 1 1 1 1 0 1 1 1 1 2
6 21 37 38 38 40 43 67 83 85 88 131 677