The purpose of this project is to investigate whether there is a relationship between temperature and carbon monoxide (CO) levels in the air. The Air Quality dataset contains measurements of different air pollutants and environmental conditions. This analysis focuses on temperature and carbon monoxide to explore whether changes in temperature are associated with changes in CO levels.
The research question is: Is there a relationship between temperature and the amount of carbon monoxide (CO) in the air?
The null hypothesis states that there is no linear correlation between temperature and carbon monoxide levels. The alternative hypothesis states that there is a linear correlation between the two variables.
library(tidyverse)
## ── Attaching core tidyverse packages ──────────────────────── tidyverse 2.0.0 ──
## âś” dplyr 1.2.1 âś” readr 2.2.0
## âś” forcats 1.0.1 âś” stringr 1.6.0
## âś” ggplot2 4.0.3 âś” tibble 3.3.1
## âś” lubridate 1.9.5 âś” tidyr 1.3.2
## âś” purrr 1.2.2
## ── Conflicts ────────────────────────────────────────── tidyverse_conflicts() ──
## âś– dplyr::filter() masks stats::filter()
## âś– dplyr::lag() masks stats::lag()
## ℹ Use the conflicted package (<http://conflicted.r-lib.org/>) to force all conflicts to become errors
air_quality <- read.csv2(
"~/Downloads/air+quality/AirQualityUCI.csv",
header = TRUE,
na.strings = c("", "NA", "-200"),
dec = ","
)
air_quality <- air_quality[, 1:15]
# View the dataset dimensions
dim(air_quality)
## [1] 9471 15
# View the variable names
names(air_quality)
## [1] "Date" "Time" "CO.GT." "PT08.S1.CO."
## [5] "NMHC.GT." "C6H6.GT." "PT08.S2.NMHC." "NOx.GT."
## [9] "PT08.S3.NOx." "NO2.GT." "PT08.S4.NO2." "PT08.S5.O3."
## [13] "T" "RH" "AH"
# Examine the structure of the dataset
str(air_quality)
## 'data.frame': 9471 obs. of 15 variables:
## $ Date : chr "10/03/2004" "10/03/2004" "10/03/2004" "10/03/2004" ...
## $ Time : chr "18.00.00" "19.00.00" "20.00.00" "21.00.00" ...
## $ CO.GT. : num 2.6 2 2.2 2.2 1.6 1.2 1.2 1 0.9 0.6 ...
## $ PT08.S1.CO. : int 1360 1292 1402 1376 1272 1197 1185 1136 1094 1010 ...
## $ NMHC.GT. : int 150 112 88 80 51 38 31 31 24 19 ...
## $ C6H6.GT. : num 11.9 9.4 9 9.2 6.5 4.7 3.6 3.3 2.3 1.7 ...
## $ PT08.S2.NMHC.: int 1046 955 939 948 836 750 690 672 609 561 ...
## $ NOx.GT. : int 166 103 131 172 131 89 62 62 45 NA ...
## $ PT08.S3.NOx. : int 1056 1174 1140 1092 1205 1337 1462 1453 1579 1705 ...
## $ NO2.GT. : int 113 92 114 122 116 96 77 76 60 NA ...
## $ PT08.S4.NO2. : int 1692 1559 1555 1584 1490 1393 1333 1333 1276 1235 ...
## $ PT08.S5.O3. : int 1268 972 1074 1203 1110 949 733 730 620 501 ...
## $ T : num 13.6 13.3 11.9 11 11.2 11.2 11.3 10.7 10.7 10.3 ...
## $ RH : num 48.9 47.7 54 60 59.6 59.2 56.8 60 59.7 60.2 ...
## $ AH : num 0.758 0.726 0.75 0.787 0.789 ...
# View the first six observations
head(air_quality)
## Date Time CO.GT. PT08.S1.CO. NMHC.GT. C6H6.GT. PT08.S2.NMHC.
## 1 10/03/2004 18.00.00 2.6 1360 150 11.9 1046
## 2 10/03/2004 19.00.00 2.0 1292 112 9.4 955
## 3 10/03/2004 20.00.00 2.2 1402 88 9.0 939
## 4 10/03/2004 21.00.00 2.2 1376 80 9.2 948
## 5 10/03/2004 22.00.00 1.6 1272 51 6.5 836
## 6 10/03/2004 23.00.00 1.2 1197 38 4.7 750
## NOx.GT. PT08.S3.NOx. NO2.GT. PT08.S4.NO2. PT08.S5.O3. T RH AH
## 1 166 1056 113 1692 1268 13.6 48.9 0.7578
## 2 103 1174 92 1559 972 13.3 47.7 0.7255
## 3 131 1140 114 1555 1074 11.9 54.0 0.7502
## 4 172 1092 122 1584 1203 11.0 60.0 0.7867
## 5 131 1205 116 1490 1110 11.2 59.6 0.7888
## 6 89 1337 96 1393 949 11.2 59.2 0.7848
# Get descriptive statistics
summary(air_quality)
## Date Time CO.GT. PT08.S1.CO.
## Length :9471 Length :9471 Min. :-200.0000 Min. : 647
## N.unique : 391 N.unique : 24 1st Qu.: 1.0000 1st Qu.: 937
## N.blank : 0 N.blank : 0 Median : 1.8000 Median :1063
## Min.nchar: 10 Min.nchar: 8 Mean : -0.2163 Mean :1100
## Max.nchar: 10 Max.nchar: 8 3rd Qu.: 2.9000 3rd Qu.:1231
## NAs : 114 NAs : 114 Max. : 11.9000 Max. :2040
## NAs :1706 NAs :480
## NMHC.GT. C6H6.GT. PT08.S2.NMHC. NOx.GT.
## Min. : 7.0 Min. :-200.000 Min. : 383.0 Min. : 2.0
## 1st Qu.: 67.0 1st Qu.: 4.000 1st Qu.: 734.5 1st Qu.: 98.0
## Median : 150.0 Median : 7.900 Median : 909.0 Median : 180.0
## Mean : 218.8 Mean : 1.866 Mean : 939.2 Mean : 246.9
## 3rd Qu.: 297.0 3rd Qu.: 13.600 3rd Qu.:1116.0 3rd Qu.: 326.0
## Max. :1189.0 Max. : 63.700 Max. :2214.0 Max. :1479.0
## NAs :8557 NAs :114 NAs :480 NAs :1753
## PT08.S3.NOx. NO2.GT. PT08.S4.NO2. PT08.S5.O3.
## Min. : 322.0 Min. : 2.0 Min. : 551 Min. : 221.0
## 1st Qu.: 658.0 1st Qu.: 78.0 1st Qu.:1227 1st Qu.: 731.5
## Median : 806.0 Median :109.0 Median :1463 Median : 963.0
## Mean : 835.5 Mean :113.1 Mean :1456 Mean :1022.9
## 3rd Qu.: 969.5 3rd Qu.:142.0 3rd Qu.:1674 3rd Qu.:1273.5
## Max. :2683.0 Max. :340.0 Max. :2775 Max. :2523.0
## NAs :480 NAs :1756 NAs :480 NAs :480
## T RH AH
## Min. :-1.90 Min. : 9.20 Min. :0.1847
## 1st Qu.:11.80 1st Qu.:35.80 1st Qu.:0.7368
## Median :17.80 Median :49.60 Median :0.9954
## Mean :18.32 Mean :49.23 Mean :1.0255
## 3rd Qu.:24.40 3rd Qu.:62.50 3rd Qu.:1.3137
## Max. :44.60 Max. :88.70 Max. :2.2310
## NAs :480 NAs :480 NAs :480
The dataset uses -200 to represent missing measurements. These values need to be treated as missing data before analyzing the variables.
# Count any remaining -200 values in carbon monoxide
sum(air_quality$CO.GT. == -200, na.rm = TRUE)
## [1] 91
# Replace remaining -200 values in carbon monoxide with NA
air_quality$CO.GT.[air_quality$CO.GT. == -200] <- NA
# Convert the Date column into a date format
air_quality$Date <- as.Date(
air_quality$Date,
format = "%d/%m/%Y"
)
# Remove observations without a valid date
air_quality <- air_quality %>%
filter(!is.na(Date))
# Create a cleaned dataset without missing temperature or CO values
air_quality_clean <- air_quality %>%
filter(!is.na(T), !is.na(CO.GT.))
# Check the cleaned dataset dimensions
dim(air_quality_clean)
## [1] 7344 15
# Check for missing values in the two main variables
colSums(is.na(air_quality_clean[, c("T", "CO.GT.")]))
## T CO.GT.
## 0 0
The following summaries describe the temperature and carbon monoxide measurements in the cleaned dataset.
# Temperature summary
summary(air_quality_clean$T)
## Min. 1st Qu. Median Mean 3rd Qu. Max.
## -1.90 11.20 16.90 17.77 23.80 44.60
# Carbon monoxide summary
summary(air_quality_clean$CO.GT.)
## Min. 1st Qu. Median Mean 3rd Qu. Max.
## 0.10 1.10 1.80 2.13 2.80 11.90
# Average temperature
mean(air_quality_clean$T)
## [1] 17.77042
# Average carbon monoxide level
mean(air_quality_clean$CO.GT.)
## [1] 2.129711
The scatterplot below compares temperature and carbon monoxide levels. The points represent individual observations, and the blue line shows the fitted linear trend.
ggplot(air_quality_clean, aes(x = T, y = CO.GT.)) +
geom_point(alpha = 0.4) +
geom_smooth(method = "lm", color = "blue") +
labs(
title = "Temperature and Carbon Monoxide Levels",
x = "Temperature (°C)",
y = "Carbon Monoxide (CO)"
) +
theme_minimal()
## `geom_smooth()` using formula = 'y ~ x'
Pearson’s correlation measures the strength and direction of a linear relationship between two quantitative variables.
# Calculate the Pearson correlation coefficient
cor(
air_quality_clean$T,
air_quality_clean$CO.GT.
)
## [1] 0.02210912
# Perform the Pearson correlation test
correlation_test <- cor.test(
air_quality_clean$T,
air_quality_clean$CO.GT.,
method = "pearson"
)
correlation_test
##
## Pearson's product-moment correlation
##
## data: air_quality_clean$T and air_quality_clean$CO.GT.
## t = 1.8949, df = 7342, p-value = 0.05815
## alternative hypothesis: true correlation is not equal to 0
## 95 percent confidence interval:
## -0.0007627671 0.0449578939
## sample estimates:
## cor
## 0.02210912
The following commands provide additional information about the cleaned dataset and demonstrate the use of dplyr functions.
# Select the two main variables
select(air_quality_clean, T, CO.GT.) %>%
head()
## T CO.GT.
## 1 13.6 2.6
## 2 13.3 2.0
## 3 11.9 2.2
## 4 11.0 2.2
## 5 11.2 1.6
## 6 11.2 1.2
# Create a Fahrenheit temperature variable
air_quality_clean <- air_quality_clean %>%
mutate(Temperature_F = (T * 9/5) + 32)
# View the new Fahrenheit values
head(air_quality_clean$Temperature_F)
## [1] 56.48 55.94 53.42 51.80 52.16 52.16
# Summarize Fahrenheit temperatures
summary(air_quality_clean$Temperature_F)
## Min. 1st Qu. Median Mean 3rd Qu. Max.
## 28.58 52.16 62.42 63.99 74.84 112.28
# Check the updated dataset dimensions
dim(air_quality_clean)
## [1] 7344 16
The Pearson correlation test produced a correlation coefficient of approximately 0.0221 and a p-value of approximately 0.0582. Since the p-value is greater than the 0.05 significance level, the null hypothesis is not rejected.
The results do not provide sufficient statistical evidence of a significant linear relationship between temperature and carbon monoxide levels in this dataset. The scatterplot also shows no clear upward or downward pattern. Although the correlation is slightly positive, the relationship is extremely weak.
This analysis does not establish whether temperature causes changes in carbon monoxide levels. It only examines whether the two variables have a linear association in the observations analyzed.