-
Notifications
You must be signed in to change notification settings - Fork 2
/
auxFunctionsSimulation.R
211 lines (160 loc) · 6.19 KB
/
auxFunctionsSimulation.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
### Some functions to use later on:
# Function to begin a new cohort: delete the last element and add a new rec
toNewYear = function(vec, firstVal = 5){
newvec = vec[-length(vec)]
newvec = c(firstVal, newvec)
return(newvec)
}
toLenAgeMatSampled = function(mat, vec){
tmpmat = matrix(NA, ncol = ncol(mat), nrow = nrow(mat))
for(lk in 1:nrow(mat)){
if(vec[lk] > 0){
selcol = sample(x = 1:ncol(mat), size = vec[lk], replace = TRUE, prob = mat[lk,])
tmp2 = table(selcol)
tmpmat[lk ,as.numeric(names(tmp2))] = as.vector(tmp2)
}
}
tmpmat[is.na(tmpmat)] = 0
return(tmpmat)
}
sampleVec = function(x, ...) x[sample(length(x), ...)]
ageSample = function(mat, vec){
newlist = apply(X = mat, MARGIN = 1, FUN = rep, x = allAges)
newvec2 = mapply(sampleVec, newlist, size = vec, replace = FALSE) # sample of ages should be with a probability of age abundance?
newvec3 = unlist(newvec2)
return(newvec3)
}
NindAgeSample = function(vec, maxSam = 50){
nsam = sum(vec)
nrandom = nsam - maxSam
while(nrandom > 0){
vec[which.max(vec)] = vec[which.max(vec)] - 1
nsam = sum(vec)
nrandom = nsam - maxSam
}
return(vec)
}
agesCRL = function(ages, freq, thr = 1){
firstZero = which(freq <= thr)[1]
cutAge2 = ages[firstZero - 1]
return(cutAge2)
}
map.heatmap = function (lat, lon, data,
color_low="white",color_high="darkred",
color_na=gray(0.9),zeroiswhite=FALSE,
xlim=NULL, ylim=NULL, zlim=NULL,
mainTitle="", legendTitle="") {
# Store the base data of the underlying map
baseData <- map_data("state")
# Combine the data into a dataframe
dfMap <- as.data.frame(cbind(lon, lat, data))
colnames(dfMap) <- c("lon", "lat", "Value")
# Set limits for x, y, z if not specified as parameters
if (is.null(xlim)) { xlim <- range( lon,na.rm=TRUE) }
if (is.null(ylim)) { ylim <- range( lat,na.rm=TRUE) }
if (is.null(zlim)) { zlim <- range(data,na.rm=TRUE) }
# Create the plot
p <- ggplot(dfMap, aes(x=lon, y=lat, fill=Value)) + theme_bw()
p <- p + geom_tile()
p <- p + geom_polygon(data=baseData, aes(x=long, y=lat, group=group),
colour="black", fill=8)
p <- p + labs(title=paste(mainTitle,"\n",sep=""), x="", y="")
#p <- p + theme(plot.title = element_text(size = rel(1.5)))
p <- p + coord_fixed(ratio=1, xlim=xlim, ylim=ylim)
if(zeroiswhite){
p <- p + scale_fill_gradient2(low=color_low,
high=color_high,
na.value=color_na,
limits=zlim,
breaks = c(min(dfMap$Value), 0, max(dfMap$Value)),
name=legendTitle,
guide = guide_colourbar(direction = "horizontal"))
}
if(!zeroiswhite){
p <- p + scale_fill_gradient(low=color_low,
high=color_high,
na.value=color_na,
limits=zlim,
name=legendTitle)
}
return(p)}
map.heatmap2 = function (lat, lon, data, data2,
color_low="white",color_high="darkred",
color_na=gray(0.9),zeroiswhite=FALSE,
xlim=NULL, ylim=NULL, zlim=NULL,
mainTitle="", legendTitle="", pSize = 2) {
# Store the base data of the underlying map
#baseData <- map_data("state")
# Combine the data into a dataframe
dfMap <- as.data.frame(cbind(lon, lat, data))
colnames(dfMap) <- c("lon", "lat", "Value")
# Set limits for x, y, z if not specified as parameters
if (is.null(xlim)) { xlim <- range( lon,na.rm=TRUE) }
if (is.null(ylim)) { ylim <- range( lat,na.rm=TRUE) }
if (is.null(zlim)) { zlim <- range(data,na.rm=TRUE) }
# Create the plot
p <- ggplot(data = dfMap, aes(x =lon, y=lat)) + theme_bw()
p <- p + geom_tile(aes(fill=Value))
p <- p + geom_point(data = data2, mapping = aes(x = lon, y = lat, z = NULL), size = pSize)
#p <- p + geom_polygon(data=baseData, aes(x=long, y=lat, group=group),
# colour="black", fill=8)
p <- p + labs(title=paste(mainTitle,"\n",sep=""), x="", y="")
#p <- p + theme(plot.title = element_text(size = rel(1.5)))
p <- p + coord_fixed(ratio=1, xlim=xlim, ylim=ylim)
if(zeroiswhite){
p <- p + scale_fill_gradient2(low=color_low,
high=color_high,
na.value=color_na,
limits=zlim,
breaks = c(min(dfMap$Value), 0, max(dfMap$Value)),
name=legendTitle,
guide = guide_colourbar(direction = "horizontal"))
}
if(!zeroiswhite){
p <- p + scale_fill_gradient(low=color_low,
high=color_high,
na.value=color_na,
limits=zlim,
name=legendTitle)
}
return(p)}
# addalpha()
addalpha <- function(colors, alpha=1.0) {
r <- col2rgb(colors, alpha=T)
# Apply alpha
r[4,] <- alpha*255
r <- r/255.0
return(rgb(r[1,], r[2,], r[3,], r[4,]))
}
# colorRampPaletteAlpha()
colorRampPaletteAlpha <- function(colors, n=32, interpolate='linear') {
# Create the color ramp normally
cr <- colorRampPalette(colors, interpolate=interpolate)(n)
# Find the alpha channel
a <- col2rgb(colors, alpha=T)[4,]
# Interpolate
if (interpolate=='linear') {
l <- approx(a, n=n)
} else {
l <- spline(a, n=n)
}
l$y[l$y > 255] <- 255 # Clamp if spline is > 255
cr <- addalpha(cr, l$y/255.0)
return(cr)
}
normDF = function(dat, fac1 = 'AGE', fac2 = 'YEAR', normcol = 'LENGTH', maxAge = 8){
outdat = NULL
dat = dat[dat[,fac1] <= maxAge, ]
dat[, fac1] = as.character(dat[, fac1])
fctrs2 = sort(unique(dat[, fac2]))
for(k in seq_along(fctrs2)){
tmp2 = dat[dat[, fac2] == fctrs2[k], ]
fctrs1 = sort(unique(tmp2[, fac1]))
for(l in seq_along(fctrs1)){
tmp = tmp2[tmp2[, fac1] == fctrs1[l], ]
tmp[, normcol] = normalize(x = tmp[, normcol], method = 'range', range = c(-1,1))
outdat = rbind(outdat, tmp)
}
}
return(outdat)
}