Objective:
You will use R to analyze the built-in airquality dataset, applying descriptive statistics techniques to explore environmental data. The assignment covers measures of central tendency, spread, histograms, boxplots, scatterplots, correlations, and summary tables, aligning with the Week 6 agenda on Descriptive Statistics.
Dataset
Source: Built-in R dataset airquality.
Description: Contains 153 observations of daily air quality measurements in New York from May to September 1973.
Variables (selected for this assignment):
Notes
-The airquality dataset has missing values in Ozone and Solar.R. The code uses na.rm = TRUE or use = “complete.obs” to handle them.
-If you encounter errors, check that tidyverse and corrplot are installed and loaded.
-Feel free to modify plot aesthetics (e.g., colors, binwidth) to enhance clarity.
Instructions:
Complete the following tasks using R to analyze the airquality dataset. Submit your Rpubs link that includes code, outputs (tables and plots), and written interpretations for each task. Ensure you load the dataset using data(airquality) and install/load the tidyverse and corrplot packages.
#Load your dataset
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
library(corrplot)
## corrplot 0.95 loaded
data("airquality")
Tasks and Questions
Using functions you learned this week, compute mean, median, standard deviation, min, and max separately for Ozone, Temp, and Wind.
#Your code for Ozone goes here
ozone_stats <- airquality |>
summarise(
mean = mean(Ozone, na.rm = TRUE),
median = median(Ozone, na.rm = TRUE),
standard_deviation = sd(Ozone, na.rm = TRUE),
minimum = min(Ozone, na.rm = TRUE),
maximum = max(Ozone, na.rm = TRUE)
)
ozone_stats
## mean median standard_deviation minimum maximum
## 1 42.12931 31.5 32.98788 1 168
#Your code for Temp goes here
temp_stats <- airquality |>
summarise(
mean = mean(Temp, na.rm = TRUE),
median = median(Temp, na.rm = TRUE),
standard_deviation = sd(Temp, na.rm = TRUE),
minimum = min(Temp, na.rm = TRUE),
maximum = max(Temp, na.rm = TRUE)
)
temp_stats
## mean median standard_deviation minimum maximum
## 1 77.88235 79 9.46527 56 97
#Your code for Wind goes here
wind_stats <- airquality |>
summarise(
mean = mean(Wind, na.rm = TRUE),
median = median(Wind, na.rm = TRUE),
standard_deviation = sd(Wind, na.rm = TRUE),
minimum = min(Wind, na.rm = TRUE),
maximum = max(Wind, na.rm = TRUE)
)
wind_stats
## mean median standard_deviation minimum maximum
## 1 9.957516 9.7 3.523001 1.7 20.7
Question: Compare the mean and median for each variable. Are they similar or different, and what does this suggest about the distribution (e.g., skewness)? What does the standard deviation indicate about variability?
Mean vs Median: In terms of ozone levels, the mean is clearly higher than the median, implying right-skewness. With regards to the temperature and winds, the difference between the mean and median is minimal, meaning that the distributions are fairly symmetrical.
Standard Deviation: From the standard deviation figures, we can see that the ozone levels are highly dispersed when compared to temperatures and winds because of its tail of high concentrations.
Generate the histogram for Ozone.
#Your code goes here
ggplot(airquality, aes(x = Ozone)) +
geom_histogram(binwidth = 10, fill = "steelblue", color = "white", na.rm = TRUE) +
labs(title = "Histogram of Daily Ozone Levels",
x = "Ozone concentration (ppb)",
y = "Number of days") + theme_minimal()
Question: Describe the shape of the ozone distribution (e.g., normal, skewed, unimodal). Are there any outliers or unusual features?
The distribution for ozone levels is characterized by being unimodal and right-skewed, such that more days exhibit low or moderate amounts of ozone, but some days exhibit high amounts of ozone. The high values on the right are the outliers.
Create a boxplot of ozone levels (Ozone) by month, with months
displayed as names (May, June, July, August, September) instead of
numbers (5–9).Recode the Month variable into a new column called
month_name with month names using case_when
from week 4.Generate a boxplot of Ozone by month_name.
# Your code here
airquality_months <- airquality |>
mutate(month_name = case_when(
Month == 5 ~ "May",
Month == 6 ~ "June",
Month == 7 ~ "July",
Month == 8 ~ "August",
Month == 9 ~ "September",
TRUE ~ NA_character_ ), month_name = factor(month_name,
levels = c("May", "June", "July", "August", "September")))
ggplot(airquality_months, aes(x = month_name, y = Ozone)) +
geom_boxplot(fill = "orange", na.rm = TRUE) +
labs(title = "Ozone Levels by Month",
x = "Month",
y = "Ozone concentration (ppb)") +
theme_minimal()
Question: How do ozone levels vary across months? Which month has the highest median ozone? Are there outliers in any month, and what might they indicate?
Higher ozone values are more common during warmer mid-season months like July and August while lower ozone values are common during May and September. July is likely to have the highest median for any month within the data set. Points that lie outside the feeler can be considered outliers because they show values for days with either very high or very low ozone values.
Produce the scatterplot of Temp vs. Ozone, colored by Month.
# Your code goes here
ggplot(airquality_months, aes(x = Temp, y = Ozone, color = month_name)) +
geom_point(alpha = 0.8, na.rm = TRUE) +
labs(
title = "Scatterplot of Temp vs. Ozone by Month",
x = "Temperature (°F)",
y = "Ozone concentration (ppb)",
color = "Month") +
theme_minimal()
Question: Is there a visible relationship between temperature and ozone levels? Do certain months cluster together (e.g., higher ozone in warmer months)? Describe any patterns.
The scatterplot shows a positive relationship between temperature and ozone, with warmer summer months (July–August) clustering at higher ozone levels. However, temperature alone doesn’t account for all a change.
Compute and visualize the correlation matrix for Ozone, Temp, and Wind.
# Your code goes here
cor_matrix <- airquality |>
select(Ozone, Temp, Wind) |>
cor(use = "complete.obs")
round(cor_matrix, 3)
## Ozone Temp Wind
## Ozone 1.000 0.698 -0.602
## Temp 0.698 1.000 -0.511
## Wind -0.602 -0.511 1.000
corrplot(
cor_matrix,
method = "color",
type = "upper",
addCoef.col = "black",
tl.col = "black",
tl.srt = 45,
title = "Correlation Matrix: Ozone, Temperature, and Wind",
mar = c(0, 0, 2, 0))
Question: Identify the strongest and weakest correlations. For example, is ozone more strongly correlated with temperature or wind speed? Explain what the correlation values suggest about relationships between variables.
The correlation between Ozone and Temperature is moderate-strong positive correlation (≈ 0.70), whereas correlation between Ozone and Wind is a negative correlation (≈ -0.60). Correlation between Temperature and Wind is the lowest, which is (≈ -0.51). The correlation between Ozone and Temperature is the highest.
Generate the summary table grouped by Month.Generate the summary table grouped by Month. It should include count, average mean of ozone, average mean of temperature, and average mean of wind per month.
# your code goes here
monthly_summary <- airquality_months |>
group_by(month_name) |>
summarise(
count = n(),
mean_ozone = mean(Ozone, na.rm = TRUE),
mean_temperature = mean(Temp, na.rm = TRUE),
mean_wind = mean(Wind, na.rm = TRUE),
.groups = "drop"
) |>
mutate(
mean_ozone = round(mean_ozone, 2),
mean_temperature = round(mean_temperature, 2),
mean_wind = round(mean_wind, 2)
)
monthly_summary
## # A tibble: 5 × 5
## month_name count mean_ozone mean_temperature mean_wind
## <fct> <int> <dbl> <dbl> <dbl>
## 1 May 31 23.6 65.6 11.6
## 2 June 30 29.4 79.1 10.3
## 3 July 31 59.1 83.9 8.94
## 4 August 31 60.0 84.0 8.79
## 5 September 30 31.4 76.9 10.2
Question: Which month has the highest average ozone level? How do temperature and wind speed vary across months? What environmental factors might explain these differences?
The month of August has the highest average ozone concentration (59.96), followed by July (59.12). High temperatures are recorded in these months, but the speed of the wind is low. On the other hand, cold months such as May have low levels of ozone and high wind speeds. High temperatures lead to ozone production.
Submission Requirements
Publish it to Rpubs and submit your link on blackboard