-
Notifications
You must be signed in to change notification settings - Fork 0
/
visualization.R
198 lines (164 loc) · 7.12 KB
/
visualization.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
require(scales)
#Plot and compare AIC loss results
plotGAIC <- function(res, robF="", resCols="black", randMod=F, resLtys=1,
legLabs=NA, xlim=NA, figLab="", ylim=NA) {
#@param res: The result of a GAIC run. This is the "Main" result
#@param robF: A file containing a robust run (ie. repeated runs from the multiGAIC function)
#@param resCols: The set of colours for each result
#@param resLtys: The set of line types for each result
#@param randMod: If comparing to a random model
#@param legLabs: The text to go into the legend describing res, and resComp/resRob
#@param xlim: Manually set xrange for magnification
#@param ylim: Manually set yrange for magnification
#@param figLab: A label for the top left hand of the image. For multiple plotting
#Searching for Robustness run info
if(file.exists(robF)) {
resRob <- readRDS(robF)
} else {
resRob <- (res[[1]])[numeric(0)]
}
#Calculating robustness info
robLine <- sapply(res[[1]]$MaxD, function(d) {
dGAIC <- resRob[MaxD==d, (GAIC)]
return(c(mean(dGAIC), sd(dGAIC)))
})
#Create plot
x <- c(sapply(res, function(x){x$MaxD}), resRob$MaxD)
y <- c(sapply(res, function(x){x$GAIC}), resRob$GAIC)
par(mar=c(5,5,5.2,2)+0.1)
if(is.na(xlim)) {xlim = range(x)}
if(is.na(ylim)) {ylim = range(y)*1.10}
plot(x, y, type='n', ylab="AIC Difference from Null Model", xlab = "Maximum Distance Threshold", xaxs = "i",
xlim=xlim, ylim= ylim, cex.axis=1.4, cex.lab=1.65)
#Background
rect(par("usr")[1],par("usr")[3],par("usr")[2],par("usr")[4],col = "antiquewhite")
ticStep <- (par()$yaxp[2]-par()$yaxp[1])/(par()$yaxp[3])
ticLoc <- seq(par()$yaxp[1],par()$yaxp[2], ticStep)
abline(h=ticLoc, col="white", lwd=2.5)
abline(h=(ticLoc+ticStep/2), col="white", lwd=1)
abline(h=0, lty=2)
resMins <- sapply(res, function(x){x$MaxD[which.min(x$GAIC)]})
abline(v=resMins, col="grey", lty=1, lwd=1.5)
#Plot res
for(i in 1:length(res)) {
lines(res[[i]]$MaxD, res[[i]]$GAIC, col=resCols[i], lty=resLtys[i], lwd=3)
}
#Plot Rob
lines(res[[1]]$MaxD, robLine[1,], col=resCols[length(resCols)], lwd=2, lty=resLtys[length(resLtys)])
abline(v=res[[1]]$MaxD[which.min(robLine[1,])], col="grey", lty=1, lwd=1.5)
polygon(c(res[[1]]$MaxD, rev(res[[1]]$MaxD)),
c(robLine[1,]+robLine[2,],rev(robLine[1,]-robLine[2,])),
col=alpha("black", 0.08), border = alpha("black", 0.65), lty=1, lwd=0.5)
#Define steps for text labels on plot
vStep <- (par("usr")[4]-par("usr")[3])/50
hStep <- (par("usr")[2]-par("usr")[1])/50
if(nrow(resRob)>0) {
if(which.min(res[[1]]$GAIC)>which.min(robLine[1,])) {hStep <- -hStep}
}
#Highlight min loc (if different)
for(i in 1:length(unique(resMins))){
resMin <- unique(resMins)[i]
lines(rep(resMin, 2), c(par("usr")[3], par("usr")[3]+vStep*2), col=resCols[i], lwd=4)
lines(rep(resMin, 2), c(par("usr")[3], par("usr")[3]+vStep*1), col="white", lwd=0.75)
points(resMin, par("usr")[3])
#text(resMin+hStepi*2, par("usr")[3]+vStep*4, resMin, cex=1.25)
}
if(nrow(resRob)>0){
#Highlight min loc for Rob (if different)
if(which.min(res[[1]]$GAIC)!=which.min(robLine[1,])) {
lines(rep(res[[1]]$MaxD[which.min(robLine[1,])], 2), c(par("usr")[3], par("usr")[3]+vStep*2),
col=resCols[length(resCols)],lty=resLtys[length(resLtys)], lwd=4)
lines(rep(res[[1]]$MaxD[which.min(robLine[1,])], 2), c(par("usr")[3], par("usr")[3]+vStep*1), col="white", lwd=0.5)
points(res[[1]]$MaxD[which.min(robLine[1,])], par("usr")[3])
text(res[[1]]$MaxD[which.min(robLine[1,])]+hStep*2, par("usr")[3]+vStep*4, res$MaxD[which.min(robLine[1,])], cex=1.25)
}
}
#Legend and figure labelling
par(xpd=NA)
if(!is.na(legLabs)){
legend("bottomright", legend = legLabs, fill = resCols, lty = resLtys, bty='n',cex=1.25)
}
shiftx <- par('usr')[1] - strwidth(figLab, cex=2)*2
shifty <- par('usr')[4] + strheight(figLab, cex=2)*3
text(shiftx, shifty, figLab, cex=2.4)
}
#Plot and compare AIC loss results
plotMinLoc <- function(robFs, cols, legLabs=NA, figLab="", xlim=NA, bw="nrd0") {
#@param robF: A set of files containing robust runs (ie. repeated runs from the multiGAIC function)
#@param cols: A set of colours for the results
#@param legLabs: The text to go into the legend describing res, and resComp/resRob
#@param figLab: A label for the top left hand of the image. For multiple plotting
#@param xlim: Manually set xrange for magnification
#Obtain all optima locations
minLocs <- lapply(robFs, function(f){
resRob <- readRDS(f)
sapply(range(resRob$RunID)[1]:range(resRob$RunID)[2], function(i){
x <- resRob[RunID==i]
x[which.min(GAIC), (MaxD)]
})
})
#Get density plots for optima locations
ds <- lapply(minLocs, function(x){
density(x, bw=bw)
})
#Create a plot
par(mar=c(5,5,5.2,2)+0.1)
x <- unlist(lapply(ds, function(d){d$x}))
y <- unlist(lapply(ds, function(d){d$y}))
if(is.na(xlim)) {xlim = range(x)}
plot(x,y,type='n', xlim=xlim, xaxs="i", yaxs="i", xlab="Optimal Threshold", ylab="Density",
cex.axis=1.4, cex.lab=1.65)
#Background
rect(par("usr")[1],par("usr")[3],par("usr")[2],par("usr")[4],col = "seashell1")
ticStep <- (par()$xaxp[2]-par()$xaxp[1])/(par()$xaxp[3])
ticLoc <- seq(par()$xaxp[1],par()$xaxp[2], ticStep)
abline(v=ticLoc, col="white", lwd=3)
abline(v=(ticLoc+ticStep/4), col="white", lwd=1.8)
abline(v=(ticLoc+2*ticStep/4), col="white", lwd=1.8)
abline(v=(ticLoc+3*ticStep/4), col="white", lwd=1.8)
abline(h=0, lty=2)
#Add density lines
for(i in 1:length(ds)){
l <- ds[[i]]
cl <- cols[[i]]
lines(l, col=cl, lwd=3)
polygon(l$x, l$y,col = alpha(cols[[i]], 0.20))
}
#Legend and figure labelling
par(xpd=NA)
if(!is.na(legLabs)){
legend("topright", legend = legLabs, fill = cols, bty='n',cex=1.5)
}
shiftx <- par('usr')[1] - strwidth(figLab, cex=2)*2
shifty <- par('usr')[4] + strheight(figLab, cex=2)*3
text(shiftx, shifty, figLab, cex=2.4)
}
plotEdgeLength <- function(elL, cols, legLabs=NA, figLab="", ylim=NA, xlim=NA, breaks=NA, xlab="") {
if(is.na(breaks)){
blSpan <- max(unlist(elL))
breaks <- seq(0, max(blSpan), max(blSpan)/10)
}
#Get hist plots for optima locations
hs <- lapply(elL, function(x) {hist(x, breaks=breaks, plot = F)})
hts <- sapply(1:(length(breaks)-1), function(i){
sapply(hs, function(h){
(h$counts[i]/sum(h$counts))
})
})
if(is.na(xlim)) {xlim <- c(1,length(breaks))}
if(is.na(ylim)) {ylim <- c(0,1)}
#Create a plot
par(mar=c(5,5,5.2,2)+0.1)
barplot(hts[,xlim[1]:(xlim[2]-1)], beside=T , col=cols, cex.axis=1.4, cex.lab=1.65,
xlab = xlab, ylab="Frequency", ylim = ylim)
axis(side = 1, labels = round(breaks[xlim[1]:xlim[2]], 3),
at=seq(0.5, (length(elL)+1)*length(breaks[xlim[1]:xlim[2]]), length(elL)+1),cex.axis=1.4)
#Legend and figure labelling
par(xpd=NA)
if(!is.na(legLabs)){
legend("topright", legend = legLabs, fill = cols, bty='n',cex=1.5)
}
shiftx <- par('usr')[1] - strwidth(figLab, cex=2)*2
shifty <- par('usr')[4] + strheight(figLab, cex=2)*3
text(shiftx, shifty, figLab, cex=2.4)
}