clinical_heart.failure <- read.csv(“~/MSCR/MSCR 598 - B2K/clinical_heart-failure.csv”) View(clinical_heart.failure) heart_failure <- clinical_heart.failure view(heart_failure)

heart_failure <- heart_failure %>% mutate(death = factor(death, levels = c(0, 1), labels = c(“alive”, “dead”))) view(heart_failure) ## Linear regression fit_lm <- lm(serum_creatinine ~ age + diabetes + high_blood_pressure + serum_sodium, data = heart_failure) summary(fit_lm)

Age, and serum_sodium are associated with serum_creatinine (p = 0.01, p=0.001, respectively), but only higher age is associated with increased serum creatinine. Higher serum sodium is associated with lower creatinine.

c <- 0 point <- tribble(~age, ~anaemia, ~creatinine_phosphokinase, ~diabetes, ~ejection_fraction, ~high_blood_pressure, ~platelets, ~serum_sodium, ~sex, ~smoking, ~death, 70, c, c, 1, c, 1, c, 130, c, c, c) point predict(fit_lm, point)

Logistic regression

fit_glm <- glm(death ~ age + anaemia + creatinine_phosphokinase + diabetes + ejection_fraction + high_blood_pressure + platelets + serum_sodium + sex, data = heart_failure, family = “binomial”) summary(fit_glm)

Now splitting the data

set.seed(404)

library(rsample) heart_failure <- heart_failure %>% mutate(row = row_number()) heart_failure heart_failure_split <- initial_split(heart_failure, prob = 0.75, strata = death)

HF_train <- training(heart_failure_split) HF_test <- testing(heart_failure_split)

Checking to see if the split worked

HF_train %>% arrange(row) HF_test %>% arrange(row)

view(HF_train)

Decision Trees

library(rpart) HF_train <- HF_train %>% select(-row) HF_test <- HF_test %>% select(-row) fit_tree <- rpart(formula = death ~ ., data = HF_train)

library(rpart.plot)

prp(fit_tree, extra = 2)

fit_tree2 <- rpart(death ~ ., data = HF_train, minsplit = 50) prp(fit_tree2, extra = 2)

ROC AUC of the model on the training set - NOT WORKING

predict(fit_tree, HF_train) %>% as_tibble() %>% select(dead) %>% bind_cols(HF_train) %>% roc_auc(truth = death, estimate = dead, event_level = “second”)

predict(fit_tree2, HF_train) %>% as_tibble() %>% select(dead) %>% bind_cols(HF_train) %>% roc_auc(truth = death, estimate = dead, event_level = “second”)

predict(fit_tree, HF_test) %>% as_tibble() %>% select(dead) %>% bind_cols(HF_test) %>% roc_auc(truth = death, estimate = dead, event_level = “second”)

predict(fit_tree2, HF_test) %>% as_tibble() %>% select(dead) %>% bind_cols(HF_test) %>% roc_auc(truth = death, estimate = dead, event_level = “second”)

Using LASSO to perform variable selection

library(glmnet)

x_train <- HF_train %>% select(-death) %>% as.matrix()

y_train <- HF_train %>% mutate(death = as.numeric(death) - 1) %>% pull(death)

fit_lasso <- glmnet(x_train, y_train, alpha = 1, family = “binomial”)

plot(fit_lasso, xvar = “lambda”, label = TRUE)

coef(fit_lasso, s = exp(-3))