-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathheart_attack.Rmd
More file actions
261 lines (202 loc) · 5.05 KB
/
heart_attack.Rmd
File metadata and controls
261 lines (202 loc) · 5.05 KB
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
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
---
title: "Heart_Attack Analysis "
author: "Bernard Asante"
date: "2024-11-23"
output: html_document
---
##Loading library
```{r, warning=FALSE}
library(tidyverse)
library(vtable)
```
##REading data
```{r}
df <- read.csv('Heart Attack.csv')
df %>%
colnames()
```
##Data Exploration
```{r, warning=FALSE}
df %>%
glimpse()
df %>%
head() #displaying first six rows
df %>%
tail() #Displaying last 6 rows
```
##Data cleaning
```{r}
df <- df %>%
select(age, gender, impluse, pressurehight, pressurelow, glucose, troponin, class) %>%
rename(pulse = impluse, systolic = pressurehight, diastolic = pressurelow) %>%
mutate(`glucose_mmol/l` = glucose / 18) %>%
mutate(class= as.factor(class)) %>%
mutate(gender = recode(gender,
`0` = 'Female',
`1` = 'Male')) %>%
select(-glucose)
head(df)
df %>%
summarise_all(~sum(is.na(.)))
head(df)
```
##Data manipulation on categorical vaiables
```{r}
df %>%
glimpse() #observing columns with character or factor
freq_gender <- df %>%
count(gender) %>%
rename(freq = n)
freq_gender
freq_class <- df %>%
count(class) %>%
rename(freq = n)
freq_class
# Contingency(Two by two )table
gender_class <- df %>%
select(gender, class) %>%
table()
gender_class
```
##Data manipulation on numeric variables
```{r}
df_mean <- df %>%
summarise_if(is.numeric, mean) #displaying mean fro all numeric values
df_mean
mean_age <- df %>%
summarise(mean_age = mean(age)) #displaying mean for a single numeric value
mean_age
df_fivenum <- df %>%
summarise_if(is.numeric, fivenum)
st(df_fivenum)
```
## Data manipulation (cat and num variables)
```{r}
means_for_gender <- df %>%
group_by(gender) %>%
summarise_if(is.numeric, mean)
means_for_gender
means_for_class <- df %>%
group_by(class) %>%
summarise_if(is.numeric, mean)
means_for_class # this can further be evaluated using t test
```
##Data visualization
#Visualizing categorical variables (count)
```{r}
bar_gender <- df %>%
ggplot(aes(gender, fill = gender))+
geom_bar()+
labs(title = 'Bar chart of gender distrubution')
bar_gender
bar_class <- df %>%
ggplot(aes(class, fill = class))+
geom_bar()+
labs(title = 'Bar chart of class distrubution')
bar_class
#Displaying across other cat variables
bar_gender_class <- df %>%
ggplot(aes(gender, fill = gender))+
geom_bar()+
labs(title = 'Bar chart of gender distrubution betweeen class')+
facet_grid(~class)
bar_gender_class
bar_class_gender <- df %>%
ggplot(aes(class, fill = class))+
geom_bar()+
labs(title = 'Bar chart of gender distrubution betweeen class')+
facet_grid(~gender)
bar_class_gender
```
#Visualizing Numeric variables
```{r}
hist_age <- df %>%
ggplot(aes(age, fill = gender))+
geom_histogram(bin = 10)+
labs(title = 'Histogram for age')
hist_age
box_age <- df %>%
ggplot(aes(gender, age, fill = gender))+
geom_boxplot()+
facet_grid(~class)+
labs(title = 'Boxplot for age bewteen class')
box_age
hist_troponin <- df %>%
ggplot(aes(troponin, fill = class))+
geom_histogram(bin = 20)+
labs(title = 'Histogram for tropnin ')+
facet_grid(~class)
hist_troponin
box_troponin <- df %>%
ggplot(aes(gender, troponin, fill = gender))+
geom_boxplot()+
facet_grid(~class)+
labs(title = 'Boxplot for troponin bewteen class')
box_troponin
#Scatter plot
glucose_troponin_point <- df %>%
ggplot(aes(`glucose_mmol/l`, troponin, colour = class))+
geom_point()+
labs(title = 'Scatterplot for glucose and troponin')
glucose_troponin_point
age_troponin_point <- df %>%
ggplot(aes(age, troponin, colour = class))+
geom_point()+
geom_smooth()+
labs(title = 'Scatterplot for age and troponin')
age_troponin_point
```
##Testing hypothesis
#Chi_square
```{r}
chi_gender_class <- df %>%
select(gender,class) %>%
table()
chi_gender_class #Same as gender_class table
chisq.test(chi_gender_class)
```
# t test
```{r}
test_gender_troponin <- df %>%
select(gender, troponin) %>%
t.test(troponin~gender, data = .)
test_gender_troponin
test_gender_age <- df %>%
select(gender, age) %>%
t.test(age~gender, data = .)
test_gender_age
```
#Correlation
```{r}
cor.test(df$age, df$troponin)
```
#Regression models
```{r, warning=FALSE}
model <- glm(class ~ age + gender + troponin,
data = df,
family = binomial)
summary(model)
```
#Visualizing model
```{r}
library(ggplot2)
# Extract coefficients
coefficients <- summary(model)$coefficients
coeff_df <- data.frame(
Predictor = rownames(coefficients),
Estimate = coefficients[, "Estimate"],
StdError = coefficients[, "Std. Error"],
pValue = coefficients[, "Pr(>|z|)"]
)
# Plot coefficients
ggplot(coeff_df, aes(x = Predictor, y = Estimate)) +
geom_point() +
geom_errorbar(aes(ymin = Estimate - StdError, ymax = Estimate + StdError), width = 0.2) +
theme_minimal() +
labs(title = "Logistic Regression Coefficients", y = "Estimate", x = "Predictor")
```
#Linear model
```{r}
linear_model <- lm(troponin ~ age+gender+class+`glucose_mmol/l`, data = df)
summary(linear_model)
```