
################################################################
#                                                              #
#     NAME : SRISKANTHARAJA MITHUSHAN                          #
#     STUDENT ID : 2232358                                     #
#     MODULE : Advanced Statistical Programming (ACCA7022)     #
#     ASSIGNMENT 2                                             #
#                                                              #
################################################################

#importing readxl library to read the xlsx file. 
library(readxl)

# Importing and reading the wales Climate Data excel file
#Assigin the "Wales Climate Data.xlsx" to wb
wb <- read_excel("Wales Climate Data.xlsx", sheet = 1) 


# =======================  Task 1.1 START  ======================= 

# Extract relevant data for seasonal maximum temperatures
seasons_data <- wb[, c("year", "win", "spr", "sum", "aut")]

# Filter data for every tenth year
every_tenth_year <- seasons_data[seq(1, nrow(seasons_data), by = 10), ]

# Assuming your data frame is named df
every_tenth_year[, c("win", "spr", "sum", "aut")] <- lapply(every_tenth_year[, c("win", "spr", "sum", "aut")], as.numeric)

# Calculate row means
every_tenth_year$average <- rowMeans(every_tenth_year[, c("win", "spr", "sum", "aut")], na.rm = TRUE)

# Plot the data
ggplot(every_tenth_year, aes(x = year, y = average)) +
  geom_line() +
  geom_point() +
  labs(title = "Average Values Over Years",
       x = "Year",
       y = "Average") +
  theme_minimal()

# =======================  Task 1.1 END  ======================= 

# =======================  Task 1.2 START  ======================= 
# Task 1.2

wb2 <- read_excel("Wales Climate Data.xlsx", sheet = "Rainfall")


# Extract relevant data for total annual rainfall and annual maximum temperature
rainfall <- wb2$ann
max_temp <- wb$ann

# Calculate the correlation
correlation <- cor(rainfall[49:length(rainfall)], max_temp)

# Print the correlation
cat("Correlation between Total Annual Rainfall and Annual Maximum Temperature:", correlation, "\n")

# =======================  Task 1.2 END  ======================= 


# =======================  Task 1.3 START  ======================= 
# Task 1.3

wb3 <- read_excel("Wales Climate Data.xlsx", sheet = "Min Temp")

# Create two vectors for the two groups
group1 <- wb3$aug[wb3$year >= 1890 & wb3$year <= 1910]
group2 <- wb3$aug[wb3$year >= 2000 & wb3$year <= 2020]

# Perform independent samples t-test
t_test_result <- t.test(group1, group2)

# Print the result
print(t_test_result)

# =======================  Task 1.3 END  ======================= 

# =======================  Task 1.4 START  ======================= 

# Task 1.4

wb4 <- read_excel("Wales Climate Data.xlsx",  sheet = "Rain Days")

# Create a new variable 'decade' to represent the complete decades
wb4$decade <- cut(wb4$year, breaks = seq(1890, 2020, by = 10), labels = FALSE)

# Perform ANOVA
anova_result <- aov(ann ~ decade, data = wb4)

print(summary(anova_result))

# =======================  Task 1.4 END  ======================= 
