#kush kumar patel (2409544)
# Load necessary libraries if not already installed
if (!require("tidyr")) install.packages("tidyr")
if (!require("dplyr")) install.packages("dplyr")
if (!require("ggplot2")) install.packages("ggplot2")

# Load libraries
library(tidyr)
library(dplyr)
library(ggplot2)

# 1. Load the data
lifeData <- read.csv("~/Desktop/Life Expectancy.csv", header = TRUE, stringsAsFactors = FALSE)

# Convert necessary columns to factors
lifeData$Continent <- as.factor(lifeData$Continent)
lifeData$Hemisphere <- as.factor(lifeData$Hemisphere)
lifeData$Human.Development.Groups <- as.factor(lifeData$Human.Development.Groups)

# Display the resulting data frame
print(head(lifeData))

lifeDataLong <- lifeData %>%
  pivot_longer(
    cols = starts_with("Life.Expectancy.at.Birth"),
    names_to = "Year",
    values_to = "LifeExpectancy"
  ) %>%
  mutate(
    Year = as.numeric(sub(".*\\.(\\d{4})\\.", "\\1", Year))
  )
# 2. Filter for years divisible by 5 and calculate mean life expectancy
continentData <- lifeDataLong %>%
  filter(Year %% 5 == 0) %>%
  group_by(Continent, Year) %>%
  summarize(MeanLifeExpectancy = mean(LifeExpectancy, na.rm = TRUE), .groups = "drop")

# View the result
print(continentData)


# 3. Create a function HDIRankCorrelation
HDIRankCorrelation <- function() {
  correlation <- cor(
    lifeData$`HDI.Rank..2021.`,
    lifeData$`Life.Expectancy.at.Birth..2021.`,
    use = "complete.obs"
  )
  return(correlation)
}

# Call the function and print the correlation
correlation <- HDIRankCorrelation()
print(paste("Correlation between HDI Rank and Life Expectancy in 2021:", correlation))

# 4. Create a function displayGraph
displayGraph <- function(country, startYear, endYear) {
  countryData <- lifeDataLong %>%
    filter(Country == country & Year >= startYear & Year <= endYear)
  
  ggplot(countryData, aes(x = Year, y = LifeExpectancy)) +
    geom_line() +
    geom_point() +
    ggtitle(paste("Life Expectancy in", country)) +
    xlab("Year") +
    ylab("Life Expectancy")
}

# Display graph for Australia (1995-2020)
displayGraph("Australia", 1995, 2020)

# 5. Create a function compareCountries
compareCountries <- function(startYear, endYear, countries) {
  selectedData <- lifeDataLong %>%
    filter(Country %in% countries & Year >= startYear & Year <= endYear)
  
  ggplot(selectedData, aes(x = Year, y = LifeExpectancy, color = Country)) +
    geom_line() +
    ggtitle("Life Expectancy Comparison") +
    xlab("Year") +
    ylab("Life Expectancy")
}

# Display comparison graph for selected countries
compareCountries(2000, 2020, c("Australia", "USA", "India", "Brazil", "China", "Germany"))

# 6. Perform a t-test
test1990 <- lifeDataLong %>% filter(Year == 1990) %>% pull(LifeExpectancy)
test2020 <- lifeDataLong %>% filter(Year == 2020) %>% pull(LifeExpectancy)

# State the hypotheses in comments
# H0: There is no significant difference in global life expectancy between 1990 and 2020.
# H1: There is a significant difference in global life expectancy between 1990 and 2020.

testResult <- t.test(test1990, test2020)
print(testResult)

# Decision
if (testResult$p.value < 0.05) {
  print("Reject the null hypothesis: There is a significant difference.")
} else {
  print("Fail to reject the null hypothesis: No significant difference found.")
}

# 7. Perform ANOVA test
data2010 <- lifeDataLong %>% filter(Year == 2010)

# State the hypotheses in comments
# H0: There is no significant difference in life expectancy across continents in 2010.
# H1: There is a significant difference in life expectancy across continents in 2010.

anovaResult <- aov(LifeExpectancy ~ Continent, data = data2010)
summary(anovaResult)

# Post-hoc test if significant difference is found
if (summary(anovaResult)[[1]][["Pr(>F)"]][1] < 0.05) {
  print("Performing Tukey's HSD test...")
  posthocResult <- TukeyHSD(anovaResult)
  print(posthocResult)
}

