# 必要パッケージ読みこみ
pacman::p_load(tidyverse , RSQLite , lubridate, tableone, skimr,rms,ggplot2,
               gtsummary,summarytools,naniar,mice,readr,zoo,rmarkdown)

# 緑の読み込み
df_green <- read.csv("緑.csv", na = c("", " ", "NA"),fileEncoding = "Shift-JIS")
colnames(df_green)
##  [1] "X"                  "患者ID"             "年"                
##  [4] "月"                 "アルブミン"         "クレアチニン"      
##  [7] "eGFR"               "蛋白"               "尿クレアチニン"    
## [10] "尿酸"               "尿蛋白"             "患者年齢.x"        
## [13] "性別.女.0..男.1..x" "尿蛋白gCr"          "尿蛋白gCr分類"     
## [16] "eGFR分類"           "CKDステージ"        "年_後"             
## [19] "月_後"              "アルブミン_後"      "クレアチニン_後"   
## [22] "eGFR_後"            "蛋白_後"            "尿クレアチニン_後" 
## [25] "尿酸_後"            "尿蛋白_後"          "患者年齢.y"        
## [28] "性別.女.0..男.1..y" "尿蛋白gCr_後"       "尿蛋白gCr分類_後"  
## [31] "eGFR分類_後"        "CKDステージ_後"     "CKDの差"           
## [34] "CKD悪化"
table(df_green$CKD悪化)
## 
##   0   1 
## 224 119
attach(df_green)
sapply(df_green, class)
##                  X             患者ID                 年                 月 
##          "integer"          "integer"          "integer"          "integer" 
##         アルブミン       クレアチニン               eGFR               蛋白 
##          "numeric"          "numeric"          "numeric"          "integer" 
##     尿クレアチニン               尿酸             尿蛋白         患者年齢.x 
##          "numeric"          "numeric"          "integer"          "integer" 
## 性別.女.0..男.1..x          尿蛋白gCr      尿蛋白gCr分類           eGFR分類 
##          "integer"          "numeric"        "character"        "character" 
##        CKDステージ              年_後              月_後      アルブミン_後 
##          "integer"          "integer"          "integer"          "numeric" 
##    クレアチニン_後            eGFR_後            蛋白_後  尿クレアチニン_後 
##          "numeric"          "numeric"          "integer"          "numeric" 
##            尿酸_後          尿蛋白_後         患者年齢.y 性別.女.0..男.1..y 
##          "numeric"          "integer"          "integer"          "integer" 
##       尿蛋白gCr_後   尿蛋白gCr分類_後        eGFR分類_後     CKDステージ_後 
##          "numeric"        "character"        "character"          "integer" 
##            CKDの差            CKD悪化 
##          "integer"          "integer"
model_green<-glm(CKD悪化~患者年齢.x+性別.女.0..男.1..x+尿酸,family=binomial)
summary(model_green)
## 
## Call:
## glm(formula = CKD悪化 ~ 患者年齢.x + 性別.女.0..男.1..x + 
##     尿酸, family = binomial)
## 
## Coefficients:
##                     Estimate Std. Error z value Pr(>|z|)   
## (Intercept)        -1.760444   0.572657  -3.074  0.00211 **
## 患者年齢.x          0.013533   0.006897   1.962  0.04975 * 
## 性別.女.0..男.1..x  0.037582   0.259547   0.145  0.88487   
## 尿酸                0.087826   0.096695   0.908  0.36373   
## ---
## Signif. codes:  0 '***' 0.001 '**' 0.01 '*' 0.05 '.' 0.1 ' ' 1
## 
## (Dispersion parameter for binomial family taken to be 1)
## 
##     Null deviance: 442.83  on 342  degrees of freedom
## Residual deviance: 437.05  on 339  degrees of freedom
## AIC: 445.05
## 
## Number of Fisher Scoring iterations: 4
exp(coef(model_green))
##        (Intercept)         患者年齢.x 性別.女.0..男.1..x               尿酸 
##          0.1719684          1.0136252          1.0382969          1.0917983
exp(confint(model_green))
## Waiting for profiling to be done...
##                         2.5 %    97.5 %
## (Intercept)        0.05478713 0.5200367
## 患者年齢.x         1.00009121 1.0275605
## 性別.女.0..男.1..x 0.62336314 1.7276734
## 尿酸               0.90404346 1.3221714
# 全体
df_all <- read.csv("全体.csv", na = c("", " ", "NA"),fileEncoding = "Shift-JIS")
colnames(df_all)
##  [1] "X"                  "患者ID"             "年"                
##  [4] "月"                 "アルブミン"         "クレアチニン"      
##  [7] "eGFR"               "蛋白"               "尿クレアチニン"    
## [10] "尿酸"               "尿蛋白"             "患者年齢.x"        
## [13] "性別.女.0..男.1..x" "尿蛋白gCr"          "尿蛋白gCr分類"     
## [16] "eGFR分類"           "CKDステージ"        "年_後"             
## [19] "月_後"              "アルブミン_後"      "クレアチニン_後"   
## [22] "eGFR_後"            "蛋白_後"            "尿クレアチニン_後" 
## [25] "尿酸_後"            "尿蛋白_後"          "患者年齢.y"        
## [28] "性別.女.0..男.1..y" "尿蛋白gCr_後"       "尿蛋白gCr分類_後"  
## [31] "eGFR分類_後"        "CKDステージ_後"     "CKDの差"           
## [34] "CKD悪化"
table(df_all$CKD悪化)
## 
##   0   1 
## 731 259
attach(df_all)
##  以下のオブジェクトは df_green からマスクされています:
## 
##     CKDステージ, CKDステージ_後, CKDの差, CKD悪化, eGFR, eGFR_後,
##     eGFR分類, eGFR分類_後, X, アルブミン, アルブミン_後, クレアチニン,
##     クレアチニン_後, 患者ID, 患者年齢.x, 患者年齢.y, 月, 月_後,
##     性別.女.0..男.1..x, 性別.女.0..男.1..y, 蛋白, 蛋白_後,
##     尿クレアチニン, 尿クレアチニン_後, 尿酸, 尿酸_後, 尿蛋白,
##     尿蛋白_後, 尿蛋白gCr, 尿蛋白gCr_後, 尿蛋白gCr分類,
##     尿蛋白gCr分類_後, 年, 年_後
sapply(df_all, class)
##                  X             患者ID                 年                 月 
##          "integer"          "integer"          "integer"          "integer" 
##         アルブミン       クレアチニン               eGFR               蛋白 
##          "numeric"          "numeric"          "numeric"          "integer" 
##     尿クレアチニン               尿酸             尿蛋白         患者年齢.x 
##          "numeric"          "numeric"          "integer"          "integer" 
## 性別.女.0..男.1..x          尿蛋白gCr      尿蛋白gCr分類           eGFR分類 
##          "integer"          "numeric"        "character"        "character" 
##        CKDステージ              年_後              月_後      アルブミン_後 
##          "integer"          "integer"          "integer"          "numeric" 
##    クレアチニン_後            eGFR_後            蛋白_後  尿クレアチニン_後 
##          "numeric"          "numeric"          "integer"          "numeric" 
##            尿酸_後          尿蛋白_後         患者年齢.y 性別.女.0..男.1..y 
##          "numeric"          "integer"          "integer"          "integer" 
##       尿蛋白gCr_後   尿蛋白gCr分類_後        eGFR分類_後     CKDステージ_後 
##          "numeric"        "character"        "character"          "integer" 
##            CKDの差            CKD悪化 
##          "integer"          "integer"
model_all<-glm(CKD悪化~患者年齢.x+性別.女.0..男.1..x+尿酸,data=df_all,family=binomial)
summary(model_all)
## 
## Call:
## glm(formula = CKD悪化 ~ 患者年齢.x + 性別.女.0..男.1..x + 
##     尿酸, family = binomial, data = df_all)
## 
## Coefficients:
##                     Estimate Std. Error z value Pr(>|z|)    
## (Intercept)        -2.715856   0.377848  -7.188 6.59e-13 ***
## 患者年齢.x          0.021101   0.004338   4.864 1.15e-06 ***
## 性別.女.0..男.1..x  0.096552   0.159716   0.605    0.545    
## 尿酸                0.084644   0.052591   1.609    0.108    
## ---
## Signif. codes:  0 '***' 0.001 '**' 0.01 '*' 0.05 '.' 0.1 ' ' 1
## 
## (Dispersion parameter for binomial family taken to be 1)
## 
##     Null deviance: 1138.0  on 989  degrees of freedom
## Residual deviance: 1106.3  on 986  degrees of freedom
## AIC: 1114.3
## 
## Number of Fisher Scoring iterations: 4
exp(coef(model_all))
##        (Intercept)         患者年齢.x 性別.女.0..男.1..x               尿酸 
##         0.06614833         1.02132484         1.10136713         1.08832918
exp(confint(model_all))
## Waiting for profiling to be done...
##                         2.5 %    97.5 %
## (Intercept)        0.03116154 0.1372532
## 患者年齢.x         1.01276047 1.0301463
## 性別.女.0..男.1..x 0.80581687 1.5079041
## 尿酸               0.98158631 1.2066284
# オレンジ
df_orange <- read.csv("オレンジ.csv", na = c("", " ", "NA"),fileEncoding = "Shift-JIS")
colnames(df_orange)
##  [1] "X"                  "患者ID"             "年"                
##  [4] "月"                 "アルブミン"         "クレアチニン"      
##  [7] "eGFR"               "蛋白"               "尿クレアチニン"    
## [10] "尿酸"               "尿蛋白"             "患者年齢.x"        
## [13] "性別.女.0..男.1..x" "尿蛋白gCr"          "尿蛋白gCr分類"     
## [16] "eGFR分類"           "CKDステージ"        "年_後"             
## [19] "月_後"              "アルブミン_後"      "クレアチニン_後"   
## [22] "eGFR_後"            "蛋白_後"            "尿クレアチニン_後" 
## [25] "尿酸_後"            "尿蛋白_後"          "患者年齢.y"        
## [28] "性別.女.0..男.1..y" "尿蛋白gCr_後"       "尿蛋白gCr分類_後"  
## [31] "eGFR分類_後"        "CKDステージ_後"     "CKDの差"           
## [34] "CKD悪化"
table(df_orange$CKD悪化)
## 
##   0   1 
## 508 123
attach(df_orange)
##  以下のオブジェクトは df_all からマスクされています:
## 
##     CKDステージ, CKDステージ_後, CKDの差, CKD悪化, eGFR, eGFR_後,
##     eGFR分類, eGFR分類_後, X, アルブミン, アルブミン_後, クレアチニン,
##     クレアチニン_後, 患者ID, 患者年齢.x, 患者年齢.y, 月, 月_後,
##     性別.女.0..男.1..x, 性別.女.0..男.1..y, 蛋白, 蛋白_後,
##     尿クレアチニン, 尿クレアチニン_後, 尿酸, 尿酸_後, 尿蛋白,
##     尿蛋白_後, 尿蛋白gCr, 尿蛋白gCr_後, 尿蛋白gCr分類,
##     尿蛋白gCr分類_後, 年, 年_後
##  以下のオブジェクトは df_green からマスクされています:
## 
##     CKDステージ, CKDステージ_後, CKDの差, CKD悪化, eGFR, eGFR_後,
##     eGFR分類, eGFR分類_後, X, アルブミン, アルブミン_後, クレアチニン,
##     クレアチニン_後, 患者ID, 患者年齢.x, 患者年齢.y, 月, 月_後,
##     性別.女.0..男.1..x, 性別.女.0..男.1..y, 蛋白, 蛋白_後,
##     尿クレアチニン, 尿クレアチニン_後, 尿酸, 尿酸_後, 尿蛋白,
##     尿蛋白_後, 尿蛋白gCr, 尿蛋白gCr_後, 尿蛋白gCr分類,
##     尿蛋白gCr分類_後, 年, 年_後
sapply(df_orange, class)
##                  X             患者ID                 年                 月 
##          "integer"          "integer"          "integer"          "integer" 
##         アルブミン       クレアチニン               eGFR               蛋白 
##          "numeric"          "numeric"          "numeric"          "integer" 
##     尿クレアチニン               尿酸             尿蛋白         患者年齢.x 
##          "numeric"          "numeric"          "integer"          "integer" 
## 性別.女.0..男.1..x          尿蛋白gCr      尿蛋白gCr分類           eGFR分類 
##          "integer"          "numeric"        "character"        "character" 
##        CKDステージ              年_後              月_後      アルブミン_後 
##          "integer"          "integer"          "integer"          "numeric" 
##    クレアチニン_後            eGFR_後            蛋白_後  尿クレアチニン_後 
##          "numeric"          "numeric"          "integer"          "numeric" 
##            尿酸_後          尿蛋白_後         患者年齢.y 性別.女.0..男.1..y 
##          "numeric"          "integer"          "integer"          "integer" 
##       尿蛋白gCr_後   尿蛋白gCr分類_後        eGFR分類_後     CKDステージ_後 
##          "numeric"        "character"        "character"          "integer" 
##            CKDの差            CKD悪化 
##          "integer"          "integer"
model_orange<-glm(CKD悪化~患者年齢.x+性別.女.0..男.1..x+尿酸+アルブミン,data=df_orange,family=binomial)
summary(model_orange)
## 
## Call:
## glm(formula = CKD悪化 ~ 患者年齢.x + 性別.女.0..男.1..x + 
##     尿酸 + アルブミン, family = binomial, data = df_orange)
## 
## Coefficients:
##                      Estimate Std. Error z value Pr(>|z|)    
## (Intercept)        -4.2988176  0.6004688  -7.159 8.12e-13 ***
## 患者年齢.x          0.0370888  0.0064824   5.721 1.06e-08 ***
## 性別.女.0..男.1..x -0.0614248  0.2222282  -0.276   0.7822    
## 尿酸                0.1342535  0.0747090   1.797   0.0723 .  
## アルブミン          0.0009115  0.0230288   0.040   0.9684    
## ---
## Signif. codes:  0 '***' 0.001 '**' 0.01 '*' 0.05 '.' 0.1 ' ' 1
## 
## (Dispersion parameter for binomial family taken to be 1)
## 
##     Null deviance: 622.53  on 630  degrees of freedom
## Residual deviance: 580.89  on 626  degrees of freedom
## AIC: 590.89
## 
## Number of Fisher Scoring iterations: 4
exp(coef(model_orange))
##        (Intercept)         患者年齢.x 性別.女.0..男.1..x               尿酸 
##         0.01358461         1.03778513         0.94042366         1.14368276 
##         アルブミン 
##         1.00091189
exp(confint(model_orange))
## Waiting for profiling to be done...
##                          2.5 %    97.5 %
## (Intercept)        0.004047817 0.0428185
## 患者年齢.x         1.024975126 1.0514075
## 性別.女.0..男.1..x 0.608603912 1.4567279
## 尿酸               0.987425108 1.3242282
## アルブミン         0.945876225 1.0461517
# 黄色
df_yellow<- read.csv("黄色.csv", na = c("", " ", "NA"),fileEncoding = "Shift-JIS")
colnames(df_yellow)
##  [1] "X"                  "患者ID"             "年"                
##  [4] "月"                 "アルブミン"         "クレアチニン"      
##  [7] "eGFR"               "蛋白"               "尿クレアチニン"    
## [10] "尿酸"               "尿蛋白"             "患者年齢.x"        
## [13] "性別.女.0..男.1..x" "尿蛋白gCr"          "尿蛋白gCr分類"     
## [16] "eGFR分類"           "CKDステージ"        "年_後"             
## [19] "月_後"              "アルブミン_後"      "クレアチニン_後"   
## [22] "eGFR_後"            "蛋白_後"            "尿クレアチニン_後" 
## [25] "尿酸_後"            "尿蛋白_後"          "患者年齢.y"        
## [28] "性別.女.0..男.1..y" "尿蛋白gCr_後"       "尿蛋白gCr分類_後"  
## [31] "eGFR分類_後"        "CKDステージ_後"     "CKDの差"           
## [34] "CKD悪化"
table(df_yellow$CKD悪化)
## 
##   0   1 
## 259 111
attach(df_yellow)
##  以下のオブジェクトは df_orange からマスクされています:
## 
##     CKDステージ, CKDステージ_後, CKDの差, CKD悪化, eGFR, eGFR_後,
##     eGFR分類, eGFR分類_後, X, アルブミン, アルブミン_後, クレアチニン,
##     クレアチニン_後, 患者ID, 患者年齢.x, 患者年齢.y, 月, 月_後,
##     性別.女.0..男.1..x, 性別.女.0..男.1..y, 蛋白, 蛋白_後,
##     尿クレアチニン, 尿クレアチニン_後, 尿酸, 尿酸_後, 尿蛋白,
##     尿蛋白_後, 尿蛋白gCr, 尿蛋白gCr_後, 尿蛋白gCr分類,
##     尿蛋白gCr分類_後, 年, 年_後
##  以下のオブジェクトは df_all からマスクされています:
## 
##     CKDステージ, CKDステージ_後, CKDの差, CKD悪化, eGFR, eGFR_後,
##     eGFR分類, eGFR分類_後, X, アルブミン, アルブミン_後, クレアチニン,
##     クレアチニン_後, 患者ID, 患者年齢.x, 患者年齢.y, 月, 月_後,
##     性別.女.0..男.1..x, 性別.女.0..男.1..y, 蛋白, 蛋白_後,
##     尿クレアチニン, 尿クレアチニン_後, 尿酸, 尿酸_後, 尿蛋白,
##     尿蛋白_後, 尿蛋白gCr, 尿蛋白gCr_後, 尿蛋白gCr分類,
##     尿蛋白gCr分類_後, 年, 年_後
##  以下のオブジェクトは df_green からマスクされています:
## 
##     CKDステージ, CKDステージ_後, CKDの差, CKD悪化, eGFR, eGFR_後,
##     eGFR分類, eGFR分類_後, X, アルブミン, アルブミン_後, クレアチニン,
##     クレアチニン_後, 患者ID, 患者年齢.x, 患者年齢.y, 月, 月_後,
##     性別.女.0..男.1..x, 性別.女.0..男.1..y, 蛋白, 蛋白_後,
##     尿クレアチニン, 尿クレアチニン_後, 尿酸, 尿酸_後, 尿蛋白,
##     尿蛋白_後, 尿蛋白gCr, 尿蛋白gCr_後, 尿蛋白gCr分類,
##     尿蛋白gCr分類_後, 年, 年_後
sapply(df_yellow, class)
##                  X             患者ID                 年                 月 
##          "integer"          "integer"          "integer"          "integer" 
##         アルブミン       クレアチニン               eGFR               蛋白 
##          "numeric"          "numeric"          "numeric"          "integer" 
##     尿クレアチニン               尿酸             尿蛋白         患者年齢.x 
##          "numeric"          "numeric"          "integer"          "integer" 
## 性別.女.0..男.1..x          尿蛋白gCr      尿蛋白gCr分類           eGFR分類 
##          "integer"          "numeric"        "character"        "character" 
##        CKDステージ              年_後              月_後      アルブミン_後 
##          "integer"          "integer"          "integer"          "numeric" 
##    クレアチニン_後            eGFR_後            蛋白_後  尿クレアチニン_後 
##          "numeric"          "numeric"          "integer"          "numeric" 
##            尿酸_後          尿蛋白_後         患者年齢.y 性別.女.0..男.1..y 
##          "numeric"          "integer"          "integer"          "integer" 
##       尿蛋白gCr_後   尿蛋白gCr分類_後        eGFR分類_後     CKDステージ_後 
##          "numeric"        "character"        "character"          "integer" 
##            CKDの差            CKD悪化 
##          "integer"          "integer"
model_yellow<-glm(CKD悪化~患者年齢.x+性別.女.0..男.1..x+尿酸+アルブミン,data=df_yellow,family=binomial)
summary(model_yellow)
## 
## Call:
## glm(formula = CKD悪化 ~ 患者年齢.x + 性別.女.0..男.1..x + 
##     尿酸 + アルブミン, family = binomial, data = df_yellow)
## 
## Coefficients:
##                     Estimate Std. Error z value Pr(>|z|)   
## (Intercept)        -0.636991   1.478866  -0.431  0.66667   
## 患者年齢.x          0.016865   0.007417   2.274  0.02298 * 
## 性別.女.0..男.1..x -0.049704   0.269495  -0.184  0.85367   
## 尿酸                0.245059   0.087440   2.803  0.00507 **
## アルブミン         -0.595374   0.314791  -1.891  0.05858 . 
## ---
## Signif. codes:  0 '***' 0.001 '**' 0.01 '*' 0.05 '.' 0.1 ' ' 1
## 
## (Dispersion parameter for binomial family taken to be 1)
## 
##     Null deviance: 452.04  on 369  degrees of freedom
## Residual deviance: 430.61  on 365  degrees of freedom
## AIC: 440.61
## 
## Number of Fisher Scoring iterations: 4
exp(coef(model_yellow))
##        (Intercept)         患者年齢.x 性別.女.0..男.1..x               尿酸 
##          0.5288814          1.0170080          0.9515107          1.2776971 
##         アルブミン 
##          0.5513561
exp(confint(model_yellow))
## Waiting for profiling to be done...
##                        2.5 %   97.5 %
## (Intercept)        0.0284559 9.682310
## 患者年齢.x         1.0024597 1.032106
## 性別.女.0..男.1..x 0.5608751 1.616982
## 尿酸               1.0783786 1.521586
## アルブミン         0.2945251 1.019974