-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathfunctions.R
More file actions
406 lines (357 loc) · 11.5 KB
/
Copy pathfunctions.R
File metadata and controls
406 lines (357 loc) · 11.5 KB
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
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
# ============================================================================
# functions.R - Domain logic for DRAW Model Prediction App
# ============================================================================
# Contains: wet bulb calculations, data preparation, prediction plotting
# ============================================================================
library(dplyr)
library(tidyr)
library(ggplot2)
library(brms)
library(lme4)
library(nleqslv)
library(scales)
library(gridExtra)
# ============================================================================
# Download model files from GitHub Releases if not present locally
# ============================================================================
RELEASE_URL <- "https://github.com/SprayDriftModels/DRAW-Model/releases/download/v1.0.0"
download_model_file <- function(filename) {
dest <- file.path("program", "data", filename)
if (!file.exists(dest)) {
url <- paste0(RELEASE_URL, "/", filename)
message("Downloading ", filename, " from GitHub Release...")
message(" URL: ", url)
tryCatch({
download.file(url, dest, mode = "wb")
message(" Done.")
}, error = function(e) {
stop(
"Failed to download ", filename, ": ", conditionMessage(e), "\n",
"Download manually from: ", url, "\n",
"Place in: ", normalizePath(dirname(dest), mustWork = FALSE),
call. = FALSE
)
})
}
}
download_model_file("fit.STD.NoAngle.addTemp.rda")
download_model_file("mod.STD.NoAngle.addTemp.rda")
# -- Load model objects and reference data --
load("program/data/fit.STD.NoAngle.addTemp.rda")
load("program/data/mod.STD.NoAngle.addTemp.rda")
load("program/data/rautmann.rda")
# comparedat.rda may be missing
comparedat <- NULL
comparedat_available <- FALSE
if (file.exists("program/data/comparedat.rda")) {
tryCatch({
load("program/data/comparedat.rda")
comparedat_available <- TRUE
}, error = function(e) {
message("Note: comparedat.rda could not be loaded (", conditionMessage(e), ")")
message(" Trial Comparisons tab will be disabled.")
})
} else {
message("Note: comparedat.rda not found. Trial Comparisons tab will be disabled.")
}
compareTrials <- read.csv("program/data/ComparisonCases.csv")
# ============================================================================
# Wet Bulb Depression
# ============================================================================
#' Wet Bulb Depression (rigorous thermodynamic calculation)
#'
#' @param Tair Dry air temperature (Celsius)
#' @param Patm Barometric pressure (mmHg abs)
#' @param RH Relative humidity (%)
#' @return Named numeric vector: DTwb (depression) and Twb (wet bulb T)
wet_bulb <- function(Tair = 17.689, Patm = 760, RH = 35.65) {
aw <- 18.92676
bw <- -4169.627
cw <- -33.568
air <- 6.917
bair <- 9.911e-4
cair <- 7.627e-7
dair <- -4.696e-10
Dh0 <- 717.2184
n <- 0.33246
MWair <- 2 * (0.79 * 14.007 + 0.21 * 15.994)
MWw <- 2 * 1.008 + 15.9994
Psw <- function(T) {
exp(aw + bw / (T + 273.15 + cw))
}
Cpair <- function(T) {
(air + bair * T + cair * T^2 + dair * T^3) / MWair
}
DHv <- function(T) {
Dh0 * (1 - (T + 273.15) / 647.3)^n
}
Tdp <- bw / (log(Psw(Tair) * RH / 100) - aw) - 273.15 - cw
Eqn <- function(T) {
Psw(Tdp) - Psw(T) - Patm * MWair / MWw * Cpair(T) * (T - Tair) / DHv(T)
}
Twb <- nleqslv::nleqslv(0, Eqn)$x[1]
DTwb <- Tair - Twb
c(DTwb = DTwb, Twb = Twb)
}
#' Wet Bulb Depression (Stull approximation)
#'
#' @param Tair Dry air temperature (Celsius)
#' @param RH Relative humidity (%)
#' @return Named numeric vector: DTwb (depression) and Twb (wet bulb T)
wet_bulb_Stull <- function(Tair = 17.689, RH = 50) {
Twb <- Tair *
atan(0.151977 * (RH + 8.313659)^(1 / 2)) +
atan(Tair + RH) -
atan(RH - 1.676331) +
0.00391838 * (RH)^(3 / 2) * atan(0.023101 * RH) -
4.686035
DTwb <- Tair - Twb
c(DTwb = DTwb, Twb = Twb)
}
# ============================================================================
# Crop height classification
# ============================================================================
#' Classify crop height into categories
#'
#' @param Crop.Height Numeric crop height in meters
#' @return Character height category
getHeight1 <- function(Crop.Height) {
sapply(Crop.Height, function(x) {
if (is.na(x)) {
NA_character_
} else if (x < 0.2) {
"0-0.2"
} else if (x <= 0.4) {
"0.2-0.4"
} else {
"0.4-1"
}
})
}
# ============================================================================
# Data preparation for prediction
# ============================================================================
#' Build prediction newdata from user inputs
#'
#' @param Windspeed Wind speed (m/s)
#' @param Boom.height Boom height above crop (m)
#' @param Pressure Nozzle pressure (bar)
#' @param Temp Temperature (Celsius)
#' @param Crop.Height Crop height (m)
#' @param Speed Tractor forward speed (m/s)
#' @param Rate Application rate
#' @param WBD Wet bulb depression (Celsius)
#' @param RH Relative humidity (%), used if WBD is NULL
#' @param Cot Cotyledon category: "monocot", "dicot", "Bare Ground"
#' @return data.frame suitable for brms/lme4 prediction
getNewData <- function(
Windspeed = 2.8,
Boom.height = 0.5,
Pressure = 3,
Temp = 18,
Crop.Height = 0.5,
Speed = 8,
Rate = 225,
WBD = 3.7,
RH = NULL,
Cot = "monocot"
) {
if (missing(WBD) || is.null(WBD)) {
if (!missing(RH) && !is.null(RH)) {
WBD <- wet_bulb(Tair = Temp, RH = RH)[["DTwb"]]
}
}
expand.grid(
Distance = c(1, 2, 5, 15, 20, 25, 30, 50),
Windspeed = Windspeed,
Boom.height = Boom.height,
Pressure = Pressure,
Temp = Temp,
Crop.Height = Crop.Height,
Speed = Speed,
Rate = Rate,
WBD = WBD,
Cot = Cot
) |>
mutate(
sRate = Rate / 100,
Height = getHeight1(Crop.Height),
CotHeight = interaction(Cot, Height),
logDist = log(Distance)
)
}
# ============================================================================
# Prediction and plotting
# ============================================================================
# Valid CotHeight levels in the training data
valid_cotheight_levels <- c(
"Bare Ground.0-0.2",
"monocot.0-0.2",
"dicot.0.2-0.4",
"monocot.0.2-0.4",
"dicot.0.4-1",
"monocot.0.4-1"
)
#' Generate drift prediction plot and tables
#'
#' @param mod A brmsfit or lmerMod model object
#' @param newdata Prediction data from getNewData()
#' @param raneff Logical: include trial-level random effects?
#' @param pred Logical: include prediction intervals?
#' @param probs Quantile bounds for credible/prediction intervals
#' @param allow_new_levels Logical: allow new random effect levels?
#' @param sample_new_levels How to sample new levels (brms argument)
#' @return List with: p (ggplot), preddat, fitdat, plotdat
DrawPlot <- function(
mod,
newdata,
raneff = FALSE,
pred = FALSE,
probs = c(0.025, 0.975),
allow_new_levels = FALSE,
sample_new_levels = "uncertainty"
) {
# Filter to valid CotHeight levels
newdata <- droplevels(
subset(newdata, CotHeight %in% valid_cotheight_levels)
)
# Build Rautmann reference data
rautmann1 <- merge(
data.frame(Distance = rautmann$Distance, Estimate = rautmann$Value),
unique(newdata[, -c(1, ncol(newdata))]),
by = NULL
)
rautmann1$Data_Source <- "Rautmann"
nx <- ncol(newdata)
preddat <- NULL
if (inherits(mod, "brmsfit")) {
# --- Fitted values ---
if (allow_new_levels) {
newdata1 <- newdata
newdata1$TrialG <- "Tnew"
yfit <- fitted(
mod,
newdata = newdata1,
re_formula = ~ (1 | TrialG),
probs = probs,
allow_new_levels = TRUE
)[, c(1, 3, 4)]
} else {
yfit <- fitted(
mod,
newdata = newdata,
re_formula = NA,
probs = probs
)[, c(1, 3, 4)]
}
fitdat <- cbind(newdata, exp(yfit))
fitdat$Data_Source <- "Model Fit"
plotdat <- dplyr::full_join(
fitdat,
rautmann1,
by = intersect(names(fitdat), names(rautmann1))
)
ny <- ncol(plotdat)
names(plotdat)[(nx + 2):(nx + 3)] <- c("lwrQ", "uprQ")
p <- ggplot(plotdat, aes(x = Distance, y = Estimate, col = Data_Source)) +
geom_point() +
geom_line(aes(x = Distance, y = Estimate)) +
facet_wrap(CotHeight + Crop.Height ~ ., scale = "free") +
scale_x_log10() +
geom_ribbon(
aes(ymin = lwrQ, ymax = uprQ, fill = Data_Source),
alpha = 0.3
) +
scale_y_continuous(labels = scales::percent_format()) +
ylab("Estimated Drift") +
theme_minimal()
# --- Prediction intervals ---
if (pred) {
if (allow_new_levels) {
newdata1 <- newdata
newdata1$TrialG <- "Tnew"
ypred <- predict(
mod,
newdata = newdata1,
re_formula = NA,
probs = probs,
allow_new_levels = TRUE
)[, c(1, 3, 4)]
} else {
ypred <- predict(
mod,
newdata = newdata,
re_formula = NA,
probs = probs
)[, c(1, 3, 4)]
}
preddat <- cbind(newdata, exp(ypred))
preddat$Data_Source <- "Model Prediction"
names(preddat)[(nx + 2):(nx + 3)] <- c("lwrQ", "uprQ")
plotdat <- dplyr::full_join(
plotdat,
preddat,
by = intersect(names(plotdat), names(preddat))
)
p <- ggplot(plotdat, aes(x = Distance, y = Estimate, col = Data_Source)) +
geom_point() +
facet_wrap(CotHeight + Crop.Height ~ ., scale = "free") +
scale_x_log10() +
geom_ribbon(
aes(ymin = lwrQ, ymax = uprQ, fill = Data_Source),
alpha = 0.2,
colour = NA
) +
geom_line(aes(x = Distance, y = Estimate)) +
scale_y_continuous(labels = scales::percent_format()) +
ylab("Estimated Drift") +
theme_minimal()
preddat <- preddat[, c(1:10, (nx + 1):(nx + 3))]
}
fitdat <- fitdat[, c(1:10, (nx + 1):(nx + 3))]
plotdat <- subset(plotdat, Data_Source != "Rautmann")
} else if (inherits(mod, "lmerMod")) {
yfit <- predict(mod, newdata = newdata, re.form = NA)
fitdat <- cbind(newdata, Estimate = exp(yfit))
fitdat$Data_Source <- "Model Fit"
plotdat <- dplyr::full_join(
fitdat,
rautmann1,
by = intersect(names(fitdat), names(rautmann1))
)
p <- ggplot(plotdat, aes(x = Distance, y = Estimate, col = Data_Source)) +
geom_point() +
geom_line(aes(x = Distance, y = Estimate)) +
facet_wrap(CotHeight + Crop.Height ~ ., scale = "free") +
scale_x_log10() +
scale_y_continuous(labels = scales::percent_format()) +
ylab("Estimated Drift") +
theme_minimal()
fitdat <- fitdat[, c(1:10, (nx + 1))]
plotdat <- subset(plotdat, Data_Source != "Rautmann")
}
list(p = p, preddat = preddat, fitdat = fitdat, plotdat = plotdat)
}
# ============================================================================
# Reference scenario (for documentation/defaults)
# ============================================================================
RefScenario <- expand.grid(
Distance = c(1, 3, 5, 10, 15, 20),
Boom.height = 0.5,
Windspeed = 2.8,
Speed = 6,
Temp = 18,
Pressure = 3,
WBD = 3.92,
Rate = 225,
sRate = 2.25,
Cot1 = factor(c("monocot", "Bare Ground", "dicot")),
Crop.Height = c(0.1, 0.2, 0.4, 0.6)
) |>
mutate(
logDist = log(Distance),
Height1 = getHeight1(Crop.Height),
CotHeight = interaction(Cot1, Height1)
) |>
filter(CotHeight %in% valid_cotheight_levels) |>
droplevels()