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

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
mean(airquality$Ozone, na.rm = TRUE)
## [1] 42.12931
median(airquality$Ozone, na.rm = TRUE)
## [1] 31.5
sd(airquality$Ozone, na.rm = TRUE)
## [1] 32.98788
min(airquality$Ozone, na.rm = TRUE)
## [1] 1
max(airquality$Ozone, na.rm = TRUE)
## [1] 168
#Your code for Temp goes here
mean(airquality$Temp, na.rm = TRUE)
## [1] 77.88235
median(airquality$Temp, na.rm = TRUE)
## [1] 79
sd(airquality$Temp, na.rm = TRUE)
## [1] 9.46527
min(airquality$Temp, na.rm = TRUE)
## [1] 56
max(airquality$Temp, na.rm = TRUE)
## [1] 97
#Your code for Wind goes here
mean(airquality$Wind, na.rm = TRUE)
## [1] 9.957516
median(airquality$Wind, na.rm = TRUE)
## [1] 9.7
sd(airquality$Wind, na.rm = TRUE)
## [1] 3.523001
min(airquality$Wind, na.rm = TRUE)
## [1] 1.7
max(airquality$Wind, na.rm = TRUE)
## [1] 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 has a mean of 42.12931 and a median of 31.5. The mean is greater than the median, so the distribution is right-skewed. Standard deviation is 32.98788, indicating high variability.

Temp has a mean of 77.88235 and a median of 79. The mean is slightly less than the median, so the distribution is slightly left-skewed. Standard deviation is 9.46527, indicating low variability.

Wind has a mean of 9.957516 and a median of 9.7. The mean is very slightly less than the median, so the distribution is very slightly left-skewed or symmetrical. Standard deviation is 3.523001, indicating low variability.

Task 2: Histogram

Generate the histogram for Ozone.

#Your code goes here
ggplot(airquality, aes(x = Ozone)) +
  geom_histogram(binwidth = 2, fill = "lightblue", color = "black") +
  labs(title = "Histogram of Mean Ozone Concentration", x = "Parts Per Billion (ppb)", 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 ozone data is centered around 22-23ppb. The distribution is very skewed to the right, as many values stretch up past 100 ppb. It seems unlikely that the mean ozone level would be zero, so that could be a potential error. The highest measurement is well above 150 ppm and appears to be a potential outlier.

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"))

ggplot(airquality2, aes(x=Ozone, y=month_name)) +
  geom_boxplot()+
  labs(title = "Box of Mean Ozone Concentration by Month", x = "Parts Per Billion (ppb)", y = "Month") +
  theme_minimal()
## Warning: Removed 37 rows containing non-finite outside the scale range
## (`stat_boxplot()`).

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 appear less varied in September, May, June, July, and August. There were many outliers in September, which may indicate that some sensors in lower-ozone areas were offline that day and brought that day’s mean measurement higher. The month with the highest median ozone is July. There is a rather high outlier in the month of August, which may indicate a technical issue.

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(size = 2)
## 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.

There is a visible positive relationship between temperature and ozone levels. The warmer months such as August and July to cluster in the upper right section of the graph, while the month of may clusters in lower-Temp and lower-Ozone areas.

Task 5: Correlation Matrix

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

# Your code goes here

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

cor_matrix
##            Ozone       Temp       Wind
## Ozone  1.0000000  0.6983603 -0.6015465
## Temp   0.6983603  1.0000000 -0.5110750
## Wind  -0.6015465 -0.5110750  1.0000000
corrplot(cor_matrix, method = "color", type = "upper", order = "hclust",
         tl.col = "black", tl.srt = 45, addCoef.col = "black",
         title = "Correlation Matrix of Ozone, Temp, & Wind")

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 correlation is between Ozone and Temp with a value of 0.70. The weakest correlation is between Wind and Temp with a value of -0.51. Ozone is more correlated with Temp (0.70) than it is with Wind speed (-0.60)

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
airquality2 |>
 group_by(Month) |>
  summarise(
    Count = n(),
    mean_Ozone = mean(Ozone, na.rm = TRUE),
    mean_Wind = sd(Wind, na.rm = TRUE),
    mean_Temp = max(Temp, na.rm = TRUE)
  )
## # A tibble: 5 × 5
##   Month Count mean_Ozone mean_Wind mean_Temp
##   <int> <int>      <dbl>     <dbl>     <int>
## 1     5    31       23.6      3.53        81
## 2     6    30       29.4      3.77        93
## 3     7    31       59.1      3.04        92
## 4     8    31       60.0      3.23        97
## 5     9    30       31.4      3.46        93

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?

Month 8 (August) has the highest mean Ozone, with Month 7 (July) having a nearly identical Ozone level that is much higher than any other month. August has the highest temperature of any month and a the second lowest mean wind speed. Months 6 (June) and 9 (September) have a slightly higher temperature than Month 7, but a much faster wind speed. This suggests that a combination of high heat and low wind speed is related to a high ozone level. This could be because high winds scatter the ozone particles around the air.

Submission Requirements

Publish it to Rpubs and submit your link on blackboard