# Data input#

data<- read.csv("C:/Users/ggtz1/Desktop/healthcare-dataset-stroke-data-desktop.csv", header=TRUE)

head(data)
##      id gender age hypertension heart_disease ever_married     work_type
## 1  9046   Male  67            0             1          Yes       Private
## 2 51676 Female  61            0             0          Yes Self-employed
## 3 31112   Male  80            0             1          Yes       Private
## 4 60182 Female  49            0             0          Yes       Private
## 5  1665 Female  79            1             0          Yes Self-employed
## 6 56669   Male  81            0             0          Yes       Private
##   Residence_type avg_glucose_level  bmi  smoking_status stroke
## 1          Urban            228.69 36.6 formerly smoked      1
## 2          Rural            202.21  N/A    never smoked      1
## 3          Rural            105.92 32.5    never smoked      1
## 4          Urban            171.23 34.4          smokes      1
## 5          Rural            174.12   24    never smoked      1
## 6          Urban            186.21   29 formerly smoked      1
str(data)
## 'data.frame':    5110 obs. of  12 variables:
##  $ id               : int  9046 51676 31112 60182 1665 56669 53882 10434 27419 60491 ...
##  $ gender           : chr  "Male" "Female" "Male" "Female" ...
##  $ age              : num  67 61 80 49 79 81 74 69 59 78 ...
##  $ hypertension     : int  0 0 0 0 1 0 1 0 0 0 ...
##  $ heart_disease    : int  1 0 1 0 0 0 1 0 0 0 ...
##  $ ever_married     : chr  "Yes" "Yes" "Yes" "Yes" ...
##  $ work_type        : chr  "Private" "Self-employed" "Private" "Private" ...
##  $ Residence_type   : chr  "Urban" "Rural" "Rural" "Urban" ...
##  $ avg_glucose_level: num  229 202 106 171 174 ...
##  $ bmi              : chr  "36.6" "N/A" "32.5" "34.4" ...
##  $ smoking_status   : chr  "formerly smoked" "never smoked" "never smoked" "smokes" ...
##  $ stroke           : int  1 1 1 1 1 1 1 1 1 1 ...
# Graphs#

f_1<- subset(data, gender=="Female", select=(c(gender, hypertension, heart_disease,smoking_status, stroke)))

f<-f_1[f_1$smoking_status=="formerly smoked" | f_1$smoking_status=="smokes" | f_1$smoking_status=="never smoked", ]

head(f)
##    gender hypertension heart_disease smoking_status stroke
## 2  Female            0             0   never smoked      1
## 4  Female            0             0         smokes      1
## 5  Female            1             0   never smoked      1
## 8  Female            0             0   never smoked      1
## 11 Female            1             0   never smoked      1
## 12 Female            0             1         smokes      1
f_rtotal<- nrow(f)

m_1<- subset(data, gender=="Male", select=(c(gender, hypertension, heart_disease,smoking_status, stroke)))

m<-m_1[m_1$smoking_status=="formerly smoked" | m_1$smoking_status=="smokes" |m_1$smoking_status=="never smoked", ]

m_rtotal<- nrow(m)

all_fm<- rbind(f,m)

# Female Stroke and Smoking Bar Chart#
f[f$stroke,]<- as.character(f$stroke)
## Warning in matrix(value, n, p): data length [2158] is not a sub-multiple or
## multiple of the number of rows [116]
(fsns<- nrow(f[f$stroke=="1" & f$smoking_status=="never smoked",]))
## [1] 62
(fsfs<- nrow(f[f$stroke=="1" & f$smoking_status=="formerly smoked",]))
## [1] 34
(fss<-nrow(f[f$stroke=="1" & f$smoking_status=="smokes",]))
## [1] 19
(fnsns<- nrow(f[f$stroke=="0" & f$smoking_status=="never smoked",]))
## [1] 1166
(fnsfs<- nrow(f[f$stroke=="0" & f$smoking_status=="formerly smoked",]))
## [1] 443
(fnss<-nrow(f[f$stroke=="0" & f$smoking_status=="smokes",]))
## [1] 433
f_stroke<- matrix(c(fsns, fnsns, fsfs, fnsfs, fss, fnss), ncol =3, dimnames= list(c("stroke", "no stroke")))

barplot(f_stroke, beside=TRUE, names.arg= c("Never", "Former", "Current"), main= "Female Smoking Status and Stroke", col= c("gold4", "gold"), xlab= "Smoking Status", ylab="Number of Females", ylim=c(0, 1400))
legend("topright", c("Stroke", "No Stroke"), cex=0.9, fill=c("gold4", "gold"))

# Male Stroke and Smoking Bar Chart#
m[m$stroke,]<- as.character(m$stroke)
## Warning in matrix(value, n, p): data length [1407] is not a sub-multiple or
## multiple of the number of rows [86]
(msns<- nrow(m[m$stroke=="1" & m$smoking_status=="never smoked",]))
## [1] 27
(msfs<- nrow(m[m$stroke=="1" & m$smoking_status=="formerly smoked",]))
## [1] 35
(mss<- nrow(m[m$stroke=="1" & m$smoking_status=="smokes",]))
## [1] 23
(mnsns<- nrow(m[m$stroke=="0" & m$smoking_status=="never smoked",]))
## [1] 636
(mnsfs<- nrow(m[m$stroke=="0" & m$smoking_status=="formerly smoked",]))
## [1] 371
(mnss<-nrow(m[m$stroke=="0" & m$smoking_status=="smokes",]))
## [1] 314
m_stroke<- matrix(c(msns, mnsns, msfs, mnsfs, mss, mnss), ncol =3, dimnames= list(c("stroke", "no stroke")))

barplot(m_stroke, beside=TRUE, names.arg= c("Never", "Former", "Current"), main= "Male Smoking Status and Stroke", col= c("khaki4", "khaki"), xlab= "Smoking Status", ylab="Number of Males", ylim=c(0, 1400))
legend("topright", c("Stroke", "No Stroke"), cex=0.9, fill=c("khaki4", "khaki"))

# Smoking Status/Stroke and Heart Disease Graph#
(hd.y_s.y_ss.ns<- nrow(all_fm[all_fm$heart_disease=="1" & all_fm$stroke=="1" & all_fm$smoking_status=="never smoked",]))
## [1] 12
(hd.n_s.n_ss.ns<- nrow(all_fm[all_fm$heart_disease=="0" & all_fm$stroke=="0" & all_fm$smoking_status=="never smoked",]))
## [1] 1724
(hd.n_s.y_ss.ns<- nrow(all_fm[all_fm$heart_disease=="0" & all_fm$stroke=="1" & all_fm$smoking_status=="never smoked",]))
## [1] 78
(hd.y_s.n_ss.ns<- nrow(all_fm[all_fm$heart_disease=="1" & all_fm$stroke=="0" & all_fm$smoking_status=="never smoked",]))
## [1] 78
(hd.y_s.y_ss.fs<- nrow(all_fm[all_fm$heart_disease=="1" & all_fm$stroke=="1" & all_fm$smoking_status=="formerly smoked",]))
## [1] 12
(hd.n_s.n_ss.fs<- nrow(all_fm[all_fm$heart_disease=="0" & all_fm$stroke=="0" & all_fm$smoking_status=="formerly smoked",]))
## [1] 749
(hd.n_s.y_ss.fs<- nrow(all_fm[all_fm$heart_disease=="0" & all_fm$stroke=="1" & all_fm$smoking_status=="formerly smoked",]))
## [1] 58
(hd.y_s.n_ss.fs<- nrow(all_fm[all_fm$heart_disease=="1" & all_fm$stroke=="0" & all_fm$smoking_status=="formerly smoked",]))
## [1] 65
(hd.y_s.y_ss.s<- nrow(all_fm[all_fm$heart_disease=="1" & all_fm$stroke=="1" & all_fm$smoking_status=="smokes",]))
## [1] 15
(hd.n_s.n_ss.s<- nrow(all_fm[all_fm$heart_disease=="0" & all_fm$stroke=="0" & all_fm$smoking_status=="smokes",]))
## [1] 701
(hd.n_s.y_ss.s<- nrow(all_fm[all_fm$heart_disease=="0" & all_fm$stroke=="1" & all_fm$smoking_status=="smokes",]))
## [1] 27
(hd.y_s.n_ss.s<- nrow(all_fm[all_fm$heart_disease=="1" & all_fm$stroke=="0" & all_fm$smoking_status=="smokes",]))
## [1] 46
all_hd_s_ss<- matrix(c(hd.y_s.y_ss.ns, hd.n_s.n_ss.ns, hd.n_s.y_ss.ns, hd.y_s.n_ss.ns,hd.y_s.y_ss.fs, hd.n_s.n_ss.fs,hd.n_s.y_ss.fs, hd.y_s.n_ss.fs, hd.y_s.y_ss.s, hd.n_s.n_ss.s, hd.n_s.y_ss.s,hd.y_s.n_ss.s ), ncol = 3, dimnames=list(c("s/hd 1", "s/hd 0", "s/hd 1/0", "s/hd 0/1")))

barplot(all_hd_s_ss, beside=TRUE, names.arg= c("Never", "Former", "Current"), main= "Heart Disease & Smoking Status & Stroke", col= c("black","plum1", "orchid", "purple"), xlab= "Smoking Status", ylab="Number of People", ylim=c(0, 2000))
legend("topright", c("Stroke: yes, HD: yes", "Stroke: no, HD: no", "Stroke: yes, HD: no", "Stroke: no, HD: yes"), cex=0.9, fill=c("black","plum1", "orchid", "purple"))

# Smoking Status/Stroke and Hypertension Graph#
(h.y_s.y_ss.ns<- nrow(all_fm[all_fm$hypertension=="1" & all_fm$stroke=="1" & all_fm$smoking_status=="never smoked",]))
## [1] 32
(h.n_s.n_ss.ns<- nrow(all_fm[all_fm$hypertension=="0" & all_fm$stroke=="0" & all_fm$smoking_status=="never smoked",]))
## [1] 1602
(h.n_s.y_ss.ns<- nrow(all_fm[all_fm$hypertension=="0" & all_fm$stroke=="1" & all_fm$smoking_status=="never smoked",]))
## [1] 58
(h.y_s.n_ss.ns<- nrow(all_fm[all_fm$hypertension=="1" & all_fm$stroke=="0" & all_fm$smoking_status=="never smoked",]))
## [1] 200
(h.y_s.y_ss.fs<- nrow(all_fm[all_fm$hypertension=="1" & all_fm$stroke=="1" & all_fm$smoking_status=="formerly smoked",]))
## [1] 19
(h.n_s.n_ss.fs<- nrow(all_fm[all_fm$hypertension=="0" & all_fm$stroke=="0" & all_fm$smoking_status=="formerly smoked",]))
## [1] 713
(h.n_s.y_ss.fs<- nrow(all_fm[all_fm$hypertension=="0" & all_fm$stroke=="1" & all_fm$smoking_status=="formerly smoked",]))
## [1] 51
(h.y_s.n_ss.fs<- nrow(all_fm[all_fm$hypertension=="1" & all_fm$stroke=="0" & all_fm$smoking_status=="formerly smoked",]))
## [1] 101
(h.y_s.y_ss.s<- nrow(all_fm[all_fm$hypertension=="1" & all_fm$stroke=="1" & all_fm$smoking_status=="smokes",]))
## [1] 11
(h.n_s.n_ss.s<- nrow(all_fm[all_fm$hypertension=="0" & all_fm$stroke=="0" & all_fm$smoking_status=="smokes",]))
## [1] 664
(h.n_s.y_ss.s<- nrow(all_fm[all_fm$hypertension=="0" & all_fm$stroke=="1" & all_fm$smoking_status=="smokes",]))
## [1] 31
(h.y_s.n_ss.s<- nrow(all_fm[all_fm$hypertension=="1" & all_fm$stroke=="0" & all_fm$smoking_status=="smokes",]))
## [1] 83
all_h_s_ss<- matrix(c(h.y_s.y_ss.ns, h.n_s.n_ss.ns, h.n_s.y_ss.ns, h.y_s.n_ss.ns,h.y_s.y_ss.fs, h.n_s.n_ss.fs, h.n_s.y_ss.fs, h.y_s.n_ss.fs, h.y_s.y_ss.s, h.n_s.n_ss.s, h.n_s.y_ss.s, h.y_s.n_ss.s ), ncol = 3, dimnames=list(c("s/h 1", "s/h 0", "s/h 1/0", "s/h 0/1")))

barplot(all_h_s_ss, beside=TRUE, names.arg= c("Never", "Former", "Current"), main= "Hypertension & Smoking Status & Stroke", col= c("black","orange", "salmon", "darkorange4"),xlab= "Smoking Status", ylab="Number of People", ylim=c(0, 2000))
legend("topright", c("Stroke: yes, H: yes", "Stroke: no, H: no", "Stroke: yes, H: no", "Stroke: no, H: yes"),cex=0.9, fill=c("black","orange", "salmon", "darkorange4"))

# Numerical Display#
library(stats)
hd.summary<- addmargins(xtabs(~stroke+smoking_status+heart_disease,data=all_fm))

hd.summary[,,-3]
## , , heart_disease = 0
## 
##       smoking_status
## stroke formerly smoked never smoked smokes  Sum
##    0               749         1724    701 3174
##    1                58           78     27  163
##    Sum             807         1802    728 3337
## 
## , , heart_disease = 1
## 
##       smoking_status
## stroke formerly smoked never smoked smokes  Sum
##    0                65           78     46  189
##    1                12           12     15   39
##    Sum              77           90     61  228
hyper.summary<- addmargins(xtabs(~stroke+smoking_status+hypertension, data=all_fm))
                                 
hyper.summary[,,-3]
## , , hypertension = 0
## 
##       smoking_status
## stroke formerly smoked never smoked smokes  Sum
##    0               713         1602    664 2979
##    1                51           58     31  140
##    Sum             764         1660    695 3119
## 
## , , hypertension = 1
## 
##       smoking_status
## stroke formerly smoked never smoked smokes  Sum
##    0               101          200     83  384
##    1                19           32     11   62
##    Sum             120          232     94  446
gender.summary<- addmargins(xtabs(~stroke+smoking_status+gender, data=all_fm))
                                  
gender.summary[,,-3]
## , , gender = Female
## 
##       smoking_status
## stroke formerly smoked never smoked smokes  Sum
##    0               443         1166    433 2042
##    1                34           63     19  116
##    Sum             477         1229    452 2158
## 
## , , gender = Male
## 
##       smoking_status
## stroke formerly smoked never smoked smokes  Sum
##    0               371          636    314 1321
##    1                36           27     23   86
##    Sum             407          663    337 1407