################################################################################
######## NAME: NIVEDITHA KM, AREZOU,STACEY #################################
######## : SEJAL, AISHWARYA, SANA, SABRINA #################################
######## ASSIGNMENT 4 - DATA MINING#############################################
################################################################################
library(tidyverse) # data manipulation
library(ggpubr)
#install.packages("tidymodels")
#install.packages("glmnet")
library(readr)
library(tidymodels)
library(glmnet)
library(caret)
#install.packages("caret")
################################################################################
#DATA SETS IMPORT
#DATA PRE-PROCESSING STARTS
################################################################################
#GET THE DATASETS IMPORTED
test_df <- read.csv("test.csv")
train_df <- read.csv("train.csv")
gender_df <- read.csv("gender_submission.csv")
tst_df <- test_df
trn_df <- train_df
#DATA PREPROCESSING/CLEAN UP BY REPLACING MEAN VALUES IN PLACE OF NA VALUES IN AGE FIELD
trn_df <- trn_df %>% mutate(across(Age, ~replace_na(., mean(., na.rm=TRUE))))
tst_df <- tst_df %>% mutate(across(Age, ~replace_na(., mean(., na.rm=TRUE))))
#summary(trn_df)
#summary(tst_df)
#view(trn_df)
#FOLLOWING PIECE OF CODE IS TO REMOVE COMMAS FROM COLUMNS IF ANY(Not necessary for this though)
tst_df <- data.frame(lapply(tst_df, function(x) {
gsub(",", "", x)
}))
trn_df <- data.frame(lapply(trn_df, function(x) {
gsub(",", "", x)
}))
#################################################################################
#FOLLOWING PIECE OF CODE IS TO CHECK IF NUMERIC CONVERSION CAN HAPPEN ON COLUMN
#THIS IS DONE ON BOTH TEST AND TRAINING DATASET
#USUALLY FULL DATASET IS BROKEN INTO TEST AND TRAINING. IN THIS CASE THESE WERE
#PROVIDED SEPARATELY WHICH WAS HELPFUL.
#################################################################################
is_all_numeric <- function(x) {
!any(is.na(suppressWarnings(as.numeric(na.omit(x))))) & is.character(x)
}
tst_df %>%
mutate_if(is_all_numeric,as.numeric) %>%
str()
## 'data.frame': 418 obs. of 11 variables:
## $ PassengerId: num 892 893 894 895 896 897 898 899 900 901 ...
## $ Pclass : num 3 3 2 3 3 3 3 2 3 3 ...
## $ Name : chr "Kelly Mr. James" "Wilkes Mrs. James (Ellen Needs)" "Myles Mr. Thomas Francis" "Wirz Mr. Albert" ...
## $ Sex : chr "male" "female" "male" "male" ...
## $ Age : num 34.5 47 62 27 22 14 30 26 18 21 ...
## $ SibSp : num 0 1 0 0 1 0 0 1 0 2 ...
## $ Parch : num 0 0 0 0 1 0 0 1 0 0 ...
## $ Ticket : chr "330911" "363272" "240276" "315154" ...
## $ Fare : num 7.83 7 9.69 8.66 12.29 ...
## $ Cabin : chr "" "" "" "" ...
## $ Embarked : chr "Q" "S" "Q" "S" ...
trn_df %>%
mutate_if(is_all_numeric,as.numeric) %>%
str()
## 'data.frame': 891 obs. of 12 variables:
## $ PassengerId: num 1 2 3 4 5 6 7 8 9 10 ...
## $ Survived : num 0 1 1 1 0 0 0 0 1 1 ...
## $ Pclass : num 3 1 3 1 3 3 1 3 3 2 ...
## $ Name : chr "Braund Mr. Owen Harris" "Cumings Mrs. John Bradley (Florence Briggs Thayer)" "Heikkinen Miss. Laina" "Futrelle Mrs. Jacques Heath (Lily May Peel)" ...
## $ Sex : chr "male" "female" "female" "female" ...
## $ Age : num 22 38 26 35 35 ...
## $ SibSp : num 1 1 0 1 0 0 0 3 0 1 ...
## $ Parch : num 0 0 0 0 0 0 0 1 2 0 ...
## $ Ticket : chr "A/5 21171" "PC 17599" "STON/O2. 3101282" "113803" ...
## $ Fare : num 7.25 71.28 7.92 53.1 8.05 ...
## $ Cabin : chr "" "C85" "" "C123" ...
## $ Embarked : chr "S" "C" "S" "S" ...
gender_df %>%
mutate_if(is_all_numeric,as.numeric) %>%
str()
## 'data.frame': 418 obs. of 2 variables:
## $ PassengerId: int 892 893 894 895 896 897 898 899 900 901 ...
## $ Survived : int 0 1 0 0 1 0 1 0 1 0 ...
# CONVERTS CHARACTER COLUMNS TO NUMERIC COLUMNS
tst_df <- tst_df %>% mutate_if(is_all_numeric,as.numeric)
trn_df <- trn_df %>% mutate_if(is_all_numeric,as.numeric)
gender_df <- gender_df %>% mutate_if(is_all_numeric,as.numeric)
###############################################################################
# COLUMNS WERE MODIFIED TO NUMBERS, SO THAT IT CAN BE USED IN LOGISTIC
# REGRESSION
###############################################################################
# Print the pivot table Here We can find that two columns are Blanks
pivot_table <- table(trn_df$Embarked)
#print('Before picture')
#print(pivot_table)
# Fill blank columns with character values FOR EMBARKED
trn_df$Embarked <- ifelse(trn_df$Embarked == "", "S", trn_df$Embarked)
pivot_table <- table(trn_df$Embarked)
#print('After Picture')
#print(pivot_table)
#CREATE GENDER COLUMN WITH NUMERIC VALUES BASED ON SEX COLUMN
trn_df$Gender <- with(trn_df, ifelse(trn_df$Sex == 'male', 1 , 0))
tst_df$Gender <- with(tst_df, ifelse(tst_df$Sex == 'male', 1 , 0))
#CREATE CABINORNOT COLUMN AS NUMERIC COLUMN BASED ON CABIN COLUMN VALUES
trn_df$Cabinornot <- with(trn_df, ifelse(trn_df$Cabin == '', 0 , 1))
tst_df$Cabinornot <- with(tst_df, ifelse(tst_df$Cabin == '', 0 , 1))
#CREATE EMBARKEDNUM COLUMN AS NUMERIC BASED ON EMBARKED COLUMN WHICH HAS LOCATION VALUES AS C - Cherbourg, S - Southampton, Q - Queenstown
trn_df$EmbarkedNum <- with(trn_df, ifelse(trn_df$Embarked == 'C',1,
ifelse(trn_df$Embarked == 'Q',2,3)))
tst_df$EmbarkedNum <- with(tst_df, ifelse(tst_df$Embarked == 'C',1,
ifelse(tst_df$Embarked == 'Q',2,3)))
#Create bins on Fare column on a dataset so that it can be used in Logistic Regression
trn_df$Farebins <- cut(trn_df$Fare, breaks = c(-1, 7.9104, 14.4542, 31, 512.330), labels = FALSE)
trn_df$labels <- factor(trn_df$Farebins, labels = c("low", "medium-low", "medium", "high"))
tst_df$Farebins <- cut(tst_df$Fare, breaks = c(-1, 7.9104, 14.4542, 31, 512.330), labels = FALSE)
#Survived Columns were made as factors so that it can used in Logistic Regression
trn_df$Survived_f <- as.factor(trn_df$Survived)
gender_df$Survived_f <- as.factor(gender_df$Survived)
#New test data set was created with test dataframe with columns which were
#converted as numeric columns
new_tst_df <- subset(tst_df, select = c(Pclass, Gender,Age, SibSp,Parch,Cabinornot,EmbarkedNum,Farebins))
################################################################################
#The Gender data frame was bound to new dataframe created so that it
# can used for model evaluation later
###############################################################################
n_tst_df <- cbind(new_tst_df,gender_df)
#print('TEST DATASET SUMMARY')
#summary(test_df)
#print('TRAINING DATASET SUMMARY')
#summary(trn_df)
################################################################################
#DATA PRE-PROCESSING ENDS
################################################################################
view(trn_df)
view(tst_df)
view(n_tst_df)
################################################################################
# UNIVARIATE ANALYSIS OF THE VARIOUS DATA ATTRIBUTES #
################################################################################
#UNIVARIATE/SINGLE VARIABLE ANALYSIS
library(ggplot2)
count_Pclass <- as.data.frame(table(trn_df$Pclass))
#(count_Pclass)
colnames(count_Pclass)[colnames(count_Pclass) == "Var1"] <- "Pclass"
colnames(count_Pclass)[colnames(count_Pclass) == "Freq"] <- "Count_Of_Pclass"
#.1 PLOT OF PCLASS COUNT
ggplot(count_Pclass, aes(x = Pclass, y = Count_Of_Pclass, fill = Pclass)) +
geom_bar(stat = "identity") +
geom_text(aes(label = Count_Of_Pclass), vjust = -0.5, color = "black", size = 3) +
labs(title = "Count of PCLASS", x = "Category", y = "Count") +
scale_fill_manual(values = c("1" = "red", "2" = "blue", "3" = "green")) +
theme_minimal()
count_Survived <- as.data.frame(table(trn_df$Survived))
#(count_Pclass)
colnames(count_Survived)[colnames(count_Survived) == "Var1"] <- "Survived"
colnames(count_Survived)[colnames(count_Survived) == "Freq"] <- "Count_Of_Survived"
#.2 PLOT OF SURVIVED COUNT
ggplot(count_Survived, aes(x = Survived, y = Count_Of_Survived, fill = Survived)) +
geom_bar(stat = "identity") +
geom_text(aes(label = Count_Of_Survived), vjust = -0.5, color = "black", size = 3) +
labs(title = "Count of Survived", x = "Survived", y = "Count") +
scale_fill_manual(values = c("1" = "red", "2" = "blue", "3" = "green")) +
theme_minimal()
#.3 PLOT OF AGE HISTOGRAM
ggplot(data=trn_df, aes(x=Age,)) +
geom_histogram(binwidth = 5) +
xlab("Age")
#.4 PLOT OF FARE HISTOGRAM
ggplot(data=trn_df, aes(x=Fare,)) +
geom_histogram(binwidth = 15) +
xlab("Fare")
#BIVARIATE/TWO VARIABLE ANALYSIS
#.5 PLOT OF AGE AND SURVIVED. SHOWS THAT SURVIVAL WAS MORE AROUND AGE OF 30 AND DEATH IS MORE AROUND THE SAME AGE GROUP
ggplot(trn_df, aes(x = Age, color = factor(Survived))) +
geom_freqpoly(binwidth = 5) + # Adjust binwidth as needed
labs(title = "Frequency Polygon of Age with Color by Survival",
x = "Age", y = "Frequency", color = "Survived") +
scale_color_manual(values = c("0" = "red", "1" = "blue")) + # Customizing colors
theme_minimal()
count_data <- trn_df %>%
group_by(Sex, Survived) %>%
summarise(count = n())
## `summarise()` has grouped output by 'Sex'. You can override using the `.groups`
## argument.
#count_data
#.6 PLOT OF SEX VS SURVIVED COUNT. SHOWS THAT THE FEMALES SURVIVED MORE THAN MEN
# Create a bar plot with color
p <- ggplot(count_data, aes(x = Sex, y = count, fill = factor(Survived))) +
geom_bar(stat = "identity", position = "dodge") + # Display bars side by side
# geom_text(aes(label = count), vjust = 0.5, color = "black", size = 3) +
labs(title = "Count of Individuals by Survival and Sex",
x = "Sex", y = "Count", fill = "Survived") +
scale_fill_manual(values = c("red", "green")) + # Customizing colors
theme_minimal()
p + geom_text(aes(label = count, group = Survived),
position = position_dodge(width = 0.9), # Adjust position
vjust = -0.5, size = 3)
count_data <- trn_df %>%
group_by(Pclass, Survived) %>%
summarise(count = n())
## `summarise()` has grouped output by 'Pclass'. You can override using the
## `.groups` argument.
#count_data
#.7 PLOT OF PCLASS VS SURVIVED COUNT. CLASS 1 HAD MORE SURVIVAL
# Create a bar plot with color
p <- ggplot(count_data, aes(x = Pclass, y = count, fill = factor(Survived))) +
geom_bar(stat = "identity", position = "dodge") + # Display bars side by side
# geom_text(aes(label = count), vjust = -1, color = "black", size = 3) +
labs(title = "Count of Individuals by Survival and Pclass",
x = "Pclass", y = "Count", fill = "Survived") +
scale_fill_manual(values = c("red", "green")) + # Customizing colors
theme_minimal()
p + geom_text(aes(label = count, group = Survived),
position = position_dodge(width = 0.9), # Adjust position
vjust = -0.5, size = 3)
#Age and Sex
#.8 PLOT SHOWS AGE AND GENDER GRAPH. SHOWS MOST OF THE MALE AND FEMALE WERE IN AGE RANGE OF 30
ggplot(trn_df, aes(x = Age, color = factor(Sex))) +
geom_freqpoly(binwidth = 2.5) + # Adjust binwidth as needed
labs(title = "Frequency Polygon of Age with Color by Sex",
x = "Age", y = "Frequency", color = "Sex") +
scale_color_manual(values = c("male" = "blue", "female" = "pink")) + # Customizing colors
theme_minimal()
#Age and Pclass
#.8 PLOT SHOWS AGE AND PCLASS GRAPH. SHOWS MOST OF THE MALE AND FEMALE WERE IN AGE RANGE OF 30 IN PCLASS OF 3
ggplot(trn_df, aes(x = Age, color = factor(Pclass))) +
geom_freqpoly(binwidth = 2.5) + # Adjust binwidth as needed
labs(title = "Frequency Polygon of Age with Color by Pclass",
x = "Age", y = "Frequency", color = "Pclass") +
scale_color_manual(values = c("1" = "blue", "2" = "violet", "3"="green")) + # Customizing colors
theme_minimal()
# .9 PLOT OF SELECTED COLUMNS SHOW THAT ONLY AGE AND FARE GENERATE PLOTS DUE TO NUMBERS
chosen <- c("Survived", "Pclass", "Sex","Age","Fare")
plot(trn_df[,colnames(trn_df) %in% chosen])
#############################################################
# .10 PLOT OF AGEGROUPS ABOARD TITANIC
#############################################################
# Define age groups
age_groups <- cut(trn_df$Age, breaks = c(0, 10, 20, 30, 40, 50, 60, 70, 80),
labels = c("0-10", "11-20", "21-30", "31-40", "41-50", "51-60", "61-70", "71-80"))
# Create a color palette for age groups
age_colors <- c("#1f77b4", "#ff7f0e", "#2ca02c", "#d62728", "#9467bd", "#8c564b", "#e377c2", "#7f7f7f")
# Convert age_groups to a factor
age_groups <- factor(age_groups)
# Create a dataframe with age groups
age_data <- data.frame(age_groups)
# Plot the distribution of age groups
ggplot(age_data, aes(x = age_groups,fill = age_groups)) +
#geom_bar(fill = "skyblue", color = "black") +
geom_bar() +
scale_fill_manual(values = age_colors) +
labs(title = "Distribution of Age Groups for Titanic Passengers",
x = "Age Groups", y = "Frequency") +
theme_minimal() +
scale_fill_brewer(palette = "Set1") +
theme(axis.text.x = element_text(angle = 45, hjust = 1)) # Rotate x-axis labels for better readability
## Scale for fill is already present.
## Adding another scale for fill, which will replace the existing scale.
##################################################################################
# .11 Plot the number of siblings/spouses onboard Titanic with or without their
# stacked by passenger class. THE PERSON WITH NO SIBLING OR SPOUSE SURVIVED A LOT
##################################################################################
ggplot(trn_df, aes(x = factor(SibSp), fill = factor(Survived))) +
geom_bar(position = "stack") +
labs(title = "Number of Siblings/Spouses Onboard Titanic by Survival Status",
x = "Number of Siblings/Spouses", y = "Count", fill = "Survival Status") +
scale_fill_manual(values = c("blue", "purple"),
breaks = c("0", "1"),
labels = c("Not Survived", "Survived")) + # Customizing colors and labels
theme_minimal()
##################################################################################
# .12 PLOT OF GENDER AND SURIVAL RATE
##################################################################################
ggplot(trn_df, aes(x = factor(Sex), fill = factor(Survived))) +
geom_bar(position = "stack") +
labs(title = "Number of male/female Onboard Titanic by Survival Status",
x = "Number of male/female", y = "Count", fill = "Survival Status") +
scale_fill_manual(values = c("blue", "purple"),
breaks = c("0", "1"),
labels = c("Not Survived", "Survived")) + # Customizing colors and labels
theme_minimal()
#################################################################################
# CORRELATION #
#################################################################################
#view(trn_df)
#Correlation confirms how close two variables are related. We can see the values are close to 1 hence both x and y of Training and Test dataset are closely related.
cor(trn_df$Pclass, trn_df$Survived)
## [1] -0.338481
cor(trn_df$Gender, trn_df$Survived)
## [1] -0.5433514
cor(trn_df$Age, trn_df$Survived)
## [1] -0.06980852
cor(trn_df$SibSp, trn_df$Survived)
## [1] -0.0353225
cor(trn_df$Parch, trn_df$Survived)
## [1] 0.08162941
cor(trn_df$Fare, trn_df$Survived)
## [1] 0.2573065
cor(trn_df$Cabinornot, trn_df$Survived)
## [1] 0.3169115
cor(trn_df$EmbarkedNum, trn_df$Survived)
## [1] -0.1676753
# Train a logistic regression model
model1 <- logistic_reg(mixture = double(1), penalty = double(1)) %>%
set_engine("glmnet") %>%
set_mode("classification") %>%
#fit(Survived_f ~ Pclass + Gender + Age + SibSp + Parch + Cabinornot + EmbarkedNum + Farebins, data = trn_df)
fit(Survived_f ~ Age + SibSp + Parch + Cabinornot + EmbarkedNum + Farebins, data = trn_df)
model2 <- logistic_reg(mixture = double(1), penalty = double(1)) %>%
set_engine("glmnet") %>%
set_mode("classification") %>%
#fit(Survived_f ~ Pclass + Gender + Age + SibSp + Parch + Cabinornot + EmbarkedNum + Farebins, data = trn_df)
fit(Survived_f ~ Gender+Pclass+Age + SibSp + Parch + Cabinornot + EmbarkedNum + Farebins, data = trn_df)
model3 <- logistic_reg(mixture = double(1), penalty = double(1)) %>%
set_engine("glmnet") %>%
set_mode("classification") %>%
#fit(Survived_f ~ Pclass + Gender + Age + SibSp + Parch + Cabinornot + EmbarkedNum + Farebins, data = trn_df)
fit(Survived_f ~ Gender+Pclass, data = trn_df)
# Model summary
tidy(model1)
tidy(model2)
tidy(model3)
######################################################################
#TRAINED MODEL ON TEST DATASET
######################################################################
#MODEL1 PREDICTING TRAIN DATA SET
pred_class1_trn <- predict(model1,
new_data = trn_df,
type = "class")
# Class Probabilities
pred_proba1_trn <- predict(model1,
new_data = trn_df,
type = "prob")
trn_results1 <- trn_df %>%
select(Survived_f) %>%
bind_cols(pred_class1_trn, pred_proba1_trn)
#accuracy(trn_results1, truth = Survived_f, estimate = .pred_class)
print("MODEL1 ACCURACY PRECISION RECALL ETC ON TRAIN DATASET")
## [1] "MODEL1 ACCURACY PRECISION RECALL ETC ON TRAIN DATASET"
confusionMatrix(data = trn_results1$.pred_class, reference = trn_results1$Survived_f, mode = "prec_recall")
## Confusion Matrix and Statistics
##
## Reference
## Prediction 0 1
## 0 471 181
## 1 78 161
##
## Accuracy : 0.7093
## 95% CI : (0.6783, 0.739)
## No Information Rate : 0.6162
## P-Value [Acc > NIR] : 3.546e-09
##
## Kappa : 0.3485
##
## Mcnemar's Test P-Value : 2.328e-10
##
## Precision : 0.7224
## Recall : 0.8579
## F1 : 0.7843
## Prevalence : 0.6162
## Detection Rate : 0.5286
## Detection Prevalence : 0.7318
## Balanced Accuracy : 0.6643
##
## 'Positive' Class : 0
##
#MODEL1 PREDICTING TEST DATA SET
pred_class1_tst <- predict(model1,
new_data = n_tst_df,
type = "class")
# Class Probabilities
pred_proba1_tst <- predict(model1,
new_data = n_tst_df,
type = "prob")
test_results1 <- n_tst_df %>%
select(Survived_f) %>%
bind_cols(pred_class1_tst, pred_proba1_tst)
#accuracy(test_results1, truth = Survived_f, estimate = .pred_class)
print("MODEL1 ACCURACY PRECISION RECALL ETC ON TEST DATASET")
## [1] "MODEL1 ACCURACY PRECISION RECALL ETC ON TEST DATASET"
confusionMatrix(data = test_results1$.pred_class, reference = test_results1$Survived_f, mode = "prec_recall")
## Confusion Matrix and Statistics
##
## Reference
## Prediction 0 1
## 0 206 97
## 1 59 55
##
## Accuracy : 0.6259
## 95% CI : (0.5775, 0.6725)
## No Information Rate : 0.6355
## P-Value [Acc > NIR] : 0.677753
##
## Kappa : 0.147
##
## Mcnemar's Test P-Value : 0.003053
##
## Precision : 0.6799
## Recall : 0.7774
## F1 : 0.7254
## Prevalence : 0.6355
## Detection Rate : 0.4940
## Detection Prevalence : 0.7266
## Balanced Accuracy : 0.5696
##
## 'Positive' Class : 0
##
#MODEL 2
#MODEL2 PREDICTING TRAIN DATA SET
pred_class2_trn <- predict(model2,
new_data = trn_df,
type = "class")
# Class Probabilities
pred_proba2_trn <- predict(model2,
new_data = trn_df,
type = "prob")
trn_results2 <- trn_df %>%
select(Survived_f) %>%
bind_cols(pred_class2_trn, pred_proba2_trn)
#accuracy(trn_results2, truth = Survived_f, estimate = .pred_class)
print("MODEL2 ACCURACY PRECISION RECALL ETC ON TRAIN DATASET")
## [1] "MODEL2 ACCURACY PRECISION RECALL ETC ON TRAIN DATASET"
confusionMatrix(data = trn_results2$.pred_class, reference = trn_results2$Survived_f, mode = "prec_recall")
## Confusion Matrix and Statistics
##
## Reference
## Prediction 0 1
## 0 471 96
## 1 78 246
##
## Accuracy : 0.8047
## 95% CI : (0.7771, 0.8303)
## No Information Rate : 0.6162
## P-Value [Acc > NIR] : <2e-16
##
## Kappa : 0.583
##
## Mcnemar's Test P-Value : 0.1975
##
## Precision : 0.8307
## Recall : 0.8579
## F1 : 0.8441
## Prevalence : 0.6162
## Detection Rate : 0.5286
## Detection Prevalence : 0.6364
## Balanced Accuracy : 0.7886
##
## 'Positive' Class : 0
##
#MODEL2 PREDICTING TEST DATA SET
pred_class2_tst <- predict(model2,
new_data = n_tst_df,
type = "class")
# Class Probabilities
pred_proba2_tst <- predict(model2,
new_data = n_tst_df,
type = "prob")
test_results2 <- n_tst_df %>%
select(Survived_f) %>%
bind_cols(pred_class2_tst, pred_proba2_tst)
#accuracy(test_results2, truth = Survived_f, estimate = .pred_class)
print("MODEL2 ACCURACY PRECISION RECALL ETC ON TEST DATASET")
## [1] "MODEL2 ACCURACY PRECISION RECALL ETC ON TEST DATASET"
confusionMatrix(data = test_results2$.pred_class, reference = test_results2$Survived_f, mode = "prec_recall")
## Confusion Matrix and Statistics
##
## Reference
## Prediction 0 1
## 0 247 10
## 1 18 142
##
## Accuracy : 0.9329
## 95% CI : (0.9044, 0.9549)
## No Information Rate : 0.6355
## P-Value [Acc > NIR] : <2e-16
##
## Kappa : 0.8567
##
## Mcnemar's Test P-Value : 0.1859
##
## Precision : 0.9611
## Recall : 0.9321
## F1 : 0.9464
## Prevalence : 0.6355
## Detection Rate : 0.5923
## Detection Prevalence : 0.6163
## Balanced Accuracy : 0.9331
##
## 'Positive' Class : 0
##
#MODEL 3
#MODEL3 PREDICTING TRAIN DATA SET
pred_class3_trn <- predict(model3,
new_data = trn_df,
type = "class")
# Class Probabilities
pred_proba3_trn <- predict(model3,
new_data = trn_df,
type = "prob")
trn_results3 <- trn_df %>%
select(Survived_f) %>%
bind_cols(pred_class3_trn, pred_proba3_trn)
#accuracy(trn_results3, truth = Survived_f, estimate = .pred_class)
print("MODEL3 ACCURACY PRECISION RECALL ETC ON TRAIN DATASET")
## [1] "MODEL3 ACCURACY PRECISION RECALL ETC ON TRAIN DATASET"
confusionMatrix(data = trn_results3$.pred_class, reference = trn_results3$Survived_f, mode = "prec_recall")
## Confusion Matrix and Statistics
##
## Reference
## Prediction 0 1
## 0 468 109
## 1 81 233
##
## Accuracy : 0.7868
## 95% CI : (0.7584, 0.8132)
## No Information Rate : 0.6162
## P-Value [Acc > NIR] : < 2e-16
##
## Kappa : 0.5421
##
## Mcnemar's Test P-Value : 0.05014
##
## Precision : 0.8111
## Recall : 0.8525
## F1 : 0.8313
## Prevalence : 0.6162
## Detection Rate : 0.5253
## Detection Prevalence : 0.6476
## Balanced Accuracy : 0.7669
##
## 'Positive' Class : 0
##
#MODEL3 PREDICTING TEST DATA SET
pred_class3_tst <- predict(model3,
new_data = n_tst_df,
type = "class")
# Class Probabilities
pred_proba3_tst <- predict(model3,
new_data = n_tst_df,
type = "prob")
test_results3 <- n_tst_df %>%
select(Survived_f) %>%
bind_cols(pred_class3_tst, pred_proba3_tst)
#accuracy(test_results3, truth = Survived_f, estimate = .pred_class)
print("MODEL3 ACCURACY PRECISION RECALL ETC ON TEST DATASET")
## [1] "MODEL3 ACCURACY PRECISION RECALL ETC ON TEST DATASET"
confusionMatrix(data = test_results3$.pred_class, reference = test_results3$Survived_f, mode = "prec_recall")
## Confusion Matrix and Statistics
##
## Reference
## Prediction 0 1
## 0 266 0
## 1 0 152
##
## Accuracy : 1
## 95% CI : (0.9912, 1)
## No Information Rate : 0.6364
## P-Value [Acc > NIR] : < 2.2e-16
##
## Kappa : 1
##
## Mcnemar's Test P-Value : NA
##
## Precision : 1.0000
## Recall : 1.0000
## F1 : 1.0000
## Prevalence : 0.6364
## Detection Rate : 0.6364
## Detection Prevalence : 0.6364
## Balanced Accuracy : 1.0000
##
## 'Positive' Class : 0
##
#install.packages("ROCR")
library(ROCR)
#set.seed(123)
cv_model1 <- train(
Survived_f ~ Age + SibSp + Parch + Cabinornot + EmbarkedNum + Farebins,
data = trn_df,
method = "glm",
family = "binomial",
trControl = trainControl(method = "cv", number = 10)
)
#set.seed(123)
cv_model2 <- train(
Survived_f ~ Gender+Pclass+Age + SibSp + Parch + Cabinornot + EmbarkedNum + Farebins,
data = trn_df,
method = "glm",
family = "binomial",
trControl = trainControl(method = "cv", number = 10)
)
#set.seed(123)
cv_model3 <- train(
Survived_f ~ Gender+Pclass,
data = trn_df,
method = "glm",
family = "binomial",
trControl = trainControl(method = "cv", number = 10)
)
# extract out of sample performance measures
summary(
resamples(
list(
model1 = cv_model1,
model2 = cv_model2,
model3 = cv_model3
)
)
)$statistics$Accuracy
## Min. 1st Qu. Median Mean 3rd Qu. Max. NA's
## model1 0.6516854 0.6882022 0.7207241 0.7149631 0.7387640 0.7727273 0
## model2 0.7333333 0.7780899 0.8044944 0.8003328 0.8272153 0.8651685 0
## model3 0.6629213 0.7576779 0.7977528 0.7867104 0.8217228 0.8651685 0
library(ROCR)
# Compute predicted probabilities
m1_prob <- predict(cv_model1, trn_df, type = "prob")
m2_prob <- predict(cv_model2, trn_df, type = "prob")
m3_prob <- predict(cv_model3, trn_df, type = "prob")
#m1_prob$"0" <- with(m1_prob, ifelse(m1_prob$"0" < 0.5 , 0 , 1))
#m1_prob$"1" <- with(m1_prob, ifelse(m1_prob$"0" < 0.5 , 0 , 1))
#m3_prob$"0" <- with(m3_prob, ifelse(m3_prob$"0" < 0.5 , 0 , 1))
#m3_prob$"1" <- with(m3_prob, ifelse(m3_prob$"0" < 0.5 , 0 , 1))
# Compute AUC metrics for cv_model1 and cv_model3
perf1 <- prediction(m1_prob[,"1"], trn_df$Survived_f) %>%
performance(measure = "tpr", x.measure = "fpr")
perf2 <- prediction(m2_prob[,"1"], trn_df$Survived_f) %>%
performance(measure = "tpr", x.measure = "fpr")
perf3 <- prediction(m3_prob[,"1"], trn_df$Survived_f) %>%
performance(measure = "tpr", x.measure = "fpr")
pred1 <- prediction(m1_prob[,"1"], trn_df$Survived_f)
pred2 <- prediction(m2_prob[,"1"], trn_df$Survived_f)
pred3 <- prediction(m3_prob[,"1"], trn_df$Survived_f)
auc1 <- performance(pred1, measure = "auc")@y.values[[1]]
auc2 <- performance(pred2, measure = "auc")@y.values[[1]]
auc3 <- performance(pred3, measure = "auc")@y.values[[1]]
# Plot ROC curves for cv_model1 and cv_model3
plot(perf1, col = "black", lty = 2)
plot(perf2, add = TRUE, col = "blue")
plot(perf3, add = TRUE, col = "green")
#legend(0.8, 0.2, legend = c("cv_model1","cv_model2", "cv_model3"),
# col = c("black", "blue","green"), lty = 2:1, cex = 0.6)
legend(0.5, 0.2, legend = c(paste0("cv_model1 (AUC = ", round(auc1, 3), ")"),paste0("cv_model2 (AUC = ", round(auc2, 3), ")"),paste0("cv_model3 (AUC = ", round(auc3, 3), ")")),
col = c("black", "blue", "green"), lty = 2:1, cex = 0.6)
# Define the logistic regression model with penalty and mixture hyperparameters
log_reg <- logistic_reg(mixture = tune(), penalty = tune(), engine = "glmnet")
# Define the grid search for the hyperparameters
grid <- grid_regular(mixture(), penalty(), levels = c(mixture = 4, penalty = 3))
# Define the workflow for the model
log_reg_wf <- workflow() %>%
add_model(log_reg) %>%
add_formula(Survived_f ~ Pclass + Gender + Age + SibSp + Parch + Cabinornot + EmbarkedNum + Farebins)
# Define the resampling method for the grid search
folds <- vfold_cv(trn_df, v = 5)
# Tune the hyperparameters using the grid search
log_reg_tuned <- tune_grid(
log_reg_wf,
resamples = folds,
grid = grid,
control = control_grid(save_pred = TRUE)
)
select_best(log_reg_tuned, metric = "roc_auc")
# Fit the model using the optimal hyperparameters found above which is 0.0000000001
log_reg_final <- logistic_reg(penalty = 0.0000000001, mixture = 0) %>%
set_engine("glmnet") %>%
set_mode("classification") %>%
fit(Survived_f ~ Pclass + Gender + Age + SibSp + Parch + Cabinornot + EmbarkedNum + Farebins, data = trn_df)
# Evaluate the model performance on the testing set
pred_class <- predict(log_reg_final,
new_data = n_tst_df,
type = "class")
pred_proba <- predict(log_reg_final,
new_data = n_tst_df,
type = "prob")
results <- n_tst_df %>%
select(Survived_f) %>%
bind_cols(pred_class, pred_proba)
#view(n_tst_df)
# Create confusion matrix
#conf_mat(results, truth = Survived_f,
# estimate = .pred_class)
confusionMatrix(data = results$.pred_class, reference = results$Survived_f, mode = "prec_recall")
## Confusion Matrix and Statistics
##
## Reference
## Prediction 0 1
## 0 247 10
## 1 18 142
##
## Accuracy : 0.9329
## 95% CI : (0.9044, 0.9549)
## No Information Rate : 0.6355
## P-Value [Acc > NIR] : <2e-16
##
## Kappa : 0.8567
##
## Mcnemar's Test P-Value : 0.1859
##
## Precision : 0.9611
## Recall : 0.9321
## F1 : 0.9464
## Prevalence : 0.6355
## Detection Rate : 0.5923
## Detection Prevalence : 0.6163
## Balanced Accuracy : 0.9331
##
## 'Positive' Class : 0
##
library(lattice)
library(ggplot2)
library(caret)
library(e1071)
##
## Attaching package: 'e1071'
## The following object is masked from 'package:tune':
##
## tune
## The following object is masked from 'package:rsample':
##
## permutations
## The following object is masked from 'package:parsnip':
##
## tune
# Using the processed data as our training data
training_data <- trn_df
test_data <- n_tst_df
# Simple logistic model
model1 <- glm(Survived_f ~ Age + SibSp + Parch + Cabinornot + EmbarkedNum + Farebins, family = "binomial"(link='logit'), data = training_data)
model2 <- glm(Survived_f ~ Gender+Pclass+Age + SibSp + Parch + Cabinornot + EmbarkedNum + Farebins, family = "binomial"(link='logit'), data = training_data)
model3 <- glm(Survived_f ~ Gender+Pclass, family = "binomial"(link='logit'), data = training_data)
#summary(model1)
#summary(model2)
#summary(model3)
#exp(coef(model1))
#exp(coef(model2))
#exp(coef(model3))
#MODEL1 FIT ON TRAINING DATASET
training_data$score_t1 <- predict(model1, newdata = training_data, type="response")
training_data$prediction_t1 <- ifelse(training_data$score_t1>=0.5, 1, 0)
training_data$prediction_f_t1 <- as.factor(training_data$prediction_t1)
#view(training_data)
table(factor(training_data$prediction_t1), factor(training_data$Survived))
##
## 0 1
## 0 466 171
## 1 83 171
print("MODEL1 ACCURACY PRECISION RECALL ETC ON TRAIN DATASET")
## [1] "MODEL1 ACCURACY PRECISION RECALL ETC ON TRAIN DATASET"
confusionMatrix(data = training_data$prediction_f_t1, reference = training_data$Survived_f, mode = "prec_recall")
## Confusion Matrix and Statistics
##
## Reference
## Prediction 0 1
## 0 466 171
## 1 83 171
##
## Accuracy : 0.7149
## 95% CI : (0.684, 0.7444)
## No Information Rate : 0.6162
## P-Value [Acc > NIR] : 3.912e-10
##
## Kappa : 0.3666
##
## Mcnemar's Test P-Value : 4.792e-08
##
## Precision : 0.7316
## Recall : 0.8488
## F1 : 0.7858
## Prevalence : 0.6162
## Detection Rate : 0.5230
## Detection Prevalence : 0.7149
## Balanced Accuracy : 0.6744
##
## 'Positive' Class : 0
##
#MODEL1 FIT ON TEST DATASET
test_data$score_t1 <- predict(model1, newdata = test_data, type="response")
test_data$prediction_t1 <- ifelse(test_data$score_t1>=0.5, 1, 0)
test_data$prediction_f_t1 <- as.factor(test_data$prediction_t1)
#view(test_data)
table(factor(test_data$prediction_t1), factor(test_data$Survived))
##
## 0 1
## 0 202 95
## 1 63 57
print("MODEL1 ACCURACY PRECISION RECALL ETC ON TEST DATASET")
## [1] "MODEL1 ACCURACY PRECISION RECALL ETC ON TEST DATASET"
confusionMatrix(data = test_data$prediction_f_t1, reference = test_data$Survived_f, mode = "prec_recall")
## Confusion Matrix and Statistics
##
## Reference
## Prediction 0 1
## 0 202 95
## 1 63 57
##
## Accuracy : 0.6211
## 95% CI : (0.5726, 0.6679)
## No Information Rate : 0.6355
## P-Value [Acc > NIR] : 0.74661
##
## Kappa : 0.1437
##
## Mcnemar's Test P-Value : 0.01365
##
## Precision : 0.6801
## Recall : 0.7623
## F1 : 0.7189
## Prevalence : 0.6355
## Detection Rate : 0.4844
## Detection Prevalence : 0.7122
## Balanced Accuracy : 0.5686
##
## 'Positive' Class : 0
##
############################################################
#MODEL2 FIT ON TRAINING DATASET
training_data$score_t2 <- predict(model2, newdata = training_data, type="response")
training_data$prediction_t2 <- ifelse(training_data$score_t2>=0.5, 1, 0)
training_data$prediction_f_t2 <- as.factor(training_data$prediction_t2)
#view(training_data)
table(factor(training_data$prediction_t2), factor(training_data$Survived))
##
## 0 1
## 0 467 94
## 1 82 248
print("MODEL2 ACCURACY PRECISION RECALL ETC ON TRAIN DATASET")
## [1] "MODEL2 ACCURACY PRECISION RECALL ETC ON TRAIN DATASET"
confusionMatrix(data = training_data$prediction_f_t2, reference = training_data$Survived_f, mode = "prec_recall")
## Confusion Matrix and Statistics
##
## Reference
## Prediction 0 1
## 0 467 94
## 1 82 248
##
## Accuracy : 0.8025
## 95% CI : (0.7748, 0.8281)
## No Information Rate : 0.6162
## P-Value [Acc > NIR] : <2e-16
##
## Kappa : 0.5796
##
## Mcnemar's Test P-Value : 0.407
##
## Precision : 0.8324
## Recall : 0.8506
## F1 : 0.8414
## Prevalence : 0.6162
## Detection Rate : 0.5241
## Detection Prevalence : 0.6296
## Balanced Accuracy : 0.7879
##
## 'Positive' Class : 0
##
#MODEL2 FIT ON TEST DATASET
test_data$score_t2 <- predict(model2, newdata = test_data, type="response")
test_data$prediction_t2 <- ifelse(test_data$score_t2>=0.5, 1, 0)
test_data$prediction_f_t2 <- as.factor(test_data$prediction_t2)
#view(test_data)
table(factor(test_data$prediction_t2), factor(test_data$Survived))
##
## 0 1
## 0 244 10
## 1 21 142
print("MODEL2 ACCURACY PRECISION RECALL ETC ON TEST DATASET")
## [1] "MODEL2 ACCURACY PRECISION RECALL ETC ON TEST DATASET"
confusionMatrix(data = test_data$prediction_f_t2, reference = test_data$Survived_f, mode = "prec_recall")
## Confusion Matrix and Statistics
##
## Reference
## Prediction 0 1
## 0 244 10
## 1 21 142
##
## Accuracy : 0.9257
## 95% CI : (0.8961, 0.9489)
## No Information Rate : 0.6355
## P-Value [Acc > NIR] : < 2e-16
##
## Kappa : 0.842
##
## Mcnemar's Test P-Value : 0.07249
##
## Precision : 0.9606
## Recall : 0.9208
## F1 : 0.9403
## Prevalence : 0.6355
## Detection Rate : 0.5851
## Detection Prevalence : 0.6091
## Balanced Accuracy : 0.9275
##
## 'Positive' Class : 0
##
###########################################################################
#MODEL3 FIT ON TRAINING DATASET
training_data$score_t3 <- predict(model3, newdata = training_data, type="response")
training_data$prediction_t3 <- ifelse(training_data$score_t3>=0.5, 1, 0)
training_data$prediction_f_t3 <- as.factor(training_data$prediction_t3)
#view(training_data)
table(factor(training_data$prediction_t3), factor(training_data$Survived))
##
## 0 1
## 0 468 109
## 1 81 233
print("MODEL3 ACCURACY PRECISION RECALL ETC ON TRAIN DATASET")
## [1] "MODEL3 ACCURACY PRECISION RECALL ETC ON TRAIN DATASET"
confusionMatrix(data = training_data$prediction_f_t3, reference = training_data$Survived_f, mode = "prec_recall")
## Confusion Matrix and Statistics
##
## Reference
## Prediction 0 1
## 0 468 109
## 1 81 233
##
## Accuracy : 0.7868
## 95% CI : (0.7584, 0.8132)
## No Information Rate : 0.6162
## P-Value [Acc > NIR] : < 2e-16
##
## Kappa : 0.5421
##
## Mcnemar's Test P-Value : 0.05014
##
## Precision : 0.8111
## Recall : 0.8525
## F1 : 0.8313
## Prevalence : 0.6162
## Detection Rate : 0.5253
## Detection Prevalence : 0.6476
## Balanced Accuracy : 0.7669
##
## 'Positive' Class : 0
##
#MODEL3 FIT ON TEST DATASET
test_data$score_t3 <- predict(model3, newdata = test_data, type="response")
test_data$prediction_t3 <- ifelse(test_data$score_t3>=0.5, 1, 0)
test_data$prediction_f_t3 <- as.factor(test_data$prediction_t3)
#view(test_data)
table(factor(test_data$prediction_t3), factor(test_data$Survived))
##
## 0 1
## 0 266 0
## 1 0 152
print("MODEL3 ACCURACY PRECISION RECALL ETC ON TEST DATASET")
## [1] "MODEL3 ACCURACY PRECISION RECALL ETC ON TEST DATASET"
confusionMatrix(data = test_data$prediction_f_t3, reference = test_data$Survived_f, mode = "prec_recall")
## Confusion Matrix and Statistics
##
## Reference
## Prediction 0 1
## 0 266 0
## 1 0 152
##
## Accuracy : 1
## 95% CI : (0.9912, 1)
## No Information Rate : 0.6364
## P-Value [Acc > NIR] : < 2.2e-16
##
## Kappa : 1
##
## Mcnemar's Test P-Value : NA
##
## Precision : 1.0000
## Recall : 1.0000
## F1 : 1.0000
## Prevalence : 0.6364
## Detection Rate : 0.6364
## Detection Prevalence : 0.6364
## Balanced Accuracy : 1.0000
##
## 'Positive' Class : 0
##
Data exploration various variables are done in Univariate and Bi-Variate Analysis.
Count of Pclass, Survived in Barcharts, Age and Fare as histograms.
Bivariate analysis of Age and Survived shows that survival around age of 30 is high.
Bivariate analysis of Gender and Survived shows survival is more across Females.
Bivariate analysis of Pclass and Survival shows survival more amongst 1st Class passengers
We can see that most people were amongst age group of 30 from the bar graph.
Survival is more with people with less or no sibling/spouses.
Correlation of Variables against Survival is done. The correlation in positive or close to 1 means closely related else not closely related.
Logistic Regression from CARET package against different permutations of variables(formula) was done
MODEL1 formula was Survived_f ~ Age + SibSp + Parch + Cabinornot + EmbarkedNum + Farebins
MODEL2 formula was Survived_f ~ Gender+Pclass+Age + SibSp + Parch + Cabinornot + EmbarkedNum + Farebins
MODEL3 formula was Survived_f ~ Gender+Pclass. Here Survived field is Response or prediction variable.
Confusion Matrix was created using above models against training and test datasets to showcase Accuracy, Precision, Recall, Confidence interval etc
MODEL1 training dataset Accuracy was 0.7093, test dataset accuracy was 0.6259
MODEL2 training dataset Accuracy was 0.8047, test dataset accuracy was 0.9329.
MODEL3 training dataset Accuracy was 0.7868, test dataset accuracy was 1
Based on Accuracy MODEL3 is the best model(100%) which means model trained based on Gender and Pclass performs better
A receiver operating characteristic curve, or ROC curve, is a graphical plot that illustrates the performance of a binary classifier model (can be used for multi class classification as well) at varying threshold values.
ROC has a measure called AUC(Area under Curve) higher the value on AUC better the model.
Based on the AUC graph it can be found that MODEL2 has higher AUC of 0.8666 better than MODEL3 which has AUC of 0.833.
In machine learning, a hyperparameter is a parameter, such as the learning rate or choice of optimizer, which specifies details of the learning process, hence the name hyperparameter. This is in contrast to parameters which determine the model itself.
In this MODEL was run with various hyperparameters to find the best prediction(based on roc_auc measure). It was found that the best penalty was 0.0000000001. However applying that did not change the MODELs accuracy(Model formula is similar or MODEL2 formula used)
Logistic regression using the GLM (Generalized Linear Models). Variables was same as what was used for CARET package.
MODEL1 formula was Survived_f ~ Age + SibSp + Parch + Cabinornot + EmbarkedNum + Farebins
MODEL2 formula was Survived_f ~ Gender+Pclass+Age + SibSp + Parch + Cabinornot + EmbarkedNum + Farebins.
MODEL3 formula was Survived_f ~ Gender+Pclass. Here Survived field is Response or prediction variable.
Confusion Matrix was created using above models against training and test datasets to showcase Accuracy, Precision, Recall, Confidence interval etc
MODEL1 training dataset Accuracy was 0.7149, test dataset accuracy was 0.6211
MODEL2 training dataset Accuracy was 0.8025, test dataset accuracy was 0.9257
MODEL3 training dataset Accuracy was 0.7868, test dataset accuracy was 1
Based on Result it can be seen that LOGISTIC REGRESSION using CARET package or GLM function did not make a difference. The results almost close. Just differing in decimals only.