<jupyter_start><jupyter_text>Random forest on ICAN clinical data**Objective:** implement a random forest model with the simulated ICAN clinical data.  Library importation<jupyter_code>library(randomForest)
library(ggplot2)<jupyter_output>randomForest 4.6-14

Type rfNews() to see new features/changes/bug fixes.


Attaching package: 'ggplot2'


The following object is masked from 'package:randomForest':

    margin<jupyter_text>Data loading<jupyter_code>ican <- read.csv("simulated_ican_clinical_data.csv", sep = "#", encoding = "utf8")
dim(ican)
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["bmi"] <- as.factor(ifelse(is.na(ican$bmi), NA, 
                                ifelse(ican$bmi < 20, "<20", 
                                       ifelse(ican$bmi < 25, "20-25", 
                                              ifelse(ican$bmi < 30, "25-30", 
                                                     ifelse(ican$bmi >= 30, ">=30", "Other"))))))
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$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$age <- factor(as.character(ican$age), levels = c("<50", "50-59", "60-69", ">=70"))
ican$bmi <- factor(as.character(ican$bmi), levels = c("<20", "20-25", "25-30", ">=30"))
ican$adjusted_size_ratio <- factor(as.character(ican$adjusted_size_ratio), levels = c("<3", ">=3"))<jupyter_output><empty_output><jupyter_text>Random Forest<jupyter_code>fit1 <- randomForest(rupture ~ ., data = ican, importance = TRUE)
print(fit1)
df = as.data.frame(fit1$importance)
df["index"] = c("Sex ", "Age ", "Body mass index ", "Sporadic case ", "Multiple IA ", "IA location ", 
                "Adjusted size ratio ", "Tobacco ", "Alcohol ", "Hypertension ", "Headaches ", "Diabetes ", "Dyslipidemia ",
                "Ischemic stroke history ", "Ischemic heart disease history ", "PAD history ", 
                "Carotid artery stenosis history ", "Aortic aneurysm history ", "Statin treatment ", 
                "Platelet aggregation inhibting treatment ", "VKA or anticoagulant treatment ", 
                "Anti-inflammatory treatment ", "Hormone therapy treatment ", "Allergy ", "Asthma ", "Atopy ", "Eczema ")
df["importance"] = importance(fit1, scale = TRUE, type = 1)
options(repr.plot.width = 10, repr.plot.height = 12)
ggplot(df, aes(x = reorder(index, importance), y = importance, fill = importance))+ 
    geom_bar(stat="identity", position="dodge")+ coord_flip()+
    ylab("Variable Importance\n(Mean Decrease Accuracy)")+
    xlab("")+
    theme(
    axis.title.x = element_text(color="black", size=16, face="bold"),
    axis.text.x = element_text(color="black", size=14, face = "bold"),
    axis.text.y = element_text(color="black", size=14)
    )+
    guides(fill=F)+
    scale_fill_gradient(low="lightblue", high="darkblue")<jupyter_output><empty_output>
{'fraction_non_alphanumeric': 0.1603960396039604, 'fraction_numerical': 0.026025459688826025, 'mean_word_length': 2.753715498938429, 'pattern_counts': {'":': 0, '<': 35, '<?xml version=': 0, '>': 21, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 14, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'chain_length': '3', 'dir': 'jupyter-structured-clean-dedup', 'id': '309145', 'license': 'MIT', 'n_tokens_mistral': 1210, 'n_tokens_neox': 1070, 'n_words': 226, 'path': 'notebooks/4-Random-Forest-Model.ipynb', 'repo_name': 'albangaignard/ICAN-ml-experiments', 'size': '3535'}