-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathTMax_trend.Rmd
284 lines (205 loc) · 9.5 KB
/
TMax_trend.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
---
title: "Assessing Climate Change Effects in VA Reservoirs"
subtitle: "Part 1: Deriving Surface Temperature (TMax) Trends by Month and Station"
author: "Andrew Cameron"
date: "2024-02-14"
output: html_document
---
Data wrangling and median-based linear modeling to determine warming rates in reservoirs over time.
```{r setup, include=FALSE}
knitr::opts_chunk$set(echo = TRUE, warning = FALSE, message = FALSE)
library(tidyverse)
```
```{r `read in data`}
trendSites.df <- openxlsx::read.xlsx("input_data/data_original.xlsx", sheet = 2)
allData.df <- openxlsx::read.xlsx("input_data/data_original.xlsx", sheet = 3)
# Convert `date` column from Excel encoded date to a more legible date format. Otherwise date shows as numeric value, e.g. '44230'.
allData.df$Date <- allData.df$Date * 86400 # 86400 = seconds in a day.
allData.df$Date <- as.POSIXct(allData.df$Date, origin = "1899-12-30", tz = "UTC")
```
```{r 'remove anomlies'}
# a surface temperature (Tmax) of 2.8 C in August
# 13774 6ACNR000.00 8/28/1990 0.3 NA NA NA NA
which(grepl("13774", allData.df$X1))
allData.df[13770, (5:8)] <- NA
```
```{r `subset data`}
# filter for rows corresponding to the 41 stations
station_IDS <- trendSites.df$FDT_STA_ID
subset.df <- allData.df %>%
mutate(MonthNum = lubridate::month(Date)) %>%
filter(FDT_STA_ID %in% station_IDS) %>%
filter(MonthNum %in% 5:10)
monthlyStation_stats <- subset.df %>%
group_by(FDT_STA_ID, MonthNum) %>%
summarize(Mean = mean(TMax, na.rm = TRUE),
Min = min(TMax, na.rm = TRUE),
Max = max(TMax, na.rm = TRUE),
Nobs = n(),
.groups = 'drop') %>%
mutate(Month = month.name[MonthNum])
# write_csv(monthlyStation_stats, "TMax_stats_monthlyByStation.csv")
```
The next step is to create a new variable in the Merged Profile & Meta Data dataset, which is the **Temperature Anomaly**.
This is the difference between the observed temperature on a given date and the long-term average temperature.
For example, for the first station (2-JKS044.60), the long-term mean surface T (i.e., the long term MAX temp -- temps are hottest at the surface) for May is 19.0, so you would subtract that from all May observations for this station (N=18). When the observed T is greater than mean T, this returns a positive number (above-average T), and when observed < average you get a negative anomaly.
```{r `derive Temp Anomaly`}
cols_to_keep <- colnames(subset.df)
subset.df <- subset.df %>%
left_join(monthlyStation_stats, by = c("FDT_STA_ID", "MonthNum")) %>%
select(cols_to_keep, Mean, Nobs) %>%
mutate(TAnomaly = TMax - Mean)
```
```{r `fit regressions for each month-station`}
# fit a linear regression for anomaly values vs. year by month-station using the mblm function (median based linear model) as this is thought to be less sensitive to outliers.
# install.packages("mblm")
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
subset.df$Year <- year(subset.df$Date)
# remove NA values -- mblm() does not handle NA as lm() does
subset.df <- subset.df %>%
filter(!is.na(TAnomaly)) %>%
filter(!is.na(TMax))
# create list to store regression results
modelSummaries_TMaxAnomaly <- list()
for (station_id in unique(subset.df$FDT_STA_ID)) {
for (month_num in 5:10) {
month_station <- subset.df %>%
filter(FDT_STA_ID == station_id & MonthNum == month_num)
model <- mblm(TAnomaly ~ Year, data = month_station)
mod.sum <- summary.mblm(model)
# store results
modelSummaries_TMaxAnomaly[[paste(station_id, 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]
)
}
}
# 6/4 Do the same as above, but for TMean non-normalized. This is needed for the data viz (August only trend line plots)
modelSummaries_TMax <- list()
for (station_id in unique(subset.df$FDT_STA_ID)) {
for (month_num in 5:10) {
month_station <- subset.df %>%
filter(FDT_STA_ID == station_id & MonthNum == month_num)
model <- mblm(TMax ~ Year, data = month_station)
mod.sum <- summary.mblm(model)
# store results
modelSummaries_TMax[[paste(station_id, 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]
)
}
}
## --------TMax, with 1975 as y intercept-------------
q <- subset.df %>%
mutate(Year1975 = Year - 1975) %>%
filter(MonthNum == 8)
modelSummaries_TMax1975 <- list()
for (station_id in unique(q$FDT_STA_ID)) {
data = subset(q, FDT_STA_ID == station_id)
model <- mblm(TMax ~ Year1975, data = data)
mod.sum <- summary.mblm(model)
# store results
modelSummaries_TMax1975[[paste(station_id)]] <- list(
slope = mod.sum$coefficients[2,1],
MAD = mod.sum$coefficients["Year1975", "MAD"] ,
pvalue = mod.sum$coefficients["Year1975", 4],
intercept = mod.sum$coefficients[1,1]
)
}
# mblm does not return a standard error. Instead, summary.mblm can be used to extract the MAD or Median Absolute Deviation." It's a robust measure of variability that is less sensitive to outliers than the standard deviation, which is commonly used in traditional statistical analyses.
```
```{r `add reg stats to summary df`}
# create station-month key
monthly_TMaxAnomaly <- monthlyStation_stats %>%
mutate( key = paste(FDT_STA_ID, MonthNum, sep="_"),
model_slope = NA,
model_MAD = NA,
model_pval = NA,
model_intercept = NA)
for (i in 1:nrow(monthly_TMaxAnomaly)) {
key = monthly_TMaxAnomaly$key[i]
monthly_TMaxAnomaly$model_slope[i] <- modelSummaries_TMaxAnomaly[[key]]$slope
monthly_TMaxAnomaly$model_MAD[i] <- modelSummaries_TMaxAnomaly[[key]]$MAD
monthly_TMaxAnomaly$model_pval[i] <- modelSummaries_TMaxAnomaly[[key]]$pvalue
monthly_TMaxAnomaly$model_intercept[i] <- modelSummaries_TMaxAnomaly[[key]]$intercept
}
#---------------------------------------------------
# Same as above, but for TMax non-normalized
monthly_TMax <- monthlyStation_stats %>%
mutate(key = paste(FDT_STA_ID, MonthNum, sep="_"),
model_slope = NA,
model_MAD = NA,
model_pval = NA,
model_intercept = NA)
for (i in 1:nrow(monthly_TMax)) {
key = monthly_TMax$key[i]
monthly_TMax$model_slope[i] <- modelSummaries_TMax[[key]]$slope
monthly_TMax$model_MAD[i] <- modelSummaries_TMax[[key]]$MAD
monthly_TMax$model_pval[i] <- modelSummaries_TMax[[key]]$pvalue
monthly_TMax$model_intercept[i] <- modelSummaries_TMax[[key]]$intercept
}
#--------------------------------------------
# reg stats for August TRange with 1975 as y intercept
monthly_TMax1975 <- monthly_TMax %>%
mutate( model_slope = NA,
model_MAD = NA,
model_pval = NA,
model_intercept = NA) %>%
filter(MonthNum == 8)
for (i in 1:nrow(monthly_TMax1975)) {
key = monthly_TMax1975$FDT_STA_ID[i]
monthly_TMax1975$model_slope[i] <- modelSummaries_TMax1975[[key]]$slope
monthly_TMax1975$model_MAD[i] <- modelSummaries_TMax1975[[key]]$MAD
monthly_TMax1975$model_pval[i] <- modelSummaries_TMax1975[[key]]$pvalue
monthly_TMax1975$model_intercept[i] <- modelSummaries_TMax1975[[key]]$intercept
}
monthly_TMax1975 <- monthly_TMax1975 %>%
mutate(variable = "TMax")
```
```{r `write out data`}
#write_csv(monthly_TMaxAnomaly, "output_data/TMaxAnomaly_mblmModelStatistics.csv")
#write_csv(monthly_TMax, "output_data/TMax_mblmModelStatistics.csv")
#write_csv(monthly_TMax1975, "output_data/TMax_y1975.csv")
```
## Create Single CSV with all variables August model results and 1975 y intercept
```{r}
DO <- readr::read_csv("output_data/1975_y_intercept_DOVars_model_results.csv")
TMax <- readr::read_csv("output_data/TMax_y1975.csv")
Temp_other <- readr::read_csv("output_data/1975_y_intercept_TempVars_model_results.csv")
names(Temp_other) -> colsTOKeep
DO <- DO %>%
select(colsTOKeep)
TMax <- TMax %>%
select(colsTOKeep)
rbind(DO, TMax, Temp_other) %>% write_csv("output_data/1975_y_intercept_allVars.csv")
rbind(DO, TMax, Temp_other) -> z
z
```
## TMax and TMax Anomaly monthly trends across all 41 stations
7/8/2024 " for a given year (I think we started with 2000 or 2001) and a given month (May-October only) I would like to know the average Tmax across all stations that were sampled in that month and year. It would be helpful to also know the number of stations that were sampled in that month and year. I would like to test for a relationship between air and water Temp anomalies to see how closely the reservoirs track changes in air temp."
```{r}
tmax_byMonthYear <- subset.df %>%
group_by(Year, MonthNum) %>%
summarize(Mean_TMax = mean(TMax, na.rm = TRUE),
Mean_TMaxAnom = mean(TAnomaly, na.rm = TRUE),
Nobs = n(),
.groups = 'drop') %>%
mutate(Month = month.name[MonthNum])
write_csv(tmax_byMonthYear, "output_data/TMax_byMonthYear.csv")
countPerGroup <- subset.df %>%
group_by(FDT_STA_ID, MonthNum) %>%
summarise(Nobs = n()) %>%
mutate(key = paste(FDT_STA_ID, MonthNum, sep="_"))
insufficient_Nobs <- countPerGroup %>%
filter(Nobs < 10)
```