-
Notifications
You must be signed in to change notification settings - Fork 0
/
Lab5_CarissaFeliciano.qmd
263 lines (221 loc) · 6.87 KB
/
Lab5_CarissaFeliciano.qmd
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
---
title: "Lab 5"
author: "Carissa Feliciano"
format: html
embed-resources: true
---
# Prepare the data
```{r}
library(data.table)
library(dtplyr)
library(dplyr)
library(DT)
#Load the data
if (!file.exists(file.path("~", "Downloads", "met_all.gz")))
download.file(
url = "https://raw.githubusercontent.com/USCbiostats/data-science-data/master/02_met/met_all.gz",
destfile = file.path("~", "Downloads", "met_all.gz"),
method = "libcurl",
timeout = 60
)
met <- fread(file.path("~", "Downloads", "met_all.gz"))
met <- as.data.frame(met)
if (!file.exists(file.path("~", "Downloads", "stations.gz")))
download.file(
url = "https://noaa-isd-pds.s3.amazonaws.com/isd-history.csv",
destfile = file.path("~", "Downloads", "stations.gz"),
method = "libcurl",
timeout = 60
)
stations <- fread(file.path("~", "Downloads", "stations.gz"))
stations <- as.data.frame(stations)
stations$USAF <- as.integer(stations$USAF)
```
```{r}
# Dealing with NAs and 999999s
stations$USAF[stations$USAF == 999999] <- NA
stations$CTRY[stations$CTRY == ""] <- NA
stations$STATE[stations$STATE == ""] <- NA
```
```{r}
# Selecting the three relevant columns and keeping unique records
stations <- unique(stations[, c('USAF', 'CTRY', 'STATE')])
```
```{r}
# Dropping the NAs
stations <- stations[!is.na('USAF'), ]
```
```{r}
# Removing duplicates
stations <- stations[!duplicated(stations$USAF), ]
```
```{r}
# Merge the data
met <- merge(
x = met,
y = stations,
by.x = "USAFID",
by.y = "USAF",
all.x = TRUE,
all.y = FALSE
)
```
# 1. Representative station for the US
```{r}
# Calculate medians
median_temp <- quantile(met$temp, probs = 0.5, na.rm = TRUE)
median_wind <- quantile(met$wind.sp, probs = 0.5, na.rm = TRUE)
median_atm <- quantile(met$atm.press, probs = 0.5, na.rm = TRUE)
median_temp
median_wind
median_atm
```
```{r}
# Determine the three weather stations that best represent the continental US
met$dist_temp <- abs(met$temp - median_temp)
met$dist_wind <- abs(met$wind.sp - median_wind)
met$dist_atm <- abs(met$atm.press - median_atm)
met$dist_all <- met$dist_temp + met$dist_wind + met$dist_atm
met[order(met$dist_all), ][1:3, ]
```
The three weather stations that best represent the continental US are stations 722246 (FL), 722390 (LA), and 723066 (NC). These three stations are all located in different states.
```{r}
met <- met |> select (-dist_temp, -dist_wind, -dist_atm, -dist_all)
```
#2. Identify the most representative station per state.
```{r}
# Calculate the median for each variable by state
medians <- met |>
group_by(STATE) |>
summarize(
med_temp = median(met$temp, na.rm = TRUE),
med_wind = median(met$wind.sp, na.rm = TRUE),
med_atm = median(met$atm.press, na.rm = TRUE)
)
#Add the medians to the dataset
met_medians <- met |>
left_join(medians, by = "STATE")
# Calculate the euclidean distance from the median values
met_medians <- met_medians |>
mutate(
euc = sqrt(
(temp - med_temp)^2 +
(wind.sp - med_wind)^2 +
(atm.press - med_atm)^2
)
)
```
```{r}
# Find the stations closest to the median per state
median_stations <- met_medians |>
group_by(STATE) |>
filter(euc == min(euc, na.rm = TRUE)) |>
slice(which.min(lat)) |>
ungroup() |>
select(USAFID, STATE, temp, wind.sp, atm.press, med_temp, med_wind, med_atm, lat, lon)
nrow(median_stations)
datatable(median_stations, options = list(scrollX = TRUE, pageLength = 10))
```
The representative stations per state are displayed in the table above. Representative stations are missing for ND and WA because there are no stations in these states that have values recorded for temperature, wind speed, and atmospheric pressure.
# 3. Identify the station closest to the mid-point of the state.
Determine the mid-point for each state.
```{r}
library(dplyr)
library(ggplot2)
# Get the map data for US states
states <- map_data("state")
# Calculate the mid-point for each state
midpoints <- states |>
group_by(region) |>
summarize(
mid_lon = mean(long, na.rm = TRUE),
mid_lat = mean(lat, na.rm = TRUE)
)
# Replace region column with lowercase state names with abbreviations
state_midpoints <- midpoints |>
mutate(STATE = state.abb[match(region, tolower(state.name))])
state_midpoints <- state_midpoints |>
select(-region)
# Merge midpoint data with met dataset
met_midpoints <- merge(met, state_midpoints, by = "STATE")
```
Identify the station closest to the mid-point for each state.
```{r}
# Calculate the distance from the stations to the mid-point
library(geosphere)
met_midpoints <- met_midpoints |>
mutate(dist = distHaversine(cbind(lon,lat), cbind(mid_lon, mid_lat)))
```
```{r}
midpoint_stations <- met_midpoints |>
group_by(STATE) |>
filter(dist == min(dist, na.rm = TRUE)) |>
ungroup() |>
distinct(USAFID, .keep_all = TRUE) |>
select(STATE, USAFID, lat, lon, mid_lon, mid_lat, dist)
nrow(midpoint_stations)
datatable(midpoint_stations, options = list(scrollX = TRUE, pageLength = 10))
```
Create a leaflet map of all points
```{r}
# Add label column to median stations dataset
median_stations$category <- "Median"
# Add label column to midpoint stations dataset
midpoint_stations$category <- "Mid-point"
# Merge columns
all_stations <- bind_rows(
median_stations |> select(STATE, USAFID, lat, lon, category),
midpoint_stations |> select(STATE, USAFID, lat, lon, category)
)
nrow(all_stations)
```
```{r}
#Plot leaflet map
library(leaflet)
pal <- colorFactor(c("blue", "orange"), domain = c("Median", "Mid-point"))
leaflet(all_stations) |>
addTiles() |>
addCircleMarkers(
lng = ~lon,
lat = ~lat,
color = ~pal(category),
stroke = FALSE,
opacity = 0.7, fillOpacity = 0.7, radius = 5,
popup = ~paste("Station ID:", USAFID, "<br> Category:", category)
) |>
addLegend("bottomleft",
pal = pal,
values = ~category,
title = "Category",
opacity = 1)
```
# 4. Means of means
```{r}
# Compute the states' average temperature
met <- met |>
group_by(STATE) |>
mutate(
mean_temp = mean(temp, na.rm = TRUE)) |>
ungroup()
# Use the state avg temp to classify them
met$temp_cat <- rep(NA, nrow(met))
met$temp_cat[met$mean_temp <20] <- 'low'
met$temp_cat[met$mean_temp >= 20 & met$mean_temp <25] <- 'mid'
met$temp_cat[met$mean_temp >= 25] <- 'high'
```
```{r}
summary_table <- met |>
group_by(temp_cat) |>
summarize(
`Number of entries` = n(),
`Number of NA entries` = sum(is.na(temp) | is.na(wind.sp) | is.na(atm.press)),
`Number of stations` = n_distinct(USAFID),
`Number of states` = n_distinct(STATE),
`Mean temp` = mean(temp, na.rm = TRUE),
`Mean wind speed` = mean(wind.sp, na.rm = TRUE),
`Mean atm pressure` = mean(atm.press, na.rm = TRUE)
) |>
mutate(temp_cat = factor(temp_cat, levels = c("low", "mid", "high"))) |>
arrange(temp_cat)
datatable(summary_table, options = list(scrollX = TRUE, pageLength = 3))
```