###   The program was written by Heng Liu(2106258)
###   ASP Assignment  task 2
###   This program processes and visualizes the  COVID-19 data from Swansea and Wuhan
###                    January 2022
###   NB - You need to install the openxlsx, reshape2 and ggplot2 package before running this program

library(openxlsx)
library(reshape2)
library(ggplot2)


### Function Definitions

### generateDescStats - generates the descriptive statistics
# Parameters passed: vector v
# Returns: a vector containing the descriptive statistics values

generateDescStats<-function(v)
{
  
  # Create DescStats  to hold the statistics, ready to be included in the data frame
  
  vDescStats<-NULL
  
  # Calculating basic descriptive statistics - mean
  
  vDescStats<-c(vDescStats,mean(v,na.rm=TRUE))
  
  # Calculate standard deviation
  
  vDescStats<-c(vDescStats,sd(v,na.rm=TRUE))
  
  # Calculating basic descriptive statistics - median
  
  vDescStats<-c(vDescStats,median(v,na.rm=TRUE))
  
  # Calculating more basic descriptive statistics - max, min
  
  vDescStats<-c(vDescStats,max(v,na.rm=TRUE))
  
  vDescStats<-c(vDescStats,min(v,na.rm=TRUE))
  
  # Calculate range
  
  vrange<-range(v,na.rm=TRUE)
  vDescStats<-c(vDescStats,vrange[2]-vrange[1])
  
  # Return vDescStats
  
  return(vDescStats)
}


### Main Script ###


### 1. Load data into a data frame covidData

# Load data file
# NB -
# Because the original file has two sheets, it cannot be imported
# So first read China's data and load it into covidDataChina
# The second time to read UK's covid data, named covidDataUK
# Then merge the two data.frames into coviddata

covidDateChina<-read.xlsx("covid.xlsx",sheet = 1)
covidDateUK<-read.xlsx("covid.xlsx",sheet = 2)
covidDate<-rbind(covidDateChina,covidDateUK)


### 2. plot the number of cases and deaths in the two countries

# Because the chart is plotted on a monthly basis, the data span 22 months
# Create labels for twocountries data frame

bymonth<-1:22
bymonth

# Summary of all cases in China on a monthly basis

chinatotalcases<-c(covidDate[10,5],covidDate[39,5],covidDate[70,5],covidDate[100,5],
                   covidDate[131,5],covidDate[161,5],covidDate[192,5],covidDate[223,5],
                   covidDate[253,5],covidDate[284,5],covidDate[314,5],covidDate[345,5],
                   covidDate[376,5],covidDate[404,5],covidDate[435,5],covidDate[465,5],
                   covidDate[496,5],covidDate[526,5],covidDate[557,5],covidDate[588,5],
                   covidDate[618,5],covidDate[649,5])

# Summary of all deaths cases in China on a monthly basis

chinatotaldeaths<-c(covidDate[10,8],covidDate[39,8],covidDate[70,8],covidDate[100,8],
                    covidDate[131,8],covidDate[161,8],covidDate[192,8],covidDate[223,8],
                    covidDate[253,8],covidDate[284,8],covidDate[314,8],covidDate[345,8],
                    covidDate[376,8],covidDate[404,8],covidDate[435,8],covidDate[465,8],
                    covidDate[496,8],covidDate[526,8],covidDate[557,8],covidDate[588,8],
                    covidDate[618,8],covidDate[649,8])

# Summary of all cases in UK on a monthly basis

uktotalcases<-c(covidDate[650,5],covidDate[679,5],covidDate[710,5],covidDate[740,5],
                covidDate[771,5],covidDate[801,5],covidDate[832,5],covidDate[863,5],
                covidDate[893,5],covidDate[924,5],covidDate[954,5],covidDate[985,5],
                covidDate[1016,5],covidDate[1044,5],covidDate[1075,5],covidDate[1105,5],
                covidDate[1136,5],covidDate[1166,5],covidDate[1197,5],covidDate[1228,5],
                covidDate[1258,5],covidDate[1289,5])

# Summary of all deaths cases in UK on a monthly basis

uktotaldeaths<-c(covidDate[650,8],covidDate[679,8],covidDate[710,8],covidDate[740,8],
                covidDate[771,8],covidDate[801,8],covidDate[832,8],covidDate[863,8],
                covidDate[893,8],covidDate[924,8],covidDate[954,8],covidDate[985,8],
                covidDate[1016,8],covidDate[1044,8],covidDate[1075,8],covidDate[1105,8],
                covidDate[1136,8],covidDate[1166,8],covidDate[1197,8],covidDate[1228,8],
                covidDate[1258,8],covidDate[1289,8])

# Merge the above four data into the twocountries data frame

twocountries<-data.frame(bymonth,chinatotalcases,chinatotaldeaths,uktotalcases,uktotaldeaths)
twocountries

# Because ggPlot requires three columns of data, twocountries is transformed
# NB - For this part you need to install the reshape2 package

twocountries<-melt(twocountries,id="bymonth",na.rm = TRUE)

# Display twocountries
twocountries

# ggplot takes month as x and quantity as y and classifies according to variable

ggplot(data = twocountries,aes(x=bymonth,y=value,group=variable,color=variable,shape=variable))+
  geom_point()+
  geom_line()+
  geom_line()+
  xlab("month")+
  ylab("number")+
  theme_bw()+
  scale_x_continuous(limits = c(1,22),breaks = seq(1,22,1))


### 3. Present descriptive statistics

# Create labels for china four month new data frame

fourmonth<-c(rep("ChinaApr2020",times=30),rep("ChinaOct2020",times=31),
             rep("ChinaApr2021",times=30),rep("ChinaOct2021",times=31))

# The total number of new cases in China over four months

chinafourmonth<-c(covidDate[c(71:100),6],covidDate[c(254:284),6],
                  covidDate[c(436:465),6],covidDate[c(619:649),6])
chinafourmonth<-data.frame(month=fourmonth,data=chinafourmonth)

# Create labels for uk four month new data frame

fourmonth<-c(rep("UKApr2020",times=30),rep("UKOct2020",times=31),
             rep("UKApr2021",times=30),rep("UKOct2021",times=31))

# The total number of new cases in uk over four months

ukfourmonth<-c(covidDate[c(711:740),6],covidDate[c(894:924),6],
                  covidDate[c(1076:1105),6],covidDate[c(1259:1289),6])

ukfourmonth<-data.frame(month=fourmonth,data=ukfourmonth)

# Create data frame from labels chinafourmonth and ukfourmonth data

fourmonthnew<-rbind(chinafourmonth,ukfourmonth)

# Create labels for descStats data frame

descStatsLabels<-c("Mean", "SD", "Median", "Max", "Min", "Range")

# Four months of descriptive statistics were calculated for each country

ChinaApr2020<-generateDescStats(fourmonthnew[1:30,2])
ChinaOct2020<-generateDescStats(fourmonthnew[31:61,2])
ChinaApr2021<-generateDescStats(fourmonthnew[62:91,2])
ChinaOct2021<-generateDescStats(fourmonthnew[92:122,2])
UKApr2020<-generateDescStats(fourmonthnew[123:152,2])
UKOct2020<-generateDescStats(fourmonthnew[153:183,2])
UKApr2021<-generateDescStats(fourmonthnew[184:213,2])
UKOct2021<-generateDescStats(fourmonthnew[214:244,2])

# Create data frame from labels and descriptive statistics data

CovidSummary<-data.frame(Lable=descStatsLabels,
                         ChinaApr2020,ChinaOct2020,ChinaApr2021,ChinaOct2021,
                         UKApr2020,UKOct2020,UKApr2021,UKOct2021)
CovidSummary


### 4. t-test ###

# t-test: The number of new cases in a two-month period

# Extract the number of new cases for each month

october20<-covidDate[894:924,6]
october21<-covidDate[1259:1289,6]

# Is each data sample normal? Use Shapiro test to find out

shapiro.test(october20)
shapiro.test(october21)

# Both of these show as p>0.05, so they are normally distributed
# Perform t-test for independent samples

# HO: There is no difference between the means, i.e. no significant difference in the number of new cases
# H1: There is a difference between the means, i.e. significant differences in the number of new cases

t.test(october20, october21)

# The calculated p-value for this is 2.2e-16. This is smaller than 0.05,
# so we cannot accept the null hypothesis at the p=0.05 level.
# We therefore reject the null hypothesis that There is a significant difference 
# in the number of new cases in the UK between October 2020 and October 2021. 


### 5. ANOVA TEST ###

# Difference between April 2020, October 2020, April 2021 and October 2021

# H0: There is no difference between April 2020, October 2020, April 2021 and October 2021
# h1: There is a significant difference between at least two of the groups

anovaData<-covidDate[c(71:100,254:284,436:465,619:649),6]

chinamonth<-c(rep("April2020",times=30),rep("October2020",times=31),
              rep("April2021",times=30),rep("October2021",times=31))

anovaData<-data.frame(month=chinamonth,number=anovaData)

# carry out ANOVA test

# compute the analysis if variance
resaov<-aov(anovaData$number ~ anovaData$month,data = anovaData)

# summary of the analysis
summary(resaov)

#                  Df Sum Sq Mean Sq F value  Pr(>F)   
# anovaData$month   3  17843    5948   5.202 0.00207 **
# Residuals       118 134921    1143                   
# ---
#  Signif. codes:  0 ??***?? 0.001 ??**?? 0.01 ??*?? 0.05 ??.?? 0.1 ?? ?? 1

# There is a significant difference between groups at the p=0.01 level
# we can reject the null hypothesis and accept the alternative hypothesis
# It is therefore necessary to perform post-hoc tests to find out where there difference

# perform Tukey Pair-wise post-hoc analysis

TukeyHSD(resaov)

# Tukey multiple comparisons of means
# 95% family-wise confidence level

# Fit: aov(formula = anovaData$number ~ anovaData$month, data = anovaData)

# $`anovaData$month`
#                             diff        lwr       upr     p adj
# April2021-April2020     -29.966667 -52.719262 -7.214072 0.0045305
# October2020-April2020   -26.262366 -48.830726 -3.694005 0.0155795
# October2021-April2020   -10.068817 -32.637178 12.499543 0.6514888
# October2020-April2021     3.704301 -18.864059 26.272661 0.9736185
# October2021-April2021    19.897849  -2.670511 42.466210 0.1044000
# October2021-October2020  16.193548  -6.189061 38.576158 0.2398662

# The Tukey test shows that there are two significant difference at the p=0.05 level
# between April2021-April2020 and October2020-April2020
# but not between other pairs of groups

