image source: rand.org importing libraries
##
## Attaching package: 'dplyr'
## The following object is masked from 'package:doBy':
##
## order_by
## The following objects are masked from 'package:stats':
##
## filter, lag
## The following objects are masked from 'package:base':
##
## intersect, setdiff, setequal, union
## ── Attaching core tidyverse packages ──────────────────────── tidyverse 2.0.0 ──
## ✔ forcats 1.0.0 ✔ readr 2.1.4
## ✔ ggplot2 3.4.2 ✔ stringr 1.5.0
## ✔ lubridate 1.9.2 ✔ tibble 3.2.1
## ✔ purrr 1.0.1 ✔ tidyr 1.3.0
## ── Conflicts ────────────────────────────────────────── tidyverse_conflicts() ──
## ✖ dplyr::filter() masks stats::filter()
## ✖ dplyr::lag() masks stats::lag()
## ✖ dplyr::order_by() masks doBy::order_by()
## ℹ Use the conflicted package (<http://conflicted.r-lib.org/>) to force all conflicts to become errors
##
## Attaching package: 'data.table'
##
## The following objects are masked from 'package:lubridate':
##
## hour, isoweek, mday, minute, month, quarter, second, wday, week,
## yday, year
##
## The following object is masked from 'package:purrr':
##
## transpose
##
## The following objects are masked from 'package:dplyr':
##
## between, first, last
Loading Data Set
health_rec <- read.table('https://raw.githubusercontent.com/xrander/Health-data/master/Health_data.csv',
header = T,
sep = ',',
na.strings = NA,
dec = '.',
strip.white = T)
health_rec <- setDT(health_rec)
head(health_rec)##Data Explorartion
Some of the data is having NA, that happens in big data, outcome is the most important here and cannot be left as NA
health_rec <- health_rec[!is.na(health_rec$outcome),] #removing the missing outcome data
length(health_rec$outcome) ## [1] 1176
There is no missing data again in the part of the outcome. – The data have reduced to 1176 from 1177
some data are categorical data and they’ve been changed to factor type of data
health_rec$outcome <-as.factor(health_rec$outcome)
health_rec$gender <- as.factor(health_rec$gender)
health_rec$COPD <- as.factor(health_rec$COPD)
health_rec$Renal_failure <-as.factor(health_rec$Renal_failure)
health_rec$hypertensive <- as.factor(health_rec$hypertensive)
health_rec$atrialfibrillation <- as.factor(health_rec$atrialfibrillation)
health_rec$CHD_with_no_MI <- as.factor(health_rec$CHD_with_no_MI)
health_rec$diabetes <- as.factor(health_rec$diabetes)
health_rec$deficiencyanemias <- as.factor(health_rec$deficiencyanemias)
health_rec$depression <- as.factor(health_rec$depression)
health_rec$Hyperlipemia <- as.factor(health_rec$Hyperlipemia)
health_rec$Renal_failure <- as.factor(health_rec$Renal_failure)We assigned some values to the codes to aid easy analysis
health_rec$outcome <- ifelse(health_rec$outcome == '0', 'Alive', 'Dead')
health_rec$gender <- ifelse(health_rec$gender == '1', 'Male', 'Female')
health_rec$atrialfibrillation <- ifelse(health_rec$atrialfibrillation == '0',
'Having', 'Not having')
health_rec$depression <- ifelse(health_rec$depression == 0, 'Having',
'Not Having')
health_rec$hypertensive <- ifelse(health_rec$hypertensive == '0',
'Having','Not Having')
health_rec$Renal_failure <- ifelse(health_rec$Renal_failure == '0',
'Not having','Having')
health_rec$Hyperlipemia <- ifelse(health_rec$Hyperlipemia == 0,
'Having','Not Having')
health_rec$deficiencyanemias <- ifelse(health_rec$deficiencyanemia == 0,
'Having', 'Not Having')
health_rec$diabetes <- ifelse(health_rec$diabetes== 0, 'Having','Not Having')Which age is the most in the hospital?
##
## 89 84 81 80 78 83 85 79 82 87 75 77 86 72 66 76 88 63 64 69
## 141 48 43 40 39 39 38 35 34 33 32 32 32 30 28 27 27 26 26 25
## 68 67 71 65 62 70 73 74 53 56 55 60 61 58 48 57 52 51 54 59
## 23 22 22 21 19 19 18 18 16 16 15 15 15 13 11 10 9 8 8 8
## 47 45 46 50 90 91 92 93 96 35 37 42 43 44 38 41 19 39 40 49
## 7 6 6 6 6 5 5 5 5 4 4 4 4 4 3 3 2 2 2 2
## 94 97 25 28 32 95 98 99
## 2 2 1 1 1 1 1 1
# Visuals
barplot(sort(table(health_rec$age), decreasing = T),
col = 'blue',
main = 'Age distribution of patients',
ylim = c(0, 160),
ylab = 'Number of patients',
xlab = 'Age of Patients')ANSWER Patients that are 89 years of age are the highest.
Which age group is the most in the hospital?
breaks <- seq(from = 1, to = max(health_rec$age), by = 10) #this starts
#at the lowest age and ends at the lowest
age_interval <- cut(health_rec$age, breaks = breaks,
right = FALSE,
include.lowest = TRUE) # this makes the class interval from the list
age_group <- cbind(data.frame(age_interval))
age_groups <- sort(table(age_interval), decreasing = T) # generating frequency table
barchart(age_groups,
horizontal = FALSE,
xlab = 'Age_interval',
ylab = 'Frequency',
ylim = c(0, 450),
col = 'red')ANSWER Patients between the age 81 to 91 are the highest in the hospital with age 89 been the highest
which age group of patients dies more in the hospital?
The result shows the frequency table for dead patients according to age.
barplot(sort(table(dead_patients$age)),
col = 'purple',
xlab = 'age (years)',
ylab = 'Frequency',
main = 'Distribution of Age for Dead Patients',
ylim = c(0, 30))ANSWER Patients at age 89 are the highest to die, with 23 patients dying
What is the most prevalent gender in the hospital
gender <- as.data.frame(sort(table(health_rec$gender), decreasing = T)) # This gives a frequency of the genders
## Female gender is more prevalent with 618 patients
colnames(gender) <- c('gender','num') #changing column nameswhat is the death rate of both gender
gender_dead_count <- as.data.frame(sort(table(dead_patients$gender))) #Frequency for dead patients according to gender
colnames(gender_dead_count) <- c('gender', 'count')
gender[3] <- gender_dead_count$count
names(gender)[3] <- 'dead' # this is similar to using colnames
gender$rate <- gender$dead/gender$num * 100 #rate of death per hundredIt seems the male gender is having more dead rate than female
barplot(gender$rate,
names.arg = gender$gender,
xlab = substitute(paste(bold('gender'))),
ylab = substitute(paste(bold('death rate per hundred'))),
main = 'Death rate of genders',
col = c(5,7))Gender group with the highest number of death?
##
## Male Female
## 80 79
the gender with the highest death is male with 80 individuals dead
How many patients died in the hospital with atrialfibrillation?
dead_patients_atrial <- as.data.frame(health_rec %>% select(outcome, atrialfibrillation) %>%
filter(outcome == 'Dead') %>% group_by(atrialfibrillation)%>%
summarize(length = length(atrialfibrillation)))
dead_patients_atrial67 patients died having atrialfibrillation
patients having atrialfibrillation
atrial <- as.data.frame(health_rec %>% select(gender, atrialfibrillation) %>%
filter(atrialfibrillation == 'Having') %>% group_by(gender)%>%
summarize(atrial = length(atrialfibrillation))) # this gets the number of having atril... for each gender
gender <- merge(gender, atrial, by = 'gender', all = T)645 is the number of patients having atrialfibrillation
Number of patients that died between the gender having atrialfibrillation
dead_patients_atrial_gender <-
as.data.frame(health_rec %>% select(outcome, gender,
atrialfibrillation) %>% filter(outcome == 'Dead',
atrialfibrillation=='Having') %>% group_by(gender) %>%
summarize(died_having_atrial = length(gender)))
dead_patients_atrial_gender39 female and 28 male died having atrialfibrillation
merging the result to gender table
How many patients in the hospital have depression?
##
## Not Having Having
## 140 1036
barplot(sort(table(health_rec$depression)),
ylab = 'number of patients', xlab = 'depression', main = 'patients
having depression', col = 'green')
1036 out of 1176 patients are having depression
show the total number of depressed male and female
depressed_individuals <- health_rec %>% select(gender, depression)%>%
filter(depression == 'Having') %>%
group_by(gender) %>%
summarize(depressed_individuals = length(depression))
depressed_individualsgender <- merge(gender, depressed_individuals, by = 'gender', all =
TRUE) #merging result to gender tableshow the number of patients having depression that are dead according to gender.
dead_depressed <- as.data.frame(health_rec %>%
select(outcome, gender, depression) %>%
filter(outcome == 'Dead', depression == 'Having') %>%
group_by(gender) %>%
summarize(depressed_and_dead =length(depression)))
dead_depressedgender <- merge(gender, dead_depressed, by = 'gender', all =
TRUE) # merging the result to gender tablebarchart(depressed_and_dead~gender,
data = dead_depressed,
ylim = c(0,90),
col = 'purple') ## show data value on the chart
### Question 6c Show the number of dead patients having or not having
depression according to the genders
dep_not_dep<- as.data.frame(health_rec %>%
select(outcome, gender, depression) %>%
filter(outcome == 'Dead') %>%
group_by(gender, depression) %>%
summarize(num_of_patients = length(depression)))## `summarise()` has grouped output by 'gender'. You can override using the
## `.groups` argument.
barchart(num_of_patients~depression|gender,
data = dep_not_dep,
group= depression,
xlab = substitute(paste(bold('Depression'))),
ylab = substitute(paste(bold('Number of patients'))))what is the rate of non-survived patients with hypertension?
dead_hyper <- health_rec %>%
select(outcome, hypertensive) %>%
filter(hypertensive == 'Having', outcome == 'Dead') %>%
summarize(dead_hypertensive = length(hypertensive)) # this returns the patients that are dead and had hypertension
hypertensive <- health_rec %>%
select(outcome, hypertensive) %>%
filter(hypertensive == 'Having') %>%
summarize(hypertensive = length(hypertensive)) #this returns all the patients with hypertension
dead_hyper$dead_hypertensive/ hypertensive$hypertensive * 100 #proportion of the dead hypertensive patients## [1] 17.46988
Rate of the different gender with hypertension per 100 patients
gender_hyper <- as.data.frame(health_rec %>% select(gender,
hypertensive) %>% filter (hypertensive == 'Having') %>%
group_by(gender) %>% summarize (hypertensive_tot =
length(hypertensive)))
gender <- merge(gender, gender_hyper, by = 'gender', all = T) #merging to gender
gender$rate_hyper <- gender$hypertensive_tot/gender$num * 100 ##estimating the rateANSWER 28 female and 29 male patients per 100 are having hypertension
###Question 9b patients having hypertension and dead
dead_gender_hyper <- as.data.frame(health_rec %>%
select(outcome,gender, hypertensive)%>%
filter (outcome == 'Dead',hypertensive == 'Having') %>%
group_by(gender) %>%
summarize (hypertensive_dead = length(hypertensive))) # this separates the dead hypertensive patients according to genderANSWER 31 female and 27 male of the dead patients had hypertension
How many patients with renal failure are alive in the hospital?
health_rec %>%
select(outcome, Renal_failure) %>%
filter(outcome == 'Alive', Renal_failure == 'Having') %>%
summarise(length(Renal_failure))ANSWER 392 patients are having renal failure and alive
How many patients died having renal failure
health_rec %>%
select(outcome, Renal_failure) %>%
filter(outcome == 'Dead', Renal_failure == 'Having') %>%
summarise(length(Renal_failure))ANSWER37 patients died having renal failure
People with renal failure according to gender
renal <- as.data.frame(health_rec %>%
select(gender, Renal_failure)%>%
filter(Renal_failure == 'Having') %>%
group_by(gender) %>%
summarise(renal_failure = length(Renal_failure)))
renalHow many patients in the hospital with Hyperlipemia are dead?
dead_hyperli <- as.data.frame(health_rec %>%
select(outcome, gender, Hyperlipemia) %>%
filter(outcome == 'Dead', Hyperlipemia == 'Having') %>%
group_by(gender) %>%
summarize (dead_hyperli = length(Hyperlipemia)))
gender <- merge(gender, dead_hyperli, by = 'gender', all = T) # merging to genderANSWER rsum(gender$dead_hyperli)
patients died having hyperlipermia
how many patients in the hospital with Anemia are dead?
anemia <- as.data.frame(health_rec %>%
select(outcome, gender, deficiencyanemias) %>%
filter(outcome == 'Dead', deficiencyanemias == 'Having') %>%
group_by(gender) %>%
summarise(anemia_dead = length(deficiencyanemias)))
gender<- merge(gender, anemia, by = 'gender', all = T) # merging to gender 124 is the sum of dead patients having anemia
Total sum of patients having anemia
anemia_tot <- as.data.frame(health_rec %>%
select(gender,deficiencyanemias) %>%
filter(deficiencyanemias == 'Having') %>%
group_by(gender) %>%
summarise(anemia_tot = length(deficiencyanemias)))
gender <- merge(gender, anemia_tot, by = 'gender', all = T) #merging to gender777 patients is having anemia
What is the proportion of survival and non-survival between diabetic and non-diabetic patients
Short preview of the stats relating to diabetic patients
health_rec %>% select(outcome, diabetes) %>% group_by(outcome,
diabetes) %>% summarize(count = length(diabetes))## `summarise()` has grouped output by 'outcome'. You can override using the
## `.groups` argument.
The proportion of patients having diabetes
diabeteic <- as.data.frame(health_rec %>%
select(diabetes) %>%
filter(diabetes == 'Having') %>%
summarize(length(diabetes))) # this returns the total number of diabetic patients
diabeteic$`length(diabetes)`/length(health_rec$outcome) # this returns the percentage of diabetic patients## [1] 0.5790816
ANSWER 57.9% of the patients are diabetic
Proportion of diabetic that survived
To get the proportion of diabetic patient that survived, we need to know the total number of people having diabetic and we divide that by the number of people having diabetes and survived
having_diabetes_alive <- as.data.frame(health_rec %>%
select(outcome, diabetes) %>%
filter(outcome == 'Alive', diabetes == 'Having') %>%
summarize(having_diabetes = length(diabetes))) # returns the patients having diabetes and still alive
having_diabetes <- as.data.frame(health_rec %>%
select(diabetes) %>%
filter(diabetes == 'Having') %>%
summarize(diabetic = length(diabetes))) # returns patients having diabetes dead or alive
having_diabetes_alive$having_diabetes/having_diabetes$diabetic * 100 # proportion of diabetic patients alive## [1] 85.02203
ANSWER 85% of diabetic patients are alive
Proportion of non-diabetic that survived. For this we follow the same procedure as the one above but this time, the patients are not diabetic
non_diabetic_alive <- health_rec %>%
select(outcome, diabetes) %>%
filter(outcome == 'Alive', diabetes == 'Not Having') %>%
summarize(non_diabetes = length(diabetes)) # this returns non-diabetic patients that are alive
non_diabetic <- health_rec %>%
select(diabetes) %>%
filter(diabetes == 'Not Having') %>%
summarize(non_diabetic = length(diabetes)) #this returns non-diabetic dead or alive
non_diabetic_alive$non_diabetes/non_diabetic$non_diabetic * 100 # this returns the proportion of non-diabetic that are alive ## [1] 88.48485
ANSWER88.4848485% of the patients are not having diabetes and are alive
Proportion of diabetic that died
dead_diabetic <- health_rec %>%
select(outcome, diabetes) %>%
filter(diabetes == 'Having', outcome == 'Dead') %>%
summarize (diabetic = length(diabetes)) # num of dead diabetic patients
dead_diabetic$diabetic/having_diabetes$diabetic * 100 #this returns the proportion of dead diabetics ## [1] 14.97797
ANSWER about 14.98 percent of the diabetics are dead
Proportion of non_diabetic that died
dead_non_diabetic <- health_rec %>%
select(outcome, diabetes) %>%
filter(diabetes == 'Not Having', outcome == 'Dead') %>%
summarize (non_diabetic=length(diabetes)) # num of dead non-diabetic patients
dead_non_diabetic$non_diabetic/non_diabetic$non_diabetic * 100 # this returns the proportion of dead non-diabetics## [1] 11.51515
ANSWER 11.52 percent of non diabetics are dead.
What is the proportion of survival and non-survival between depressed and non-depressed patients
Short preview of the stats relating to depressed patients
health_rec %>% select(outcome, depression) %>%
group_by(outcome, depression) %>%
summarize(count = length(outcome))## `summarise()` has grouped output by 'outcome'. You can override using the
## `.groups` argument.
What is the proportion of depressed and non-depressed that survived
depressed <- as.data.frame(health_rec %>%
select(depression) %>%
filter(depression == 'Having') %>%
summarize(depressed = length(depression))) # this returns the total number of depressed patients either dead or alive
depressed$depressed/length(health_rec$outcome) # this returns the percentage of depressed patients## [1] 0.8809524
ANSWER88.1 percent of the patients are depressed
Proportion of depressed patients that survived
having_depression_alive <- as.data.frame(health_rec %>%
select(outcome, depression) %>%
filter(outcome == 'Alive', depression == 'Having') %>%
summarize(having_depression = length(depression))) # returns the patients having depression and still alive
having_depression_alive$having_depression/depressed$depressed * 100 # proportion of depressed patients alive## [1] 85.71429
ANSWER 85.7% of depressed patients are alive
Proportion of non-depressed that survived
For this we follow the same procedure as the one above but this time, the patients are not depressed
non_depressed_alive <- health_rec %>%
select(outcome, depression) %>%
filter(outcome == 'Alive', depression == 'Not Having') %>%
summarize(non_depression = length(depression)) #this returns non-depressed patients that are alive
non_depressed <- health_rec %>% select(depression) %>%
filter(depression == 'Not Having') %>% summarize(non_depressed =
length(depression)) #this returns non-depressed patients count dead or alive
non_depressed_alive$non_depression/non_depressed$non_depressed * 100 # this returns the proportion of non-depressed patients that are alive## [1] 92.14286
ANSWER92.1% percent of the patients are not having depression and are alive
Proportion of depressed patients that died
dead_depressed <-health_rec %>%
select(outcome, depression) %>%
filter(depression == 'Having', outcome == 'Dead') %>%
summarize (depressed = length(depression)) # num of dead depressed patients
dead_depressed$depressed/depressed$depressed * 100 #this returns the proportion of dead depressed patients## [1] 14.28571
ANSWER about 14.28 percent of the depressed patients are dead
Proportion of non_depressed patients that died
dead_non_depressed <- health_rec %>%
select(outcome, depression) %>%
filter(depression== 'Not Having', outcome == 'Dead') %>%
summarize(non_depressed=length(depression)) # num of dead non-diabetic patients
dead_non_depressed$non_depressed/non_depressed$non_depressed * 100 # this returns the proportion of dead non-diabetics ## [1] 7.857143
ANSWER 7.86 percent of the non-depressed patients are dead.