-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy patheda.Rmd
193 lines (127 loc) · 4.32 KB
/
eda.Rmd
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
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
```{r setup, include=FALSE}
knitr::opts_chunk$set(warning = FALSE, message = FALSE)
```
```{r}
library(readr)
library(dplyr)
library(ggplot2)
library(corrplot)
library(gridExtra)
```
```{r}
df <- read_csv("data/heart.csv")
```
```{r}
head(df)
```
```{r}
summary(df)
```
```{r}
paste("NA Values:", sum(is.na(df)))
paste("Duplicate Values:", sum(duplicated(df)))
```
```{r}
### Continuous Attributes
# trtbps
# chol
# thalach
# age
# Viewing the Box Plots of these attributes to determine if outliers exist
par(mfrow=c(2,2))
par(mar=c(1,3,1,1))
values = c("age", "chol", "trtbps", "thalachh")
for(name in values){
value <- df[name]
boxplot(value)
stripchart(value, vertical=TRUE, col="blue", add=TRUE, pch = 20, method="jitter")
}
par(mfrow=c(1,1))
# Based on the results, outliers exist in these attributes of the data
```
```{r}
# Spearman Correlation should be weaker since there are outliers in the data
# Kendall Correlation should be weaker since most of the attributes are not dependent upon each other.
par(mfrow=c(2,2))
corrplot(cor(df, method=c("spearman")), title="Spearman",mar=c(0,0,2,0), type="lower", tl.pos="n", cl.pos="n", method="square", order="FPC")
corrplot(cor(df, method=c("pearson")), title="Pearson", mar=c(0,0,2,0), type="lower", tl.pos="n", cl.pos="n", method="square", order="FPC")
corrplot(cor(df, method=c("kendall")), title="Kendall", mar=c(0,0,2,0), type="lower", tl.pos="n", cl.pos="n", method="square", order="FPC")
par(mfrow=c(1,1))
# As expected, the strength of the correlation in Pearson is slightly better than Spearman and Kendall
```
```{r}
# Spearman is our desired correlation method
corrplot(cor(df, method=c("spearman")), type="lower", method="square", order="FPC")
```
```{r}
# Viewing the Age/Output Distribution
ggplot(data=df, aes(x=age, fill=output, group=output)) +
geom_histogram(position="stack")
# Ages 30-50 has a high chance of heart attack
# Ages 50-70 had a 0.5 chance on average
```
```{r}
# Viewing the Gender/Output Distribution
ggplot(data = df, aes(x = sex, fill=output, group=output)) +
geom_histogram(stat="count") +
ggtitle("Gender Survival Rate Distribution")
# People with sex = 0 had a high chance of heart attack
# People with sex = 1 had a 0.5 chance on average
```
```{r}
# Viewing the (Chest Pain)/Output Distribution
ggplot(data=df, aes(x=cp, fill=output, group=output)) +
geom_histogram(stat="count")
# Chest Pain Type 1,2,3 had a high chance of heart attack
# Chest Pain Type 0 had a lower than 0.5 chance
```
```{r}
# Viewing the (Resting Electrocardiographic Results)/Output Distribution
ggplot(data=df, aes(x=restecg, fill=output, group = output)) +
geom_histogram(stat="count")
# Type 1 had a high chance of heart attack
# Type 0 had a 0.5 chance
# Type 2 had a low chance of heart attack
```
```{r}
# Viewing the (Exercise Induced Angina)/Output Distribution
ggplot(data=df, aes(x=exng, fill=output, group = output)) +
geom_histogram(stat="count")
# Type 0 had around a 75% chance
# Type 1 had around a 25% chance
```
```{r}
# Viewing the (Number of Major Vessels)/Output Distribution
ggplot(data=df, aes(x=caa, fill=output, group = output)) +
geom_histogram(stat="count")
# Type 1,2,3 had a low chance
# Type 4 had a very high chance
# Type 0 had around a 75% chance
```
```{r}
# Viewing the (Fasting Blood Sugar > 120)/Output Distribution
ggplot(data=df, aes(x=fbs, fill=output, group = output)) +
geom_histogram(stat="count")
# Both types have a 50% chance
# This attribute is not very useful in classification
```
```{r}
# Viewing the (Resting Blood Pressure)/Output Distribution
ggplot(data=df, aes(x=trtbps, fill=output, group=output)) +
geom_density(alpha=0.6)
# Mostly equal in terms of values of 1.0 and 0
# At Blood Pressure of higher than 190, the chance of heart attack decreases
```
```{r}
# Viewing the (Maximum Heart Rate Achieved)/Output Distribution
ggplot(data=df, aes(x=thalachh, fill=output, group=output)) +
geom_density(alpha=0.6)
# Left end of the spectrum had lower chance
# Chance increases drastically at 150
```
```{r}
# Viewing the Output Distribution
ggplot(data=df, aes(x=output)) +
geom_histogram(position="stack", binwidth=1, color="black", size=2)
# We have slightly more values for 1.0 than 0.0 so we will need to sample properly
```