-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathfig_working_compiled.RMD
3401 lines (2655 loc) · 95.5 KB
/
fig_working_compiled.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
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
---
title: "FIG Data Analysis"
author: "Jesse Puka-Beals"
date: '`r format(Sys.Date(), "%m %d %Y")`'
output: html_document
editor_options:
markdown:
wrap: 72
chunk_output_type: console
chunk_output_type: console
---
# Overview
This markdown file has everything you need for the forage timing "FIG" experiment. Data import, data analysis, figure generation, table generation and commentary along the way.
# Settings
```{r setup}
knitr::opts_chunk$set(echo = FALSE,
message = FALSE,
warning = FALSE)
if (!require("pacman"))
install.packages("pacman")
pacman::p_load(
tidyverse,
lattice,
MASS, #fitting distributions
agricolae,
nlme, #when homogeneity of variance is violated
lme4,
car,
lsmeans, #least square means from mixed models
emmeans, #estimating marginal means from mixed models
multcomp, #compact letter display of pairwise mean comparisons
# multcompView,
# googlesheets4,
# googledrive,
lubridate, #working with dates
ggpubr, #figure arrangement, similar to cowplot
RColorBrewer,
plotrix # std.error summary
)
options(scipen = 999) #reduce scientific notation
options(digits = 4) # only print 4 sig figs
# options(device = "windows")
source("ggplot_custom_theme.R")
theme_set(theme_jpb())
# scale_y_continuous(labels=round_decimals)
round_decimals <- function(x) sprintf("%.0f", x)
# Remember to add chunk output in console for figure generation
cbp <- c("#999999", "#E69F00", "#56B4E9", "#009E73", "#F0E442", "#0072B2", "#D55E00", "#CC79A7")
```
```{r import data}
source("fig_import-data.R")
```
## missing and problematic data
*missing data*\
The data for both follow-up cuts of at the R70 site in 2018 are missing
from the dataset\
The data from some plots where the spring harvest was at the grain stage
is missing from the dataset
*problematic data*\
The IWG at the I2 site was 6 years old and did not grow back after the
first spring harvest. Of the data that was collected for the spring
harvest in 2017 (environment=I2.2017), the data had a negative
relationship between accumulated GDD and yield, unlike all other
environments. Considering the I2.2017 IWG did not grow back after the
spring harvest and the negative relationship between GDD and yield in
2017, its very likely the old age of this site imposed a large random
effect on the response variables of interest in this experiment. Since
the other two sites did not have such an old kernza stand, we decided
that including the I2 site in our pooled analysis would likely reduce
our ability to detect the effects of our treatments on the responses
variables of interest in this experiment, and that the inclusion of a
dataset from a 6 year old kernza field would not be meaningful to our
research questions. As a result, all data from the I2 site was removed
from the dataset we analyzed to answer our research questions.
```{r working datasets, include=FALSE, cache=TRUE}
dat1 <- dat_wide %>%
filter(timing.1cut!="grain" & site!="I2")
#for response variables ending in .1cut
dat3 <- dat_wide %>%
filter(site!="I2"&env!="R70.2018"&timing.1cut!="grain") %>%
filter(id!=164) %>% #removing NA for total yield
filter(id!=179) #removing NA from RFV.total
# for response variables ending in .2cut or .3cut
```
## outliers
Outlier identification was subjective, using boxplots to identify
outliers within environments and then deciding whether the outlier value
was reasonable.
In general, for high value outliers, if the distance between outlier and
the maximum was greater than the distance between the maximum and the
upper quartile, it was considered an unreasonable outlier and subject to
removal from the dataset. Similarly for low value outliers, if the
distance between the outlier and the minimum was greater than the
distance between the minimum and the lower quartile, it was considered
an unreasonable outlier and subject to removal from the dataset
```{r eval=T}
library(lattice)
bwplot(~yield.1cut|env,dat1)
dat1.y1.out1 <- dat1 %>%
filter(env=="R100.2019"&yield.1cut>10000) %>%
dplyr::select(id,yield.1cut) %>%
pull(id)
dat1.y1.out2 <- dat1 %>%
filter(env=="R70.2017"&yield.1cut>6000) %>%
dplyr::select(id,yield.1cut) %>%
pull(id)
dat1.y1.out <- c(dat1.y1.out1,dat1.y1.out2)
rm(dat1.y1.out1,dat1.y1.out2)
bwplot(~yield.2cut|env,dat3)
bwplot(~yield.3cut|env,dat3)
bwplot(~yield.total|env,dat3)
bwplot(~RFQ.1cut|env,dat1)
dat1.q1.out <- dat1 %>%
filter(env=="R70.2017"&RFQ.1cut>160) %>%
dplyr::select(id,RFQ.1cut) %>%
pull(id)
bwplot(~RFQ.2cut|env,dat3)
bwplot(~RFQ.3cut|env,dat3)
```
In R100.2019, plot 111 is also listed as an outlier because it's yield
for the first cut was
`r subset(dat1, env=="R100.2019" & plot=="111")$yield.1cut`,
aproximately 10x below the median yield for that treatment in that
environment, which was
`r median(subset(dat1, env=="R100.2019" & treatment=="DO")$yield.1cut)`.
```{r outlier-free datasets, include=FALSE, cache=TRUE}
dat_wide %>%
filter(env=="R100.2019") %>%
dplyr::select(id,plot,yield.1cut) %>%
arrange(yield.1cut)
dat1.y1.out <- c(dat1.y1.out,107)
dat1.y1.df <- dat1 %>%
filter(id!=138) %>%
filter(id!=187) %>%
filter(id!=107)
#this dataframe is for analysis of yield.1cut responses
dat1.q1.df <- dat1 %>%
filter(id!=189)
#this dataframe is for analysis of RFQ.1cut responses
#dat1 and dat3 are sufficient for all other cases
rm(dat1.q1.out,dat1.y1.out)
```
```{r custom colors for treatment}
## custom colors
# boot = green
# anthesis = yellow
# dough = brown
# none = light shade
# september = dark shade
# october = darkest shade
# ?brewer.pal()
# brewer.pal.info
## try diverging
# display.brewer.all(select = "Spectral")
# display.brewer.all(select = c("BrBG","PiYG"))
# display.brewer.all(select = 1:9)
# display.brewer.pal(name ="BrBG", n=9)
## try qualitative
# display.brewer.pal(name = "Set3", n=10)[c(1:8,10)]
# want it to go from green as boot to brown for dough and I want better distinction between groups
## will combine multiple palletes
# ?display.brewer.all
# display.brewer.all(type = "div")
brewer.pal(n=11,name = "BrBG")[c(3,2,1)] -> dough.colors
brewer.pal(n=11,name = "Spectral")[c(6,5,4)] -> anthesis.colors
brewer.pal(n=11,name = "RdYlGn")[c(8:10)] -> boot.colors
colors_treatments <- c(boot.colors,
anthesis.colors,
dough.colors)
levels(dat3$treatment)
# we just won't assign colors to grain cuts since we're removing them from analysis
# colors_treatments <-brewer.pal(name = "Set3", n=10)[c(1:8,10)]
names(colors_treatments) <- levels(dat3$treatment)
treatment_colors <- scale_colour_manual(name = "Harvest schedules", values = colors_treatments)
treatment_fill <- scale_fill_manual(name = "Harvest schedules", values = colors_treatments)
```
# Run all code above
```{r eval=F}
dat1
#for response variables ending in .1cut
dat1.y1.df
# for analysis of yield.1cut responses
dat1.q1.df
# for analysis of RFQ.1cut responses
dat3
# for response variables ending in .2cut .3cut or .total
```
# Analysis progression
Whole plot: Timing of first cut
Split plot: intensity of harvest
We organize this analysis based on response variable
*yield* : yield.total, yield.1cut, yield.2cut, yield.3cut.
*Quality* :RFQ.total, RFQ.1cut, RFQ.2cut, RFQ.3cut
*Profitability* : return.total, return.1cut, return.2cut, return.3cut
For all responses, we begin with a global model, as described by
Anderson and Burnham (2002).
Our global model is a linear mixed effect model containing the following
effects...
*Fixed effects*: timing, intensity, timing*intensity
*Random effects*: block nested within site, timing, env
Following analysis by our global model, we examined the distribution of
the response and the variance explained by random effects. We then
attempted to achieve a more parsimonious model by using generalized linear
mixed effect models that more accurately describe the distribution of
the response and we remove random effects that do not explain variance.
We test the fit of the improved models using AIC criterion and
log-liklihood tests. If all random effects are removed from a model, we
also perform box-cox corrections.
For the purposes of the publication and this rmarkdown file, we only report the global model outputs. See fig.analysis.rmd for an in-depth analysis, which ultimately finds the conclusions of the various models to be the same.
*Naming conventions of objects: objects are named using the convention
[response][effect][model number]. Response naming conventions
are...yt=yield total, y1=yield first cut, q1=relative forage quality of
first cut, r1=net returns of first cut. Effect naming conventions
are...tr=treatment, t=timing.1cut, i=follow-up cut/intensity of harvest.
For example, an object named y1.t.f.1 means the response variable is the
yield of the first cut, the fixed effects are the timing of the first
cut and the follow-up cut treatment, and this is the first model.*
*Note: we use "follow-up cut" in the analysis, but we use "harvest
intensity" in the manuscript. They are the same thing* *Note: we use
"year 1" and "year 2" to refer to the years of data collection in the
experiment, not the age of the kernza stand* *Note: we use the dat3
dataset when we are looking at yearly totals of yield and quality
because R70.2018 lacked september and october harvests*
# Histograms
Each combination of timing of first cutting and number of cutting is a unique harvest approach/system.
Compare the harvest systems in different environments (fields and years)
We can look at cumulative responses that are summarized across the year or we can look at responses at the cutting time. For the latter, we also want to facet_wrap by the cutting (xcut)
```{r histogram cumulative yield by treatment}
dat3 %>%
mutate(treatment = fct_recode(treatment,
"Anthesis-1cut" = "AN",
"Anthesis-2cut" = "AS",
"Anthesis-3cut" = "AO",
"Boot-1cut" = "BN",
"Boot-2cut" = "BS",
"Boot-3cut" = "BO",
"Dough-1cut" = "DN",
"Dough-2cut" = "DS",
"Dough-3cut" = "DO")) %>%
mutate(env = fct_relevel(env,
"R70.2017")) %>%
mutate(env = fct_recode(env,
"Field = R100 | Year = 2018" = "R100.2018",
"Field = R100 | Year =2019" = "R100.2019",
"Field = R70 | Year = 2017" = "R70.2017")) %>%
mutate(yield.total = yield.total/1000) %>%
ggplot(aes(x=yield.total,
fill=treatment)) +
stat_bin(bins = 30) +
treatment_fill +
facet_wrap(~env,
ncol = 1) +
labs(x="Cumulative forage yield\n(Mg ha yr)") +
scale_x_continuous(labels = round_decimals)
ggsave(filename = "histogram_yield-cumulative.png",
path = "figures",
width = 5,
height = 5,
units = "in",
dpi = 400)
```
```{r histogram cumulative RFV by treatment}
dat3 %>%
mutate(treatment = fct_recode(treatment,
"Anthesis-1cut" = "AN",
"Anthesis-2cut" = "AS",
"Anthesis-3cut" = "AO",
"Boot-1cut" = "BN",
"Boot-2cut" = "BS",
"Boot-3cut" = "BO",
"Dough-1cut" = "DN",
"Dough-2cut" = "DS",
"Dough-3cut" = "DO")) %>%
mutate(env = fct_relevel(env,
"R70.2017")) %>%
mutate(env = fct_recode(env,
"Field = R100 | Year = 2018" = "R100.2018",
"Field = R100 | Year =2019" = "R100.2019",
"Field = R70 | Year = 2017" = "R70.2017")) %>%
ggplot(aes(x=RFV.total,
fill=treatment)) +
stat_bin(bins = 30) +
treatment_fill +
facet_wrap(~env,
ncol = 1) +
labs(x="Cumulative forage quality\n(relative feed value)")
ggsave(filename = "histogram_RFV-cumulative.png",
path = "figures",
width = 5,
height = 5,
units = "in",
dpi = 400)
```
```{r histogram cumulative return by treatment}
dat3 %>%
mutate(treatment = fct_recode(treatment,
"Anthesis-1cut" = "AN",
"Anthesis-2cut" = "AS",
"Anthesis-3cut" = "AO",
"Boot-1cut" = "BN",
"Boot-2cut" = "BS",
"Boot-3cut" = "BO",
"Dough-1cut" = "DN",
"Dough-2cut" = "DS",
"Dough-3cut" = "DO")) %>%
mutate(env = fct_relevel(env,
"R70.2017")) %>%
mutate(env = fct_recode(env,
"Field = R100 | Year = 2018" = "R100.2018",
"Field = R100 | Year =2019" = "R100.2019",
"Field = R70 | Year = 2017" = "R70.2017")) %>%
ggplot(aes(x=return.total,
fill=treatment)) +
stat_bin(bins = 30) +
treatment_fill +
facet_wrap(~env,
ncol = 1) +
labs(x="Net return ($ per year)")
ggsave(filename = "histogram_net-returns-cumulative.png",
path = "figures",
width = 5,
height = 5,
units = "in",
dpi = 400)
```
```{r histogram yield at cutting by treatment 1}
dat_long %>%
filter(env != "I2.2017") %>%
filter(treatment != "GN" &
treatment != "GS" &
treatment != "GO") %>%
mutate(treatment = fct_recode(treatment,
"Anthesis-1cut" = "AN",
"Anthesis-2cut" = "AS",
"Anthesis-3cut" = "AO",
"Boot-1cut" = "BN",
"Boot-2cut" = "BS",
"Boot-3cut" = "BO",
"Dough-1cut" = "DN",
"Dough-2cut" = "DS",
"Dough-3cut" = "DO")) %>%
mutate(env = fct_relevel(env,
"I2.2017",
"R70.2017",
"R70.2018")) %>%
mutate(env = fct_recode(env,
"Field = R100 | Year = 2018" = "R100.2018",
"Field = R100 | Year =2019" = "R100.2019",
"Field = R70 | Year = 2017" = "R70.2017",
"Field = R70 | Year = 2018" = "R70.2018",
"Field = I2 | Year = 2017" = "I2.2017")) %>%
mutate(xcut = fct_recode(xcut,
"First cut" = "1",
"Second cut" = "2",
"Third cut" = "3")) %>%
mutate(yield = yield/1000) %>%
ggplot(aes(x=yield,
fill=treatment)) +
stat_bin(bins = 30) +
treatment_fill +
facet_grid(xcut~env) +
scale_x_continuous(labels = round_decimals) +
labs(x="Forage yield\n(Mg ha)")
ggsave(filename = "histogram_yield-at-cutting-1.png",
path = "figures",
width = 11,
height = 6,
units = "in",
dpi = 400)
```
```{r histogram yield at cutting by treatment 2}
dat_long %>%
# filter(env != "I2.2017") %>%
filter(treatment != "GN" &
treatment != "GS" &
treatment != "GO") %>%
mutate(treatment = fct_recode(treatment,
"Anthesis-1cut" = "AN",
"Anthesis-2cut" = "AS",
"Anthesis-3cut" = "AO",
"Boot-1cut" = "BN",
"Boot-2cut" = "BS",
"Boot-3cut" = "BO",
"Dough-1cut" = "DN",
"Dough-2cut" = "DS",
"Dough-3cut" = "DO")) %>%
mutate(env = fct_relevel(env,
"I2.2017",
"R70.2017",
"R70.2018")) %>%
mutate(env = fct_recode(env,
"Field = R100\nYear = 2018" = "R100.2018",
"Field = R100\nYear =2019" = "R100.2019",
"Field = R70\nYear = 2017" = "R70.2017",
"Field = R70\nYear = 2018" = "R70.2018",
"Field = I2\nYear = 2017" = "I2.2017")) %>%
mutate(xcut = fct_recode(xcut,
"First cut" = "1",
"Second cut" = "2",
"Third cut" = "3")) %>%
mutate(yield = yield/1000) %>%
ggplot(aes(x=yield,
fill=treatment)) +
stat_bin(bins = 30) +
treatment_fill +
facet_grid(env~xcut,
scales = "free") +
scale_y_continuous(labels = round_decimals) +
theme(panel.spacing = unit(.8, "lines"))
labs(x="Forage yield\n(Mg ha)")
ggsave(filename = "histogram_yield-at-cutting-2.png",
path = "figures",
width = 6,
height = 6,
units = "in",
dpi = 400)
```
```{r histogram_yield-at-cutting by timing.1cut and number of observations}
# histogram of yield for all cuttings
dat_long %>%
filter(timing.1cut!="grain") %>%
mutate(xcut = fct_recode(xcut,
"1st cutting" = "1",
"2nd cutting" = "2",
"3rd cutting" = "3")) %>%
group_by(xcut,env) %>%
summarise(na.count = sum(is.na(yield.Mg)),
n = n(),
nn = n-na.count,
yield.Mg = mean(na.omit(yield.Mg))) %>%
mutate(text = paste0("n=",nn))-> ncount_dat_long
dat_long %>%
filter(timing.1cut!="grain") %>%
mutate(xcut = fct_recode(xcut,
"1st cutting" = "1",
"2nd cutting" = "2",
"3rd cutting" = "3")) %>%
ggplot(aes(yield.Mg,
)) +
stat_bin(
aes(
fill=timing.1cut
),
position = position_dodge(),
bins=10,
col=1
) +
geom_text(
data=ncount_dat_long,
aes(
label = text,
x=8,
y=9
)) +
facet_grid(env~xcut
# scales = "free",
# ncol = 3
) +
labs(x=expression("Forage yield" ~ (Mg ~ ha^{-1})),
y="Count") +
scale_fill_manual(values = brewer.pal(n=9,name="Set1")[c(3,6,5)]) +
scale_y_continuous(limits = c(0,11.3),
expand = c(0,0))
ggsave("histogram_yield-with-counts-by-timing1cut.png",
path = "figures",
width = 6.5,
height = 6,
units = "in",
dpi = 400)
```
```{r histogram_yield only third cutting by timing.1cut with number of observations}
# histogram but only for third cutting
# this is mostly balanced data
dat_long %>%
# distinct(follow.cut)
filter(timing.1cut!="grain") %>%
filter(follow.cut=="october") %>%
mutate(xcut = fct_recode(xcut,
"1st cutting" = "1",
"2nd cutting" = "2",
"3rd cutting" = "3")) %>%
# summary()
group_by(xcut,env) %>%
# tally(yield.Mg)
summarise(na.count = sum(is.na(yield.Mg)),
n = n(),
nn = n-na.count,
yield.Mg = mean(na.omit(yield.Mg))) %>%
mutate(text = paste0("n=",nn))-> ncount2_dat_long
dat_long %>%
filter(timing.1cut!="grain") %>%
filter(follow.cut=="october") %>%
mutate(xcut = fct_recode(xcut,
"1st cutting" = "1",
"2nd cutting" = "2",
"3rd cutting" = "3")) %>%
# glimpse()
ggplot(aes(yield.Mg,
# col=timing.1cut
# col=xcut
)) +
# geom_density(
# # bw=800
# # bw=400
# aes(col=timing.1cut)
# ) +
stat_bin(
aes(
fill=timing.1cut
),
position = position_dodge(),
bins=10,
col=1
) +
# geom_density(aes(
# y=..count..,
# col=timing.1cut,
# fill = timing.1cut
# ),
# size=1,
# alpha=.5) +
geom_text(
data=ncount2_dat_long,
aes(
label = text,
x=8,
y=9
)) +
facet_grid(env~xcut
# scales = "free",
# ncol = 3
) +
labs(x=expression("Forage yield" ~ (Mg ~ ha^{-1})),
y="Count") +
scale_fill_manual(values = brewer.pal(n=9,name="Set1")[c(3,6,5)]) +
scale_y_continuous(limits = c(0,11.3),
expand = c(0,0))
ggsave("histogram_yield_3-cut-only.png",
path = "figures",
width = 6.5,
height = 6,
units = "in",
dpi = 400)
```
```{r histogram_rfq all cuttings}
# histogram of rfq for all cuttings
dat_long %>%
mutate(xcut = fct_recode(xcut,
"1st cutting" = "1",
"2nd cutting" = "2",
"3rd cutting" = "3")) %>%
# summary()
group_by(xcut,env) %>%
# tally(yield.Mg)
summarise(na.count = sum(is.na(RFQ)),
n = n(),
nn = n-na.count,
yield.Mg = mean(na.omit(RFQ))) %>%
mutate(text = paste0("n=",nn))-> ncount_dat_long
dat_long %>%
mutate(xcut = fct_recode(xcut,
"1st cutting" = "1",
"2nd cutting" = "2",
"3rd cutting" = "3")) %>%
ggplot(aes(RFQ,
# col=timing.1cut
# col=xcut
)) +
stat_bin(
aes(
fill=timing.1cut
),
position = position_dodge(),
bins=10,
col=1
) +
geom_text(
data=ncount_dat_long,
aes(
label = text,
x=175,
y=9
)) +
facet_grid(env~xcut
# scales = "free",
# ncol = 3
) +
labs(x=expression("Relative forage quality"),
y="Count") +
scale_fill_brewer(type = "qual",
palette = 1) +
scale_color_brewer(type = "qual",
palette = 1) +
scale_y_continuous(limits = c(0,11.3),
expand = c(0,0))
# +
# theme(panel.spacing = unit(.5, "lines"))
ggsave("histogram_rfq-color.png",
path = "figures",
width = 6.5,
height = 6,
units = "in",
dpi = 400)
# are we missing boot quality cuts for first cutting
dat_long %>%
filter(env=="R100.2018" &
xcut=="1" &
timing.1cut=="boot") %>%
dplyr::select(RFQ)
dat_long %>%
filter(env=="R100.2018") %>%
ggplot(aes(RFQ)) +
stat_bin() +
facet_grid(xcut~timing.1cut)
# no we aren't, it's just not showing up in histogram_rfq-color.png
```
```{r cumulative responses}
## yield.total
dat3 %>%
# glimpse()
mutate(yield.total = yield.total/1000) %>%
ggplot(aes(yield.total)) +
stat_bin(position = position_dodge(),
bins=18) +
geom_density(aes(y=..count..),
bw=2) +
facet_grid(follow.cut~timing.1cut) +
labs(x="Cumulative forage yield (Mg ha yr)") +
scale_x_continuous(limits = c(0,11))
ggsave("histogram_yield-cumulative.png",
path = "figures",
width = 6,
height = 4,
dpi=400)
## RFQ.total
dat3 %>%
ggplot(aes(RFQ.total)) +
stat_bin(position = position_dodge(),
bins=18) +
geom_density(aes(y=..count..*10),
bw=15) +
facet_grid(follow.cut~timing.1cut) +
labs(x="Cumulative relative feed value") +
scale_x_continuous(expand = c(.1,0))
ggsave("histogram_rfq.png",
path = "figures",
width = 6,
height = 4,
dpi=400)
## RFV.total
dat3 %>%
ggplot(aes(RFV.total)) +
stat_bin(position = position_dodge(),
bins=18) +
geom_density(aes(y=..count..*10),
bw=10) +
facet_grid(follow.cut~timing.1cut) +
labs(x="Cumulative relative feed value")
ggsave("histogram_rfv.png",
path = "figures",
width = 6,
height = 4,
dpi=400)
# return.total
dat3 %>%
ggplot(aes(return.total)) +
stat_bin(position = position_dodge(),
bins=18) +
geom_density(aes(y=..count..*100),
bw=90) +
facet_grid(follow.cut~timing.1cut) +
labs(x="Net returns")
ggsave("histogram_return.png",
path = "figures",
width = 6,
height = 4,
dpi=400)
```
Briefly, let's look at general trends in the data
```{r anova assumption testing}
# normality
dat3 %>%
.$yield.total %>%
shapiro.test(.)
# homogeneity of variance
bartlett.test(dat3$yield.total,
dat3$treatment)
# independence
# violated--plots sampled multiple times
# lol--every assumption is violated
```
# 3-way full model
```{r}
# yield.Mg
global.yield <- lmer(yield.Mg~timing.1cut*follow.cut*factor(xcut) +
(1|site/block/timing.1cut) + (1|env),
dat_long)
summary(global.yield)
car::Anova(global.yield)
# protein
global.protein <- lmer(protein~timing.1cut*follow.cut*factor(xcut) +
(1|site/block/timing.1cut) + (1|env),
dat_long)
# summary(global.protein)
car::Anova(global.protein)
# ADF
global.adf <- lmer(ADF~timing.1cut*follow.cut*factor(xcut) +
(1|site/block/timing.1cut) + (1|env),
dat_long)
# summary(global.adf)
car::Anova(global.adf)
# NDF
global.ndf <- lmer(NDF~timing.1cut*follow.cut*factor(xcut) +
(1|site/block/timing.1cut) + (1|env),
dat_long)
# summary(global.ndf)
car::Anova(global.ndf)
# NDFD48
global.ndfd <- lmer(NDFD48~timing.1cut*follow.cut*factor(xcut) +
(1|site/block/timing.1cut) + (1|env),
dat_long)
# summary(global.ndfd)
car::Anova(global.ndfd)
# RFV
global.rfv <- lmer(RFV~timing.1cut*follow.cut*factor(xcut) +
(1|site/block/timing.1cut) + (1|env),
dat_long)
# summary(global.rfv)
car::Anova(global.rfv)
```
This results in a rank deficient model, which is not necessarily a problem, but it isn't ideal. This may indicate that we simply don't have enough data to model the number of different parameters (interactions) we are asking the model to do. This makes sense to me.
Read these posts about the issue.
https://stackoverflow.com/questions/37090722/lme4lmer-reports-fixed-effect-model-matrix-is-rank-deficient-do-i-need-a-fi
https://stats.stackexchange.com/questions/35071/what-is-rank-deficiency-and-how-to-deal-with-it/35077#35077
```{r add annual cumulative yield into dat_long}
# need to unmelt data frame
# select every part of a plots identity except cutting
# site,year,plot
# rowsums yield, na.rm=T
```
# Yield.total
Response: total dry matter yield accumulated from a given plot over 1
year in kg ha
## global model
```{r with timing.1cut as random effect}
yt.t.i.1 <- lmer(yield.total~timing.1cut*follow.cut +
(1|site/block/timing.1cut) + (1|env),
dat3)
summary(yt.t.i.1)
car::Anova(yt.t.i.1)
# car::Anova(yt.t.i.1, test.statistic="F")
# car::Anova(yt.t.i.1, error.estimate="deviance")
cld(emmeans(yt.t.i.1, ~follow.cut))
multcomp::cld(emmeans(yt.t.i.1, ~follow.cut))
multcomp::cld(emmeans(yt.t.i.1, ~follow.cut),
Letters = LETTERS)
##making a model
# y=Tijk +
summary(yt.t.i.1)$coefficients
qqplot(yt.t.i.1)
plot(yt.t.i.1)
yt.t.i.1 <- lmer(log(yield.total)~timing.1cut*follow.cut +
(1|site/block/timing.1cut) + (1|env),
dat3)
densityplot(dat3$yield.total)
densityplot(sqrt(dat3$yield.total))
densityplot(log(dat3$yield.total))
```
## practice with predict function
```{r, eval=F}
pred <- dat3 %>%
dplyr::select(timing.1cut,follow.cut,yield.total,site,block,env) %>% mutate(yield.total.pred=NA)
pred$yield.total.pred<-predict(yt.t.i.1,pred);pred
pred %>%
ggplot(aes(timing.1cut)) +
# geom_point(aes(y=yield.total.pred),
# color="blue") +
# geom_point(aes(y=yield.total/1000),
# color="red") +
stat_summary(aes(y=yield.total.pred),
geom = "point",
size=4) +
stat_summary(aes(y=yield.total/1000),
geom = "point",
# color="orange",
size=4) +
labs(y="Mg ha forage")
#^ predicted data is much higher
```
# Yield.1cut
Response: total dry matter yield recorded at the first cut kg ha
## global model
```{r}
y1.t.i.1 <- lmer(yield.1cut~timing.1cut*follow.cut +
(1|site/block/timing.1cut) + (1|env),
dat1.y1.df)
summary(y1.t.i.1)
car::Anova(y1.t.i.1)[3]
cld(emmeans(y1.t.i.1, ~timing.1cut))