/
plot_RLum.Results.R
1175 lines (945 loc) · 38.4 KB
/
plot_RLum.Results.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
#' Plot function for an RLum.Results S4 class object
#'
#' The function provides a standardised plot output for data of an RLum.Results
#' S4 class object
#'
#' The function produces a multiple plot output. A file output is recommended
#' (e.g., [pdf]).
#'
#' @param object [RLum.Results-class] (**required**):
#' S4 object of class `RLum.Results`
#'
#' @param single [logical] (*with default*):
#' single plot output (`TRUE/FALSE`) to allow for plotting the results in as
#' few plot windows as possible.
#'
#' @param ... further arguments and graphical parameters will be passed to
#' the `plot` function.
#'
#' @return Returns multiple plots.
#'
#' @note
#' Not all arguments available for [plot] will be passed!
#' Only plotting of `RLum.Results` objects are supported.
#'
#' @section Function version: 0.2.1
#'
#' @author
#' Christoph Burow, University of Cologne (Germany) \cr
#' Sebastian Kreutzer, Institute of Geography, Heidelberg University (Germany)
#'
#' @seealso [plot], [plot_RLum]
#'
#' @keywords aplot
#'
#' @examples
#'
#'
#' ###load data
#' data(ExampleData.DeValues, envir = environment())
#'
#' # apply the un-logged minimum age model
#' mam <- calc_MinDose(data = ExampleData.DeValues$CA1, sigmab = 0.2, log = TRUE, plot = FALSE)
#'
#' ##plot
#' plot_RLum.Results(mam)
#'
#' # estimate the number of grains on an aliquot
#' grains<- calc_AliquotSize(grain.size = c(100,150), sample.diameter = 1, plot = FALSE, MC.iter = 100)
#'
#' ##plot
#' plot_RLum.Results(grains)
#'
#'
#' @md
#' @export
plot_RLum.Results<- function(
object,
single = TRUE,
...
){
##============================================================================##
## CONSISTENCY CHECK OF INPUT DATA
##============================================================================##
##check if object is of class RLum.Data.Curve
if(!is(object,"RLum.Results")){
stop("[plot_RLum.Results()] Input object is not of type 'RLum.Results'")
}
##============================================================================##
## SAFE AND RESTORE PLOT PARAMETERS ON EXIT
##============================================================================##
par.old <- par(no.readonly = TRUE)
on.exit(suppressWarnings(par(par.old)))
##============================================================================##
## ... ARGUMENTS
##============================================================================##
##deal with addition arguments
extraArgs <- list(...)
##main
main <- if("main" %in% names(extraArgs)) {extraArgs$main} else
{""}
##mtext
mtext <- if("mtext" %in% names(extraArgs)) {extraArgs$mtext} else
{""}
##log
log <- if("log" %in% names(extraArgs)) {extraArgs$log} else
{""}
##lwd
lwd <- if("lwd" %in% names(extraArgs)) {extraArgs$lwd} else
{1}
##lty
lty <- if("lty" %in% names(extraArgs)) {extraArgs$lty} else
{1}
##type
type <- if("type" %in% names(extraArgs)) {extraArgs$type} else
{"l"}
##pch
pch <- if("pch" %in% names(extraArgs)) {extraArgs$pch} else
{1}
##col
col <- if("col" %in% names(extraArgs)) {extraArgs$col} else
{"black"}
##============================================================================##
## PLOTTING
##============================================================================##
#~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~#
## CASE 1: Minimum Age Model / Maximum Age Model
if(object@originator=="calc_MinDose" || object@originator=="calc_MaxDose") {
## single MAM estimate
# plot profile log likelhood
profiles <- object@data$profile
if (object@data$args$log) {
profiles@profile$gamma$par.vals[ ,"gamma"] <- exp(profiles@profile$gamma$par.vals[ ,"gamma"])
profiles@profile$sigma$par.vals[ ,"sigma"] <- exp(profiles@profile$sigma$par.vals[ ,"sigma"])
if (object@data$args$par == 4)
profiles@profile$mu$par.vals[ ,"mu"] <- exp(profiles@profile$mu$par.vals[ ,"mu"])
}
if (single)
par(mfrow=c(2, 2))
param <- c("gamma", "sigma", "p0", "mu")
for (i in param) {
if (object@data$summary$par == 3 && i == "mu")
break
tryCatch({
xvals <- as.data.frame(profiles@profile[[i]]$par.vals)[[i]]
xlim <- range(xvals[xvals > 0])
suppressWarnings(
bbmle::plot(profiles, which = i, xlab = "", xaxt = "n", xlim = xlim)
)
axis(1, mgp = c(3, 0.5, 0))
title(xlab = i, line = 1.2)
if (i %in% c("gamma", "sigma", "mu") && object@data$args$log && object@data$args$log.output) {
axis(1, at = axTicks(1),
labels = format(round(log(axTicks(1)), 2), nsmall = 2),
line = 2.5, mgp = c(3, 0.5, 0))
title(xlab = paste0("log(", i, ")"), line = 4)
}
}, error = function(e) {
message(paste("Unable to plot the Likelihood profile for:", i, "(Likelihood probably infinite)"))
})
}
par(mfrow=c(1,1))
# })
## bootstrap MAM estimates
if(object@data$args$bootstrap==TRUE) {
# save previous plot parameter and set new ones
.pardefault<- par(no.readonly = TRUE)
# get De-llik pairs
pairs<- object@data$bootstrap$pairs$gamma
# get polynomial fit objects
poly.lines<- list(poly.three=object@data$bootstrap$poly.fits$poly.three,
poly.four=object@data$bootstrap$poly.fits$poly.four,
poly.five=object@data$bootstrap$poly.fits$poly.five,
poly.six=object@data$bootstrap$poly.fits$poly.six)
# define polynomial curve functions for plotting
poly.curves<- list(poly.three.curve=function(x) { poly.lines$poly.three$coefficient[4]*x^3 + poly.lines$poly.three$coefficient[3]*x^2 + poly.lines$poly.three$coefficient[2]*x + poly.lines$poly.three$coefficient[1] },
poly.four.curve=function(x) { poly.lines$poly.four$coefficient[5]*x^4 + poly.lines$poly.four$coefficient[4]*x^3 + poly.lines$poly.four$coefficient[3]*x^2 + poly.lines$poly.four$coefficient[2]*x + poly.lines$poly.four$coefficient[1] },
poly.five.curve=function(x) { poly.lines$poly.five$coefficient[6]*x^5 + poly.lines$poly.five$coefficient[5]*x^4 + poly.lines$poly.five$coefficient[4]*x^3 + poly.lines$poly.five$coefficient[3]*x^2 + poly.lines$poly.five$coefficient[2]*x + poly.lines$poly.five$coefficient[1] },
poly.six.curve=function(x) { poly.lines$poly.six$coefficient[7]*x^6 + poly.lines$poly.six$coefficient[6]*x^5 + poly.lines$poly.six$coefficient[5]*x^4 + poly.lines$poly.six$coefficient[4]*x^3 + poly.lines$poly.six$coefficient[3]*x^2 + poly.lines$poly.six$coefficient[2]*x + poly.lines$poly.six$coefficient[1] })
## --------- PLOT "RECYCLE" BOOTSTRAP RESULTS ------------ ##
if(single==TRUE) {
layout(cbind(c(1,1,2, 5,5,6), c(3,3,4, 7,7,8)))
par(cex = 0.6)
} else {
layout(matrix(c(1,1,2)),2,1)
par(cex = 0.8)
}
for(i in 1:4) {
## ----- LIKELIHOODS
# set margins (bottom, left, top, right)
par(mar=c(0,5,5,3))
# sort De and likelihoods by De (increasing)
pairs<- pairs[order(pairs[,1]),]
# remove invalid NA values
pairs<- na.omit(pairs)
plot(x=pairs[,1],
y=pairs[,2],
xlab="Equivalent Dose [Gy]",
ylab="Likelihood",
xlim=range(pretty(pairs[,1])),
ylim=range(pretty(c(0, as.double(quantile(pairs[,2],probs=0.98))))),
xaxt = "n",
xaxs = "i",
yaxs = "i",
bty = "l",
main="Recycled bootstrap MAM-3")
axis(side = 1, labels = FALSE, tick = FALSE)
# add subtitle
mtext(as.expression(bquote(italic(M) == .(object@data$args$bs.M) ~ "|" ~
italic(N) == .(object@data$args$bs.N) ~ "|" ~
italic(sigma[b]) == .(object@data$args$sigmab) ~
"\u00B1" ~ .(object@data$args$sigmab.sd) ~ "|" ~
italic(h) == .(round(object@data$args$bs.h,1))
)
),
side = 3, line = 0.3, adj = 0.5,
cex = if(single){0.5}else{0.8})
# add points
points(x=pairs[,1], y=pairs[,2], pch=1, col = "grey80")
# get polynomial function
poly.curve<- poly.curves[[i]]
# add curve to plot
curve(poly.curve, from = min(pairs[,1]), to = (max(pairs[,1])),
col = "black", add = TRUE, type = "l")
# add legend
legend<- c("Third degree", "Fourth degree", "Fifth degree", "Sixth degree")
legend("topright", xjust = 0,
legend = legend[i],
y.intersp = 1.2,
bty = "n",
title = "Polynomial Fit",
lty = 1,
lwd= 1.5)
## ----- RESIDUALS
# set margins (bottom, left, top, right)
par(mar=c(5,5,0,3))
plot(x = pairs[,1],
y = residuals(poly.lines[[i]]),
ylim = c(min(residuals(poly.lines[[i]]))*1.2,
as.double(quantile(residuals(poly.lines[[i]]),probs=0.99))),
xlim=range(pretty(pairs[,1])),
xaxt = "n",
bty = "l",
xaxs = "i",
col = "grey80",
ylab = "Fit residual",
xlab = "Equivalent dose [Gy]")
axis(side = 1, labels = TRUE, tick = TRUE)
# add horizontal line
abline(h = 0, lty=2)
# calculate residual sum of squares (RSS) and add to plot
rss<- sum(residuals(poly.lines[[i]])^2)
mtext(text = paste("RSS =",round(rss,3)), adj = 1,
side = 3, line = -2,
cex = if(single){0.6}else{0.8})
## ----- PROPORTIONS
}##EndOf::Plot_loop
# restore previous plot parameters
par(.pardefault)
### TODO: plotting of the LOESS fit needs to be fleshed out
### possibly integrate this in the prior polynomial plot loop
### LOESS PLOT
pairs<- object@data$bootstrap$pairs$gamma
pred<- predict(object@data$bootstrap$loess.fit)
loess<- cbind(pairs[,1], pred)
loess<- loess[order(loess[,1]),]
# plot gamma-llik pairs
plot(pairs,
ylim = c(0, as.double(quantile( pairs[,2],probs=0.99))),
ylab = "Likelihood",
xlab = "Equivalent dose [Gy]",
col = "gray80")
# add LOESS line
lines(loess, type = "l", col = "black")
### ------ PLOT BOOTSTRAP LIKELIHOOD FIT
par(mar=c(5,4,4,4))
xlim<- range(pretty(object@data$data[,1]))
xlim[1]<- xlim[1]-object@data$data[which.min(object@data$data[,1]),2]
xlim[2]<- xlim[2]+object@data$data[which.max(object@data$data[,1]),2]
xlim<- range(pretty(xlim))
# empty plot
plot(NA,NA,
xlim=xlim,
ylim=c(0,2),
xlab="Equivalent dose [Gy]",
ylab="",
bty="l",
axes=FALSE,
xaxs="i",
yaxs="i",
yaxt="n")
axis(side = 1)
axis(side = 2, at = c(0,0.5,1))
mtext(text = "Normalised likelihood / density", side = 2, line = 2.5, adj = 0)
# set the polynomial to plot
poly.curve<- poly.curves[[1]] # three degree poly
# plot a nice grey polygon as in the publication
step<- 0.1
x<- seq(min(pairs[,1]), max(pairs[,1]), step)
y<- poly.curve(x)
# normalise y-values
y<- y/max(y)
x<- c(min(pairs[,1]), x, max(pairs[,1]))
y<- c(0, y, 0)
# cutoff negative y values
neg<- which(y<0)
if (length(neg) != 0) {
y<- y[-neg]
x<- x[-neg]
}
# add bootstrap likelihood polygon to plot
polygon(x, y, col = "grey80", border = NA)
if (all(x > max(xlim)) || all(x < min(xlim)))
warning("Bootstrap estimates out of x-axis range.", call. = FALSE)
### ----- PLOT MAM SINGLE ESTIMATE
# symmetric errors, might not be appropriate
mean<- object@data$summary$de
sd<- object@data$summary$de_err
if (any(is.na(c(mean, sd)))) {
warning("Unable to plot the MAM single estimate (NA value).", call. = FALSE)
} else {
x<- seq(mean-5*sd, mean+5*sd, 0.001)
y<- dnorm(seq(mean-5*sd, mean+5*sd, 0.001), mean, sd)
# normalise y-values
y<- y/max(y)
points(x, y,
type="l",
col="red")
## asymmetric errors
x<- unlist(object@data$profile@profile$gamma$par.vals[,1])
y<- abs(unlist(object@data$profile@profile$gamma$z))
if(object@data$args$log == TRUE) {
x<- exp(x)
}
# now invert the data by shifting
y<- -y
y<- y-min(y)
y<- y/max(y)
# fit a smoothing spline
l<- spline(x = x, y = y, method = "n", n = 1000)
# make the endpoints zero
l$y[1]<- l$y[length(l$y)]<- 0
# add profile log likelihood curve to plot
lines(l, col="blue", lwd=1)
# add vertical lines of the mean values
#points(x = 80, y = 100,type = "l")
}
#### ------ PLOT DE
par(new = TRUE)
# sort the data in ascending order
dat<- object@data$data[order(object@data$data[,1]),]
x<- dat[,1]
y<- 1:length(object@data$data[,1])
plot(x = x, y = y,
xlim=xlim,
ylim=c(0, max(y)+1),
axes = FALSE,
pch = 16,
xlab = "",
ylab="",
xaxs="i",
yaxs="i")
axis(side = 4)
mtext(text = "# Grain / aliquot", side = 4, line = 2.5)
# get sorted errors
err<- object@data$data[order(object@data$data[,1]),2]
# fancy error bars
arrows(x0 = x-err, y0 = y,
x1 = x+err, y1 = y,
code = 3, angle = 90, length = 0.05)
### ---- AUXILLARY
# add legend
legend("bottomright",
bty = "n",
col = c("grey80", "red", "blue", "black"),
pch = c(NA,NA,NA,16),
lty = c(1,1,1,1),
lwd=c(10,2,2,2),
legend = c("Bootstrap likelihood", "Profile likelihood (gaussian fit)","Profile likelihood", "Grain / aliquot"),
)
}##EndOf::Bootstrap_plotting
}#EndOf::CASE1_MinimumAgeModel-3
#~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~#
## CASE 2: Central Age Model
if(object@originator=="calc_CentralDose") {
# get profile log likelihood data
sig<- object@data$profile$sig*100
llik<- object@data$profile$llik
# save previous plot parameter and set new ones
.pardefault<- par(no.readonly = TRUE)
# plot the profile log likeihood
par(oma=c(2,1,2,1),las=1,cex.axis=1.2, cex.lab=1.2)
plot(sig,llik,type="l",xlab=as.expression(bquote(sigma[OD]~"[%]")),ylab="Log likelihood",lwd=1.5)
abline(h=0,lty=3)
abline(h=-1.92,lty=3)
title(as.expression(bquote("Profile log likelihood for" ~ sigma[OD])))
# find upper and lower confidence limits for sigma
sigmax<- sig[which.max(llik)]
tf<- abs(llik+1.92) < 0.05
sig95<- sig[tf]
ntf<- length(sig95)
sigL<- sig95[1]
sigU<- sig95[ntf]
# put them on the graph
abline(v=sigL)
abline(v=sigmax)
abline(v=sigU)
dx<- 0.006
dy<- 0.2
ytext<- min(llik) + dy
res<- c(sigL,sigmax,sigU)
text(res+dx,rep(ytext,3),round(res,2),adj=0)
# restore previous plot parameters
par(.pardefault)
rm(.pardefault)
}##EndOf::Case 2 - calc_CentralDose()
#~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~#
## CASE 3: Fuchs & Lang 2001
if(object@originator=="calc_FuchsLang2001") {
##deal with addition arguments
extraArgs <- list(...)
main <- if("main" %in% names(extraArgs)) {extraArgs$main} else {"Fuchs & Lang (2001)"}
xlab <- if("xlab" %in% names(extraArgs)) {extraArgs$xlab} else {expression(paste(D[e]," [s]"))}
ylab <- if("ylab" %in% names(extraArgs)) {extraArgs$ylab} else {"# Aliquots"}
sub <- if("sub" %in% names(extraArgs)) {extraArgs$sub} else {""}
cex <- if("cex" %in% names(extraArgs)) {extraArgs$cex} else {1}
lwd <- if("lwd" %in% names(extraArgs)) {extraArgs$lwd} else {1}
pch <- if("pch" %in% names(extraArgs)) {extraArgs$pch} else {19}
ylim <- if("ylim" %in% names(extraArgs)) {extraArgs$ylim} else {c(1,length(object@data$data[,1])+3)}
xlim <- if("xlim" %in% names(extraArgs)) {extraArgs$xlim} else {c(min(object@data$data[,1])-max(object@data$data[,2]), max(object@data$data[,1])+max(object@data$data[,2]))}
mtext <- if("mtext" %in% names(extraArgs)) {extraArgs$mtext} else {"unknown sample"}
# extract relevant plotting parameters
o<- order(object@data$data[[1]])
data_ordered<- object@data$data[o,]
usedDeValues<- object@data$usedDeValues
n.usedDeValues<- object@data$summary$n.usedDeValues
par(cex = cex, mfrow=c(1,1))
##PLOT
counter<-seq(1,max(o))
plot(NA,NA,
ylim = ylim,
xlim = xlim,
xlab = xlab,
ylab = ylab,
main = main,
sub = sub)
##SEGMENTS
segments(data_ordered[,1]-data_ordered[,2],1:length(data_ordered[,1]),
data_ordered[,1]+data_ordered[,2],1:length(data_ordered[,1]),
col="gray")
##POINTS
points(data_ordered[,1], counter,pch=pch)
##LINES
##BOUNDARY INFORMATION
##lower boundary
lines(c(
usedDeValues[length(usedDeValues[,1])-n.usedDeValues+1,1], #boundary_counter for incorporate skipped values
usedDeValues[length(usedDeValues[,1])-n.usedDeValues+1,1]),
c(min(o)-0.5,max(o)+0.5),
col="red",
lty="dashed", lwd = lwd)
#upper boundary
lines(c(max(usedDeValues[,1]),max(usedDeValues[,1])),c(min(o)-0.5,max(o)+0.5),
col="red",lty="dashed", lwd = lwd)
#plot some further informations into the grafik
arrows(
usedDeValues[length(usedDeValues[,1])-n.usedDeValues+1,1]+usedDeValues[length(usedDeValues[,1])-n.usedDeValues+1,1]*0.02, #x1
max(o)+0.5, #y1
max(usedDeValues[,1]-usedDeValues[,1]*0.02), #x2
max(o)+0.5, #y2,
code=3,
length=0.03)
text(
c(
usedDeValues[length(usedDeValues[,1])-n.usedDeValues+1,1],
usedDeValues[length(usedDeValues[,1])-n.usedDeValues+1,1]),
c(max(o)+2,max(o)+2),
labels=paste("used values = ", n.usedDeValues),
cex=0.6*cex,
adj=0)
##MTEXT
mtext(side=3,mtext,cex=cex)
}
#~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~#
## CASE 4: Finite Mixture Model --------
if(object@originator == "calc_FiniteMixture") {
if(length(object@data$args$n.components) > 1) {
##deal with addition arguments
extraArgs <- list(...)
main <- if("main" %in% names(extraArgs)) {extraArgs$main} else {"Finite Mixture Model"}
plot.proportions<- if("plot.proportions" %in% names(extraArgs)) {extraArgs$plot.proportions} else {TRUE}
pdf.colors<- if("pdf.colors" %in% names(extraArgs)) {extraArgs$pdf.colors} else {"gray"}
pdf.weight<- if("pdf.weight" %in% names(extraArgs)) {extraArgs$pdf.weight} else {TRUE}
pdf.sigma<- if("pdf.sigma" %in% names(extraArgs)) {extraArgs$pdf.sigma} else {"sigmab"}
# extract relevant data from object
n.components <- object@data$args$n.components
comp.n <- object@data$components
sigmab <- object@data$args$sigmab
BIC.n <- object@data$BIC$BIC
LLIK.n <- object@data$llik$llik
# save previous plot parameter and set new ones
.pardefault<- par(no.readonly = TRUE)
## DEVICE AND PLOT LAYOUT
n.plots<- length(n.components) #number of PDF plots in plot area #1
seq.vertical.plots<- seq(from = 1, to = n.plots, by = 1) #indices
ID.plot.two<- n.plots+if(plot.proportions==TRUE){1}else{0} #ID of second plot area
ID.plot.three<- n.plots+if(plot.proportions==TRUE){2}else{1} #ID of third plot area
#empty vector for plot indices
seq.matrix<- vector(mode="integer", length=4*n.plots)
#fill vector with plot indices in correct order
cnt<- 1
seq<- seq(1,length(seq.matrix),4)
for(i in seq) {
seq.matrix[i]<- cnt
seq.matrix[i+1]<- cnt
seq.matrix[i+2]<- if(plot.proportions==TRUE){ID.plot.two}else{cnt}
seq.matrix[i+3]<- ID.plot.three
cnt<- cnt+1
}
# create device layout
layout(matrix(c(seq.matrix),4,n.plots))
# outer margins (bottom, left, top, right)
par(oma=c(2.5,5,3,5))
# general plot parameters (global scaling, allow overplotting)
par(cex = 0.8, xpd = NA)
# define colour palette for prettier output
if(pdf.colors == "colors") {
col.n<- c("red3", "slateblue3", "seagreen", "tan3", "yellow3",
"burlywood4", "magenta4", "mediumpurple3", "brown4","grey",
"aquamarine")
poly.border<- FALSE
}
if(pdf.colors == "gray" || pdf.colors == "grey") {
col.n<- gray.colors(length(n.components)*2)
poly.border<- FALSE
}
if(pdf.colors == "none") {
col.n<- NULL
poly.border<- TRUE
}
##--------------------------------------------------------------------------
## PLOT 1: EQUIVALENT DOSES OF COMPONENTS
## create empty plot without x-axis
for(i in 1:n.plots) {
pos.n<- seq(from = 1, to = n.components[i]*3, by = 3)
# set margins (bottom, left, top, right)
par(mar=c(1,0,2,0))
# empty plot area
plot(NA, NA,
xlim=c(min(n.components)-0.2, max(n.components)+0.2),
ylim=c(min(comp.n[pos.n,]-comp.n[pos.n+1,], na.rm = TRUE),
max((comp.n[pos.n,]+comp.n[pos.n+1,])*1.1, na.rm = TRUE)),
ylab="",
xaxt="n",
yaxt="n",
xlab="")
# add text in upper part of the plot ("k = 1,2..n")
mtext(bquote(italic(k) == .(n.components[i])),
side = 3, line = -2, cex=0.8)
# add y-axis label (only for the first plot)
if(i==1) {
mtext(expression(paste("D"[e]," [Gy]")), side=2,line=2.7, cex=1)
}
# empty list to store normal distribution densities
sapply.storage<- list()
## NORMAL DISTR. OF EACH COMPONENT
options(warn=-1) #suppress warnings for NA values
# LOOP - iterate over number of components
for(j in 1:max(n.components)) {
# draw random values of the ND to check for NA values
comp.nd.n<- sort(rnorm(n = length(object@data$data[,1]),
mean = comp.n[pos.n[j],i],
sd = comp.n[pos.n[j]+1,i]))
# proceed if no NA values occurred
if(length(comp.nd.n)!=0) {
# weight - proportion of the component
wi<- comp.n[pos.n[j]+2,i]
# calculate density values with(out) weights
fooX<- function(x) {
dnorm(x, mean = comp.n[pos.n[j],i],
sd = if(pdf.sigma=="se"){comp.n[pos.n[j]+1,i]}
else{if(pdf.sigma=="sigmab"){comp.n[pos.n[j],i]*sigmab}}
)*
if(pdf.weight==TRUE){wi}else{1}
}
# x-axis scaling - determine highest dose in first cycle
if(i==1 && j==1){
max.dose<- max(object@data$data[,1])+sd(object@data$data[,1])/2
min.dose<- min(object@data$data[,1])-sd(object@data$data[,1])/2
# density function to determine y-scaling if no weights are used
fooY<- function(x) {
dnorm(x, mean = na.exclude(comp.n[pos.n,]),
sd = na.exclude(comp.n[pos.n+1,]))
}
# set y-axis scaling
dens.max<-max(sapply(0:max.dose, fooY))
}##EndOfIf::first cycle settings
# override y-axis scaling if weights are used
if(pdf.weight==TRUE){
sapply.temp<- list()
for(b in 1:max(n.components)){
# draw random values of the ND to check for NA values
comp.nd.n<- sort(rnorm(n = length(object@data$data[,1]),
mean = comp.n[pos.n[b],i],
sd = comp.n[pos.n[b]+1,i]))
# proceed if no NA values occurred
if(length(comp.nd.n)!=0) {
# weight - proportion of the component
wi.temp<- comp.n[pos.n[b]+2,i]
fooT<- function(x) {
dnorm(x, mean = comp.n[pos.n[b],i],
sd = if(pdf.sigma=="se"){comp.n[pos.n[b]+1,i]}
else{if(pdf.sigma=="sigmab"){comp.n[pos.n[b],i]*sigmab}}
)*wi.temp
}
sapply.temp[[b]]<- sapply(0:max.dose, fooT)
}
}
dens.max<- max(Reduce('+', sapply.temp))
}
# calculate density values for 0 to maximum dose
sapply<- sapply(0:max.dose, fooX)
# save density values in list for sum curve of gaussians
sapply.storage[[j]]<- sapply
## determine axis scaling
# x-axis (dose)
if("dose.scale" %in% names(extraArgs)) {
y.scale<- extraArgs$dose.scale
} else {
y.scale<- c(min.dose,max.dose)
}
# y-axis (density)
if("pdf.scale" %in% names(extraArgs)) {
x.scale<- extraArgs$pdf.scale
} else {
x.scale<- dens.max*1.1
}
## PLOT Normal Distributions
par(new=TRUE)
plot(sapply, 1:length(sapply)-1,
type="l", yaxt="n", xaxt="n", col=col.n[j], lwd=1,
ylim=y.scale,
xlim=c(0,x.scale),
xaxs="i", yaxs="i",
ann=FALSE, xpd = FALSE)
# draw coloured polygons under curve
polygon(x=c(min(sapply), sapply, min(sapply)),
y=c(0, 0:max.dose, 0),
col = adjustcolor(col.n[j], alpha.f = 0.66),
yaxt="n", border=poly.border, xpd = FALSE, lty = 2, lwd = 1.5)
}
}##EndOf::Component loop
#turn warnings on again
options(warn=0)
# Add sum of Gaussian curve
par(new = TRUE)
plot(Reduce('+', sapply.storage),1:length(sapply)-1,
type="l", yaxt="n", xaxt="n", col="black",
lwd=1.5, lty = 1,
ylim=y.scale,
xlim=c(0,x.scale),
xaxs="i", yaxs="i", ann=FALSE, xpd = FALSE)
# draw additional info during first k-cycle
if(i == 1) {
# plot title
mtext("Normal distributions",
side = 3, font = 2, line = 0, adj = 0, cex = 0.8)
# main title
mtext(main,
side = 3, font = 2, line = 3.5, adj = 0.5,
at = grconvertX(0.5, from = "ndc", to = "user"))
# subtitle
mtext(as.expression(bquote(italic(sigma[b]) == .(sigmab) ~
"|" ~ n == .(length(object@data$data[,1])))),
side = 3, font = 1, line = 2.2, adj = 0.5,
at = grconvertX(0.5, from = "ndc", to = "user"), cex = 0.9)
# x-axis label
mtext("Density [a.u.]",
side = 1, line = 0.5, adj = 0.5,
at = grconvertX(0.5, from = "ndc", to = "user"))
# draw y-axis with proper labels
axis(side=2, labels = TRUE)
}
if(pdf.colors == "colors") {
# create legend labels
dose.lab.legend<- paste("c", 1:n.components[length(n.components)], sep="")
if(max(n.components)>8) {
ncol.temp<- 8
yadj<- 1.025
} else {
ncol.temp<- max(n.components)
yadj<- 0.93
}
# add legend
if(i==n.plots) {
legend(grconvertX(0.55, from = "ndc", to = "user"),
grconvertY(yadj, from = "ndc", to = "user"),
legend = dose.lab.legend,
col = col.n[1:max(n.components)],
pch = 15, adj = c(0,0.2), pt.cex=1.4,
bty = "n", ncol=ncol.temp, x.intersp=0.4)
mtext("Components: ", cex = 0.8,
at = grconvertX(0.5, from = "ndc", to = "user"))
}
}
}##EndOf::k-loop and Plot 1
##--------------------------------------------------------------------------
## PLOT 2: PROPORTION OF COMPONENTS
if(plot.proportions==TRUE) {
# margins for second plot
par(mar=c(2,0,2,0))
# create matrix with proportions from a subset of the summary matrix
prop.matrix<- comp.n[pos.n+2,]*100
# stacked barplot of proportions without x-axis
barplot(prop.matrix,
width=1,
xlim=c(0.2, length(n.components)-0.2),
ylim=c(0,100),
axes=TRUE,
space=0,
col=col.n,
xpd=FALSE,
xaxt="n")
# y-axis label
mtext("Proportion [%]",
side=2,line=3, cex=1)
# add x-axis with corrected tick positions
axis(side = 1, labels = n.components, at = n.components+0.5-n.components[1])
# draw a box (not possible with barplot())
box(lty=1, col="black")
# add subtitle
mtext("Proportion of components",
side = 3, font = 2, line = 0, adj = 0, cex = 0.8)
}
##--------------------------------------------------------------------------
## PLOT 3: BIC & LLIK
# margins for third plot
par(mar=c(2,0,2,0))
# prepare scaling for both y-axes
BIC.scale<- c(min(BIC.n)*if(min(BIC.n)<0){1.2}else{0.8},
max(BIC.n)*if(max(BIC.n)<0){0.8}else{1.2})
LLIK.scale<- c(min(LLIK.n)*if(min(LLIK.n)<0){1.2}else{0.8},
max(LLIK.n)*if(max(LLIK.n)<0){0.8}else{1.2})
# plot BIC scores
plot(n.components, BIC.n,
main= "",
type="b",
pch=22,
cex=1.5,
xlim=c(min(n.components)-0.2, max(n.components)+0.2),
ylim=BIC.scale,
xaxp=c(min(n.components), max(n.components), length(n.components)-1),
xlab=expression(paste(italic(k), " Components")),
ylab=expression(paste("BIC")),
cex.lab=1.25)
# following plot should be added to previous
par(new = TRUE)
# plot LLIK estimates
plot(n.components, LLIK.n,
xlim=c(min(n.components)-0.2, max(n.components)+0.2),
xaxp=c(min(n.components), max(n.components), length(n.components)-1),
ylim=LLIK.scale,
yaxt="n", type="b", pch=16, xlab="", ylab="", lty=2, cex = 1.5)
# subtitle
mtext("Statistical criteria",
side = 3, font = 2, line = 0, adj = 0, cex = 0.8)
# second y-axis with proper scaling
axis(side = 4, ylim=c(0,100))
# LLIK axis label
mtext(bquote(italic(L)[max]),
side=4,line=3, cex=1.3)
# legend
legend(grconvertX(0.75, from = "nfc", to = "user"),
grconvertY(0.96, from = "nfc", to = "user"),
legend = c("BIC", as.expression(bquote(italic(L)[max]))),
pch = c(22,16), pt.bg=c("white","black"),
adj = 0, pt.cex=1.3, lty=c(1,2),
bty = "n", horiz = TRUE, x.intersp=0.5)
## restore previous plot parameters
par(.pardefault)
}
}##EndOf::Case 4 - Finite Mixture Model
#~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~#
## CASE 5: Aliquot Size
if(object@originator=="calc_AliquotSize") {
if(!is.null(object@data$MC$estimates)) {
extraArgs <- list(...)
main <- if("main" %in% names(extraArgs)) { extraArgs$main } else { "Monte Carlo Simulation" }
xlab <- if("xlab" %in% names(extraArgs)) { extraArgs$xlab } else { "Amount of grains on aliquot" }
# extract relevant data
MC.n<- object@data$MC$estimates
MC.n.kde<- object@data$MC$kde
MC.stats<- object@data$MC$statistics
MC.q<- object@data$MC$quantile
MC.iter<- object@data$args$MC.iter
# set layout of plotting device
layout(matrix(c(1,1,2)),2,1)
par(cex = 0.8)
## plot MC estimate distribution
# set margins (bottom, left, top, right)
par(mar=c(2,5,5,3))
# plot histogram
hist(MC.n, freq=FALSE, col = "gray90",
main="", xlab=xlab,
xlim = c(min(MC.n)*0.95, max(MC.n)*1.05),
ylim = c(0, max(MC.n.kde$y)*1.1))
# add rugs to histogram
rug(MC.n)
# add KDE curve
lines(MC.n.kde, col = "black", lwd = 1)
# add mean, median and quantils (0.05,0.95)
abline(v=c(MC.stats$mean, MC.stats$median, MC.q),
lty=c(2, 4, 3,3), lwd = 1)
# add main- and subtitle
mtext(main, side = 3, adj = 0.5,
line = 3, cex = 1)
mtext(as.expression(bquote(italic(n) == .(MC.iter) ~ "|" ~
italic(hat(mu)) == .(round(MC.stats$mean)) ~ "|" ~
italic(hat(sigma)) == .(round(MC.stats$sd.abs)) ~ "|" ~
italic(frac(hat(sigma),sqrt(n))) == .(round(MC.stats$se.abs)) ~ "|" ~
italic(v) == .(round(MC.stats$skewness, 2))
)