# Xiyu Han 2300244
# assignment2

library(xlsx)
library(ggplot2)
library(dplyr)
library(tidyr)
library(corrplot)
library(GGally)
library(PerformanceAnalytics)
library(mice)

## -------------------------- Question1 --------------------------

# Read the file 'Max Temp' into a data frame named 'climate'
climate = read.xlsx("E:/IELTS/R/2assignment/WalesClimateData.xlsx",1)

# Filter the data frame for rows where 'year' is between 1892 and 2022 
climate_year_filter <- climate[climate$year >= 1892 & climate$year <= 2022 & climate$year %% 10 == 2, ]

# Select the columns about season from the filtered data frame
climate_season_filter <- climate_year_filter[,c('year','win','spr','sum','aut')]

# Reshape the filtered data to a long format
climate_long <- gather(climate_season_filter, season, temperature, win:aut)

# Convert the 'temperature' column to numeric
climate_long$temperature <- as.numeric(as.character(climate_long$temperature))

# Create the line plot
ggplot(climate_long, aes(x = year, y = temperature, group = season, color = season)) +
  geom_line() +
  geom_point() +
  theme_minimal() +
  labs(title = "Average Seasonal Maximum Temperatures from 1892 to 2022",
       x = "Year", y = "Temperature (°C)", color = "Season") +
  scale_x_continuous(breaks = seq(1892, 2022, by = 10)) +
  scale_y_continuous(breaks = seq(5, 25, by = 5))


## -------------------------- Question2 --------------------------

# Read the file 'Max Temp' into a data frame named 'climate_max'
climate_max = read.xlsx("E:/IELTS/R/2assignment/WalesClimateData.xlsx",1)

# Read the file 'Rainfall' into a data frame named 'climate_rainfall'
climate_rainfall = read.xlsx("E:/IELTS/R/2assignment/WalesClimateData.xlsx",4)

# Merge the 'climate_max' and 'climate_rainfall' data frames by the 'year' column
climate_combined <- merge(climate_max[, c('year', 'ann')], climate_rainfall[, c('year', 'ann')], by = "year")

# Rename the columns
colnames(climate_combined) <- c("year", "AnnualMaxTemp", "AnnualRainfall")

# Calculate the Pearson correlation coefficient 
cor_data = cor(climate_combined$AnnualMaxTemp,climate_combined$AnnualRainfall,method="pearson",use="complete.obs")

# Test the correlation between paired samples
cormtest_data = cor.mtest(climate_combined, conf.level = 0.95, method = "pearson", use="complete.obs") 

# Creates a matrix of scatterplots and correlation tests for all pairwise 
ggpairs(climate_combined)

# A function from the 'PerformanceAnalytics' package that plots a correlation matrix
chart.Correlation(climate_combined)

# Create a scatterplot
ggplot(climate_combined, aes(x = AnnualMaxTemp, y = AnnualRainfall)) +
  geom_point() + 
  geom_smooth(method = "lm", se = FALSE) +  
  labs(title = "Relationship between Annual Max Temperature and Annual Rainfall",
       x = "Annual Max Temperature (°C)",
       y = "Annual Rainfall (mm)") +
  theme_minimal()  


## -------------------------- Question3 --------------------------

# Read the file 'Min Temp' into a data frame named 'climate_min'
climate_min = read.xlsx("E:/IELTS/R/2assignment/WalesClimateData.xlsx",2)

# Filter 'climate_min' data frame for rows where 'year' is between 1890 and 1910
climate_min_filter1 <- climate_min[climate_min$year >= 1890 & climate$year <= 1910, ]

# Select columns 'year' and 'aug' for the years 1890 to 1910 from the filtered data
climate_min_1890 <- climate_min_filter1[,c('year','aug')]

# Filter 'climate_min' data frame for rows where 'year' is between 2000 and 2020
climate_min_filter2 <- climate_min[climate_min$year >= 2000 & climate$year <= 2020, ]

# Select columns 'year' and 'aug' for the years 2000 to 2020 
climate_min_2000 <- climate_min_filter2[,c('year','aug')]

# Perform a t-test to compare the mean of 'aug' temperatures between the years 1890-1910 and 2000-2020
t.test(climate_min_1890$aug, climate_min_2000$aug)


## -------------------------- Question4 --------------------------

# Read the file 'Min Temp' into a data frame named 'climate_min'
climate_rainy = read.xlsx("E:/IELTS/R/2assignment/WalesClimateData.xlsx",5)

# Select columns 'year' and 'ann' 
climate_rainy_ann <- climate_rainy[,c('year','ann')]

# This groups years into decades starting with 1891, 1901, ..., 2010, 2020
climate_rainy_ann$decade <- (as.integer((climate_rainy_ann$year - 1) / 10) * 10) + 1

# Convert the 'decade' column to a factor for ANOVA analysis
climate_rainy_ann$decade <- as.factor(climate_rainy_ann$decade)

# Perform ANOVA test to compare the mean annual numbers of wet days across the decades
anova_result <- aov(ann ~ decade, data = climate_rainy_ann)

# Display the ANOVA summary 
summary(anova_result)

# Perform Tukey's post-hoc test
hoc_result <- TukeyHSD(anova_result)
print(hoc_result)
