#Huang Xudong
#2011454


#To get a convenient end-of-the-month date, download the "LubriDate" extension
#Download the "Psych" extension pack to help you calculate all kinds of statistics
#To facilitate reading excel files, download the XLSX extension pack
install.packages("lubridate")
install.packages("psych")
install.packages("xlsx")
library(lubridate)
library(xlsx)
library(psych)

Sys.setlocale("LC_TIME", "English") #The Chinese operation interface will cause the months given by the months instruction to be Chinese, so we need to switch to English regional time
readxlsx<- function(x){  #Read the XLSX file
  df1<-read.xlsx(x,sheetIndex = 1,header =TRUE,stringsAsFactors = FALSE)
  df2<-read.xlsx(x,sheetIndex = 2,header =TRUE,stringsAsFactors = FALSE)
  df<-rbind(df1,df2)
  return(df)
}

covidData<-readxlsx("/covid.xlsx")

a <- c(ymd("2020-1-31") %m+% months(0:11),ymd("2021-1-31") %m+% months(0:9)) #Get the end of each month date
Theline<-NULL
for (i in 1:length(a)){
  Theline<-c(Theline,which(covidData$date==a[i])) #Gets the row coordinates of the date at the end of each month
}

CMonth2020<-c(month(covidData[Theline[seq(1,24,2)],"date"]))
CDeath2020<-c(covidData[Theline[seq(1,24,2)],"total_deaths"])
Ccases2020<-c(covidData[Theline[seq(1,24,2)],"total_cases"])
CMonth2021<-c(month(covidData[Theline[seq(25,44,2)],"date"]))
CDeath2021<-c(covidData[Theline[seq(25,44,2)],"total_deaths"])
Ccases2021<-c(covidData[Theline[seq(25,44,2)],"total_cases"])
EMonth2020<-c(month(covidData[Theline[seq(2,24,2)],"date"]))
EDeath2020<-c(covidData[Theline[seq(2,24,2)],"total_deaths"])
Ecases2020<-c(covidData[Theline[seq(2,24,2)],"total_cases"])
EMonth2021<-c(month(covidData[Theline[seq(26,44,2)],"date"]))
EDeath2021<-c(covidData[Theline[seq(26,44,2)],"total_deaths"])
Ecases2021<-c(covidData[Theline[seq(26,44,2)],"total_cases"]) #Total monthly deaths and total monthly cases for each year in the two countries were included in different vectors


plot(CMonth2020,CDeath2020,type="o",main = "Covid",xlab = "Month",ylab="China",col="Red",lty=2,pch=0,ylim =c(0,100000))
par(new=T)
plot(CMonth2020,Ccases2020,col = "Red",type = "o",axes=F,xlab="",ylab="",lty=2,pch=1,ylim =c(0,100000))
par(new=T)
plot(CMonth2021,CDeath2021,type = "o",xlab = "",ylab="",col="Red",lty=1,pch=0,ylim =c(0,100000),axes=F)
par(new=T)
plot(CMonth2021,Ccases2021,col = "Red",type = "o",axes=F,xlab="",ylab="",lty=1,pch=1,ylim =c(0,100000)) #Plot monthly cases and monthly deaths in China for 2020 and 2021

par(new=T)
plot(EMonth2020,EDeath2020,type="o",xlab = "",ylab="",col="Blue",axes=F,lty=2,pch=0,ylim =c(0,200000))
axis(4,col="Blue",col.ticks="Blue",col.axis="Blue") #Draw a Y-axis that is convenient for representing the relevant data for the UK
par(new=T)
plot(EMonth2020,Ecases2020,col = "Blue",type = "o",axes=F,xlab="",ylab="",lty=2,pch=1,ylim =c(0,10000000))
axis(4,col="Blue",col.ticks="Blue",col.axis="Blue",at=c(0,2000000,4000000,6000000,8000000,10000000)) #Draw a Y-axis that is convenient for representing the relevant data for the UK
par(new=T)
plot(EMonth2021,EDeath2021,type = "o",xlab = "",ylab="",col="Blue",lty=1,pch=0,ylim =c(0,200000),axes=F)
par(new=T)
plot(EMonth2021,Ecases2021,col = "Blue",type = "o",axes=F,xlab="",ylab="",lty=1,pch=1,ylim =c(0,10000000))#Plot the same graph of monthly cases and monthly deaths in the UK for 2020 and 2021


AprilCovid<-c(covidData[which(months(covidData$date)=="April"),"new_cases"])
CA20<-c(AprilCovid[1:30])
CA21<-c(AprilCovid[31:60])
EA20<-c(AprilCovid[61:90])
EA21<-c(AprilCovid[91:120]) #The number of new cases in April in different years in China and the UK were divided

Octobercovid<-c(covidData[which(months(covidData$date)=="October"),"new_cases"])
CO20<-c(Octobercovid[1:31])
CO21<-c(Octobercovid[32:62])
EO20<-c(Octobercovid[63:93])
EO21<-c(Octobercovid[94:125]) #The number of new cases in October in different years in China and the UK were divided

CovidSummary<-rbind(describe(CA20),describe(CA21),describe(CO20),describe(CO21),describe(EA20),describe(EA21),describe(EO20),describe(EO21)) #Obtain statistical information for several elements
Month<-c("2020-04","2021-04","2020-10","2021-10","2020-04","2021-04","2020-10","2021-10")
Country<-c("China","China","China","China","British","British","British","British")
CovidSummary<-cbind(CovidSummary,Month,Country)

t.test(EO20,EO21,mu=(CovidSummary[8,"mean"]-CovidSummary[7,"mean"])) #Since p-value is less than 0.05, the data of October 2020 and October 2021 in the UK do not have the nature of normal distribution

ChinaANONA<-NULL
ChinaANONA<-cbind(CA20,CA21)
Chinaan<-c(CA20,CA21,CO20,CO21)
Chinaandf<-data.frame(Chinaan)
groups<-factor(c(rep("ca20",30),rep("ca21",30),rep("co20",31),rep("co21",31)))
aggregate(Chinaan,by=list(groups),FUN=mean)
fit<-aov(Chinaan~groups,data = Chinaandf)  #Since PR is less than 0.05, the data of these four months in China do not belong to normal distribution, and there is a statistically significant difference
summary(fit)
TukeyHSD(fit) #As can be seen from the data, the difference in April 2020 in China is quite significant. As can be seen from the differences among other data, April 2020 is strongly correlated with October 2021, while April 2020 is strongly correlated with October 2021. The correlation between April 2021 and October 2021 is weak, while October 2020 and October 2021 have some correlation. Since There is no correlation between April 2020 and April 2021, April 2020 and October 2020 are also very different. So there may be other factors influencing April 2020 relative to other months.






