首先加载本报告所需要的库
library(tidyverse)
## ── Attaching core tidyverse packages ──────────────────────── tidyverse 2.0.0 ──
## ✔ dplyr 1.1.4 ✔ readr 2.1.5
## ✔ forcats 1.0.0 ✔ stringr 1.5.1
## ✔ ggplot2 3.5.1 ✔ tibble 3.2.1
## ✔ lubridate 1.9.3 ✔ tidyr 1.3.1
## ✔ purrr 1.0.2
## ── 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(ggplot2)
library(readr)
library(survival)
这里是本报告的数据集
## Rows: 299 Columns: 13
## ── Column specification ────────────────────────────────────────────────────────
## Delimiter: ","
## dbl (13): age, anaemia, creatinine_phosphokinase, diabetes, ejection_fractio...
##
## ℹ 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.
## # A tibble: 6 × 13
## age anaemia creatinine_phosphokinase diabetes ejection_fraction
## <dbl> <dbl> <dbl> <dbl> <dbl>
## 1 75 0 582 0 20
## 2 55 0 7861 0 38
## 3 65 0 146 0 20
## 4 50 1 111 0 20
## 5 65 1 160 1 20
## 6 90 1 47 0 40
## # ℹ 8 more variables: high_blood_pressure <dbl>, platelets <dbl>,
## # serum_creatinine <dbl>, serum_sodium <dbl>, sex <dbl>, smoking <dbl>,
## # time <dbl>, DEATH_EVENT <dbl>
heart_failure_data %>%
mutate(age=ifelse(age<median(age),'L','H'),
creatinine_phosphokinase=ifelse(creatinine_phosphokinase<median(creatinine_phosphokinase),'L','H'),
ejection_fraction=ifelse(ejection_fraction<median(ejection_fraction),'L','H'),
platelets=ifelse(platelets<300000,'L','H'),
serum_creatinine=ifelse(serum_creatinine<median(serum_creatinine),'L','H'),
serum_sodium=ifelse(serum_sodium<median(serum_sodium),'L','H'),
anaemia=ifelse(anaemia==0,'N','Y'),
diabetes=ifelse(diabetes==0,'N','Y'),
high_blood_pressure=ifelse(high_blood_pressure==0,'N','Y'),
sex=ifelse(sex==0,'FM','M'),
smoking=ifelse(smoking==0,'N','Y'),
)->df1
head(df1)
## # A tibble: 6 × 13
## age anaemia creatinine_phosphokinase diabetes ejection_fraction
## <chr> <chr> <chr> <chr> <chr>
## 1 H N H N L
## 2 L N H N H
## 3 H N L N L
## 4 L Y L N L
## 5 H Y L Y L
## 6 H Y L N H
## # ℹ 8 more variables: high_blood_pressure <chr>, platelets <chr>,
## # serum_creatinine <chr>, serum_sodium <chr>, sex <chr>, smoking <chr>,
## # time <dbl>, DEATH_EVENT <dbl>
检查缺失值
## [1] 0
则该数据没有缺失值,无需处理缺失值
计算四分位数,进行截尾或温赛处理,不过本数据集没有异常值,跳过该步骤
因为该数据不需要对重复值进行处理,所以跳过该步骤
概览数据集
## age anaemia creatinine_phosphokinase
## Length:299 Length:299 Length:299
## Class :character Class :character Class :character
## Mode :character Mode :character Mode :character
##
##
##
## diabetes ejection_fraction high_blood_pressure platelets
## Length:299 Length:299 Length:299 Length:299
## Class :character Class :character Class :character Class :character
## Mode :character Mode :character Mode :character Mode :character
##
##
##
## serum_creatinine serum_sodium sex smoking
## Length:299 Length:299 Length:299 Length:299
## Class :character Class :character Class :character Class :character
## Mode :character Mode :character Mode :character Mode :character
##
##
##
## time DEATH_EVENT
## Min. : 4.0 Min. :0.0000
## 1st Qu.: 73.0 1st Qu.:0.0000
## Median :115.0 Median :0.0000
## Mean :130.3 Mean :0.3211
## 3rd Qu.:203.0 3rd Qu.:1.0000
## Max. :285.0 Max. :1.0000
年龄分布
年龄分布
年龄与死亡时间的关系
## Scale for fill is already present.
## Adding another scale for fill, which will replace the existing scale.
性别与死亡时间之间的关系
## Scale for fill is already present.
## Adding another scale for fill, which will replace the existing scale.
## Start: AIC=994.06
## Surv(time, , DEATH_EVENT == 1) ~ age + anaemia + creatinine_phosphokinase +
## diabetes + ejection_fraction + high_blood_pressure + platelets +
## serum_creatinine + serum_sodium + sex + smoking
##
## Df AIC
## - smoking 1 992.11
## - diabetes 1 992.25
## - creatinine_phosphokinase 1 992.27
## - sex 1 992.35
## - platelets 1 993.19
## <none> 994.06
## - age 1 995.59
## - ejection_fraction 1 996.61
## - anaemia 1 997.16
## - serum_sodium 1 997.86
## - high_blood_pressure 1 998.40
## - serum_creatinine 1 1005.82
##
## Step: AIC=992.11
## Surv(time, , DEATH_EVENT == 1) ~ age + anaemia + creatinine_phosphokinase +
## diabetes + ejection_fraction + high_blood_pressure + platelets +
## serum_creatinine + serum_sodium + sex
##
## Df AIC
## - creatinine_phosphokinase 1 990.31
## - diabetes 1 990.32
## - sex 1 990.35
## - platelets 1 991.28
## <none> 992.11
## - age 1 993.59
## - ejection_fraction 1 994.75
## - anaemia 1 995.16
## - serum_sodium 1 995.86
## - high_blood_pressure 1 996.47
## - serum_creatinine 1 1003.85
##
## Step: AIC=990.31
## Surv(time, , DEATH_EVENT == 1) ~ age + anaemia + diabetes + ejection_fraction +
## high_blood_pressure + platelets + serum_creatinine + serum_sodium +
## sex
##
## Df AIC
## - diabetes 1 988.49
## - sex 1 988.51
## - platelets 1 989.48
## <none> 990.31
## - age 1 991.80
## - ejection_fraction 1 993.13
## - anaemia 1 993.21
## - serum_sodium 1 993.97
## - high_blood_pressure 1 994.56
## - serum_creatinine 1 1001.92
##
## Step: AIC=988.49
## Surv(time, , DEATH_EVENT == 1) ~ age + anaemia + ejection_fraction +
## high_blood_pressure + platelets + serum_creatinine + serum_sodium +
## sex
##
## Df AIC
## - sex 1 986.65
## - platelets 1 987.64
## <none> 988.49
## - age 1 990.06
## - ejection_fraction 1 991.19
## - anaemia 1 991.30
## - serum_sodium 1 991.99
## - high_blood_pressure 1 992.76
## - serum_creatinine 1 1000.40
##
## Step: AIC=986.65
## Surv(time, , DEATH_EVENT == 1) ~ age + anaemia + ejection_fraction +
## high_blood_pressure + platelets + serum_creatinine + serum_sodium
##
## Df AIC
## - platelets 1 985.85
## <none> 986.65
## - age 1 988.21
## - ejection_fraction 1 989.24
## - anaemia 1 989.42
## - serum_sodium 1 990.13
## - high_blood_pressure 1 991.19
## - serum_creatinine 1 998.41
##
## Step: AIC=985.85
## Surv(time, , DEATH_EVENT == 1) ~ age + anaemia + ejection_fraction +
## high_blood_pressure + serum_creatinine + serum_sodium
##
## Df AIC
## <none> 985.85
## - age 1 987.18
## - ejection_fraction 1 988.41
## - serum_sodium 1 988.52
## - anaemia 1 989.01
## - high_blood_pressure 1 990.49
## - serum_creatinine 1 997.68
## Call:
## coxph(formula = Surv(time, , DEATH_EVENT == 1) ~ age + anaemia +
## ejection_fraction + high_blood_pressure + serum_creatinine +
## serum_sodium, data = df1)
##
## n= 299, number of events= 96
##
## coef exp(coef) se(coef) z Pr(>|z|)
## ageL -0.4011 0.6696 0.2241 -1.790 0.073530 .
## anaemiaY 0.4761 1.6098 0.2083 2.286 0.022281 *
## ejection_fractionL 0.4525 1.5722 0.2142 2.112 0.034699 *
## high_blood_pressureY 0.5576 1.7464 0.2118 2.632 0.008490 **
## serum_creatinineL -0.8708 0.4186 0.2461 -3.538 0.000402 ***
## serum_sodiumL 0.4630 1.5888 0.2169 2.135 0.032789 *
## ---
## Signif. codes: 0 '***' 0.001 '**' 0.01 '*' 0.05 '.' 0.1 ' ' 1
##
## exp(coef) exp(-coef) lower .95 upper .95
## ageL 0.6696 1.4935 0.4315 1.0389
## anaemiaY 1.6098 0.6212 1.0702 2.4214
## ejection_fractionL 1.5722 0.6361 1.0331 2.3926
## high_blood_pressureY 1.7464 0.5726 1.1530 2.6453
## serum_creatinineL 0.4186 2.3887 0.2584 0.6781
## serum_sodiumL 1.5888 0.6294 1.0386 2.4305
##
## Concordance= 0.679 (se = 0.027 )
## Likelihood ratio test= 44.56 on 6 df, p=6e-08
## Wald test = 40.78 on 6 df, p=3e-07
## Score (logrank) test = 43.11 on 6 df, p=1e-07
## randomForest 4.7-1.1
## Type rfNews() to see new features/changes/bug fixes.
##
## 载入程辑包:'randomForest'
## The following object is masked from 'package:dplyr':
##
## combine
## The following object is masked from 'package:ggplot2':
##
## margin
## Warning in randomForest.default(m, y, ...): The response has five or fewer
## unique values. Are you sure you want to do regression?
##
## Call:
## randomForest(formula = DEATH_EVENT ~ ., data = df1_train, importance = TRUE)
## Type of random forest: regression
## Number of trees: 500
## No. of variables tried at each split: 4
##
## Mean of squared residuals: 0.1110683
## % Var explained: 47.06
## %IncMSE IncNodePurity
## age 0.0028888361 1.4461537
## anaemia 0.0038550747 1.4050420
## creatinine_phosphokinase 0.0007782986 1.1250205
## diabetes -0.0016026172 0.8869293
## ejection_fraction 0.0020638840 1.6499406
## high_blood_pressure -0.0004704896 1.1372421
## 载入需要的程辑包:lattice
##
## 载入程辑包:'caret'
## The following object is masked from 'package:survival':
##
## cluster
## The following object is masked from 'package:purrr':
##
## lift
##
## train_predict 0 1
## 0 152 6
## 1 0 59
##
## test_predict 0 1
## 0 46 14
## 1 5 17