library(dplyr)
library(patchwork)
library(ggplot2)
library(tidyverse)
library(readr)
library(forcats)
library(DT)
library(plotly)
weather_forecasts <- read_csv("~/proj2-BellaT654/data/weather_forecasts.csv")
forecast_cities <- read_csv("~/proj2-BellaT654/data/forecast_cities.csv")
The data used in this project is from the National Weather Service, which includes a 16-month forecast from 167 cities in the United States. The data sets specifically are weather_forecasts.csv and forecast_cities.csv. Weather forecasts include data about a state’s city weather forecasts. In the data wrangling, I focused on city, state, and high_or_low (whether the forecast is the high or low temp). In the forecast cities data set the observations offer more detailed information about the each city, such as state, city, longitude, and latitude, the elevation (meters), the distance the city is from a coast (miles), wind (mean speed), and the annual average precipitation (inches).
Within the data, finding the average difference between the forecast temperature and observed temperatures from the weather forecasts (which I named forecast_error) and grouping by city and state results in the average temperature errors that are either high or low. Following these calculations and looking at the mutated data set, an error has occurred in various cities with the same averages for the calculated temperature for high and low. This is a quality issue for the National Weather Services data, in three different states (CA, RI, and VA) all have a city named Richmond, with general geographical knowledge these states are not very close and have various conditions such as how far away they are to a coast and their elevation differences, which can be factors contributing to temperature variance.
Plotting the data offers insight into the trends and environmental factors impacting the error margin size of high and low temperatures. Among elevation and annual precipitation observations, plotting on a graph reflects a relationship among these types of observations and the average error margin for high and low temperatures. A downward sloping relationship exists between the annual rainfall and the forecasting error for high and low temperatures. As the yearly precipitation increases, the margin of error decreases in size, which means more accuracy in temperature predictions. From this data, there are cities with more wet climates annually, resulting in lower forecasting temperature errors. For elevation, there is a positive slope correlation. In cities with elevations between 0 and 250 meters, as the elevation increases within this range, there is a relationship between the margin of error increasing for both low and high temperatures. This suggests that higher elevations pose more challenges for forecasting high and low temperatures.
Among the cities that have similar margin of error calculations for high and low temperatures (NY, WY, RI, CA), the environmental observations, such as precipitation and elevation, differ, which is another clue to the data quality issues.
#adding a column in weather_forecasts with the abs value of observed temp and forecast temp
weather_forecasts <- weather_forecasts |>
mutate(
tempurature_error = abs(observed_temp - forecast_temp)
)
# pivot the data set to focus on high_or_low temps and errors, this also adds a new colum seperating between high and low temp errors
pivot_weather_forecasts <- weather_forecasts |>
pivot_wider(names_from = high_or_low,
values_from = tempurature_error, names_prefix = "error-")
# Grouping by the city and state to find the mean high and low errors
city_temp_errors <- pivot_weather_forecasts |>
group_by(city, state) |>
summarize(
avg_error_high = mean(`error-high`, na.rm = TRUE),
avg_error_low = mean(`error-low`, na.rm = TRUE)
)
# left joining the forecast cities data set with the city temp errors for comparison
joint_df <- forecast_cities|>
left_join(city_temp_errors) |>
drop_na(c("avg_error_high", "avg_error_low"))
# Map plot of the cities with the average errors of high temps
states <- map_data("state")
state_map <- ggplot(states) +
geom_polygon(aes(x=long, y=lat, group=group), color="black", alpha = 0.5, fill="lightgray") +
geom_point(data = joint_df,mapping = aes(x=lon, y = lat, color = avg_error_high, text = city)) +
labs(x = "Longitude",
y = "Latitude",
title = "Average Error of High Tempuratures") +
theme_minimal() +
scale_color_viridis_c()
ggplotly(state_map) #map it the state/county
# Map plot of the cities with the average errors of low temps
states <- map_data("state")
low_states <- ggplot(states) +
geom_polygon(aes(x=long, y=lat, group=group), color= "black", fill= "lightgrey", alpha = 0.5) +
geom_point(data = joint_df,mapping = aes(x=lon, y = lat, color = avg_error_low, text = city)) +
labs(x = "Longitude",
y = "Latitude",
title = "Average Error of Low Tempuratures") +
theme_minimal() +
scale_color_viridis_c()
ggplotly(low_states)
p1 <- ggplot(joint_df,
aes(x = distance_to_coast, y = avg_error_low)) +
geom_point(alpha = 0.8, color = "darkgreen") +
geom_smooth(method = "lm", se = FALSE, color = "gold") +
labs(x = "Distance To Coast (mile)",
y = "Average Low Tempurature Error",
title = "Distance To Coasts effects On Average Error of Low Tempuratures") +
theme_minimal()
p2 <- ggplot(joint_df,
aes(x = distance_to_coast, y = avg_error_high)) +
geom_point(alpha = 0.8, color = "darkgreen") +
geom_smooth(method = "lm", se = FALSE, color = "gold") +
labs(x = "Distance To Coast (mile)",
y = "Average High Tempurature Error",
title = "Distance To Coasts effects On Average Error of High Tempuratures") +
theme_minimal()
p3 <- ggplot(joint_df, aes(x = avg_annual_precip, y = avg_error_high)) +
geom_point(alpha = 0.8, color = "blue") +
geom_smooth(method = "lm", se = FALSE, color = "orange") +
labs(x = "Average Annual Precipitation (inch)",
y = "Average High Tempurature Error",
title = "Effect Of Annual Precipitation On High Tempuratures Forecast Error") +
theme_minimal()
p4 <- ggplot(joint_df, aes(x = avg_annual_precip, y = avg_error_low)) +
geom_point(alpha = 0.8, color = "blue") +
geom_smooth(method = "lm", se = FALSE, color = "orange") +
labs(x = "Average Annual Precipitation (inch)",
y = "Average Low Tempurature Error",
title = "Effect Of Annual Precipitation On Low Tempuratures Forecast Error") +
theme_minimal()
p5 <- ggplot(joint_df, aes(x = wind, y = avg_error_low)) +
geom_point(alpha = 0.9, color = "darkorchid2") +
geom_smooth(method = "lm", se = FALSE, color = "cyan4")+
labs(x = "Mean Wind Speed (mph)",
y = "Average Low Tempurature Error",
title = "Effect Of Mean Wind Speed On Low Tempuratures Forecast Error") +
theme_minimal()
p6 <- ggplot(joint_df, aes(x = wind, y = avg_error_high)) +
geom_point(alpha = 0.9, color = "darkorchid2") +
geom_smooth(method = "lm", se = FALSE, color = "cyan4")+
labs(x = "Mean Wind Speed (mph)",
y = "Average High Tempurature Error",
title = "Effect Of Mean Wind Speed On High Tempuratures Forecast Error") +
theme_minimal()
p7 <- ggplot(joint_df, aes(x = elevation, y = avg_error_high)) +
geom_point(alpha = 0.8, color = "blueviolet") +
geom_smooth(method = "lm", se = FALSE, color = "aquamarine4")+
labs(x = "Elevation (meters)",
y = "Average High Tempurature Error",
title = "Effect Of Elevation On High Tempuratures Forecast Error") +
theme_minimal()
p8 <- ggplot(joint_df, aes(x = elevation, y = avg_error_low)) +
geom_point(alpha = 0.8, color = "blueviolet") +
geom_smooth(method = "lm", se = FALSE, color = "aquamarine4") +
labs(x = "Elevation (meters)",
y = "Average Low Tempurature Error",
title = "Effect Of Elevation On Low Tempuratures Forecast Error") +
theme_minimal()
(p1+p2)
(p3+p4)
(p5+p6)
(p7+p8)
data_table <- datatable(joint_df,
rownames = FALSE)
data_table