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
library(dplyr)
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.

# Load air quality data
#code for Ozone statistics
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)
## Warning in max(airquality$ozone, na.rm = TRUE): no non-missing arguments to
## max; returning -Inf
## [1] -Inf
#Your code for Temp goes here
#code for Temp statistics
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
#code for wind statistics
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?

Task 2: Histogram

Generate the histogram for Ozone.

#Your code goes here
#To generate histogram for ozone
hist(
     na.omit(airquality$Ozone),
     main = "Histrogram for Ozone level" , 
     xlab = "Ozone",
     ylab = "Frequency" ,
     col  = "lightblue" ,
     border = "black"
     )

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

Response: The Ozone distribution is unimodal and positively skewed.

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.

# Mutate/Define the months 
airquality <- airquality %>%
 mutate(
  month_name = case_when(
   Month == 5 ~ "May",
   Month == 6 ~ "June",
   Month == 7 ~ "July",
   Month == 8 ~ "August",
   Month == 9 ~ "September" 
  )
  )

# Creating boxplot of Ozone by month
ggplot(airquality, aes(x = month_name, y = Ozone)) +
  geom_boxplot(fill = "lightblue", color = "black")
## Warning: Removed 37 rows containing non-finite outside the scale range
## (`stat_boxplot()`).

  labs(
    title = "Boxplot of Ozone Levels by Month", 
    X = "Month"  ,
    Y = "Ozone"
  )+
theme_minimal()
## NULL

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?

Response: Ozone levels are generally lower in May and June. July and August is highest. Declines in September. Ozone occure high during peak summer.

Task 4: Scatterplot

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

# coding for Scatterplot of Temperature vs Ozone by month
ggplot(airquality, aes(x = Temp, y = Ozone, color = factor(Month))) +
 geom_point(size = 3) + 
   labs(
     title = "Scatter Plot of Temp vs Ozone" ,
        x = "Temperature", 
        y = "Ozone" , 
     color = "Month"
   )+
   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.

Response: Yes. It clearly indicates there is a correlation that when temperature increases then ozone concentration also increases. Trends are upwards.

Task 5: Correlation Matrix

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

# Your code goes here
cor_matrix <- cor(
  airquality[, c("Ozone", "Temp", "Wind")],
  use = "complete.obs"
)
#Display correlation matrix
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

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.

Response: strongest is 0.6983603 and Weakest is -0.6015465

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

monthly_summary <- airquality %>%
  group_by(Month) %>%
  summarise(
    count = n() ,
    mean_ozone = mean(Ozone, na.rm = TRUE) ,
    mean.temp = mean(Temp, na.rm = TRUE),
    mean_wind = mean(Wind, na.rm = TRUE)
  )

monthly_summary
## # A tibble: 5 × 5
##   Month count mean_ozone mean.temp mean_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?

Response: August month has the highest i.e. 59.96154. From this it indicates the temperature increases may thru july and very high in August and decreases in September.

Submission Requirements

Publish it to Rpubs and submit your link on blackboard