-
Notifications
You must be signed in to change notification settings - Fork 1
/
V4-diagnose-td.Rmd
411 lines (325 loc) · 11.2 KB
/
V4-diagnose-td.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
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
406
407
408
409
410
---
title: "Diagnose Trial Designs"
output: rmarkdown::html_vignette
vignette: >
%\VignetteIndexEntry{Diagnose Trial Designs}
%\VignetteEngine{knitr::rmarkdown}
%\VignetteEncoding{UTF-8}
---
```{r knitr_setup, include = FALSE}
knitr::opts_chunk$set(
collapse = TRUE,
comment = "#>",
fig.height = 6
)
```
```{r setup}
#--- pacakges ---#
library(ofpetrial)
library(terra)
library(ggplot2)
#--- file names ---#
boundary_file <- system.file("extdata", "boundary-simple1.shp", package = "ofpetrial")
abline_file <- system.file("extdata", "ab-line-simple1.shp", package = "ofpetrial")
```
## Check the alignment of the harvester and applicator/planter
It is ideal for each of the yield data points to be associated with a single input rate. However, this cannot be achieved sometimes because the width of a machine is not a multiple of that of the other machines. In such a case, you will have a mixed treatment problem where some data points are associated with more than one input rates. The `check_alignment()` function checks the alignment of harvester and applicator/planter to quantify and visualize the degree of mixed treatment problems of a trial design.
Let's first create experiment plots where plot width is specified as 90 feet, which is twice as wide as the applicator of the nitrogen fertilizer and three times as wide as the harvesters.
```{r create_exp_plots}
n_plot_info <-
prep_plot(
input_name = "NH3",
unit_system = "imperial",
machine_width = 45,
section_num = 1,
harvester_width = 30,
plot_width = 90
)
exp_data <-
make_exp_plots(
input_plot_info = n_plot_info,
boundary_data = boundary_file,
abline_data = abline_file,
abline_type = "free"
)
```
Running `check_alignment()` to `exp_data`,
```{r machine_al_check}
(
machine_alignment_check <- check_alignment(exp_data)
)
```
One of the elements of the resulting output is `g_path_alignment`, which is a `ggplot` figure of the path of the harvester (red) and applicator (blue).
```{r path_check_viz}
machine_alignment_check$g_path_alignment
```
As you can see, three yield paths fit perfectly in a single applicator path. That is, there is no mixed treatment problem in this layout of experiment plots. The `g_overlap` element is a `ggplot` figure of the histogram of the highest percentage of the harvester strip area occupied by a single rate. In this trial design, since all the strips have only a single input rate, all 45 strips have value of 100%.
```{r}
machine_alignment_check$g_overlap
```
Now, let's make the plot width 45 feet.
```{r}
n_plot_info <-
prep_plot(
input_name = "NH3",
unit_system = "imperial",
machine_width = 45,
section_num = 1,
harvester_width = 30,
plot_width = 45
)
exp_data <-
make_exp_plots(
input_plot_info = n_plot_info,
boundary_data = boundary_file,
abline_data = abline_file,
abline_type = "free"
)
#--- check alignment ---#
machine_alignment_check <- check_alignment(exp_data)
```
As you can see below, every three harvester paths is associated with two input rates.
```{r}
machine_alignment_check$g_path_alignment
```
This can be confirmed with the following figure, which shows that a third of harvester strips have 50% mix.
```{r}
machine_alignment_check$g_overlap
```
If you see this kind of problem, reconsider changing the plot width, unless it is not avoidable. If you are not sure what the right plot width is, it might be advisable to leave `plot_width` unspecified (`plot_width = NA`) in a `prep_plot()` function, in which case, the package will automatically suggest an appropriate (may not be optimal) plot width.
## Check the correlation of the input rates for a two-input experiment
Suppose you are running a two-input experiment. High correlation between the two input rates should be avoided as it will hurt statistical efficiency, rendering the estimation of the individual impact of the two inputs inaccurate. The available pre-made trial design options the `ofpetrial` packge are designed to avoid this problem. However, when you assign rates yourself using the `rank_seq_ws` and `rank_seq_as` options for the `ls` design type, you may accidentally create trial design where the two input rates are highly correlated. Here is an example.
We first create experiment plots. Note that the `plot_width` of both inputs are the same.
```{r}
seed_plot_info <-
prep_plot(
input_name = "seed",
unit_system = "imperial",
machine_width = 60,
section_num = 24,
harvester_width = 30,
plot_width = 30
)
n_plot_info <-
prep_plot(
input_name = "NH3",
unit_system = "imperial",
machine_width = 30,
section_num = 1,
harvester_width = 30,
plot_width = 30
)
exp_data <-
make_exp_plots(
input_plot_info = list(seed_plot_info, n_plot_info),
boundary_data = boundary_file,
abline_data = abline_file,
abline_type = "free"
)
```
Now, we assign rates for both inputs using the same `rank_seq_ws` and `rank_seq_as`.
```{r}
seed_rate_info <-
prep_rate(
plot_info = seed_plot_info,
gc_rate = 32000,
unit = "seed",
min_rate = 16000,
max_rate = 40000,
num_rates = 5,
design_type = "ls",
rank_seq_ws = c(5, 4, 3, 2, 1),
rank_seq_as = c(5, 1, 4, 2, 3)
)
n_rate_info <-
prep_rate(
plot_info = n_plot_info,
gc_rate = 180,
unit = "lb",
rates = c(100, 140, 180, 220, 260),
design_type = "ls",
rank_seq_ws = c(5, 4, 3, 2, 1),
rank_seq_as = c(5, 1, 4, 2, 3)
)
td_two_input <- assign_rates(exp_data, rate_info = list(seed_rate_info, n_rate_info))
```
Here is the visualization of the assigned rates for both inputs. It is easy to see that both input have exactly the same pattern because of the same `rank_seq_ws` and `plot_width`.
```{r}
viz(td_two_input)
```
You can check the correlation of the two inputs using `check_ortho_inputs()` like this.
```{r}
check_ortho_inputs(td_two_input)
```
As you can see, they are extremely highly correlated. When you see this, you should reassign rates for one of the inputs.
Now, let's change the width of the plots for NH3 to 60 feet, which is twice as wide as that for seed. We then use the same rate information to assign rates to the new experiment plots.
```{r}
#++++++++++++++++++++++++++++++++++++
#+ change the plot width for NH3
#++++++++++++++++++++++++++++++++++++
n_plot_info <-
prep_plot(
input_name = "NH3",
unit_system = "imperial",
machine_width = 60,
section_num = 1,
harvester_width = 30,
plot_width = 60
)
#++++++++++++++++++++++++++++++++++++
#+ Recreate experient plots for NH3
#++++++++++++++++++++++++++++++++++++
exp_data <-
make_exp_plots(
input_plot_info = list(seed_plot_info, n_plot_info),
boundary_data = boundary_file,
abline_data = abline_file,
abline_type = "free"
)
#++++++++++++++++++++++++++++++++++++
#+ Assign rates again with the same rate information
#++++++++++++++++++++++++++++++++++++
td_two_input_2 <- assign_rates(exp_data, rate_info = list(seed_rate_info, n_rate_info))
```
```{r}
viz(td_two_input_2)
```
Now, there are two plots of different rates of seed in a single plot of NH3. Consequently, their correlation is very low now.
```{r}
check_ortho_inputs(td_two_input_2)
```
---
Now, let's think about a case where the randomized block design is used for both inputs^[we do not recommend using the randomized block design. see add citation.] We are using the same plot width for both inputs just like the first case.
```{r}
n_plot_info <-
prep_plot(
input_name = "NH3",
unit_system = "imperial",
machine_width = 30,
section_num = 1,
harvester_width = 30,
plot_width = 30
)
exp_data <-
make_exp_plots(
input_plot_info = list(seed_plot_info, n_plot_info),
boundary_data = boundary_file,
abline_data = abline_file,
abline_type = "free"
)
```
Now, we assign rates for both inputs using the randomized block design (`rb`). Since rates are randomly assigned by block for both inputs, some cases introduce a higher correlation than others.
```{r}
seed_rate_info <-
prep_rate(
plot_info = seed_plot_info,
gc_rate = 32000,
unit = "seed",
min_rate = 16000,
max_rate = 40000,
num_rates = 5,
design_type = "rb"
)
n_rate_info <-
prep_rate(
plot_info = n_plot_info,
gc_rate = 180,
unit = "lb",
rates = c(100, 140, 180, 220, 260),
design_type = "rb"
)
```
```{r, include = FALSE}
#--- case 1 ---#
set.seed(543)
td_two_input_rb_1 <- assign_rates(exp_data, rate_info = list(seed_rate_info, n_rate_info))
cor_case_1 <- check_ortho_inputs(td_two_input_rb_1)
#--- case 2 ---#
set.seed(383543)
td_two_input_rb_2 <- assign_rates(exp_data, rate_info = list(seed_rate_info, n_rate_info))
cor_case_2 <- check_ortho_inputs(td_two_input_rb_2)
```
The first case has a correlation of `r cor_case_1`, while the second case is `r cor_case_2`.
```{r, eval = F}
#--- case 1 ---#
set.seed(543)
td_two_input_rb_1 <- assign_rates(exp_data, rate_info = list(seed_rate_info, n_rate_info))
cor_case_1 <- check_ortho_inputs(td_two_input_rb_1)
```
```{r}
cor_case_1
```
```{r, eval = F}
#--- case 2 ---#
set.seed(383543)
td_two_input_rb_2 <- assign_rates(exp_data, rate_info = list(seed_rate_info, n_rate_info))
cor_case_2 <- check_ortho_inputs(td_two_input_rb_2)
```
```{r}
cor_case_2
```
## Check the correlation of the input rates with observed field/topographic characteristics
When analyzing the experiment data and applying statistical methods to estimate yield response functions, field and topographic characteristics are often included in the model to control for their effects, which help isolate the impact of the input on yield. In designing experiments, it would be nice to have the input rates uncorrelated (orthogonal to) with those observed characteristics. The `check_ortho_with_chars()` checks this.
Let's first import an already-design trial design called `td_single_input`.
```{r}
data(td_single_input)
```
Here is the visualization of the trial desing.
```{r}
viz(td_single_input)
```
Here, we have three sets of spatial data we want to check.
+ Past yield data (`Yld_Vol_Dr`) from the same field (points data)
+ SSURGO data (polygons data)
+ clay content (`clay`)
+ soil category (`mukey`)
+ Slope (`slope`) of the field (raster data)
+ Topographic Wetness Index (`twi`) of the field (raster data)
---
**Yield data**
```{r}
yield_sf <- sf::st_read(system.file("extdata", "yield-simple1.shp", package = "ofpetrial"))
ggplot(yield_sf) +
geom_sf(aes(color = Yld_Vol_Dr)) +
scale_color_viridis_c() +
theme_void()
```
---
**SSURGO**
```{r}
ssurgo_sf <-
sf::st_read(system.file("extdata", "ssurgo-simple1.shp", package = "ofpetrial")) %>%
dplyr::mutate(mukey = factor(mukey))
ggplot(ssurgo_sf) +
geom_sf(aes(fill = mukey)) +
theme_void()
ggplot(ssurgo_sf) +
geom_sf(aes(fill = clay)) +
scale_fill_viridis_c() +
theme_void()
```
---
**Slope and TWI**
```{r}
topo_rast <-
c(
terra::rast(system.file("extdata", "slope.tif", package = "ofpetrial")),
terra::rast(system.file("extdata", "twi.tif", package = "ofpetrial"))
)
plot(topo_rast)
```
---
```{r}
checks <-
check_ortho_with_chars(
td = td_single_input,
sp_data_list = list(yield_sf, ssurgo_sf, topo_rast),
vars_list = list("Yld_Vol_Dr", c("mukey", "clay"), names(topo_rast))
)
```
```{r}
checks$summary_data[[1]]
```
```{r}
checks$summary_fig[[1]]
```