forked from Biometris/statgenHTP
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathcreateFitMod.R
914 lines (905 loc) · 39.8 KB
/
createFitMod.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
#' @keywords internal
createFitMod <- function(models,
experimentName,
what,
useRepId,
useCheck,
spatial,
timePoints) {
fitMod <- structure(models,
experimentName = experimentName,
timePoints = timePoints,
what = what,
useRepId = useRepId,
useCheck = useCheck,
spatial = spatial,
class = c("fitMod", "list"),
timestamp = Sys.time())
return(fitMod)
}
#' Summary function for fitMod objects
#'
#' Function for creating a short summary of the contents of a TP object. The
#' summary consists of the name of the experiment, the number of time points,
#' the engine used to fit the models and, in case spatial models where fitted
#' using asreml, the selected spatial model.
#'
#' @param object An object of class fitMod.
#' @param ... Ignored.
#'
#' @return No return value, a summary is printed.
#'
#' @examples
#' \donttest{
#' ## Using the first example dataset (PhenovatorDat1):
#' ## Create an object of class TP.
#' phenoTP <- createTimePoints(dat = PhenovatorDat1,
#' experimentName = "Phenovator",
#' genotype = "Genotype",
#' timePoint = "timepoints",
#' repId = "Replicate",
#' plotId = "pos",
#' rowNum = "y", colNum = "x",
#' addCheck = TRUE,
#' checkGenotypes = c("check1", "check2",
#' "check3", "check4"))
#'
#' ## Fit a SpATS model on few time points:
#' modPhenoSp <- fitModels(TP = phenoTP,
#' trait = "EffpsII",
#' timePoints = c(1, 6, 36))
#'
#' ## Create a summary.
#' summary(modPhenoSp)
#' }
#'
#' @family functions for spatial modeling
#'
#' @export
summary.fitMod <- function(object,
...) {
experimentName <- attr(x = object, which = "experimentName")
noTP <- length(object)
engine <- class(object[[1]])
if (engine == "asreml" && attr(x = object, which = "spatial")) {
tpUsed <- min(max(noTP / 5, 10), noTP)
bestSpat <- attr(x = object[[1]], which = "sumTab")[[1]][, "spatial"]
}
cat("Models in ", deparse(substitute(object)),
" where fitted for experiment ", experimentName, ".\n\n", sep = "")
cat("It contains", noTP, "time points.\n")
cat("The models were fitted using ", engine, ".\n\n", sep = "")
if (engine == "asreml" && attr(x = object, which = "spatial")) {
cat("The selected spatial model is ", bestSpat, ".\n", sep = "")
cat(tpUsed, "time points were used to select the best spatial model.\n")
}
}
#' Plot function for class fitMod
#'
#' Plotting function for objects of class \code{fitMod}. Seven different types
#' of plots can be made for an object of class \code{fitMod}. A detailed
#' description and optional extra parameters for the different plots is given
#' in the sections below.
#'
#' @section rawPred plot:
#' Plots the raw data (colored dots) overlayed with the predicted values from
#' the fitted model (black dots). For each genotype a plot is made per
#' plot/plant over time. These plots are put together in a 5x5 grid. By using
#' the parameter \code{genotypes} a selection of genotypes can be plotted.
#' Extra parameter options:
#' \describe{
#' \item{genotypes}{A character vector indicating the genotypes to be plotted.}
#' \item{plotChecks}{Should the check genotypes be included in the plot?}
#' \item{plotLine}{Should the data be displayed as lines? Default is
#' \code{FALSE}.}
#' }
#'
#' @section corrPred plot:
#' Plots the spatially corrected data (colored dots) overlayed with the
#' predicted values from the fitted model (black dors). For each genotype a plot
#' is made per plot/plant over time. These plots are put together in a 5x5 grid.
#' By using the parameter \code{genotypes} a selection of genotypes can be
#' plotted. Extra parameter options:
#' \describe{
#' \item{genotypes}{A character vector indicating the genotypes to be plotted.}
#' \item{plotChecks}{Should the check genotypes be included in the plot?}
#' \item{plotLine}{Should the data be displayed as lines? Default is
#' \code{FALSE}.}
#' }
#'
#' @section herit plot:
#' Plots the heritability over time. This plot is only available when genotype
#' is fitted as random factor in the model. If \code{geno.decomp} is used when
#' fitting the model, heritabilities are plotted for each level of geno.decomp
#' in a single plot. Extra parameter options:
#' \describe{
#' \item{yLim}{A numerical vector of length two, used for setting the limits of
#' the y-axis of the plot. If values outside of the plotting range are given,
#' then these are ignored.}
#' }
#'
#' @section effDim plot:
#' Plots the effective dimension over time for models fitted using SpATS.
#' Extra parameter options:
#' \describe{
#' \item{whichED}{A character vector indicating which effective dimensions
#' should be plotted. This should be a subset of "colId", "rowId", "fCol",
#' "fRow", "fColRow", "colfRow", "fColfRow" and "surface". When
#' \code{useRepId = TRUE}, the effective dimensions of "colId" and "rowId"
#' become "RepId:colId" and "RepId:rowId". Default all effective dimensions
#' are plotted.}
#' \item{EDType}{A character string specifying if the effective dimension
#' ("dimension") or the ratio of effective dimensions ("ratio") should be
#' plotted. Default the dimensions are plotted.}
#' \item{yLim}{A numerical vector of length two, used for setting the limits of
#' the y-axis of the plot. If values outside of the plotting range are given,
#' then these are ignored.}
#' }
#'
#' @section variance plot:
#' Plots the residual, column and row variances over time for the fitted models.
#' Extra parameter options:
#' \describe{
#' \item{yLim}{A numerical vector of length two, used for setting the limits of
#' the y-axis of the plot. If values outside of the plotting range are given,
#' then these are ignored.}
#' }
#'
#' @section timeLapse plot:
#' Creates a time lapse of the spatial trends of models fitted using SpATS over
#' time.
#'
#' @section spatial plot:
#' Creates five plots per time point, spatial plots of the raw data,
#' fitted values, residuals and either BLUEs or BLUPs, and a histogram of the
#' BLUEs or BLUPs. When SpATS was used for modeling an extra plot with the
#' fitted spatial trend is included Extra parameter options:
#' \describe{
#' \item{spaTrend}{A character string indicating how the spatial trend should
#' be displayed. Either "raw" for raw values, or "percentage" for displaying
#' as a percentage of the original phenotypic values.}
#' }
#'
#' @inheritParams plot.TP
#'
#' @param x An object of class fitMod.
#' @param outFile A character string indicating the .pdf file or .gif file
#' (for \code{plotType} = "timeLapse") to which the plots should be written.
#'
#' @return Depending on the plot type either a ggplot object or a list of
#' ggplot objects is invisibly returned.
#'
#' @examples
#' \donttest{
#' ## Using the first example dataset (PhenovatorDat1):
#' ## Create an object of class TP.
#' phenoTP <- createTimePoints(dat = PhenovatorDat1,
#' experimentName = "Phenovator",
#' genotype = "Genotype",
#' timePoint = "timepoints",
#' repId = "Replicate",
#' plotId = "pos",
#' rowNum = "y", colNum = "x",
#' addCheck = TRUE,
#' checkGenotypes = c("check1", "check2",
#' "check3", "check4"))
#'
#' ## Fit a SpATS model on three points:
#' modPhenoSp <- fitModels(TP = phenoTP,
#' trait = "EffpsII",
#' timePoints = c(1, 6, 36))
#'
#' ## Plot the spatial trends for one time point:
#' plot(modPhenoSp,
#' timePoints = 36,
#' plotType = "spatial",
#' spaTrend = "percentage")
#' }
#'
#' \dontrun{
#' ## Create a time lapse of all available time points:
#' plot(modPhenoSp,
#' plotType = "timeLapse",
#' outFile = "TimeLapse_modPhenoSp.gif")
#' }
#'
#' \donttest{
#' ## Plot the corrected values for a subset of four genotypes:
#' plot(modPhenoSp,
#' plotType = "corrPred",
#' genotypes = c("check1", "check2", "G007", "G058") )
#'
#' ## Plot the effective dimensions of all available time points in the model
#' ## for a subset of effective dimensions:
#' plot(modPhenoSp,
#' plotType = "effDim",
#' whichED = c("colId", "rowId", "fColRow","colfRow"),
#' EDType = "ratio")
#' }
#'
#' @family functions for spatial modeling
#'
#' @export
plot.fitMod <- function(x,
...,
plotType = c("rawPred", "corrPred", "herit", "effDim",
"variance", "timeLapse", "spatial"),
timePoints = names(x),
title = NULL,
output = TRUE,
outFile = NULL,
outFileOpts = NULL) {
## Checks.
timePoints <- chkTimePoints(x, timePoints)
plotType <- match.arg(plotType)
experimentName <- attr(x = x, which = "experimentName")
dotArgs <- list(...)
if (!is.null(title) && (!is.character(title) || length(title) > 1)) {
stop("title should be NULL or a character string.\n")
}
## Restrict x to selected time points.
fitMods <- x[timePoints]
## Get engine from fitted models.
engine <- class(fitMods[[1]])
if (engine == "asreml" && plotType == "effDim") {
stop("Effective dimensions can only be plotted for models fitted ",
"with SpATS.\n")
}
if (engine == "asreml" && plotType == "spatial" &&
!attr(x = fitMods, which = "spatial")) {
stop("spatial plots can only be made when setting spatial = TRUE when ",
"fitting the asreml models.\n")
}
## Get geno.decomp from fitted models.
if (engine == "SpATS") {
geno.decomp <- fitMods[[1]]$model$geno$geno.decomp
} else if (engine == "asreml") {
if ("geno.decomp" %in% all.vars(fitMods[[1]]$formulae$random)) {
geno.decomp <- "geno.decomp"
} else {
geno.decomp <- NULL
}
}
## Get trait from fitted models.
if (engine == "SpATS") {
trait <- fitMods[[1]]$model$response
} else if (engine == "asreml") {
## Trait is always the only lhs variable in the fixed part.
trait <- all.vars(update(fitMods[[1]]$formulae$fixed, .~0))
}
## Get check from fitted models.
if (engine == "SpATS") {
useCheck <- grepl(pattern = "check", x = deparse(fitMods[[1]]$model$fixed))
} else if (engine == "asreml") {
useCheck <- "check" %in% all.vars(update(fitMods[[1]]$formulae$fixed, 0~.))
}
## Get what from fitted models.
what <- attr(x = fitMods, which = "what")
if (!is.null(outFile) && plotType != "timeLapse") {
chkFile(outFile, fileType = "pdf")
output <- TRUE
outFileOpts <- c(list(file = outFile), outFileOpts)
on.exit(dev.off(), add = TRUE)
do.call(pdf, args = outFileOpts)
}
if (plotType == "rawPred") {
genotypes <- dotArgs$genotypes
plotLine <- isTRUE(dotArgs$plotLine)
if (!is.null(genotypes) && !is.character(genotypes)) {
stop("genotypes should be NULL or a character vector.\n")
}
if (is.null(title)) title <-
paste(experimentName, "- genotypic prediction + raw data")
if (isTRUE(dotArgs$plotChecks) && useCheck) {
totPred <- getGenoPred(fitMods, predictChecks = TRUE)
## Get check predictions.
genoPred <- totPred$genoPred
checkPred <- totPred$checkPred
## Rename check column to genotype so rbinding is possible.
colnames(checkPred)[colnames(checkPred) == "check"] <- "genotype"
preds <- rbind(genoPred, checkPred)
} else {
## Get genotypic predictions.
preds <- getGenoPred(fitMods)$genoPred
}
## Construct full raw data from models.
if (engine == "SpATS") {
raw <- Reduce(f = rbind, x = lapply(X = fitMods, FUN = `[[`, "data"))
} else if (engine == "asreml") {
raw <- Reduce(f = rbind, x = lapply(X = fitMods, FUN = function(fitMod) {
fitMod$call$data
}))
}
## Remove observations from raw where genotype is missing.
## These where included when fitting spatial asreml models to create
## a full grid.
raw <- raw[!is.na(raw[["genotype"]]), ]
if (!is.null(geno.decomp) && engine == "SpATS" && !useCheck) {
## Genotype was converted to an interaction term of genotype and
## geno.decomp in the proces of fitting the model. That needs to be
## undone to get the genotype back in the output again.
genoStart <- nchar(as.character(raw[["geno.decomp"]])) + 2
raw[["genotype"]] <- as.factor(substring(raw[["genotype"]],
first = genoStart))
}
## Remove check genotypes from raw data.
if (!isTRUE(dotArgs$plotChecks) && useCheck) {
raw <- droplevels(raw[!is.na(raw[["genoCheck"]]), ])
}
## Restrict genotypes.
if (!is.null(genotypes)) {
if (!all(genotypes %in% preds[["genotype"]])) {
stop("All genotypes should be in ", deparse(substitute(x)), ".\n")
}
preds <- preds[preds[["genotype"]] %in% genotypes, ]
preds <- droplevels(preds)
raw <- raw[raw[["genotype"]] %in% genotypes, ]
raw <- droplevels(raw)
}
## Restrict raw to neccessary columns so adding missing combinations works
## properly. With extra columns unneeded combinations might be added.
raw <- raw[colnames(raw) %in% c("timePoint", "genotype", "plotId", trait,
if (!is.null(geno.decomp)) "geno.decomp",
if (useCheck) c("check", "genoCheck"))]
## Add combinations missing in data to raw.
raw <- addMissVals(dat = raw, trait = trait)
p <- xyFacetPlot(baseDat = raw, overlayDat = preds, yVal = trait,
yValOverlay = "predicted.values",
facetVal = c("genotype",
if (!is.null(geno.decomp)) "geno.decomp"),
title = title, yLab = trait, output = output,
plotLine = plotLine)
} else if (plotType == "corrPred") {
genotypes <- dotArgs$genotypes
plotLine <- isTRUE(dotArgs$plotLine)
if (!is.null(genotypes) && !is.character(genotypes)) {
stop("genotypes should be NULL or a character vector.\n")
}
if (is.null(title))
title <- paste(experimentName, "- genotypic prediction + corrected data")
if (isTRUE(dotArgs$plotChecks) && useCheck) {
totPred <- getGenoPred(fitMods, predictChecks = TRUE)
## Get check predictions.
genoPred <- totPred$genoPred
checkPred <- totPred$checkPred
## Rename check column to genotype so rbinding is possible.
colnames(checkPred)[colnames(checkPred) == "check"] <- "genotype"
preds <- rbind(genoPred, checkPred)
} else {
## Get genotypic predictions.
preds <- getGenoPred(fitMods)$genoPred
}
## Get spatial corrected values.
corrected <- suppressWarnings(getCorrected(fitMods))
## Remove check genotypes from corrected data.
if (!isTRUE(dotArgs$plotChecks) && useCheck) {
corrected <- droplevels(corrected[corrected[["check"]] == "noCheck", ])
}
## Restrict genotypes.,
if (!is.null(genotypes)) {
if (!all(genotypes %in% preds[["genotype"]])) {
stop("All genotypes should be in ", deparse(substitute(x)), ".\n")
}
preds <- preds[preds[["genotype"]] %in% genotypes, ]
preds <- droplevels(preds)
corrected <- corrected[corrected[["genotype"]] %in% genotypes, ]
corrected <- droplevels(corrected)
}
newTrait <- paste0(trait, "_corr")
corrected <- corrected[c("timeNumber", "timePoint", "genotype",
newTrait, "plotId",
if (!is.null(geno.decomp)) "geno.decomp")]
## Add combinations missing in data to corrected.
corrected <- addMissVals(dat = corrected, trait = newTrait)
p <- xyFacetPlot(baseDat = corrected, overlayDat = preds, yVal = newTrait,
yValOverlay = "predicted.values",
facetVal = c("genotype",
if (!is.null(geno.decomp)) "geno.decomp"),
title = title,
yLab = trait, output = output, plotLine = plotLine)
} else if (plotType == "herit") {
if (is.null(title)) title <- paste(experimentName, "- Heritability")
## Get heritabilities.
herit <- getHerit(fitMods)
## Convert to long format needed by ggplot.
herit <- reshape2::melt(herit, measure.vars = setdiff(colnames(herit),
c("timeNumber",
"timePoint")),
variable.name = "herit", value.name = "h2")
## Manually modify limit of y-axis.
yLim <- c(min(dotArgs$yLim[1], herit[["h2"]]),
max(dotArgs$yLim[2], herit[["h2"]]))
p <- ggplot2::ggplot(herit,
ggplot2::aes_string(x = "timePoint", y = "h2",
group = "herit",
color = "herit")) +
ggplot2::geom_point(size = 3, na.rm = TRUE) +
plotTheme() +
ggplot2::theme(axis.title.y = ggplot2::element_text(angle = 0,
vjust = 0.5)) +
ggplot2::ylim(yLim) +
ggplot2::labs(title = title)
## Compute the number of breaks for the time scale.
## If there are less than 4 time points use positions of the time points.
## Otherwise use 3.
nTp <- length(unique(herit[["timePoint"]]))
if (nTp < 5) {
p <- p + ggplot2::scale_x_datetime(breaks = unique(herit[["timePoint"]]),
labels = scales::date_format("%B %d"))
} else {
## Format the time scale to Month + day.
p <- p + ggplot2::scale_x_datetime(breaks = prettier(n = 3),
labels = scales::date_format("%B %d"))
}
if (nTp > 1) {
p <- p + ggplot2::geom_line(size = 0.5, na.rm = TRUE)
}
if (output) {
plot(p)
}
} else if (plotType == "effDim") {
useRepId <- attr(x = fitMods, which = "useRepId")
colVarId <- ifelse(useRepId, "repId:colId", "colId")
rowVarId <- ifelse(useRepId, "repId:rowId", "rowId")
whichEDopts <- c(colVarId, rowVarId, "fCol", "fRow", "fColRow", "colfRow",
"fColfRow", "surface")
if (is.null(dotArgs$which)) {
whichED <- whichEDopts
} else {
whichED <- match.arg(dotArgs$whichED, choices = whichEDopts,
several.ok = TRUE)
}
EDType <- match.arg(dotArgs$EDType, choices = c("dimension", "ratio"))
if (is.null(title)) title <- paste(experimentName, "- Effective dimension")
## Get effective dimensions.
effDim <- getEffDims(fitMods, EDType = EDType)
## Convert to long format needed by ggplot.
effDim <- reshape2::melt(effDim, measure.vars = whichED,
variable.name = "effDim", value.name = "ED")
## Manually modify limit of y-axis.
yLim <- c(min(dotArgs$yLim[1], effDim[["ED"]]),
max(dotArgs$yLim[2], effDim[["ED"]]))
p <- ggplot2::ggplot(effDim,
ggplot2::aes_string(x = "timePoint", y = "ED",
group = "effDim",
color = "effDim")) +
ggplot2::geom_point(size = 3, na.rm = TRUE) +
plotTheme() +
ggplot2::theme(axis.title.y = ggplot2::element_text(angle = 0,
vjust = 0.5)) +
ggplot2::ylim(yLim) +
ggplot2::labs(title = title, color = "Effective dimension")
## Compute the number of breaks for the time scale.
## If there are less than 4 time points use positions of the time points.
## Otherwise use 3.
nTp <- length(unique(effDim[["timePoint"]]))
if (nTp < 5) {
p <- p + ggplot2::scale_x_datetime(breaks = unique(effDim[["timePoint"]]),
labels = scales::date_format("%B %d"))
} else {
## Format the time scale to Month + day.
p <- p + ggplot2::scale_x_datetime(breaks = prettier(n = 3),
labels = scales::date_format("%B %d"))
}
if (nTp > 1) {
p <- p + ggplot2::geom_line(size = 0.5, na.rm = TRUE)
}
if (output) {
plot(p)
}
} else if (plotType == "variance") {
if (is.null(title)) title <- paste(experimentName, "- Variances")
## Get variances.
variance <- getVar(fitMods)
## Get variance columns from variance, i.e. all columns starting with var.
varCols <- colnames(variance)[grepl(pattern = "^var",
x = colnames(variance))]
## Construct labels for variances.
varLabs <- c(if ("varGen" %in% varCols) "Genotypic" else
substring(varCols[1:(length(varCols) - 3)], first = 17),
"Residual", "Columns", "Rows")
## Convert to long format needed by ggplot.
variance <- reshape2::melt(variance, measure.vars = varCols,
variable.name = "var")
## Manually modify limit of y-axis.
yLim <- c(min(dotArgs$yLim[1], variance[["value"]]),
max(dotArgs$yLim[2], variance[["value"]]))
p <- ggplot2::ggplot(variance,
ggplot2::aes_string(x = "timePoint", y = "value",
group = "var", color = "var")) +
ggplot2::geom_point(size = 3, na.rm = TRUE) +
ggplot2::scale_color_discrete(labels = varLabs) +
plotTheme() +
ggplot2::theme(axis.title.y = ggplot2::element_text(angle = 0,
vjust = 0.5)) +
ggplot2::ylim(yLim) +
ggplot2::labs(title = title, color = "variance",
y = expression(sigma ^ 2))
## Compute the number of breaks for the time scale.
## If there are less than 4 time points use positions of the time points.
## Otherwise use 3.
nTp <- length(unique(variance[["timePoint"]]))
if (nTp < 5) {
p <- p + ggplot2::scale_x_datetime(breaks = unique(variance[["timePoint"]]),
labels = scales::date_format("%B %d"))
} else {
## Format the time scale to Month + day.
p <- p + ggplot2::scale_x_datetime(breaks = prettier(n = 3),
labels = scales::date_format("%B %d"))
}
if (nTp > 1) {
p <- p + ggplot2::geom_line(size = 0.5, na.rm = TRUE)
}
if (output) {
plot(p)
}
} else if (plotType == "spatial") {
p <- lapply(X = fitMods, FUN = spatPlot, trait = trait, what = what,
geno.decomp = geno.decomp, useCheck = useCheck, engine = engine,
output = output, ... = ..., experimentName = experimentName,
title = title)
} else if (plotType == "timeLapse") {
chkFile(outFile, fileType = "gif")
timeLapsePlot(fitMods, outFile = outFile, ...)
}
if (!plotType == "timeLapse") {
invisible(p)
}
}
#' Helper function for creating spatial plots.
#'
#' @noRd
#' @keywords internal
spatPlot <- function(fitMod,
trait,
what,
geno.decomp,
useCheck,
engine,
output = TRUE,
...) {
dotArgs <- list(...)
## Get plot type for spatial trend from args.
spaTrend <- match.arg(dotArgs$spaTrend, choices = c("raw", "percentage"))
## Extract data from model.
if (engine == "SpATS") {
modDat <- fitMod$data
} else if (engine == "asreml") {
modDat <- fitMod$call$data
}
## Check spatial information in modDat.
if (!chkRowCol(modDat)) {
return(NULL)
}
## Extract time point from model data.
timePoint <- modDat[["timePoint"]][1]
## Extract raw data.
raw <- modDat[c("genotype", trait, "rowNum", "colNum", geno.decomp)]
## Extract fitted values from model.
fitted <- fitted(fitMod)
## Extract predictions (BLUEs or BLUPs) from model.
if (useCheck) {
totPred <- predictGeno(fitMod, predictChecks = TRUE)
## Get check predictions.
genoPred <- totPred$predGeno
checkPred <- totPred$predCheck
## Rename check column to genotype so rbinding is possible.
colnames(checkPred)[colnames(checkPred) == "check"] <- "genotype"
pred <- rbind(genoPred, checkPred)
} else {
## Get genotypic predictions.
pred <- predictGeno(fitMod)$predGeno
}
pred <- pred[c("genotype", "predicted.values", geno.decomp)]
if (!is.null(geno.decomp) && engine == "SpATS") {
## Genotype was converted to an interaction term of genotype and
## geno.decomp in the proces of fitting the model. That needs to be
## undone to get the genotype back in the output again.
genoStart <- nchar(as.character(raw[["geno.decomp"]])) + 2
raw[["genotype"]] <- as.factor(substring(raw[["genotype"]],
first = genoStart))
}
## Create plot data by merging extracted data together and renaming some
## columns.
plotDat <- cbind(raw, fitted)
plotDat <- merge(plotDat, pred, by = c("genotype", geno.decomp))
plotDat[["predicted.values"]][is.na(plotDat[["fitted"]])] <- NA
plotDat[["resid"]] <- plotDat[[trait]] - plotDat[["fitted"]]
## Get limits for row and columns.
yMin <- min(plotDat[["rowNum"]])
yMax <- max(plotDat[["rowNum"]])
xMin <- min(plotDat[["colNum"]])
xMax <- max(plotDat[["colNum"]])
## Execute this part first since it needs plotData without missings
## removed.
## Code mimickes code from SpATS package but is adapted to create a
## data.frame useable by ggplot.
if (engine == "SpATS") {
plotDat <- plotDat[order(plotDat[["colNum"]], plotDat[["rowNum"]]), ]
nCol <- xMax - xMin + 1
nRow <- yMax - yMin + 1
p1 <- 100 %/% nCol + 1
p2 <- 100 %/% nRow + 1
## Get spatial trend from SpATS object.
spatTr <- SpATS::obtain.spatialtrend(fitMod, grid = c(nCol * p1, nRow * p2))
## spatial trend contains values for all data points, so NA in original
## data need to be removed. The kronecker multiplication is needed to
## convert the normal row col pattern to the smaller grid extending the
## missing values.
## First a matrix M is created containing information for all
## columns/rows in the field even if they are completely empty.
M <- matrix(nrow = nRow, ncol = nCol,
dimnames = list(yMin:yMax, xMin:xMax))
for (i in seq_len(nrow(plotDat))) {
M[as.character(plotDat[i, "rowNum"]),
as.character(plotDat[i, "colNum"])] <-
ifelse(is.na(plotDat[i, trait]), NA, 1)
}
spatTrDat <- kronecker(M, matrix(data = 1, ncol = p1, nrow = p2)) *
spatTr$fit
## Melt to get the data in ggplot shape. Rows and columns in the
## spatial trend coming from SpATS are swapped so therefore use t()
plotDatSpat <- reshape2::melt(t(spatTrDat),
varnames = c("colNum", "rowNum"))
## Add true values for columns and rows for plotting.
plotDatSpat[["colNum"]] <- spatTr$col.p
plotDatSpat[["rowNum"]] <- rep(x = spatTr$row.p, each = p1 * nCol)
## Remove missings from data.
plotDatSpat <- ggplot2::remove_missing(plotDatSpat, na.rm = TRUE)
}
## Now missing values can be removed from plotDat.
plotDat <- ggplot2::remove_missing(plotDat, na.rm = TRUE)
## Code taken from plot.SpATS and simplified.
## Set colors and legends.
colors <- topo.colors(100)
legends <- c("Raw data", "Fitted data", "Residuals",
"Fitted Spatial Trend",
ifelse(what == "fixed", "Genotypic BLUEs",
"Genotypic BLUPs"), "Histogram")
## Compute range of values in response + fitted data so same scale
## can be used over plots.
zlim <- range(c(plotDat[[trait]], plotDat[["fitted"]]), na.rm = TRUE)
## Create empty list for storing plots
plots <- vector(mode = "list")
## Create main plot title.
plotTitle <- ifelse(!is.null(dotArgs$title), dotArgs$title,
paste(dotArgs$experimentName, "-", trait, "-", timePoint))
## Create separate plots.
plots$p1 <- fieldPlot(plotDat = plotDat, fillVar = trait,
title = legends[1], colors = colors, zlim = zlim)
plots$p2 <- fieldPlot(plotDat = plotDat, fillVar = "fitted",
title = legends[2], colors = colors, zlim = zlim)
plots$p3 <- fieldPlot(plotDat = plotDat, fillVar = "resid",
title = legends[3], colors = colors)
## Spatial plot only for SpATS.
if (engine == "SpATS") {
## Get tickmarks from first plot to be used as ticks.
## Spatial plot tends to use different tickmarks by default.
xTicks <- ggplot2::ggplot_build(plots[[1]])$layout$panel_params[[1]]$x$breaks
if (spaTrend == "raw") {
plots$p4 <- fieldPlot(plotDat = plotDatSpat, fillVar = "value",
title = legends[4], colors = colors,
xTicks = xTicks)
} else {
phenoMean <- mean(modDat[[trait]], na.rm = TRUE)
plotDatSpat[["value"]] <- plotDatSpat[["value"]] / phenoMean
zlim <- c(-1, 1) * max(c(abs(plotDatSpat[["value"]]), 0.1))
plots$p4 <- fieldPlotPcts(plotDat = plotDatSpat, fillVar = "value",
title = legends[4], zlim = zlim,
colors = colorRampPalette(c("red", "yellow",
"blue"),
space = "Lab")(100),
xTicks = xTicks)
}
}
plots$p5 <- fieldPlot(plotDat = plotDat, fillVar = "predicted.values",
title = legends[5], colors = colors)
plots$p6 <- ggplot2::ggplot(data = plotDat) +
ggplot2::geom_histogram(ggplot2::aes_string(x = "predicted.values"),
fill = "white", col = "black", bins = 10,
boundary = 0) +
## Remove empty space between ticks and actual plot.
ggplot2::scale_x_continuous(expand = c(0, 0)) +
ggplot2::scale_y_continuous(expand = c(0, 0)) +
## No background. Center and resize title. Resize axis labels.
ggplot2::theme(panel.background = ggplot2::element_blank(),
plot.title = ggplot2::element_text(hjust = 0.5, size = 10),
axis.title = ggplot2::element_text(size = 9)) +
ggplot2::labs(y = "Frequency", x = legends[5], title = legends[6])
if (output) {
## do.call is needed since grid.arrange doesn't accept lists as input.
do.call(gridExtra::grid.arrange,
args = c(Filter(f = Negate(f = is.null), x = plots),
list(ncol = 3, top = plotTitle)))
}
return(plots)
}
#' Helper function for creating field plots.
#'
#' @noRd
#' @keywords internal
fieldPlot <- function(plotDat,
fillVar,
title,
colors,
zlim = range(plotDat[fillVar]),
xTicks = ggplot2::waiver(),
...) {
p <- ggplot2::ggplot(data = plotDat,
ggplot2::aes_string(x = "colNum", y = "rowNum",
fill = fillVar)) +
ggplot2::geom_tile() +
## Remove empty space between ticks and actual plot.
ggplot2::scale_x_continuous(expand = c(0, 0), breaks = xTicks) +
ggplot2::scale_y_continuous(expand = c(0, 0)) +
## Adjust plot colors.
ggplot2::scale_fill_gradientn(limits = zlim, colors = colors) +
## No background. Center and resize title. Resize axis labels.
## Remove legend title and resize legend entries.
ggplot2::theme(panel.background = ggplot2::element_blank(),
plot.title = ggplot2::element_text(hjust = 0.5, size = 10),
axis.title = ggplot2::element_text(size = 9),
legend.title = ggplot2::element_blank(),
legend.text = ggplot2::element_text(size = 8,
margin = ggplot2::margin(l = 5))) +
ggplot2::ggtitle(title)
return(p)
}
#' Helper function for creating field plots with percentages.
#'
#' @noRd
#' @keywords internal
fieldPlotPcts <- function(plotDat,
fillVar,
title,
colors,
zlim = range(plotDat[fillVar]),
xTicks = ggplot2::waiver(),
scaleLim = Inf,
...) {
p <- ggplot2::ggplot(
data = plotDat,
ggplot2::aes_string(x = "colNum", y = "rowNum",
fill = fillVar,
color = if (is.infinite(scaleLim)) NULL else "''")) +
ggplot2::geom_tile(na.rm = TRUE) +
## Remove empty space between ticks and actual plot.
ggplot2::scale_x_continuous(expand = c(0, 0), breaks = xTicks) +
ggplot2::scale_y_continuous(expand = c(0, 0)) +
## Adjust plot colors.
ggplot2::scale_fill_gradientn(limits = zlim, colors = colors, name = NULL,
labels = scales::percent,
breaks = seq(zlim[1], zlim[2], length.out = 5)) +
ggplot2::scale_color_manual(values = NA) +
ggplot2::guides(fill = ggplot2::guide_colorbar(order = 1),
color = ggplot2::guide_legend("Larger than scale limit",
override.aes = list(fill = "grey50",
color = "grey50"))) +
## No background. Center and resize title. Resize axis labels.
## Remove legend title and resize legend entries.
ggplot2::theme(panel.background = ggplot2::element_blank(),
plot.title = ggplot2::element_text(hjust = 0.5, size = 10),
axis.title = ggplot2::element_text(size = 9)) +
ggplot2::ggtitle(title)
return(p)
}
#' Helper function for creating time lapse plots.
#'
#' @importFrom grDevices colorRampPalette
#' @noRd
#' @keywords internal
timeLapsePlot <- function(fitMods,
outFile = "spatialTrends.gif",
...) {
dotArgs <- list(...)
if (!is.null(dotArgs$scaleLim)) {
scaleLim <- dotArgs$scaleLim / 100
} else {
scaleLim <- Inf
}
animation::saveGIF({
## Get trait from fitted models.
trait <- fitMods[[1]]$model$response
## First get plot data for all fields so a single zLim can be extracted
## for all plots.
plotSpatDats <- lapply(X = fitMods, FUN = function(fitMod) {
## Get data from fitted model
plotDat <- fitMod$data
## Get min and max values for rows and columns.
yMin <- min(plotDat$rowNum)
yMax <- max(plotDat$rowNum)
xMin <- min(plotDat$colNum)
xMax <- max(plotDat$colNum)
## Execute this part first since it needs plotData without missings
## removed.
## Code mimickes code from SpATS package but is adapted to create a
## data.frame useable by ggplot.
plotDat <- plotDat[order(plotDat$colNum, plotDat$rowNum), ]
nCol <- xMax - xMin + 1
nRow <- yMax - yMin + 1
p1 <- 100 %/% nCol + 1
p2 <- 100 %/% nRow + 1
## Get spatial trend from SpATS object.
spatTr <- SpATS::obtain.spatialtrend(fitMod,
grid = c(nCol * p1, nRow * p2))
## spatial trend contains values for all data points, so NA in original
## data need to be removed. The kronecker multiplication is needed to
## convert the normal row col pattern to the smaller grid extending the
## missing values.
## First a matrix M is created containing information for all
## columns/rows in the field even if they are completely empty.
M <- matrix(nrow = nRow, ncol = nCol,
dimnames = list(yMin:yMax, xMin:xMax))
for (i in seq_len(nrow(plotDat))) {
M[as.character(plotDat[i, "rowNum"]),
as.character(plotDat[i, "colNum"])] <-
ifelse(is.na(plotDat[i, trait]), NA, 1)
}
spatTrDat <- kronecker(M, matrix(data = 1, ncol = p1, nrow = p2)) *
spatTr[["fit"]]
## Melt to get the data in ggplot shape. Rows and columns in the
## spatial trend coming from SpATS are swapped so therefore use t()
plotDatSpat <- reshape2::melt(t(spatTrDat),
varnames = c("colNum", "rowNum"))
## Add true values for columns and rows for plotting.
plotDatSpat[["colNum"]] <- spatTr[["col.p"]]
plotDatSpat[["rowNum"]] <- rep(x = spatTr[["row.p"]], each = p1 * nCol)
## Remove missings from data.
plotDatSpat <- ggplot2::remove_missing(plotDatSpat, na.rm = TRUE)
## Divide by mean value of trait to get trend as percentage.
plotDatSpat[["mean"]] <- mean(plotDat[[trait]], na.rm = TRUE)
plotDatSpat[["value"]] <- plotDatSpat[["value"]] / plotDatSpat[["mean"]]
## Set values outside scale limits to NA.
plotDatSpat[["value"]][plotDatSpat[["value"]] > scaleLim |
plotDatSpat[["value"]] < -scaleLim] <- NA
return(plotDatSpat)
})
## Extract all zVals to use identical limits for spatial pattern
## This enables proper comparison of plots over timePoints.
zVals <- unlist(sapply(X = plotSpatDats, `[[`, "value"))
if (is.infinite(scaleLim)) {
zLim <- c(-1, 1) * max(c(abs(zVals), 0.1), na.rm = TRUE)
} else {
zLim <- c(-scaleLim, scaleLim)
}
## Create a plot of the spatial trend per time point.
for (i in seq_along(plotSpatDats)) {
p <- fieldPlotPcts(plotDat = plotSpatDats[[i]], fillVar = "value",
title = names(plotSpatDats)[i],
colors = colorRampPalette(c("red", "yellow", "blue"),
space = "Lab")(100),
zlim = zLim, scaleLim = scaleLim)
plot(p)
}
}, movie.name = outFile, autobrowse = FALSE, loop = 1)
}
#' Function for extracting objects of class fitMod that keeps class.
#'
#' @param x An object of class fitMod.
#' @param i An index specifying the element to extract of replace.
#' @param ... Ignored.
#'
#' @noRd
#' @export
`[.fitMod` <- function(x,
i,
...) {
timePoints <- chkTimePoints(x, i)
timePointsX <- attr(x, which = "timePoints")
timePointsR <- timePointsX[timePointsX[["timePoint"]] %in% timePoints, ]
if (nrow(timePointsR) > 0) {
class(x) <- "list"
r <- x[timePointsR[["timePoint"]]]
attr(r, "timePoints") <- timePointsR
attr(r, "experimentName") <- attr(x, "experimentName")
attr(r, "what") <- attr(x, "what")
attr(r, "useRepId") <- attr(x, "useRepId")
attr(r, "useCheck") <- attr(x, "useCheck")
attr(r, "spatial") <- attr(x, "spatial")
attr(r, "class") <- c("fitMod", "list")
attr(r, "timestamp") <- attr(x, "timestamp")
} else {
r <- NULL
}
return(r)
}