-
Notifications
You must be signed in to change notification settings - Fork 0
/
03_Prep_data.Rmd
578 lines (447 loc) · 18.7 KB
/
03_Prep_data.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
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
# Prepare data for meta-analysis
## Set environment and import data
```{r load_libraries, include=FALSE}
if (!require("pacman"))
install.packages("pacman")
pacman::p_load(tidyverse, kableExtra, agricolae, lme4, here)
if (!require("theme.usq"))
devtools::install_github("adamhsparks/theme.usq")
library(theme.usq)
theme_set(theme_usq()
)
```
```{r import_data}
load("cache/PreliminaryData.Rdata")
source(here("R/same.R")) # matches each element of a vector and does not return NAs
```
## Subset to trials containing DMI fungicides
```{r filterDMI_trials}
DMI_Trials <-
PM_MB_dat %>%
filter(fungicide_ai == "tebuconazole" |
fungicide_ai == "propiconazole")%>%
distinct(trial_ref) %>%
pull()
PM_MB_dat <-
PM_MB_dat[PM_MB_dat$trial_ref %in% DMI_Trials,]
```
Now remove any non-DMI treatments or controls
```{r}
PM_MB_dat <-
PM_MB_dat %>%
filter(fungicide_ai == "control" |
fungicide_ai == "tebuconazole" |
fungicide_ai == "propiconazole")
```
*****
## Standardising sample variance
Meta-analyses use variance from the data to calculate the effect sizes.
The type of variance needs to be the same across all the data incorporated in the meta-analysis.
We are narrowing the meta-analysis to only include treatments using demethylation inhibitors, tebuconazole and propiconazole.
Let's look at what type of statistical error was reported for the trials that used these fungicides.
```{r variance_types}
PM_MB_dat %>%
filter(fungicide_ai == "tebuconazole" |
fungicide_ai == "propiconazole") %>%
group_by(trial_ref, location, year , Y_error_type) %>%
summarise(DMI_treatments = length(Y_error_type), .groups = 'drop') %>%
arrange(Y_error_type) %>%
kable(caption = "Number of treatments for each trial and the error type reported",
align = "c") %>%
footnote(general = "DMI - demethylation inhibitors (tebuconazole and propiconazole).")
```
The experiment summaries report either standard deviation or least square differences.
First we will convert LSD and standard deviation, then square the stdev to get sample variance.
### Converting least-squares to sample variance
First let's get a list the of trials that reported LSD.
```{r all_LSD_trials}
Trials <- PM_MB_dat %>%
filter(Y_error_type == "lsd (P=0.05)") %>%
select(trial_ref) %>%
distinct()
Trials
```
Let's use this list of trials to subset the data and calculate sampling variances.
We will follow the method of converting 'least square differences' (LSD) to sampling variance reported by [Nugugi et.al (2011)](https://apsjournals.apsnet.org/doi/10.1094/PHYTO-08-10-0221).
```{r LSD_2_Sampling_Variance}
Tcrit <- 2.042
for (i in Trials$trial_ref) {
DFw <-
sum(PM_MB_dat[PM_MB_dat$trial_ref == i, ]$replicates) -
length(PM_MB_dat[PM_MB_dat$trial_ref == i, ]$replicates) # degrees of freedom within groups
LSD <-
PM_MB_dat[PM_MB_dat$trial_ref == i, ]$yield_error[1]
V_yield <-
(PM_MB_dat[PM_MB_dat$trial_ref == i, ]$replicates[1] *
(LSD / Tcrit) ^ 2) / 2
# method - Nugugi et.al (2011) DOI: 10.1094/phyto-08-10-0221
for (j in seq_along(PM_MB_dat[PM_MB_dat$trial_ref == i,]$replicates)) {
if (j == 1) {
Sv <-
vector(length = length(PM_MB_dat[PM_MB_dat$trial_ref == i,]$replicates))
} # reset Sv vector if this is the first loop
Sv[j] = V_yield /
PM_MB_dat[PM_MB_dat$trial_ref == i,]$replicates[j]
}
# Replace the values in the dataset
PM_MB_dat[PM_MB_dat$trial_ref == i,]$yield_error <-
Sv
PM_MB_dat[PM_MB_dat$trial_ref == i,]$Y_error_type <-
"SamplingVariance"
}
```
### Converting standard deviation to sample variance
Now to estimate the variance from the standard deviation.
```{r stdev2SampVariance}
# subset data by those which describe the Yield error with standard deviation
Trials <- PM_MB_dat %>%
filter(Y_error_type == "stdev") %>%
select(trial_ref) %>%
pull()
PM_MB_dat[PM_MB_dat$trial_ref %in% Trials,"yield_error"] <-
PM_MB_dat[PM_MB_dat$trial_ref %in% Trials,"yield_error"]^2
PM_MB_dat[PM_MB_dat$trial_ref %in% Trials,]$Y_error_type <-
"SamplingVariance"
```
### Check variance of data-set
Our data currently uses sample variance to define the yield error.
We should expect that when yield increases so does the variance.
Let's quickly plot this to check.
```{r Var_mean_sqaures}
PM_MB_dat %>%
ggplot(aes(x = grain_yield.t.ha., y = yield_error)) +
geom_point()
```
There seems to be no association between yield and sample variance, this might be due to the variability of mungbeans yields.
Lets look at the association between mean squares and yield to see if there is a more typical association between yield and variance.
```{r Var_MeanSquarePlot}
PM_MB_dat %>%
ggplot(aes(x = grain_yield.t.ha., y = Y_Msquare/replicates)) +
geom_point()
```
There is a somewhat better trend here, if sampling variance from standard deviation proves unsuitable for the analysis we may compare models using variance from stdev and mean square for the best fit.
However, seven treatments are missing mean square error.
### Inspect variance and response for analysis
Let's inspect the response variance `grain_yield.t.ha` to see if it is normally distributed.
In many cases a log transformation of the response variable improves the fit, due to the prevalence of log distributed data in nature.
```{r}
hist_usq(PM_MB_dat$grain_yield.t.ha., main = "Grain yield (t / ha)")
hist_usq(log(PM_MB_dat$grain_yield.t.ha.), main = "log(Grain yield (t / ha))")
hist_usq(sqrt(PM_MB_dat$grain_yield.t.ha.), main = "sqrt(Grain yield (t / ha))")
hist_usq(PM_MB_dat$yield_gain, main = "Mean difference to control (t / ha)")
```
The distribution of data does not seem to be improved by transformation of the data.
We will retain the continuous variable grain_yield.t.ha as the response.
Mean differences won't be used as we are following the methodology of a ARM-based model, where mean differences are calculated following the meta-analysis.
### Formatting Variance
First let's check for any entries missing variance and remove them.
```{r remove_studies}
any(is.na(PM_MB_dat$yield_error))
PM_MB_dat <-
PM_MB_dat[!is.na(PM_MB_dat$yield_error), ]
PM_MB_dat$vi <- PM_MB_dat$yield_error
```
#### Calculate sample variance from mean square errors
Currently our data frame has the sample variance calculated earlier, however we can approximate it using the mean squared error.
Let's show the rational for our calculation and then add a secondary variance column (`vi2`).
$MSE = Var + bias$
However if our MSE was calculated from an unbiased sample we can assume $bias = 0$ and therefore:
$MSE = Var$
To calculate the sample variance (for each treatment in the trial) from the mean square error (Trial variance) all we need to do is divide by the number of samples in each treatment.
We can show this because:
$MSE = \frac{\sum_{y_1}^{y_n}(y_i - \overline{y})^2}{n - 1}$
Where the sum of squares for the fungicide treatment estimator ($y$) is divided by the number of fungicide treatments ($n$).
The sample variance is a similar formula except we use $x$ instead of $y$.
$S_{Var} = \frac{\sum_{x_1}^{x_n}(x_i - \overline{x})^2}{n - 1}$
$x$ being each observation within the experiment and $n$ being the number of samples in the whole trial.
Therefore:
$S_{Var}\approx\frac{1}{n_{x}}\frac{\sum_{y_1}^{y_n}(y_i - \overline{y})^2}{n_y - 1}$
Where $n_x$ is the number of samples within each treatment.
As mentioned before there are a few treatments with no MSE (NAs), we can calculate it from the sample variance using this logic above.
```{r EstMSE}
PM_MB_dat[is.na(PM_MB_dat$Y_Msquare),"Y_Msquare"] <-
PM_MB_dat %>%
filter(is.na(Y_Msquare))%>%
mutate(Y_Msquare = yield_error * replicates)%>%
pull(Y_Msquare)
```
Now to calculate the sample variance from MSE for all treatments.
```{r MSE_2_SVar}
#calculation when using log of response
# PM_MB_dat$vi <-
# PM_MB_dat$Y_Msquare / (PM_MB_dat$n * PM_MB_dat$grain_yield.t.ha. ^
# 2)
PM_MB_dat$vi2 <-
PM_MB_dat$Y_Msquare /
PM_MB_dat$replicates
```
*******
## Classifying fungicide treatment
First are going to calculate the time between the first sign of disease and the fungicide applications.
```{r clustered_fungicide_applications}
PM_MB_dat %<>%
mutate(fungicide_timing_1 = fungicide_application_1 - first_sign_disease) %>%
mutate(fungicide_timing_2 = fungicide_application_2 - fungicide_application_1) %>%
mutate(fungicide_timing_3 = fungicide_application_3 - fungicide_application_2)
```
To ensure sufficient number of replicates, we will bin the continuous variable into three categorical variables relating to when the first fungicide application was made, relative to the first sign of disease.
These categorical variables are named:
- **Early**: First fungicide application was prior to first sign of disease.
- **Recommended**: First fungicide application was applied on the day powdery mildew was observed, or within three days of first sign.
- **Late**: First fungicide application was four or more days after first sign of disease being observed.
The number of sprays need also be defined.
```{r TreatmentTable}
data.frame(TreatmentName = c("Early", "Recommended", "Late", "EarlyPlus", "RecommendedPlus", "LatePlus"),
n_sprays = rep(c("Single", "Two - Three"), each = 3),
DaysRelativeToFirstSign = c("Prior to first sign of Powdery Mildew",
"1 - 3 days after first sign of Powdery Mildew",
"7 - 8 days after first sign of Powdery Mildew"))%>%
kable()
```
```{r simple_clusters}
PM_MB_dat <- PM_MB_dat %>%
mutate(
spray_management = case_when(
fungicide_timing_1 < 0 &
is.na(fungicide_application_2) &
is.na(fungicide_application_3) ~ "Early",
fungicide_timing_1 >= 0 &
fungicide_timing_1 < 4 &
is.na(fungicide_application_2) &
is.na(fungicide_application_3) ~ "Recommended",
fungicide_timing_1 >= 4 &
is.na(fungicide_application_2) &
is.na(fungicide_application_3) ~ "Late",
fungicide_timing_1 < 0 &
!is.na(fungicide_application_2) ~ "Early_plus",
fungicide_timing_1 >= 0 &
fungicide_timing_1 < 4 &
!is.na(fungicide_application_2) ~ "Recommended_plus",
fungicide_timing_1 >= 4 &
!is.na(fungicide_application_2) ~ "Late_plus",
TRUE ~ "Other"
)
)
PM_MB_dat[PM_MB_dat$fungicide_ai == "control",
c(
"fungicide_timing_1",
"fungicide_timing_2",
"fungicide_timing_3",
"spray_management"
)] <- "control"
```
Now to view the number break-down of the `spray_management` treatments
```{r tableSprayManagement}
table(PM_MB_dat$spray_management)
```
'Early_plus' treatments are few in number, these treatments will have too few comparisons with other treatments in the meta-analysis to provide accurate results. Therefore we will remove 'Early_plus' from the analysis.
```{r simpler_clusters_remove}
PM_MB_dat <-
PM_MB_dat %>%
filter(spray_management != "Early_plus")
```
*****
## Trial factor
We have a number of variable which could introduce variation in our analysis.
We will combine these variables into a single variable called `trial` which will help us define the between trial variation.
```{r define_trial}
PM_MB_dat <-
PM_MB_dat %>%
mutate(trial = paste(
trial_ref,
year,
location,
host_genotype,
row_spacing,
sep = "_")) %>%
select(trial_ref, trial, everything())
```
*****
## Test dose effect
Earlier, in section [Fungicide Doses](#fungicide-doses) we noted there were different doses of propiconazole, which might influence our analysis.
The current label for custodia [PER82104](http://permits.apvma.gov.au/PER82104.PDF), Containing: 120 g/L azoxystrobin and 200 g/L tebuconazole, recommends between 300 ml - 600 ml of the tebuconazole fraction per hectare, equivalent to 60 - 120 grams per hectare.
These label rates are assumed to be the same for propiconazole infering these concentrations might be within the recommended rates.
It is also worth noting the APVMA permit [PER13979](http://permits.apvma.gov.au/PER13979.PDF) which allows the use of Tebuconazole between 62.35 g / ha and 83 g / ha depending on the product formulation.
Let's add the dose variable to the `trial` variable then compare linear mixed effect models including and excluding dose in the trial variable.
We already have a factor breaking down the dose in the `trial` variable.
Let's make a new one without dose.
```{r define_Dose}
table(PM_MB_dat$dose_ai.ha)
PM_MB_dat <-
PM_MB_dat %>%
mutate(dose = case_when(
dose_ai.ha == 0 ~ "control",
dose_ai.ha <= 70 ~ "lowDose",
dose_ai.ha > 70 ~ "highDose",
TRUE ~ NA_character_
))
```
```{r dose_test}
PM_MB_dat <-
PM_MB_dat %>%
mutate(trial_dose = paste(trial_ref, year, location, host_genotype,
row_spacing,dose , sep = "_"))
cbind(NoDose = head(PM_MB_dat$trial),
Dose = head(PM_MB_dat$trial_dose)) %>%
kable()
```
Let's test the dose effect in a linear mixed effect model.
```{r dose_lmer}
m_noDose <-
lmer(log(grain_yield.t.ha. * 1000) ~ factor(spray_management) +
(1 | trial),
data = PM_MB_dat)
m_dose <-
lmer(log(grain_yield.t.ha. * 1000) ~ factor(spray_management) +
(1 | trial_dose),
data = PM_MB_dat)
anova(m_noDose, m_dose)
```
Using the model without dose produces a lower AIC and we find no significant difference between models.
We will not use dose as a defining variable in `trial`.
******
## Row spacing
We may want to consider row spacing in the trials.
However one of the locations has `NA` as the row spacing.
```{r}
PM_MB_dat%>%
filter(is.na(row_spacing))%>%
select(trial_ref,year, location, row_spacing,spray_management,host_genotype, fungicide_ai)
```
For now let's assign the most used row spacing of 0.33 m as an approximation so the model can analyse this as a continuous variable.
If we decide to use the row_spacing column as a factor in the final model we will need to remove this approximation.
```{r assign_row_spacing_missing}
PM_MB_dat[is.na(PM_MB_dat$row_spacing),"row_spacing"] <- 0.33
```
********
## Remove unnecersary variables
```{r saveDat}
PM_MB_dat <-
PM_MB_dat %>%
select(-c(trial_dose,
fungicide_timing_1,fungicide_timing_2, fungicide_timing_3,
dose, fungicide_application_4, fungicide_application_5,
fungicide_application_6, fungicide_application_7))
```
## Final data check
Now we have defined our treatments lets inspect plots to visualise how well the data compares across the trial years and locations?
```{r table_of_nTreatments}
# class spray_management as a factor and reorder them for the plot
PM_MB_dat$spray_management <-
factor(PM_MB_dat$spray_management, rev(
c(
"control",
"Early",
"Recommended",
"Recommended_plus",
"Late",
"Late_plus"
)
))
ggplot(PM_MB_dat, aes(x = as.factor(year), fill = spray_management)) +
geom_bar(position = position_dodge2(width = 0.9, preserve = "single")) +
scale_fill_usq(name = "Spray Management") +
xlab("Year")
kable(
table(PM_MB_dat$spray_management, PM_MB_dat$year),
align = rep('c', 8),
caption = "Which treatments and how many treatments are represented in each year"
) %>%
kable_styling(
"striped",
fixed_thead = TRUE,
full_width = FALSE,
position = "center"
)
```
Treatments `Late_plus` and `Early` don't have very good comparison to other treatments.
Let's visualise the spread of data in each treatments with box-plots.
```{r treatment_means_plot}
PM_MB_dat %>%
ggplot(
aes(
y = grain_yield.t.ha.,
x = spray_management,
fill = spray_management,
colour = spray_management
)
) +
geom_boxplot(alpha = 0.25) +
geom_jitter(width = 0.1, alpha = 0.5) +
labs(x = "Spray management variable",
y = "Grain yield (t/Ha)",
title = "Mean grain yield from each treatment \n categorised by spray management scenario") +
theme(plot.title = element_text(hjust = 0.5)) +
geom_hline(yintercept = 0, linetype = 2) +
scale_fill_usq() +
scale_colour_usq() +
coord_flip() +
guides(fill = FALSE, color = FALSE)
```
There seems like little difference between the treatments, with exception of `Late_plus`.
Let's do this plot again, but let's look at the proportional mean difference between each treatment and the no spray control for each study, this should reduce variation in yield due between trials.
```{r plot_yieldProportion}
PM_MB_dat %>%
filter(spray_management != "control") %>%
ggplot(
aes(
y = prop_YG,
x = spray_management,
fill = spray_management,
colour = spray_management
)
) +
geom_boxplot(alpha = 0.25) +
geom_jitter(width = 0.1, alpha = 0.5) +
labs(x = "Spray management variable",
y = "Grain yield (t/Ha)",
title = "Mean grain yield from each treatment \n categorised by spray management scenario") +
theme(plot.title = element_text(hjust = 0.5)) +
geom_hline(yintercept = 0, linetype = 2) +
scale_fill_usq() +
scale_colour_usq() +
coord_flip() +
guides(fill = FALSE, color = FALSE)
```
Using the mean difference in the treatment effect seems to show the effect of each treatment better than just raw yield and reduces the variability in the treatments.
We know there is a good deal of variability in our studies and between them.
Mungbean produces variable yields between seasons so ordinarily we should use a response that highlights the difference in the treatment effects that we are interested in and reduce the variability.
However for an arm-based analysis, which we intend to do, we calculate the differences between control and treatment after the meta-analysis.
### Simplify and save data
```{r simplifyData}
PM_MB_dat <-
PM_MB_dat %>%
mutate(spray_management =
factor(
spray_management,
c(
"control",
"Early",
"Recommended",
"Recommended_plus",
"Late",
"Late_plus"
)
),
id = row_number(),
vi = yield_error,
n = replicates) %>%
select(
trial,
trial_ref,
location,
year,
row_spacing,
host_genotype,
spray_management,
fungicide_ai,
grain_yield.t.ha.,
yield_gain,
vi,
vi2,
n,
id)
```
```{r saveData_PrepD}
save(PM_MB_dat, file = here("cache/PrepData.Rdata"))
```