-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathairAnom.Rmd
285 lines (206 loc) · 8.09 KB
/
airAnom.Rmd
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
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
---
title: "Air Temperature mblm"
author: "Andrew Cameron"
date: "2024-07-09"
output: html_document
---
````{r setup, include=FALSE}
library(tidyverse)
## have pulled in NOAA monthly average air temperatures for the 4 climate regions that contain the majority of our reservoirs. Could you run mblm regressions on the air temperature anomalies (data in attached file)?
## The mblms should be run by month, which would yield a total of 48 regressions across the 4 divisions.
## I don't think I'll need a figure for this as our paper already has 11 figures (I usually aim for 7-10), but hopefully I can describe the results in a paragraph of text.
```
```{r `Accessing historical NOAA temp data`}
downloaded_file <- "climdiv-tmaxdv-v1.0.0-20240705.txt" # file path for downloaded file
url <- "https://www.ncei.noaa.gov/data/nclimdiv-monthly/access/climdiv-tmpcdv-v1.0.0-20240705"
# Download the data
download.file(url, destfile = downloaded_file, method = "auto")
# Read into R
data <- read.table(downloaded_file, header = FALSE, sep = "", stringsAsFactors = FALSE)
# Clean up the data, filter for Virginia divisions 2, 3, 5,6
colnames(data) <- c("ID", month.abb)
data$Year <- as.numeric(substr(data$ID, 7,10))
VA_only <- data %>%
filter(substr(ID, 1, 4) %in% c("4402", "4403", "4405", "4406")) %>%
filter(Year >= 1970 & Year <=2023) %>%
mutate(division = case_when(
substr(ID, 1, 4) == "4402" ~ "VADiv2",
substr(ID, 1, 4) == "4403" ~ "VADiv3",
substr(ID, 1, 4) == "4405" ~ "VADiv5",
substr(ID, 1, 4) == "4406" ~ "VADiv6",
TRUE ~ NA_character_ # default to NA of type char if no cases match
)) %>%
mutate(across(2:13, ~ (. - 32) * 5 / 9))
long_data <- VA_only %>%
pivot_longer(cols = 2:13, names_to = "Month", values_to = "Temp")
# Calculate long term monthly averages within divisions and create a "longTermAvg" column and populate it with the appropriate month/division mean temp value
q <- long_data %>%
left_join(long_data %>%
group_by(division, Month) %>%
summarize(longTermAvg = mean(Temp, na.rm = TRUE)) %>%
select(division, Month, longTermAvg),
by = c("division", "Month"))
# Calculate temp anomalies for each month-year
df.final <- q %>%
mutate(TempAnom = Temp - longTermAvg)
write.csv(df.final, "input_AirTempData_1970_2023.csv")
```
```{r}
```
```{r}
air.data <- openxlsx::read.xlsx("air_data.xlsx", sheet = 2)
air.data %>%
group_by(Month) %>%
summarize(n())
## 21 obs per regression
```
```{r `fit regressions for each month-region`}
library(mblm)
#Usage
#mblm(formula, dataframe, repeated = TRUE)
#Arguments
#formula A formula of type y ~ x (only linear models are accepted)
#dataframe Optional dataframe
#repeated If set to true, model is computed using repeated medians. If false, a single median estimators are calculated
# create list to store regression results
div2_airAnom <- list()
div3_airAnom <- list()
div5_airAnom <- list()
div6_airAnom <- list()
allDiv_airAnom <- list()
# remove NA values -- mblm() does not handle NA as lm() does
div2 <- df.final %>%
filter(division == "VADiv2") %>%
filter(!is.na(TempAnom)) # East Piedmont
div3 <- df.final %>%
filter(division == "VADiv3") %>%
filter(!is.na(TempAnom)) # West Piedmont
div5 <- df.final %>%
filter(division == "VADiv5") %>%
filter(!is.na(TempAnom)) # Central Mtn
div6 <- df.final %>%
filter(division == "VADiv6") %>%
filter(!is.na(TempAnom)) # SW Mountain
#all_div <- air.data %>%
# filter(!is.na(AllDivMeanAnom))
## DIV 2 mblm
for (month in month.abb) {
month_region <- div2 %>%
filter(Month == month)
model <- mblm(TempAnom ~ Year, data = month_region)
mod.sum <- summary.mblm(model)
# store results
div2_airAnom[[paste("div2", month, sep = "_")]] <- list(
slope = mod.sum$coefficients[2,1],
MAD = mod.sum$coefficients["Year", "MAD"] ,
pvalue = mod.sum$coefficients["Year", 4],
intercept = mod.sum$coefficients[1,1]
)
}
div2_df <- bind_rows(div2_airAnom)
div2_final <- div2_df %>%
mutate(climate_region = "E Piedmont \n(Div 2)") %>%
mutate(month = as.numeric(gsub("div2_", "", rownames(.))))
## DIV 3 mblm
for (month in month.abb) {
month_region <- div3 %>%
filter(Month == month)
model <- mblm(TempAnom ~ Year, data = month_region)
mod.sum <- summary.mblm(model)
# store results
div3_airAnom[[paste("div3", month, sep = "_")]] <- list(
slope = mod.sum$coefficients[2,1],
MAD = mod.sum$coefficients["Year", "MAD"] ,
pvalue = mod.sum$coefficients["Year", 4],
intercept = mod.sum$coefficients[1,1]
)
}
div3_df <- bind_rows(div3_airAnom)
div3_final <- div3_df %>%
mutate(climate_region = "W Piedmont \n(Div 3)") %>%
mutate(month = as.numeric(gsub("div3_", "", rownames(.))))
## DIV 5 mblm
for (month in month.abb) {
month_region <- div5 %>%
filter(Month == month)
model <- mblm(TempAnom ~ Year, data = month_region)
mod.sum <- summary.mblm(model)
# store results
div5_airAnom[[paste("div5", month, sep = "_")]] <- list(
slope = mod.sum$coefficients[2,1],
MAD = mod.sum$coefficients["Year", "MAD"] ,
pvalue = mod.sum$coefficients["Year", 4],
intercept = mod.sum$coefficients[1,1]
)
}
div5_df <- bind_rows(div5_airAnom)
div5_final <- div5_df %>%
mutate(climate_region = "Central Mtns \n(Div 5)") %>%
mutate(month = as.numeric(gsub("div5_", "", rownames(.))))
## DIV 6 mblm
for (month in month.abb) {
month_region <- div6 %>%
filter(Month == month)
model <- mblm(TempAnom ~ Year, data = month_region)
mod.sum <- summary.mblm(model)
# store results
div6_airAnom[[paste("div6", month, sep = "_")]] <- list(
slope = mod.sum$coefficients[2,1],
MAD = mod.sum$coefficients["Year", "MAD"] ,
pvalue = mod.sum$coefficients["Year", 4],
intercept = mod.sum$coefficients[1,1]
)
}
div6_df <- bind_rows(div6_airAnom)
div6_final <- div6_df %>%
mutate(climate_region = "SW Mtns \n(Div 6)") %>%
mutate(month = as.numeric(gsub("div6_", "", rownames(.))))
mblm_results <- rbind(div2_final, div3_final, div5_final, div6_final)
write_csv(mblm_results, "airTempAnom_mblm_1970_2023.csv")
```
```{r}
## All Divisions
for (month_num in 1:12) {
month_region <- all_div %>%
filter(Month == month_num)
model <- mblm(AllDivMeanAnom ~ Year, data = month_region)
mod.sum <- summary.mblm(model)
# store results
allDiv_airAnom[[paste("all_div", month_num, sep = "_")]] <- list(
slope = mod.sum$coefficients[2,1],
MAD = mod.sum$coefficients["Year", "MAD"] ,
pvalue = mod.sum$coefficients["Year", 4],
intercept = mod.sum$coefficients[1,1]
)
}
allDiv_df <- bind_rows(allDiv_airAnom)
allDiv_final <- allDiv_df %>%
mutate(climate_region = "All Divisions") %>%
mutate(month = as.numeric(gsub("all_div_", "", rownames(.))))
mblm_results <- rbind(div2_final, div3_final, div5_final, div6_final, allDiv_final)
write_csv(mblm_results, "air_anomaly_mblm.csv")
mblm_results <- mblm_results %>%
mutate(significant = if_else(pvalue < 0.05, "significant", "not sig")) %>%
mutate(Month = factor(month, levels = 1:12, labels = month.abb)) %>%
mutate(climate_region = factor(climate_region, levels = c("E Piedmont \n(Div 2)", "W Piedmont \n(Div 3)", "Central Mtns \n(Div 5)", "SW Mtns \n(Div 6)", "All Divisions")))
```
```{r}
library(ggplot2)
ggplot(mblm_results, aes(x = Month, y = slope, color = significant)) +
geom_point(alpha = .99) +
geom_hline(yintercept = 0, linetype = "dashed") +
facet_wrap(~climate_region) +
labs(title = NULL,
x = "Month",
y = "T Trend (°C Year\u207B\u00B9)",
color = NULL) +
theme_minimal() +
scale_color_manual(values = c("significant" = "salmon3", "not sig" = "grey22")) +
theme(axis.text.x = element_text(angle = 45, hjust = 1, size = 7.6),
panel.border = element_rect(colour = "black", fill=NA, size=.5))
ggsave("airtemp_mblm.jpg", width = 8, height = 6, dpi = 750)
```
```{r}
rat <- read_csv("vgin_landcover_RAT.csv")
rat
```