-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathA&E template.R
140 lines (94 loc) · 3.13 KB
/
A&E template.R
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
#Code to explore A&E data and predict whether patients have been admitted.
# Install relevant packages
#install.packages('caret','tidyverse','caTools')
#install.packages("ggcorrplot")
# Load required packages
library(tidyverse)
library(caret)
library(caTools)
library(dplyr)
#A. Import all relevant files & read into dataframes
#download.file(url = "",destfile = "*.csv")
file_path <- "bip-ae-technical-challenge\\training_set.csv"
train_data <- read.csv(file_path)
test_path <- "bip-ae-technical-challenge\\test_set.csv"
test_data <- read.csv(test_path)
#Task 1. Explore the datasets
# Checking dimensions
dim(train_data)
dim(test_data)
# Summary statistics
str(train_data)
summary(train_data)
# Visualize variable distributions
# Histogram for numeric variables
num_col = train_data %>% select(where(is.numeric))
str(num_col)
for (i in 1:ncol(num_col)) {
hist(num_col[[i]],
col = "blue", ylim = c(0, 80000))
}
# Bar plots for categorical variables
var_col = train_data %>% select(!where(is.numeric))
str(var_col)
#1. Patient Admittance
table(train_data$Admitted_Flag)
ggplot(train_data, aes(x = Admitted_Flag)) +
geom_histogram(bins=30, fill = "magenta")+
labs(title = "Distribution of Patient Admittance",
x = "Admitted_Flag",
y = "Number of patients")
## Correlation between variables
# Correlation matrix for numeric columns
num_col = train_data %>% select(where(is.numeric))
summary(num_col)
# Display the correlation matrix as a heatmap
#Task 2. Create a validation dataset
# setting seed to generate a reproducible random sampling
set.seed(100)
# dividing the dataset into an 80:20 ratio
spl = createDataPartition(train_data$Admitted_Flag, p=0.8, list=FALSE)
# selecting part of dataset which belongs to the 80%
train = train_data[spl,]
# selecting part of dataset which belongs to the 20%
test = train_data[-spl,]
# Store X and Y for later use.
x = train %>% select(-Admitted_Flag)
y = train$Admitted_Flag
str(x)
str(y)
# overview of training data
skimmed <- skim(train)
skimmed
#B. Preprocess the training data
# Count missing values in the training set
sum(is.na(train))
# Remove columns with excessive number of NA values
thresh = 0.5 # Define threshold for NA values
#Identify columns in training set with more than 50% NA
colnames(train[which(colMeans(is.na(train)) > thresh)])
# drop columns with more than 50% NA
train <- train[, which(colMeans(!is.na(train)) > thresh)]
# data imputation for missing values
# create One-Hot Encoding(dummy variables)
# Normalize data formats
#C. Reanalyse the new training set
# Summary Statistics
#str(train)
# Missing values analysis
# Check missing values after data imputation
sum(is.na(train))
colSums(is.na(train))
# Visualize importance of variables using featurePlot()
# Feature selection using recursive feature elimination(rfe)
#E. Evaluate ML algorithms
#1. Build models
#2. Select best model
#3. Compare accuracy using visualizations
#4. Summarise best model
#5. rank features by importance
#F. Make predictions using test data and the most accurate model.
#G. Create ensemble predictions for improved accuracy (Stretch goal)
# - Bagging
# - Boosting
# - Stacking