---
title: "Air Pollution Data Analycis"
author: "Tasin Islam"
date: "`r Sys.Date()`"
output:
  html_document:
    toc: true
    toc_depth: 3
    theme: cerulean
    number_sections: true
---

```{r setup, include=FALSE}
# Load necessary libraries
library(rstatix)
library(readr)
library(ggplot2)
library(tidyverse)
library(lubridate)
library(car)
library(broom)

Introduction

This report analyzes the Port Talbot Hourly Air Quality (AQ) dataset using R.
It includes:
Data import and cleaning. Missing data analysis. Visualization of pollutant trends. Correlation and hypothesis testing. ANOVA and non-parametric tests.

Problem 1: Data Import and Cleaning
# Read CSV file

PortTalbotAQ <- read_csv("Port_Talbot_Hourly_AQ.csv",
na = c("No data", "NA", ""))

# Rename headers

PortTalbotAQ <- PortTalbotAQ %>%
rename(
Date = `Date`,
Time = `Time`,
O3 = `Ozone`,
Status = `Status`,
NO = `Nitric oxide`,
Status1 = `Status.1`,
NO2 = `Nitrogen dioxide`,
Status2 = `Status.2`,
NOx = `Nitrogen oxides as nitrogen dioxide`,
Status3 = `Status.3`,
SO2 = `Sulphur dioxide`,
Status4 = `Status.4`,
CO = `Carbon monoxide`,
Status5 = `Status.5`,
PM10 = `PM10 particulate matter (Hourly measured)`,
Status6 = `Status.6`,
PM2.5 = `PM2.5 particulate matter (Hourly measured)`,
Status7 = `Status.7`,
WindDirct = `Modelled Wind Direction`,
Status8 = `Status.8`,
WindSpd = `Modelled Wind Speed`,
Status9 = `Status.9`,
Tempt = `Modelled Temperature`,
Status10 = `Status.10`
)

# Create datetime column

PortTalbotAQ <- PortTalbotAQ %>%
mutate(
datetime = dmy_hms(paste(Date, Time), tz = "UTC")
) %>%
arrange(datetime)

head(PortTalbotAQ)

Problem 2: Percentage of Missing Data per Pollutant

pollutants <- c("O3", "NO", "NO2", "NOx", "SO2", "CO")

invalidData <- tibble(
pollutant = pollutants,
pct_missing = sapply(pollutants, function(p) {
mean(is.na(PortTalbotAQ[[p]])) * 100
})
)

invalidData


Problem 3: Visualization of NOx Between July 2015 and November 2024

displayPollutantGraph <- function(pollutant, start_month, start_year, end_month, end_year) {
start_date <- make_date(start_year, start_month, 1)
end_date <- make_date(end_year, end_month, 1)

df <- PortTalbotAQ %>%
filter(day(datetime) == 1, hour(datetime) == 12,
datetime >= start_date, datetime <= end_date) %>%
select(datetime, value = all_of(pollutant))

ggplot(df, aes(x = datetime, y = value)) +
geom_line(color="steelblue") +
geom_point(color="darkred") +
labs(
title = paste(pollutant, "at 12:00 on 1st of each month"),
x = "Date", y = paste(pollutant, "(Units)")
) +
theme_minimal()
}

displayPollutantGraph("NOx", 7, 2015, 11, 2024)

