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.2     ✔ tibble    3.3.1
## ✔ lubridate 1.9.5     ✔ tidyr     1.3.2
## ✔ purrr     1.2.1     
## ── 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
library(ggplot2)

data("airquality")

Tasks and Questions

Task 1: Measures of Central Tendency and Spread

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_Ozone = mean(Ozone, na.rm = TRUE),
    median_Ozone = median(Ozone, na.rm = TRUE),
    sd_Ozone = sd(Ozone, na.rm = TRUE),
    min_Ozone = min(Ozone, na.rm = TRUE),
    max_Ozone = max(Ozone, na.rm = TRUE)
  )

Ozone_Stats
##   mean_Ozone median_Ozone sd_Ozone min_Ozone max_Ozone
## 1   42.12931         31.5 32.98788         1       168
#Your code for Temp goes here

Temp_Stats <- airquality |>
  summarise(
    mean_Temp = mean(Temp, na.rm = TRUE),
    median_Temp = median(Temp, na.rm = TRUE),
    sd_Temp = sd(Temp, na.rm = TRUE),
    min_Temp = min(Temp, na.rm = TRUE),
    max_Temp = max(Temp, na.rm = TRUE)
  )

Temp_Stats
##   mean_Temp median_Temp sd_Temp min_Temp max_Temp
## 1  77.88235          79 9.46527       56       97
#Your code for Wind goes here

Wind_Stats <- airquality |>
  summarise(
    mean_Wind = mean(Wind, na.rm = TRUE),
    median_Wind = median(Wind, na.rm = TRUE),
    sd_Wind = sd(Wind, na.rm = TRUE),
    min_Wind = min(Wind, na.rm = TRUE),
    max_Wind = max(Wind, na.rm = TRUE)
  )

Wind_Stats
##   mean_Wind median_Wind  sd_Wind min_Wind max_Wind
## 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?

Ozone: mean (42.12) is greater than median (31.5) which means the data will be skewed to the right. The standard deviation (32.98) is very large which indicate a lot of variability.

Temp: mean (77.88) is less than the median (79) which means the data will be skewed slightly to the left. The standard deviation (9.46) is not to big which indicates there won’t be much variability.

Wind: mean (9.95) is slightly greater than the median (9.7) which means there will be a slight skew to the right. The standard deviation (3.52) is very small which indicates very little variability.

Task 2: Histogram

Generate the histogram for Ozone.

#Your code goes here

ggplot(airquality, aes(x = Ozone)) +
  geom_histogram(binwidth = 20, fill = "#1f77b4", color = "black") +
  labs(title = "Histogram of Ozone Levels", x = "Ozone", y = "Count") +
  theme_minimal()
## Warning: Removed 37 rows containing non-finite outside the scale range
## (`stat_bin()`).

Question: Describe the shape of the ozone distribution (e.g., normal, skewed, unimodal). Are there any outliers or unusual features?

The distribution is skewed to the right with outliers at the high end of the values.

Task 3: Boxplot

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

airquality2 <- airquality |>
  mutate(month_name = case_when(
    Month == 5 ~ "May",
    Month == 6 ~ "June",
    Month == 7 ~ "July",
    Month == 8 ~ "August",
    Month == 9 ~ "September"))



boxplot(Ozone ~ month_name, data = airquality2,
        ylab = "Ozone", xlab = "Month")

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?

Ozone levels start low in May and June then go to their highest in July and august and then come back down in September. The month with the highest median Ozone levels is July. There are some outliers in May, June and September. Which may indicate changes in temperature higher than the average for the month.

Task 4: Scatterplot

Produce the scatterplot of Temp vs. Ozone, colored by Month.

# Your code goes here

ggplot(airquality2, aes(x = Temp, y = Ozone, color = month_name)) +
  geom_point(alpha = 0.7) +
  labs(
    title = "Scatterplot of Temperature vs. Ozone",
    x = "Temp",
    y = "Ozone",
    color = "Month"
  ) +
  scale_color_manual(values = c("May" = "#2ca02c",  # green
                                "June" = "#FF4040",
                                "July" = "Blue",
                                "August" = "Yellow",
                                "September" = "Orange")) +  # red
  theme_minimal()
## Warning: Removed 37 rows containing missing values or values outside the scale range
## (`geom_point()`).

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.

Yes, months with higher temperatures tend to have higher levels of ozone according to this graph.

Task 5: Correlation Matrix

Compute and visualize the correlation matrix for Ozone, Temp, and Wind.

# Your code goes here

Weather_Correlation <- cor(
  airquality |>
    select(Ozone, Temp, Wind), use = "complete.obs")

Weather_Correlation
##            Ozone       Temp       Wind
## Ozone  1.0000000  0.6983603 -0.6015465
## Temp   0.6983603  1.0000000 -0.5110750
## Wind  -0.6015465 -0.5110750  1.0000000

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 strongest positive correlations are between ozone and temperature and the strongest negative correlations are between wind and ozone and and temperature and wind.

Task 6: Summary Table

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

Weather_summary <- airquality |>
  group_by(Month) |>
  summarise(
    count= n(),
    Average_Ozone = mean(Ozone, na.rm=TRUE),
    Average_Temp = mean(Temp, na.rm=TRUE),
    Average_Wind = mean(Wind, na.rm=TRUE)
  )

Weather_summary
## # A tibble: 5 × 5
##   Month count Average_Ozone Average_Temp Average_Wind
##   <int> <int>         <dbl>        <dbl>        <dbl>
## 1     5    31          23.6         65.5        11.6 
## 2     6    30          29.4         79.1        10.3 
## 3     7    31          59.1         83.9         8.94
## 4     8    31          60.0         84.0         8.79
## 5     9    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?

August has the highest average Ozone levels, It also has the highest average temperature and the lowest average wind speed. These two likely affect the levels of Ozone of the different months, especially wind speed which according to the table above is lowest when the Ozone levels are high and highest when Ozone levels are low. These differences may be explained by the changes in the season which occur throughout the year.

Submission Requirements

Publish it to Rpubs and submit your link on blackboard