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:

data("airquality")

Task 1: Measures of Central Tendency and Spread

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

Answer: The Ozone mean is about 42.13 and the median is 31.5, so the mean is higher. This suggests the ozone data is right-skewed. The Temp mean is about 77.88 and the median is 79, so they are close. The Wind mean is about 9.96 and the median is 9.7, so they are also close. Ozone has the largest standard deviation, which means ozone levels vary the most.

Task 2: Histogram

hist(
  airquality$Ozone,
  main = "Histogram of Ozone",
  xlab = "Ozone"
)

Answer: The ozone distribution is right-skewed. Most ozone values are lower, but there are a few very high values that stretch the distribution to the right.

Task 3: Boxplot

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

airquality$month_name <- factor(
  airquality$month_name,
  levels = c("May", "June", "July", "August", "September")
)

ggplot(airquality, aes(x = month_name, y = Ozone)) +
  geom_boxplot() +
  labs(
    title = "Ozone Levels by Month",
    x = "Month",
    y = "Ozone"
  )
## Warning: Removed 37 rows containing non-finite outside the scale range
## (`stat_boxplot()`).

Answer: Ozone levels are lower in May and June and increase in July and August. August has the highest median ozone level. There are also some unusually high ozone values in some months.

Task 4: Scatterplot

ggplot(airquality, aes(x = Temp, y = Ozone, color = factor(Month))) +
  geom_point(na.rm = TRUE) +
  labs(
    title = "Temperature vs. Ozone",
    x = "Temperature",
    y = "Ozone",
    color = "Month"
  )

Answer: There is a positive relationship between temperature and ozone. As temperature increases, ozone levels generally increase. July and August contain many of the warmer days and higher ozone values.

Task 5: Correlation Matrix

cor_data <- airquality |>
  select(Ozone, Temp, Wind)

cor_matrix <- cor(cor_data, 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", addCoef.col = "black")

Answer: The strongest correlation is between Ozone and Temp at about 0.70. Ozone and Wind have a negative correlation of about -0.60. This means ozone is more strongly related to temperature than wind speed.

Task 6: Summary Table

summary_table <- airquality |>
  group_by(Month) |>
  summarise(
    count = n(),
    avg_ozone = mean(Ozone, na.rm = TRUE),
    avg_temp = mean(Temp, na.rm = TRUE),
    avg_wind = mean(Wind, na.rm = TRUE)
  )

summary_table
## # A tibble: 5 × 5
##   Month count avg_ozone avg_temp avg_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

Answer: August has the highest average ozone level, followed closely by July. July and August also have higher average temperatures and lower average wind speeds. These results suggest that hotter temperatures and calmer winds may be related to higher ozone levels.

Submission Requirements

Publish it to RPubs and submit your link on Blackboard.