
## Q1

setwd("C:/Users/fc/Desktop/fcR")
library(readxl)
#Data read
covidData_China <- read_xlsx("covid.xlsx",1)
covidData_UK <- read_xlsx("covid.xlsx",2)
#combine
covidData <- rbind(covidData_China,covidData_UK)
head(covidData)


## Q2


library(reshape2)
month <- format(covidData$date,format="%Y %m")
covidData <- cbind(covidData,month)

#newcases & newdeaths Complement 0 for missing values
covidData[is.na(covidData[,6]),6] <- 0
covidData[is.na(covidData[,9]),9] <- 0

#Calculate the number of monthly cases in China
China_month_cases <-tapply(covidData[1:649,c("new_cases")],INDEX = covidData[1:649,"month"],FUN = sum)

#Calculate the number of deaths in China in a month
China_month_deaths <-tapply(covidData[1:649,c("new_deaths")],INDEX = covidData[1:649,"month"],FUN = sum)

#Calculate the monthly number of cases in Swansea
Swansea_month_cases <-tapply(covidData[650:1289,c("new_cases")],INDEX = covidData[650:1289,"month"],FUN = sum)
#Calculate the number of deaths in Swansea month
Swansea_month_deaths <-tapply(covidData[650:1289,c("new_deaths")],INDEX = covidData[650:1289,"month"],FUN = sum)

#combine
data1 <- data.frame(China_month_cases,China_month_deaths,Swansea_month_cases,Swansea_month_deaths)
data1 <- data.frame(unique(month),data1[,1:4])
colnames(data1)[1] <- "date"
data1 <- melt(data1,id.vars = "date",variable.name = "type",value.name = "count")



#Logarithmic plot
library(ggplot2)
ggplot(data1,aes(x=date,y=log(count),color=type,group=type))+
  geom_line()    +
  geom_point()



## Q3

#Group data by month
month <- format(covidData_China$date,format="%Y %m")
covidData_China <- cbind(covidData_China,month)
month <- format(covidData_UK$date,format="%Y %m")
covidData_UK <- cbind(covidData_UK,month)


#UK 
x1 <- covidData_UK[covidData_UK$month=="2020 04",c("new_cases")]
x2 <- covidData_UK[covidData_UK$month=="2020 10",c("new_cases")]
x3 <- covidData_UK[covidData_UK$month=="2021 04",c("new_cases")]
x4 <- covidData_UK[covidData_UK$month=="2021 10",c("new_cases")]

#China
y1 <- covidData_China[covidData_China$month=="2020 04",c("new_cases")]
y2 <- covidData_China[covidData_China$month=="2020 10",c("new_cases")]
y3 <- covidData_China[covidData_China$month=="2021 04",c("new_cases")]
y4 <- covidData_China[covidData_China$month=="2021 10",c("new_cases")]
my_function <- function(my_data){
  my_data1 <- c(mean(my_data),sd(my_data),median(my_data),max(my_data),min(my_data),max(my_data)-min(my_data))
  return(my_data1)
}
UK_new_cases_2020_04 <- my_function(x1)
UK_new_cases_2020_10 <- my_function(x2)
UK_new_cases_2021_04 <- my_function(x3)
UK_new_cases_2021_10 <- my_function(x4)
China_new_cases_2020_04 <- my_function(y1)
China_new_cases_2020_10 <- my_function(y2)
China_new_cases_2021_04 <- my_function(y3)
China_new_cases_2021_10 <- my_function(y4)
char <- c("mean","sd","medium","max","min","range")

#Create a data frame
CovidSummary <- data.frame(char,UK_new_cases_2020_04,UK_new_cases_2020_10,UK_new_cases_2021_04,UK_new_cases_2021_10,China_new_cases_2020_04,China_new_cases_2020_10,China_new_cases_2021_04,China_new_cases_2021_10)
CovidSummary


## Q4


#H0:x1=x2;
#There was no significant difference in the number of new cases in the UK between October 2020 and October 2021.


t.test(x2,x4)

#p-value < 2.2e-16
#Refuse H0,We believe there is a significant difference in the number of new cases in the UK between October 2020 and October 2021.

## Q5

data3 <- covidData_China[covidData_China$month=="2020 04"|covidData_China$month=="2020 10"|covidData_China$month=="2021 04"|covidData_China$month=="2021 10",c("date","month","new_cases")]

aov1<-aov(new_cases~month,data = data3)
summary(aov1)

#H0£ºThere is no statistically significant difference in the number of new cases in China in April 2020, October 2020, April 2021, and October 2021.
#H1:There are statistically significant differences in the number of new cases in China in April 2020, October 2020, April 2021, and October 2021.
# Because Pr=0.00207<0.05, we passed the significant difference test and rejected H0. We believe that there is a statistically significant difference in the number of new cases in China in April 2020, October 2020, April 2021, and October 2021.  .

