-
Notifications
You must be signed in to change notification settings - Fork 4
/
Copy pathLogistic Regression Lecture.R
137 lines (82 loc) · 2.96 KB
/
Logistic Regression Lecture.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
## Logistic Regression
# Part 1
setwd("./R Bootcamp/R-Course-HTML-Notes/R-for-Data-Science-and-Machine-Learning/Machine Learning with R")
df.train <- read.csv('titanic_train.csv')
head(df.train)
str(df.train)
library(Amelia)
library(ggplot2)
help("missmap")
missmap(df.train,main = 'Missing Map', col = c('yellow','black'),legend=FALSE)
# Survived
ggplot(df.train,aes(Survived)) + geom_bar()
# Pclass
ggplot(df.train,aes(Pclass)) + geom_bar(aes(fill=factor(Pclass)))
# Sex
ggplot(df.train,aes(Sex)) + geom_bar(aes(fill=factor(Sex)))
# Hist of Age
ggplot(df.train,aes(Age)) + geom_histogram(bins=20,fill='blue',alpha=0.5)
# SibSp
ggplot(df.train,aes(SibSp)) + geom_bar(aes(fill=factor(SibSp)))
# Fare Histogram
ggplot(df.train,aes(Fare)) + geom_histogram(fill='green',color='black',alpha=0.5,bins=20)
# Boxplot Pclass
plot <- ggplot(df.train,aes(Pclass,Age))
plot <- plot + geom_boxplot(aes(group=Pclass,fill=factor(Pclass),alpha=0.4))
plot + scale_y_continuous(breaks = seq(min(0),max(80),by=2)) + theme_bw()
# Imputation of Age Based on Class
impute_age <- function(age,class){
out <- age
for (i in 1:length(age)){
if (is.na(age[i])){
if (class[i] == 1){
out[i] <- 37
}else if (class[i] == 2){
out[i] <- 29
}else{
out[i] <- 24
}
}else{
out[i]<-age[i]
}
}
return(out)
}
# Building a Logistic Regression Model (Part 2)
fixed.ages <- impute_age(df.train$Age,df.train$Pclass)
df.train$Age <- fixed.ages
missmap(df.train, main="Titanic Training Data - Missings Map",
col=c("yellow", "black"), legend=FALSE)
## Get the str of the data
str(df.train)
## Remove What We Don't Use
head(df.train,3)
## Relevant Columns for Training
library(dplyr)
df.train <- select(df.train,-PassengerId,-Name,-Ticket,-Cabin)
head(df.train,3)
## Set Factor Columns
str(df.train)
df.train$Survived <- factor(df.train$Survived)
df.train$Pclass <- factor(df.train$Pclass)
df.train$Parch <- factor(df.train$Parch)
df.train$SibSp <- factor(df.train$SibSp)
## Train the Model
log.model <- glm(formula=Survived ~ . , family = binomial(link='logit'),data = df.train)
summary(log.model)
## Predictting Using Test Cases
library(caTools)
set.seed(101)
split = sample.split(df.train$Survived, SplitRatio = 0.70)
final.train = subset(df.train, split == TRUE)
final.test = subset(df.train, split == FALSE)
## Final Logistic Model
final.log.model <- glm(formula=Survived ~ . , family = binomial(link='logit'),data = final.train)
summary(final.log.model)
## Checking Prediction Accuracy
fitted.probabilities <- predict(final.log.model,newdata=final.test,type='response')
fitted.results <- ifelse(fitted.probabilities > 0.5,1,0)
misClasificError <- mean(fitted.results != final.test$Survived)
print(paste('Accuracy',1-misClasificError))
## Conculsion Matrix
table(final.test$Survived, fitted.probabilities > 0.5)