/
createTD.R
1096 lines (1081 loc) · 43.9 KB
/
createTD.R
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
#' S3 class TD
#'
#' \code{createTD}\cr
#' Function for creating objects of S3 class TD (Trial Data). The function
#' converts a data.frame to an object of class TD in the following steps:
#' \itemize{
#' \item{Check input data}
#' \item{Rename columns to default column names - default column names:
#' genotype, trial, loc, year, repId, subBlock, rowCoord, colCoord, rowId,
#' colId, checkId}
#' \item{Convert column types to default column types - rowCoord and colCoord
#' are converted to numeric columns, all other renamed columns to factor
#' columns. Columns other than the default columns, e.g. traits or other
#' covariates will be included in the output unchanged.}
#' \item{Split input data by trial - each trial in the input data will become
#' a list item in the output.}
#' \item{Add meta data - the trial meta data are added as attributes to the
#' different output items. The function parameters starting with "tr" provide
#' the meta data. Their values will be recycled if needed, so by setting a
#' single "trDesign", all trials will get the same design. For trLat, trLong,
#' trDesign and trDate a column in \code{data} that contains the information
#' can be specified as well. The meta data can be changed later on using
#' \code{getMeta} and \code{setMeta}}
#' }
#' \code{addTD}\cr
#' Function for adding extra trial data to an existing object of class TD. The
#' data for the new trials will be added after the data for existing trials. It
#' is possible to add data for an already existing trial, but this will cause
#' multiple items in the output with identical names, which might cause problems
#' later on in the analysis. Therefore a warning will be issued in this
#' case.\cr\cr
#' \code{dropTD}\cr
#' Function for removing data for selected trials from an existing object of
#' class TD.\cr\cr
#' \code{\link{summary.TD}} and \code{\link{plot.TD}} methods are available.
#'
#' @param data A data.frame containing trial data with at least a column for
#' genotype. The data.frame should be in a wide format, i.e. all available
#' phenotypic data should be in a separate column within the data.frame.
#' @param genotype An optional character string indicating the column in
#' \code{data} that contains genotypes.
#' @param trial An optional character string indicating the column in
#' \code{data} that contains trials.
#' @param loc An optional character string indicating the column in
#' \code{data} that contains trial locations.
#' @param year An optional character string indicating the column in \code{data}
#' that contains years.
#' @param repId An optional character string indicating the column in
#' \code{data} that contains replicates.
#' @param subBlock An optional character string indicating the column in
#' \code{data} that contains sub blocks.
#' @param plotId An optional character string indicating the column in
#' \code{data} that contains plots. This column will be combined with trial
#' to a single output factor.
#' @param rowCoord An optional character string indicating the column in
#' \code{data} that contains the row coordinates.
#' @param colCoord An optional character string indicating the column in
#' \code{data} that contains the column coordinates.
#' @param rowId An optional character string indicating the column in
#' \code{data} that contains field rows. If not supplied, this is assumed to
#' be the same as rowCoord.
#' @param colId An optional character string indicating the column in
#' \code{data} that contains field columns. If not supplied, this is assumed to
#' be the same as colCoord.
#' @param checkId An optional character string indicating the column in
#' \code{data} that contains the check IDs.
#' @param trLocation An optional character vector indicating the locations of
#' the trials. This will be used as default names when creating plots and
#' summaries. If no locations are provided, first the column loc is considered.
#' If this contains one unique value for a trial this is used as trLocation.
#' Otherwise the trial name is used.
#' @param trDate An optional character string indicating the column in
#' \code{data} that contains the date of the trial or a date vector indicating
#' the dates of the trials.
#' @param trDesign An optional character string indicating the column in
#' \code{data} that contains the design of the trial or a character vector
#' indicating the designs of the trials. Either "none" (no (known) design), "
#' ibd" (incomplete-block design), "res.ibd" (resolvable incomplete-block
#' design), "rcbd" (randomized complete block design), "rowcol" (row-column
#' design) or "res.rowcol" (resolvable row-column design).
#' @param trLat An optional character string indicating the column in \code{data}
#' that contains the latitude of the trial or a numerical vector indicating the
#' latitudes of the trials on a scale of -90 to 90.
#' @param trLong An optional character string indicating the column in
#' \code{data} that contains the latitude of the trial or a numerical vector
#' indicating the longitudes of the trials on a scale of -180 to 180.
#' @param trPlWidth An optional positive numerical vector indicating the
#' widths of the plots.
#' @param trPlLength An optional positive numerical vector indicating the
#' lengths of the plots.
#'
#' @return An object of class TD, a list of data.frames with renamed columns
#' and an attribute \code{renamedCols} containing an overview of renamed
#' columns. For each unique value of trial, the output has a data.frame in
#' the list with the same name as the trial. These data.frames have attributes
#' containing the metadata for the corresponding trial. If there is no column
#' for trial, the list will contain one item named after the input data.
#'
#' @examples
#' ## Create a data.frame to be converted to TD object.
#' ## The data consists of genotype, trial, row and column information and
#' ## two traits, yield and flowering time.
#' datT1 <- data.frame(geno = paste0("G", 1:10),
#' tr = "T1",
#' row = rep(1:5, each = 2),
#' col = rep(1:2, times = 5),
#' yield = 1:10,
#' flowering = 3:12)
#'
#' ## Convert data.frame to TD object.
#' TDT1 <- createTD(data = datT1,
#' genotype = "geno",
#' trial = "tr",
#' rowCoord = "row",
#' colCoord = "col")
#'
#' ## Create a second data.frame similar to the first with data for a second trial.
#' datT2 <- data.frame(geno = paste0("G", 1:10),
#' tr = "T2",
#' row = rep(1:2, each = 5),
#' col = rep(1:5, times = 2),
#' yield = 10:1,
#' flowering = 12:3)
#'
#' ## Add this data to the TD object created above.
#' TDTot <- addTD(TD = TDT1,
#' data = datT2,
#' genotype = "geno",
#' trial = "tr",
#' rowCoord = "row",
#' colCoord = "col")
#'
#' ## Drop the data for the first trial from the object.
#' TDT2 <- dropTD(TD = TDTot,
#' rmTrials = "T1")
#'
#' @family functions for TD objects
#'
#' @importFrom utils hasName
#'
#' @name TD
NULL
#' @rdname TD
#' @export
createTD <- function(data,
genotype = NULL,
trial = NULL,
loc = NULL,
year = NULL,
repId = NULL,
subBlock = NULL,
plotId = NULL,
rowCoord = NULL,
colCoord = NULL,
rowId = rowCoord,
colId = colCoord,
checkId = NULL,
trLocation = NULL,
trDate = NULL,
trDesign = NULL,
trLat = NULL,
trLong = NULL,
trPlWidth = NULL,
trPlLength = NULL) {
## Save name of original data for naming output.
dataName <- deparse(substitute(data))
if (length(dataName) > 1) {
dataName <- "dat"
}
## Checks.
if (missing(data) || !is.data.frame(data)) {
stop("data has to be a data.frame.\n")
}
## Convert input to data.frame. This needs to be done to be able to handle
## tibbles and possibly other data structures in the future.
data <- as.data.frame(data)
cols <- colnames(data)
for (param in c(genotype, trial, loc, year, repId, subBlock, plotId,
rowId, colId, rowCoord, colCoord, checkId)) {
if (!is.null(param) && (!is.character(param) || length(param) > 1 ||
!hasName(data, param))) {
stop(deparse(param), " has to be NULL or a column in data.\n")
}
}
checkTDMeta(trPlWidth = trPlWidth, trPlLength = trPlLength)
## Create list of reserved column names for renaming columns.
renameCols <- c("genotype", "trial", "loc", "year", "repId", "plotId",
"subBlock", "rowId", "colId", "rowCoord", "colCoord",
"checkId")
## First rename duplicate columns and add duplicated columns to data.
renameFrom <- as.character(sapply(X = renameCols, FUN = function(x) {
get(x)
}))
## Create a data.frame with renamed cols to add to TD as an attribute.
renamed <- data.frame(orig = renameFrom[renameFrom != "NULL"],
new = renameCols[renameFrom != "NULL"],
stringsAsFactors = FALSE)
## Get duplicate columns.
dupCols <- which(duplicated(renameFrom) & renameFrom != "NULL")
for (dupCol in dupCols) {
## Copy original column as extra column in data for each duplicate.
tempName <- paste0(".temp", dupCol)
data[tempName] <- data[, colnames(data) == renameFrom[dupCol]]
## Add new replacementname to cols and renameFrom.
cols[length(cols) + 1] <- tempName
renameFrom[dupCol] <- tempName
}
## Rename columns.
for (i in 1:length(renameCols)) {
cols[cols == renameFrom[i]] <- renameCols[i]
}
## Check for duplicates in cols.
## When there are renaming is attempted to an existing column.
dupCols <- cols[duplicated(cols)]
if (length(dupCols) > 0) {
stop("The following columns already exist in the input data:\n",
paste(dupCols, collapse = ","), "\n",
"Renaming another column to one of these is impossible.\n")
}
colnames(data) <- cols
## Convert columns to factor if neccessary.
factorCols <- c("genotype", "trial", "loc", "year", "repId", "subBlock",
"plotId", "rowId", "colId", "checkId")
for (factorCol in factorCols) {
if (hasName(data, factorCol) && !is.factor(data[[factorCol]])) {
data[cols == factorCol] <- as.factor(data[, cols == factorCol])
}
}
## Combine plotId and trial into a single factor if both are available.
## If trial is not available plotId itself was converted to factor in the
## previous step.
if (all(hasName(data, c("trial", "plotId")))) {
data$plotId <- interaction(data$trial, data$plotId, sep = "_")
}
## Convert columns to numeric if neccessary.
numCols <- c("rowCoord", "colCoord")
for (numCol in numCols) {
if (hasName(data, numCol) && !is.numeric(data[cols == numCol])) {
data[cols == numCol] <- as.numeric(data[, cols == numCol])
}
}
## Sort data by rowCoord and colCoord.
## This is only needed for spatial modeling with asreml but it doesn't harm
## always doing so.
if (all(hasName(data, c("rowCoord", "colCoord")))) {
data <- data[order(data[["rowCoord"]], data[["colCoord"]]), ]
## Check that row column combinations are unique within trials.
if (hasName(data, "trial")) {
rowColTab <- table(data[["trial"]], data[["rowCoord"]],
data[["colCoord"]])
if (any(rowColTab > 1)) {
warning("Combinations of row and column coordinates should be unique ",
"within trials.\n")
}
} else {
rowColTab <- table(data[["rowCoord"]], data[["colCoord"]])
if (any(rowColTab > 1)) {
warning("Combinations of row and column coordinates should be unique.\n")
}
}
}
if (hasName(data, "trial")) {
listData <- split(x = data, f = data[["trial"]], drop = TRUE)
} else {
listData <- setNames(list(data), dataName)
}
## Define meta data to set from input variables.
## trLat, trLong, trDate and trDesign might be defined from variables so
## are treated differently.
meta <- c("trLocation", "trPlWidth", "trPlLength")
## Expand input values for meta variables to number of trials.
metaVals <- sapply(X = meta, FUN = function(m) {
if (!is.null(get(m))) {
metaVal <- rep(x = get(m), length.out = length(listData))
if (is.null(names(metaVal)) || !all(hasName(listData, names(metaVal)))) {
names(metaVal) <- names(listData)
}
return(metaVal)
} else {
NULL
}
}, simplify = FALSE)
## Set meta for all trials in data.
for (tr in names(listData)) {
for (m in meta) {
## Set meta data. Set to NULL if not in input so meta variable is
## left out meta data. This to avoid a list of NULL.
attr(x = listData[[tr]], which = m) <- unname(metaVals[[m]][tr])
}
## Location should always be filled since it is used in plot titles as
## well. Use trial name as default value.
if (is.null(trLocation)) {
if (hasName(x = listData[[tr]], name = "loc") &&
length(unique(listData[[tr]][["loc"]])) == 1) {
attr(x = listData[[tr]],
which = "trLocation") <- as.character(listData[[tr]][["loc"]][1])
} else {
attr(x = listData[[tr]], which = "trLocation") <- tr
}
}
## If trLat or trLong are specifying a column in the data set meta data
## to the value for this column. To do so the value has to be unique within
## each trial.
trLatDat <- trLat
if (!is.null(trLat) && hasName(x = listData[[tr]], name = trLat)) {
## Set trLatDat to value within trial and check for uniqueness.
trLatDat <- unique(listData[[tr]][[trLat]])
if (!length(trLatDat) == 1) {
stop("trLat not unique for ", tr, ".\n")
}
}
trLongDat <- trLong
if (!is.null(trLong) && hasName(x = listData[[tr]], name = trLong)) {
## Set trLongDat to value within trial and check for uniqueness.
trLongDat <- unique(listData[[tr]][[trLong]])
if (!length(trLongDat) == 1) {
stop("trLong not unique for ", tr, ".\n")
}
}
## Check that combination of latitude and longitude set is valid.
chkLatLong(trLatDat, trLongDat)
attr(x = listData[[tr]], which = "trLat") <- trLatDat
attr(x = listData[[tr]], which = "trLong") <- trLongDat
## If trDate is specifying a column in the data set meta data to the value
## for this column. To do so the value has to be unique within each trial.
trDateDat <- trDate
if (!is.null(trDate) && hasName(x = listData[[tr]], name = trDate)) {
## Set trDateDat to value within trial and check for uniqueness.
trDateDat <- unique(listData[[tr]][[trDate]])
if (!length(trDateDat) == 1) {
stop("trDate not unique for ", tr, ".\n")
}
}
attr(x = listData[[tr]], which = "trDate") <- trDateDat
## If trDesign is specifying a column in the data set meta data to the value
## for this column. To do so the value has to be unique within each trial.
trDesignDat <- trDesign
if (!is.null(trDesign) && hasName(x = listData[[tr]], name = trDesign)) {
## Set trDateDat to value within trial and check for uniqueness.
trDesignDat <- unique(listData[[tr]][[trDesign]])
if (!length(trDesignDat) == 1) {
stop("trDesign not unique for ", tr, ".\n")
}
}
chkDesign(trDesignDat)
attr(x = listData[[tr]], which = "trDesign") <- trDesignDat
## Add a list of columns that have been renamed as attribute to TD.
attr(x = listData[[tr]], which = "renamedCols") <-
if (nrow(renamed) > 0) renamed else NULL
}
TD <- structure(listData,
class = c("TD", "list"))
return(TD)
}
#' @param TD An object of class TD which should be modified.
#'
#' @rdname TD
#' @export
addTD <- function(TD,
data,
genotype = NULL,
trial = NULL,
loc = NULL,
year = NULL,
repId = NULL,
subBlock = NULL,
plotId = NULL,
rowCoord = NULL,
colCoord = NULL,
rowId = rowCoord,
colId = colCoord,
checkId = NULL,
trLocation = NULL,
trDate = NULL,
trDesign = NULL,
trLat = NULL,
trLong = NULL,
trPlWidth = NULL,
trPlLength = NULL) {
TDNw <- createTD(data = data, genotype = genotype, trial = trial,
loc = loc, year = year, repId = repId,
subBlock = subBlock, plotId = plotId, rowCoord = rowCoord,
colCoord = colCoord, rowId = rowId, colId = colId,
checkId = checkId, trLocation = trLocation, trDate = trDate,
trDesign = trDesign, trLat = trLat, trLong = trLong,
trPlWidth = trPlWidth, trPlLength = trPlLength)
dupTrials <- names(TDNw)[names(TDNw) %in% names(TD)]
if (length(dupTrials) > 0) {
warning("The following trials already existed in TD and will be added ",
"again: ", paste(dupTrials, collapse = ", "), ".\n", call. = FALSE)
}
TDTot <- c(TD, TDNw)
class(TDTot) <- c("TD", "list")
return(TDTot)
}
#' @param rmTrials A character vector of trials that should be removed.
#'
#' @rdname TD
#' @export
dropTD <- function(TD,
rmTrials) {
naTrials <- rmTrials[!rmTrials %in% names(TD)]
if (length(naTrials) > 0) {
warning("The following trials are not in TD: ",
paste(naTrials, collapse = ", "), ".\n", call. = FALSE)
}
leftTrials <- names(TD)[!names(TD) %in% rmTrials]
if (length(leftTrials) == 0) {
warning("All trials have been removed from TD.\n", call. = FALSE)
}
return(TD[!names(TD) %in% rmTrials])
}
#' Summarizing objects of class \code{TD}
#'
#' \code{summary} method for class \code{TD}.
#'
#' @param object An object of class TD.
#' @param ... Further arguments - currently not used.
#' @param trial A character string specifying the trial to be summarized.
#' @param traits A character vector specifying the traits to be summarized.
#' @param groupBy A character string specifying a column in TD by which the
#' summary should be grouped. If \code{NULL}, no grouping is done.
#' @param what A character vector indicating which summary statistics should be
#' computed. If \code{what = "all"}, all available statistics are computed.\cr
#' Possible options are:
#' \describe{
#' \item{nVals}{The number of values, i.e. non-missing + missing values.}
#' \item{nObs}{The number of non-missing observations.}
#' \item{nMiss}{The number of missing values.}
#' \item{mean}{The mean.}
#' \item{median}{The median.}
#' \item{min}{The minimum.}
#' \item{max}{The maximum.}
#' \item{range}{The range (maximum - minimum).}
#' \item{firstQ}{The first (25pct) quantile.}
#' \item{thirdQ}{The third (75pct) quantile.}
#' \item{sd}{The standard deviation.}
#' \item{seMean}{The standard error of mean.}
#' \item{var}{The variance.}
#' \item{seVar}{The standard error of variance.}
#' \item{CV}{The coefficient of variation.}
#' \item{sum}{The sum.}
#' \item{sumSq}{The sum of squares.}
#' \item{uncorSumSq}{The uncorrected sum of squares.}
#' \item{skew}{The skewness.}
#' \item{seSkew}{The standard error of the skewness.}
#' \item{kurt}{The kurtosis.}
#' \item{seKurt}{The standard error of the kurtosis.}
#' \item{all}{All summary statistics.}
#' }
#'
#' @return A table containing the selected summary statistics.
#'
#' @family functions for TD objects
#'
#' @examples
#' ## Summarize TDHeat05.
#' summary(TDHeat05,
#' traits = "yield")
#'
#' ## Summarize TDHeat05 grouping by repId.
#' summary(TDHeat05,
#' traits = "yield",
#' groupBy = "repId")
#'
#' @export
summary.TD <- function(object,
...,
trial = names(object),
traits,
groupBy = NULL,
what = if (!is.null(groupBy)) {
c("nObs", "mean", "sd")
} else {
c("nObs", "nMiss", "mean", "median", "min", "max",
"firstQ", "thirdQ", "var")
}) {
allStat <- data.frame(stat = c("nVals", "nObs", "nMiss", "mean", "median",
"min","max", "range", "firstQ", "thirdQ", "sd",
"seMean", "var", "seVar", "CV", "sum", "sumSq",
"uncorSumSq", "skew", "seSkew", "kurt",
"seKurt"),
name = c("Number of values", "Number of observations",
"Number of missing values", "Mean", "Median",
"Min", "Max", "Range", "First quantile",
"Third quantile", "Standard deviation",
"Standard error of mean", "Variance",
"Standard error of variance",
"Coefficient of variation", "Sum of values",
"Sum of squares", "Uncorrected sum of squares",
"Skewness", "Standard Error of Skewness",
"Kurtosis", "Standard Error of Kurtosis"),
stringsAsFactors = FALSE)
## Checks.
if (!is.character(trial) || length(trial) > 1 ||
!hasName(x = object, name = trial)) {
stop("trial should be a single character string in ",
deparse(substitute(object)), ".\n")
}
trDat <- object[[trial]]
if (!is.character(traits) || !all(hasName(x = trDat, name = traits))) {
stop("All traits should be columns in trial.\n")
}
if (!is.null(groupBy) && (!is.character(groupBy) || length(groupBy) > 1 ||
!hasName(x = trDat, name = groupBy))) {
stop("groupBy should be a single character string indicating ",
"a column in trial")
}
if (what[[1]] == "all") {
what <- allStat[["stat"]]
}
if (!is.character(what) || all(!what %in% allStat[["stat"]])) {
stop("At least one statistic should be chosen.\n")
}
whichStat <- which(allStat[["stat"]] %in% what)
what <- allStat[whichStat, "stat"]
if (!is.null(groupBy)) {
groups <- unique(trDat[[groupBy]])
} else {
trDat[[".tot"]] <- 1
groupBy <- ".tot"
groups <- 1
}
## Create an array to store the values.
stats <- array(dim = c(length(what), length(traits), length(groups)),
dimnames = list(what, traits, groups))
for (i in seq_along(traits)) {
for (j in seq_along(groups)) {
trDatGr <- trDat[trDat[[groupBy]] == groups[j], traits[i]]
if ("nVals" %in% what) {
stats["nVals", i, j] <- length(trDatGr)
}
if ("nObs" %in% what) {
stats["nObs", i, j] <- length(na.omit(trDatGr))
}
if ("nMiss" %in% what) {
stats["nMiss", i, j] <- sum(is.na(trDatGr))
}
if ("mean" %in% what) {
stats["mean", i, j] <- mean(trDatGr, na.rm = TRUE)
}
if ("median" %in% what) {
stats["median", i, j] <- median(trDatGr, na.rm = TRUE)
}
if ("min" %in% what) {
stats["min", i, j] <- min(trDatGr, na.rm = TRUE)
}
if ("max" %in% what) {
stats["max", i, j] <- max(trDatGr, na.rm = TRUE)
}
if ("range" %in% what) {
stats["range", i, j] <- diff(range(trDatGr, na.rm = TRUE))
}
if ("firstQ" %in% what) {
stats["firstQ", i, j] <- quantile(trDatGr, prob = .25, na.rm = TRUE)
}
if ("thirdQ" %in% what) {
stats["thirdQ", i, j] <- quantile(trDatGr, prob = .75, na.rm = TRUE)
}
if ("sd" %in% what) {
stats["sd", i, j] <- sd(trDatGr, na.rm = TRUE)
}
if ("seMean" %in% what) {
stats["seMean", i, j] <- sd(trDatGr, na.rm = TRUE) /
sqrt(length(na.omit(trDatGr)))
}
if ("var" %in% what) {
stats["var", i, j] <- var(trDatGr, na.rm = TRUE)
}
if ("seVar" %in% what) {
stats["seVar", i, j] <- seVar(trDatGr, na.rm = TRUE)
}
if ("CV" %in% what) {
stats["CV", i, j] <- 100 * sd(trDatGr, na.rm = TRUE) /
mean(trDatGr, na.rm = TRUE)
}
if ("sum" %in% what) {
stats["sum", i, j] <- sum(trDatGr, na.rm = TRUE)
}
if ("sumSq" %in% what) {
stats["sumSq", i, j] <- sum((na.omit(trDatGr) -
mean(trDatGr, na.rm = TRUE)) ^ 2)
}
if ("uncorSumSq" %in% what) {
stats["uncorSumSq", i, j] <- sum(trDatGr ^ 2, na.rm = TRUE)
}
if ("skew" %in% what) {
stats["skew", i, j] <- skewness(trDatGr, na.rm = TRUE)
}
if ("seSkew" %in% what) {
stats["seSkew", i, j] <- seSkewness(length(na.omit(trDatGr)))
}
if ("kurt" %in% what) {
stats["kurt", i, j] <- kurtosis(trDatGr, na.rm = TRUE)
}
if ("seKurt" %in% what) {
stats["seKurt", i, j] <- seKurtosis(length(na.omit(trDatGr)))
}
}
}
rownames(stats) <- allStat[whichStat, "name"]
attr(x = stats, which = "whichStat") <- whichStat
return(structure(stats,
class = c("summary.TD", "array"),
trial = trial,
group = if (groupBy != ".tot") groupBy else NULL))
}
#' @export
print.summary.TD <- function(x,
...) {
whichStat <- attr(x, "whichStat")
groupBy <- attr(x, "group")
decimals <- c(rep(x = 1, times = 3), rep(x = 2, times = 7),
rep(x = 3, times = 5), rep(x = 2, times = 3),
rep(x = 3, times = 4))[whichStat]
xPrint <- x
for (i in seq_along(decimals)) {
xPrint[i, , ] <- format(x[i, , ], digits = decimals[i],
nsmall = decimals[i])
}
for (i in 1:ncol(xPrint)) {
trait <- colnames(xPrint)[i]
cat(paste("\nSummary statistics for", trait, "in", attr(x, "trial"),
if (!is.null(groupBy)) paste("grouped by", groupBy), "\n\n"))
if (dim(xPrint)[3] > 1) {
xPrintM <- matrix(nrow = nrow(xPrint), ncol = dim(xPrint)[3])
for (j in 1:nrow(xPrint)) {
xPrintM[j, ] <- xPrint[j, i, ]
}
dimnames(xPrintM) <- list(rownames(xPrint), dimnames(xPrint)[[3]])
print(xPrintM, quote = FALSE, right = TRUE)
} else {
xPrintM <- as.matrix(xPrint[, i , 1])
dimnames(xPrintM) <- list(rownames(xPrint), trait)
print(xPrintM, quote = FALSE, right = TRUE)
}
}
cat("\n")
}
#' Plot function for class TD
#'
#' Plotting function for objects of class TD. Plots either the layout of the
#' different trials within the TD object or locates the trials on a map. Also a
#' boxplot can be made for selected traits and trials, a plot of correlations
#' between trials and a scatter plot matrix. A detailed description and optional
#' extra parameters of the different plots is given in the sections below.
#'
#' @section Layout Plot:
#' Plots the layout of the selected trials.
#' This plot can only be made for trials that contain both row (\code{rowCoord})
#' and column (\code{colCoord}) information. If either one of those is missing
#' the trial is skipped with a warning. If blocks (\code{subBlock}) are
#' available for a trial these can be colored in different colors per block by
#' setting \code{colorSubBlock = TRUE}. If replicates (\code{repId}) are
#' available a black line is plotted between different replicates. Missing plots
#' are indicated in white. These can either be single plots in a trial or
#' complete missing columns or rows.\cr
#' Extra parameter options:
#' \describe{
#' \item{showGeno}{Should individual genotypes be indicated as text in the plot?
#' Defaults to \code{FALSE}}
#' \item{sizeGeno}{The text size for indicating individual genotypes. Defaults
#' to 2. Ignored if \code{showGeno = FALSE}.}
#' \item{highlight}{A character vector of genotypes to be highlighted in the
#' plot.}
#' \item{colHighlight}{A character vector specifying colors to use for the
#' highlighted genotypes. If not specified, default ggplot colors are used.}
#' \item{colorSubBlock}{Should blocks be colored with a different color per
#' subBlock? Defaults to \code{FALSE}. \code{colorSubBlock} is ignored when
#' highlight is used to highlight genotypes.}
#' \item{colSubBlock}{A character vector specifying colors to use for the
#' subBlocks. If not specified, default ggplot colors are used.}
#' }
#'
#' @section Map Plot:
#' A map is plotted with the locations of the trials in the TD object.
#' Mapping the trials is done based on latitude and longitude that can be
#' added when creating an object of class TD. Trials for which either latitude
#' or longitude is not available are skipped with a warning message.
#' The countries in which the trials are located will be plotted on a single
#' map and the location of the trials will be indicated on this map. The
#' actual plot is made using ggplot, but for getting the data for the borders
#' of the countries the maps package is needed.\cr
#' Extra parameter options:
#' \describe{
#' \item{colorTrialBy}{A character string indicating a column in \code{TD} by
#' which the trials on the map are colored.}
#' \item{colTrial}{A character vector with plot colors for the trials. A
#' single color when \code{colorTrialBy = NULL}, a vector of colors otherwise.}
#' \item{printTrialNames}{Should trial names be printed. Defaults to
#' \code{TRUE}. Setting this to \code{FALSE} can be useful if there are many
#' trials.}
#' \item{minLatRange}{A positive numerical value indicating the minimum range
#' (in degrees) for the latitude on the plotted map. Defaults to 10.}
#' \item{minLongRange}{A positive numerical value indicating the minimum range
#' (in degrees) for the longitude on the plotted map. Defaults to 5.}
#' }
#'
#' @section Box Plot:
#' Creates a boxplot per selected trait grouped by trial. Extra parameter
#' options:
#' \describe{
#' \item{groupBy}{A character string indicating a column in \code{TD} by which
#' the boxes in the plot should be grouped. By default the boxes are grouped
#' per trial.}
#' \item{colorTrialBy}{A character string indicating a column in \code{TD} by
#' which the boxes are colored. Coloring will be done within the groups
#' indicated by the \code{groupBy} parameter.}
#' \item{colTrial}{A character vector with plot colors for the trials. A
#' single color when \code{colorTrialBy = NULL}, a vector of colors otherwise.}
#' \item{orderBy}{A character string indicating the way the boxes should be
#' ordered. Either "alphabetic" for alphabetical ordering of the groups,
#' "ascending" for ordering by ascending mean, or "descending" for ordering by
#' descending mean. Default boxes are ordered alphabetically.}
#' }
#'
#' @section Correlation Plot:
#' Draws a heat map of correlations between trials per selected trait. If
#' genotypes are replicated within trials genotypic means are taken before
#' computing correlations. The order of the trials in the heat map is determined
#' by clustering them. Closely related trials will be plotted close to each
#' other.
#'
#' @section Scatter Plot:
#' Draws a scatter plot matrix per selected trait. If genotypes are replicated
#' within trials genotypic means are taken before plotting. The lower left of
#' the matrix contains scatter plots between trials. The diagonal contains
#' histograms of the data per trial.\cr
#' Extra parameter options:
#' \describe{
#' \item{colorGenoBy}{A character string indicating a column in \code{TD} by
#' which the genotypes in the scatter plots are colored.}
#' \item{colGeno}{A character vector with plot colors for the genotypes. A
#' single color when \code{colorGenoBy = NULL}, a vector of colors otherwise.}
#' \item{colorTrialBy}{A character string indicating a column in \code{TD} by
#' which the trials in the histograms are colored.}
#' \item{colTrial}{A character vector with plot colors for the trials. A
#' single color when \code{colorTrialBy = NULL}, a vector of colors otherwise.}
#' \item{trialOrder}{A character vector indicating the order of the trials in
#' the plot matrix (left to right and top to bottom). This vector should be a
#' permutation of all trials plotted.}
#' \item{addCorr}{A character string indicating the position of the correlation
#' between trials displayed in each plot, either "tl" for top left, "tr", for
#' top right, "bl" for bottom left or "br" for bottom right. If \code{NULL},
#' the default, then no correlation is added to the plot.}
#' }
#'
#' @param x An object of class TD.
#' @param ... Extra plot options. Described per plotType in their respective
#' section.
#' @param plotType A single character string indicating which plot should be
#' made. See the sections below for a detailed explanation of the plots.
#' @param trials A character vector indicating which trials to include in the
#' plot.
#' @param traits A character vector indicating for which traits a plot should
#' be made. Ignored if \code{plotType} = "map".
#' @param title A character string used a title for the plot. Note that when
#' a title is specified and multiple plots are created, all plots will get the
#' same title.
#' @param output Should the plot be output to the current device? If
#' \code{FALSE} only a list of ggplot objects is invisibly returned.
#'
#' @family functions for TD objects
#'
#' @examples
#' data("dropsRaw")
#'
#' ## Create a TD object.
#' dropsTD <- createTD(data = dropsRaw[dropsRaw$year == 2012, ],
#' genotype = "Variety_ID",
#' trial = "Experiment",
#' loc = "Site",
#' repId = "Replicate",
#' subBlock = "block",
#' rowCoord = "Row",
#' colCoord = "Column",
#' trLat = "Lat",
#' trLong = "Long")
#'
#' ### Layout plot.
#'
#' \donttest{
#' ## Plot the layout of one of the trials.
#' plot(dropsTD,
#' trials = "Kar12W")
#'
#' ## Highlight some of the genotypes in the layout.
#' plot(dropsTD,
#' trials = "Kar12W",
#' highlight = c("A3", "11430"))
#'
#' ### Map plot.
#'
#' ## Plot the location of the trials on the map.
#' plot(dropsTD,
#' plotType = "map")
#'
#' ### Box plot.
#'
#' ## Create a box plot for grain.yield.
#' plot(dropsTD,
#' plotType = "box",
#' traits = "grain.yield")
#'
#' ## Add coloring by scenarioFull to the boxes.
#' plot(dropsTD,
#' plotType = "box",
#' traits = "grain.yield",
#' colorTrialBy = "scenarioFull")
#'
#' ## Sort the boxes in descending order.
#' plot(dropsTD,
#' plotType = "box",
#' traits = "grain.yield",
#' orderBy = "descending")
#'
#' ### Correlation plot.
#'
#' ## Plot the correlations between trials for grain.yield.
#' plot(dropsTD,
#' plotType = "cor",
#' traits = "grain.yield")
#'
#' ### Scatter plot
#'
#' ## Plot scatter plot for grain.yield.
#' plot(dropsTD,
#' plotType = "scatter",
#' traits = "grain.yield")
#'
#' ## Create a scatter plot matrix for grain yield.
#' ## Color trials by scenario and genotypes by family.
#' plot(dropsTD,
#' plotType = "scatter",
#' traits = "grain.yield",
#' colorTrialBy = "scenarioFull",
#' colorGenoBy = "geneticGroup")
#' }
#'
#' @export
plot.TD <- function(x,
...,
plotType = c("layout", "map", "box", "cor", "scatter"),
trials = names(x),
traits = NULL,
title = NULL,
output = TRUE) {
## Checks.
trials <- chkTrials(trials, x)
plotType <- match.arg(plotType)
chkChar(title, len = 1)
dotArgs <- list(...)
## Restrict x to trials.
x <- dropTD(x, names(x)[!names(x) %in% trials])
if (plotType == "layout") {
p <- layoutPlot(x = x, trials = trials, traits = traits, title = title,
output = output, ...)
} else if (plotType == "map") {
p <- mapPlot(x = x, title = title, output = output, ...)
} else if (plotType == "box") {
p <- boxPlot(x = x, trials = trials, traits = traits, title = title,
output = output, ...)
} else if (plotType == "cor") {
p <- corPlot(x = x, trials = trials, traits = traits, title = title,
output = output, ...)
} else if (plotType == "scatter") {
p <- scatterPlot(x = x, trials = trials, traits = traits, title = title,
output = output, ...)
}
invisible(p)
}
#' Get and set metadata for TD objects
#'
#' Functions for extracting and adding metadata for objects of class TD.\cr\cr
#' \code{getMeta} extracts a data.frame with location, date, design, latitude,
#' longitude, plot width and plot length for all trials in TD.\cr\cr
#' \code{setMeta} adds metadata from a data.frame to an object of class TD. See
#' details for the specifications of the data.frame.\cr\cr
#' The most common use case is extracting metadata from a TD object, modifying
#' the content and then adding it back to the TD object.\cr
#' Information in the metadata of a TD object is used in plotting functions
#' (e.g. latitude and longitude for a map plot) and when fitting models on the
#' data (the trial design).
#'
#' When setting metadata, metadata has to be a data.frame with rownames
#' corresponding to the trials in \code{TD}. The data.frame should contain one
#' or more of the following columns:
#' \describe{
#' \item{trLocation}{The location of the trial. Used as default name when
#' creating plots and summaries.}
#' \item{trDate}{The date of the trial.}
#' \item{trDesign}{The design of the trial. One of "none" (no (known) design),
#' "ibd" (incomplete-block design), "res.ibd" (resolvable incomplete-block
#' design), "rcbd" (randomized complete block design),
#' "rowcol" (row-column design) or "res.rowcol" (resolvable row-column design).
#' Used when fitting models.}
#' \item{trLat}{The latitude of the trial on a scale of -90 to 90. Used when
#' plotting the trials on a map.}
#' \item{trLong}{The longitude of the trial on a scale of -180 to 180. Used
#' when plotting the trials on a map.}
#' \item{trPlWidth}{The width of the plot. Used in combination with trPlLength
#' to determine the size of the plots in a layout plot of a trial.}
#' \item{trPlLength}{The length of the plot. Used in combination with
#' trPlWidth to determine the size of the plots in a layout plot of a trial.}
#' }
#' The values of the metadata of TD will be set to the values in the
#' corresponding column in \code{meta}. Existing values will be overwritten,
#' but \code{NA} will be ignored so setting a value to \code{NA} won't result
#' in accidentally removing it.
#'
#' @param TD An object of class TD.
#'
#' @family functions for TD objects
#'
#' @examples
#' data("dropsRaw")
#'
#' ## Create a TD object.
#' dropsTD <- createTD(data = dropsRaw[dropsRaw$year == 2012, ],
#' genotype = "Variety_ID",
#' trial = "Experiment",
#' loc = "Site",
#' repId = "Replicate",
#' subBlock = "block",
#' rowCoord = "Row",
#' colCoord = "Column",
#' trLat = "Lat",
#' trLong = "Long")
#'
#' ## Get meta data from dropsTD.
#' (dropsMeta <- getMeta(dropsTD))
#'
#' ## Add trial date to meta data.
#' dropsMeta$trDate <- as.Date(rep("010112", times = 5), "%d%m%y")
#'
#' ## Add back meta data to wheatTD.
#' dropsTD <- setMeta(dropsTD,
#' dropsMeta)
#'
#' @export
getMeta <- function(TD) {
if (missing(TD) || !inherits(TD, "TD")) {
stop("TD should be an object of class TD.\n")
}
metaVars <- c("trLocation", "trDate", "trDesign", "trLat", "trLong",
"trPlWidth", "trPlLength")
meta <- as.data.frame(matrix(nrow = length(TD), ncol = length(metaVars),
dimnames = list(names(TD), metaVars)))
for (mv in metaVars) {
meta[mv] <- sapply(X = TD, FUN = function(tr) {
mvTr <- attr(tr, which = mv)
## Replace NULL by NA to ensure correct output format for inserting in df.
if (!is.null(mvTr)) {
return(mvTr)
} else {
return(NA)
}
})
}
class(meta$trDate) <- "Date"
return(meta)
}
#' @param TD An object of class TD.
#' @param meta A data.frame containing metadata.
#'
#' @rdname getMeta
#' @export
setMeta <- function(TD,
meta) {
if (missing(TD) || !inherits(TD, "TD")) {
stop("TD should be an object of class TD.\n")
}
if (missing(meta) || !inherits(meta, "data.frame")) {
stop("meta should be a data.frame.\n")
}
naTr <- rownames(meta)[!rownames(meta) %in% names(TD)]
if (length(naTr) > 0) {
warning("The following trials in meta are not in TD: ",
paste(naTr, collapse = ", "), ".\n", call. = FALSE)
}
metaVars <- c("trLocation", "trDate", "trDesign", "trLat", "trLong",
"trPlWidth", "trPlLength")
## Set metadata for trials in meta that are also in TD.
for (tr in rownames(meta)[rownames(meta) %in% names(TD)]) {
for (mv in metaVars) {
mvTr <- meta[tr, mv]
if (!is.na(mvTr)) {
chk <- try(do.call(what = checkTDMeta, args = setNames(list(mvTr), mv)),
silent = TRUE)
if (inherits(chk, "try-error")) {
## Get message from check function but remove first 8 chars to
## prevent having an error text with 3x error in it.