# Author: Lirui Bi
# Student number: 2106257
# Title: Statistical analysis
# Time: 01.2022
#--------------------------------------------------------------------
rm(list = ls())
# Question 1
# Batch loading of covid datasets (including China and UK)
# Index of the sheet to be read
sheet.index<-1:2 
data1<-list()
for(i in sheet.index)
{
  data1[[i]]<- xlsx::read.xlsx("covid.xlsx",sheetIndex=i, 
                               header = T)
}
# Selected China and UK
China <- data1[[1]]
UK <- data1[[2]]
# Display convidData
covidData <- data.frame(rbind(China, UK))
covidData

# Question 2 
# Extract the data for each month and calculate the total
covidData$year <- substr(covidData$date, start=1, stop = 4)
covidData$month <- substr(covidData$date, start=6, stop = 7)

# Pick out the required variables
library(tidyverse)
attach(covidData)
# View data structure
str(covidData)
# Calculating the number of cases and deaths per month
covidData[is.na(covidData)] <- 0
month_covid_tc <- aggregate(x=covidData$total_cases, by=list(covidData$location,covidData$year, covidData$month), FUN='sum' )
month_covid_td <- aggregate(x=covidData$total_deaths, by=list(covidData$location,covidData$year, covidData$month), FUN='sum' )
month_covid <- data.frame(month_covid_tc, month_covid_td$x)

library(lubridate)
# Generate year and month data
date <- as.Date("2019-12-1")+months(1:24)
date <- substr(date, start=1, stop = 7)
# Select the year and month data required
date <- c(date[1],date[13],date[2],date[14],date[3],
          date[15],date[4],date[16],date[5],date[17],date[6],
          date[18],date[7],date[19],date[8],date[20],date[9],
          date[21],date[10],date[22],date[11],date[12])
# Add to data 
month_covid$date <- rep(date,each=2)
# Change column name
colnames(month_covid) <- c("location", "year", "month", "total_cases", "total_deaths","date")

# Convert total_cases and deaths into columnar data
library(reshape2)
month_covid_m <- melt(month_covid, id=c("location","year","month","date"))

# Draw
library(ggplot2)
ggplot(month_covid_m, aes(date, y=value, fill=location ))+
  geom_bar(stat = "identity", position = "dodge")+
  facet_wrap(~variable, scales = "free_y")+
  theme(axis.text.x = element_text(angle = 30, vjust = 0.5))

# Question 3 
# Filtered data for April and October 2020 and 2021
covidData <- data.frame(rbind(China, UK))
covidData$year <- substr(covidData$date, start=1, stop = 4)
covidData$month <- substr(covidData$date, start=6, stop = 7)

# Add to data frame 
month_covid_nc <- subset(covidData, select = c(location, new_cases,  year, month))
month_covid_nc_f <- month_covid_nc %>% filter(month==c("04","10"))

# Calculate the situation in China
# Define a function to calculate descriptive statistics on data
calfun <- function(data){
  mean=mean(data, na.rm = T)
  sd=sd(data, na.rm = T)
  median=median(data, na.rm = T)
  max=max(data, na.rm = T)
  min=min(data, na.rm = T)
  range=range(data, na.rm = T)
  total = data.frame(mean,sd, median,max, min , range)
  return(total)
}
# Filtered data for April and October 2020 and 2021 for China 
China_2020_4 <- month_covid_nc_f %>% filter(location=="China"&year=="2020",month=="04")
China_2020_10 <- month_covid_nc_f %>% filter(location=="China"&year=="2020",month=="10")
China_2021_4 <- month_covid_nc_f %>% filter(location=="China"&year=="2021",month=="04")
China_2021_10 <- month_covid_nc_f %>% filter(location=="China"&year=="2021",month=="10")

# Calculated results
China_2020_4_total <- calfun(China_2020_4$new_cases)
China_2020_10_total <- calfun(China_2020_10$new_cases)
China_2021_4_total <- calfun(China_2021_4$new_cases)
China_2021_10_total <- calfun(China_2021_10$new_cases)

# Combine calculation results
China <- rbind(China_2020_4_total,China_2020_10_total,China_2021_4_total,China_2021_10_total) 

# Filtered data for April and October 2020 and 2021 for UK
UK_2020_4 <- month_covid_nc_f %>% filter(location=="United Kingdom"&year=="2020",month=="04")
UK_2020_10 <- month_covid_nc_f %>% filter(location=="United Kingdom"&year=="2020",month=="10")
UK_2021_4 <- month_covid_nc_f %>% filter(location=="United Kingdom"&year=="2021",month=="04")
UK_2021_10 <- month_covid_nc_f %>% filter(location=="United Kingdom"&year=="2021",month=="10")

# Calculated results 
UK_2020_4_total <- calfun(UK_2020_4$new_cases)
UK_2020_10_total <- calfun(UK_2020_10$new_cases)
UK_2021_4_total <- calfun(UK_2021_4$new_cases)
UK_2021_10_total <- calfun(UK_2021_10$new_cases)

# Combine calculation results 
UK <- rbind(UK_2020_4_total,UK_2020_10_total,UK_2021_4_total,UK_2021_10_total) 

# Combine the statistics of two countries
CovidSummary <- data.frame(rbind(China, UK))
# Generate location variables
CovidSummary$location <- rep(c("China","United Kingdom"), each=8)
# Generate date variables 
CovidSummary$date <- rep(c(rep(c("2020-04","2020-10","2021-04","2021-10"),each=2)),2)
# Display CovidSummary
CovidSummary

# Question 4
# Original hypothesis: There is no significant difference in the 
# mean value of new cases in the UK between the months 2020-10 
# and 2021-10.
# The results show t= -16.84, pvalue < 2.2e-16. This result 
# implies that the original hypothesis is rejected and there is a 
# significant difference between the number of new cases in the 
# UK in October 2020 and October 2021.

# Filtered data 
UK_10 <- month_covid_nc %>% filter( location=="United Kingdom"& month=="10")

# Tests for normality of the data
shapiro.test((UK_10 %>% filter(year=="2020"))[,2])
shapiro.test((UK_10 %>% filter(year=="2021"))[,2])

# t-test for the data
t.test((UK_10 %>% filter(year=="2020"))[,2],(UK_10 %>% filter(year=="2021"))[,2] )

# Question 5
# Original hypothesis: no significant difference in the number of
# new cases in China in 2020 and 2021.
# From the variance test, Fvalue =2.25, pvalue=0.0923 >0.05 .The 
# original hypothesis was rejected, implying that there was no 
# significant difference between these four dates of new cases in
# China.

# Filtered data
China <- month_covid_nc %>% filter( location=="China"& month==c("04","10"))
China_new <- tidyr::unite(China, "year_month", year, month)
China_new$year_month <- factor(China_new$year_month)
attach(China_new)
# ANOVA test
aov <- aov(new_cases~year_month)
summary(aov)
