<jupyter_start><jupyter_text>Logitic regression on ICAN clinical data**Objective:** implement a logistic regression model with the simulated ICAN clinical data.  Library importation<jupyter_code>library(ggplot2)<jupyter_output><empty_output><jupyter_text>Data loading<jupyter_code>ican <- read.csv("simulated_ican_clinical_data.csv", sep = "#", encoding = "utf8")
dim(ican)
ican["adjusted_size_ratio"] <- as.factor(ifelse(is.na(ican$adjusted_size_ratio), NA, 
                                                ifelse(ican$adjusted_size_ratio < 3, "<3", 
                                                       ifelse(ican$adjusted_size_ratio >= 3, ">=3", "Other"))))
ican["age"] <- as.factor(ifelse(is.na(ican$age), NA, 
                                ifelse(ican$age < 50, "<50", 
                                       ifelse(ican$age < 60, "50-59", 
                                              ifelse(ican$age < 70, "60-69", 
                                                     ifelse(ican$age >= 70, ">=70", "Other"))))))
ican$IA_location <- factor(as.character(ican$IA_location), levels = c("ICA", "MCA", "ACA", "PCA"))
ican$tobacco <- factor(as.character(ican$tobacco), levels = c("Non-smoker", "Minor_smoker", "Regular_smoker"))
ican$adjusted_size_ratio <- factor(as.character(ican$adjusted_size_ratio), levels = c("<3", ">=3"))
ican$age <- factor(as.character(ican$age), levels = c("<50", "50-59", "60-69", ">=70"))<jupyter_output><empty_output><jupyter_text>Univariable Logistic Regression<jupyter_code>format_values <- function(x, dec) {
  return(formatC(x, format = 'f', digits = dec))
}

write_pvalue <- function(val) {
  ifelse(val < 0.001, return("<0.001"), return(paste0("=", val)))
}


res_glm_uni <- function(df, var_dependant, var_explanatory) {
    # Univariate
    univ_formulas <- sapply(var_explanatory, function(x){as.formula(paste(var_dependant, '~', x))})
    univ_models <- lapply(univ_formulas, function(x){glm(x, data = df, family="binomial")})
    univ_results <- lapply(univ_models,
                           function(x){
                               x <- summary(x)
                               Pvalue <- format_values(x$coefficient[-1,4], 3)
                               HR <- format_values(exp(x$coefficient[-1,1]), 2)
                               HR.confint.lower <- format_values(exp(x$coefficient[-1,1] - 1.96*x$coefficient[-1,2]), 2)
                               HR.confint.upper <- format_values(exp(x$coefficient[-1,1] + 1.96*x$coefficient[-1,2]), 2)
                               HR <- paste0(HR, " (", HR.confint.lower, "-", HR.confint.upper, ", P", write_pvalue(Pvalue),")")
                               if (length(Pvalue) != 1) {
                                   res <- cbind(HR)
                                   colnames(res)<-c("OR (univariate)")
                                   return(as.data.frame(res))
                               } else {
                                   res <- HR
                                   names(res)<-c("OR (univariate)")
                                   return(t(as.data.frame(res)))
                               }
                           })
    res = NULL
    for (i in 1:length(univ_results)) {
        if (length(res) == 0){
            ifelse(nrow(univ_results[[i]]) == 1, rownames(univ_results[[i]]) <- names(univ_results)[i],
                   rownames(univ_results[[i]]) <- paste0(c(paste0(names(univ_results)[i], " (", 
                                                                 levels(df[,names(univ_results)[i]])[1], ")          "), 
                                                          rep(" ", (length(levels(df[,names(univ_results)[i]]))-2))), 
                                                        levels(df[,names(univ_results)[i]])[-1]))
            res <- univ_results[[i]]
        } else {
            ifelse(nrow(univ_results[[i]]) == 1, rownames(univ_results[[i]]) <- names(univ_results)[i], 
                   rownames(univ_results[[i]]) <- paste0(c(paste0(names(univ_results)[i], " (", 
                                                                 levels(df[,names(univ_results)[i]])[1], ")          "), 
                                                          rep("", (length(levels(df[,names(univ_results)[i]]))-2))), 
                                                        levels(df[,names(univ_results)[i]])[-1]))
            res <- rbind(res, univ_results[[i]])
        }
    }
    res <- cbind(rownames(res), res)
    rownames(res) <- NULL
    colnames(res) <- c("Explanatory variables", "OR (univariate)")
    return(as.matrix(res))
}
res_glm_uni(ican, "rupture", colnames(ican[,-dim(ican)[2]]))<jupyter_output><empty_output><jupyter_text>Multivariables Logistic Regression<jupyter_code>fit <- glm(rupture ~ sex + age + tobacco + hta + IA_location + adjusted_size_ratio + multiple_IA + headaches + statin_ttt
                      + platelet_aggregation_inhibiting_ttt + hormone_therapy_ttt + allergy, 
           family = binomial, data = ican)
# Odds Ratio and confidence interval
Odds <- as.vector(round(exp(coef(fit)),2))
CILow <- as.vector(round(exp(confint(fit)),2)[,1])
CIHigh <- as.vector(round(exp(confint(fit)),2)[,2])
boxLabels = c("(Intercept) ", "Sex (M) ", "Age (<50)           50-59 ", "60-69 ", paste0("\U2265", "70 "), 
              "Tobacco (Non-smoker)           Minor smoker ", "Regular smoker ", 
              "HTA (Yes)" , "IA location (ICA)           MCA ", "ACA ", "PCA ", 
              paste0("Adjusted size ratio (\U2265", "3) "), "Multiple IA (Yes) ", "Headaches (Yes) ", 
              "Statin treatment (Yes) ", "Platelet aggregation inhibiting treatment (Yes) ", 
              "Hormone therapy treatment (Yes) ", "Allergy (Yes) ")
df <- data.frame(yAxis = length(boxLabels):1,
                 boxOdds = Odds,
                 boxCILow = CILow,
                 boxCIHigh = CIHigh)
options(repr.plot.width = 10, repr.plot.height = 10)

p <- ggplot(df, aes(x = boxOdds, y = reorder(boxLabels, yAxis)))

p + geom_vline(aes(xintercept = 1), size = 0.5, linetype = "dashed") +
  geom_errorbarh(aes(xmax = boxCIHigh, xmin = boxCILow), size = 0.6, 
                 height = 0.5, color = "gray20") +
  geom_point(size = 6, color = "orange") +
  theme_bw()  +
  scale_x_continuous(breaks = c(1, 2, 3, 4, 5, 6, 7, 8)) +
  coord_trans(x = "log10") +
  ylab("") +
  xlab("Odds ratio (95% CI)") +
  annotate(geom = "text", y =17.5, x = Odds[1], label = Odds[1], size = 5, hjust = 0, col = "gray20") +
  annotate(geom = "text", y =16.5, x = Odds[2], label = Odds[2], size = 5, hjust = 0, col = "gray20") +
  annotate(geom = "text", y =15.5, x = Odds[3], label = Odds[3], size = 5, hjust = 0, col = "gray20") +
  annotate(geom = "text", y =14.5, x = Odds[4], label = Odds[4], size = 5, hjust = 0, col = "gray20") +
  annotate(geom = "text", y =13.5, x = Odds[5], label = Odds[5], size = 5, hjust = 0, col = "gray20") +
  annotate(geom = "text", y =12.5, x = Odds[6], label = Odds[6], size = 5, hjust = 0, col = "gray20") +
  annotate(geom = "text", y =11.5, x = Odds[7], label = Odds[7], size = 5, hjust = 0, col = "gray20") +
  annotate(geom = "text", y =10.5, x = Odds[8], label = Odds[8], size = 5, hjust = 0, col = "gray20") +
  annotate(geom = "text", y =9.5, x = Odds[9], label = Odds[9], size = 5, hjust = 0, col = "gray20") +
  annotate(geom = "text", y =8.5, x = Odds[10], label = Odds[10], size = 5, hjust = 0, col = "gray20") +
  annotate(geom = "text", y =7.5, x = Odds[11], label = Odds[11], size = 5, hjust = 0, col = "gray20") +
  annotate(geom = "text", y =6.5, x = Odds[12], label = Odds[12], size = 5, hjust = 0, col = "gray20") +
  annotate(geom = "text", y =5.5, x = Odds[13], label = Odds[13], size = 5, hjust = 0, col = "gray20") +
  annotate(geom = "text", y =4.5, x = Odds[14], label = Odds[14], size = 5, hjust = 0, col = "gray20") +
  annotate(geom = "text", y =3.5, x = Odds[15], label = Odds[15], size = 5, hjust = 0, col = "gray20") +
  annotate(geom = "text", y =2.5, x = Odds[16], label = Odds[16], size = 5, hjust = 0, col = "gray20") +
  annotate(geom = "text", y =1.5, x = Odds[17], label = Odds[17], size = 5, hjust = 0, col = "gray20") +
  annotate(geom = "text", y =0.5, x = Odds[18], label = Odds[18], size = 5, hjust = 0, col = "gray20") +

  theme(plot.title = element_text(face = "bold", colour = "gray20", size = 16, hjust = 0.5),
        axis.title.x = element_text(colour = "gray30", size = 16, face = "bold"),
        axis.text.y = element_text(color = "gray30", size= 14, face = "bold"),
        axis.text.x = element_text(color = "gray20", size= 16))<jupyter_output><empty_output>
{'fraction_non_alphanumeric': 0.1798861920798978, 'fraction_numerical': 0.0384392056671699, 'mean_word_length': 2.2050614067733534, 'pattern_counts': {'":': 0, '<': 66, '<?xml version=': 0, '>': 23, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 33, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'chain_length': '4', 'dir': 'jupyter-structured-clean-dedup', 'id': '594070', 'license': 'MIT', 'n_tokens_mistral': 3296, 'n_tokens_neox': 2944, 'n_words': 611, 'path': 'notebooks/3-Logistic-Regression-Model.ipynb', 'repo_name': 'albangaignard/ICAN-ml-experiments', 'size': '8611'}