#Vaccine Efficacy in Real-World Scenarios: A Comparative Study of Covid-19 Outcomes in Vaccinated and Unvaccinated Individuals
# Comparing how well vaccines and booster shots protect against COVID-19 variants compared to being unvaccinated
# The objective of this project is to show the results of how effective the vaccine is as well as the boosters. 

#4Methods

#Describe Variables 
#- Mortality Rate: the number of deaths in a given area or period, or from a particular cause.
#- Unvaccinated: not inoculated with a vaccine to provide immunity against a disease.
#- Vaccinated: treat (a person or animal) with a vaccine to produce immunity to a particular infectious disease or pathogen.
#-Vaccinated with booster: A booster is given when a person has completed their vaccine series and the immunity protection against the virus begins to decrease with time

#u1 = mean mortality rate of unvaccinated population
#u2 = mean mortality rate of vaccinated without booster population
#u3 = mean mortality rate of vaccinated with booster population

##Step 1 install and load required libraries
#install.packages(“readxl”)

library(readxl)
library(tidyr)
## Warning: package 'tidyr' was built under R version 4.3.3
mortality_rate <- read_excel(file.choose())
na.omit(mortality_rate)
## # A tibble: 28 × 3
##    Unvaccinated Vaccinatedwithbooster Vaccinatedwithoutbooster
##           <dbl>                 <dbl>                    <dbl>
##  1         2.40                 0                        0.464
##  2         2.51                 0.214                    0.444
##  3         2.41                 0.117                    0.479
##  4         2.48                 0.114                    0.487
##  5         2.46                 0.216                    0.555
##  6         2.85                 0.184                    0.591
##  7         2.85                 0.232                    0.598
##  8         2.71                 0.201                    0.622
##  9         2.86                 0.292                    0.633
## 10         3.24                 0.312                    0.731
## # ℹ 18 more rows
##Step 2 summarize the data


summary(mortality_rate)
##   Unvaccinated     Vaccinatedwithbooster Vaccinatedwithoutbooster
##  Min.   : 0.6144   Min.   :0.0000        Min.   :0.1322          
##  1st Qu.: 2.1639   1st Qu.:0.1965        1st Qu.:0.4375          
##  Median : 2.9769   Median :0.2711        Median :0.5944          
##  Mean   : 6.7050   Mean   :0.2741        Mean   :0.7536          
##  3rd Qu.: 9.3795   3rd Qu.:0.3381        3rd Qu.:0.8633          
##  Max.   :33.6421   Max.   :0.5331        Max.   :3.5275          
##                    NA's   :50
# Unvaccinated has a mean mortality of 6.71, vaccinated with booster has a mean of 0.27 and vaccinated without booster has a mean of 0.75

list(mortality_rate)
## [[1]]
## # A tibble: 78 × 3
##    Unvaccinated Vaccinatedwithbooster Vaccinatedwithoutbooster
##           <dbl>                 <dbl>                    <dbl>
##  1         13.4                    NA                    0.807
##  2         12.3                    NA                    0.780
##  3         10.4                    NA                    0.697
##  4         10.1                    NA                    0.679
##  5         10.8                    NA                    0.773
##  6         11.9                    NA                    0.865
##  7         14.6                    NA                    0.964
##  8         14.1                    NA                    0.915
##  9         18.7                    NA                    1.06 
## 10         19.5                    NA                    0.975
## # ℹ 68 more rows
# the mortality rate in this list shows unvaccinated having higher numbers while vaccinated with booster shows much lower rates

##Step3 import and clean the data

mor_rate <- mortality_rate%>%  
  pivot_longer(cols = c(Vaccinatedwithbooster, Vaccinatedwithoutbooster, Unvaccinated), names_to = "Vaccine_Status",values_to = "Mortality")
# We simplified the data to make it easier to read and follow. We also were able to take out any results that did not contain any numbers.

## Step 4 data visualization

boxplot(Mortality ~ Vaccine_Status, data = mor_rate,
        main = "Mortality_rate_analysis_based_on_vaccine_status", xlab = "Vaccine_Status",
        ylab = "Mortality Rate")

# The first box plot which is unvaccinated shows a right skewed box and shows outlliers
# the second box vaccinated with booster shows that it is symmetric with no outliers
# the third box vaccinated without booster shows a right skewed box with 6 outliers
# This shows that the mortality rate in vaccinated with booster is the lowest rate 

##analytical approach(anova) to test the significance of hypothesis
# Test data through levenetest, shapiro test and bartlette test
#install.packages("car")
library(car)
## Warning: package 'car' was built under R version 4.3.3
## Loading required package: carData
## Warning: package 'carData' was built under R version 4.3.3
leveneTest(mor_rate$Mortality, mor_rate$Vaccine_Status, data = mor_rate)
## Warning in leveneTest.default(mor_rate$Mortality, mor_rate$Vaccine_Status, :
## mor_rate$Vaccine_Status coerced to factor.
## Levene's Test for Homogeneity of Variance (center = median: mor_rate)
##        Df F value    Pr(>F)    
## group   2  18.022 7.285e-08 ***
##       181                      
## ---
## Signif. codes:  0 '***' 0.001 '**' 0.01 '*' 0.05 '.' 0.1 ' ' 1
# passes lavene test
#install.packages("dplyr")
library(dplyr)
## Warning: package 'dplyr' was built under R version 4.3.3
## 
## Attaching package: 'dplyr'
## The following object is masked from 'package:car':
## 
##     recode
## The following objects are masked from 'package:stats':
## 
##     filter, lag
## The following objects are masked from 'package:base':
## 
##     intersect, setdiff, setequal, union

Vaccination Rates

mor_rate %>%
  group_by(Vaccine_Status) %>%
  summarise(shapiro.test = shapiro.test(Mortality)$p.value)
## # A tibble: 3 × 2
##   Vaccine_Status           shapiro.test
##   <chr>                           <dbl>
## 1 Unvaccinated                 1.18e-11
## 2 Vaccinatedwithbooster        9.16e- 1
## 3 Vaccinatedwithoutbooster     3.79e-12
# - Unvaccinated and vaccinated without booster groups show significant deviation from normality (p < 0.001).
# - Mortality rates in the vaccinated group with boosters do not significantly deviate from normal (p > 0.05).
# passes shaporp test
bartlett.test(mor_rate$Mortality~mor_rate$Vaccine_Status, data = mor_rate)
## 
##  Bartlett test of homogeneity of variances
## 
## data:  mor_rate$Mortality by mor_rate$Vaccine_Status
## Bartlett's K-squared = 452.25, df = 2, p-value < 2.2e-16
# - The Bartlett test suggests that the differences in mortality rates between vaccine status groups have uneven spreads (p < 0.001).
# - This means we should be careful when comparing these groups because their differences in variance could affect our results.
# passes bartlett 

Anova

###anova <- aov( Vaccine_Status~ mortality_rate, data = mor_rate)
###summary(anova)
# - The analysis shows that the type of vaccine status affects mortality rates (p < 0.001).
# - This means there are differences in mortality rates between the groups with different vaccine statuses.
# - We may need to check which groups specifically differ from each other.
###plot(anova,2)
# This pot show sthe residuals and the graph shows the majority of the data following the line
# with the exception of a few outliers
#install.packages("stats")
library(stats)
result<- pairwise.t.test(mor_rate$Mortality, mor_rate$Vaccine_Status, p.adjust.method = "bonferroni")
print(result)
## 
##  Pairwise comparisons using t tests with pooled SD 
## 
## data:  mor_rate$Mortality and mor_rate$Vaccine_Status 
## 
##                          Unvaccinated Vaccinatedwithbooster
## Vaccinatedwithbooster    3.5e-07      -                    
## Vaccinatedwithoutbooster 1.3e-10      1                    
## 
## P value adjustment method: bonferroni
# - Vaccinated individuals with booster shots have significantly lower mortality rates compared to those without booster shots.
# - Vaccinated individuals, even without boosters, have significantly lower mortality rates compared to the unvaccinated.
#u1≠u2≠u3
#We can conclude that the mortality rate between unvaccinated, vaccinated with the booster and vaccinated with are not the same
#They have different means and the p value 0.00 is < 0.05 so we Reject HO!
#vaccinated with the booster had the lowest mortality rate out of the three