-
Notifications
You must be signed in to change notification settings - Fork 0
/
fixingLakeID2018_03_09.R
293 lines (222 loc) · 9.45 KB
/
fixingLakeID2018_03_09.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
library(sqldf)
library(data.table)
library(ggplot2)
library(NADA)
# Data at https://doi.org/10.5066/P9TDCH3F
## Load loon blood
loonBlood <- fread("./UseYear/LoonHGblood.csv")
loonBlood[ , LakeID := gsub( "^0", "", LakeID)]
## Load data and reformat it
fishDataRaw <- fread("./UseYear/fishUse.csv")
fishData <- fread("./UseYear/fishUse.csv")
####################### Edit lake codes
# Pool tamarac data
loonBlood[ grep("tamarac", Lake, ignore.case=T), LakeID := "3024102"]
fishData[ grep("3024100|3024102", DOWID, ignore.case=T), DOWID := "3024102"]
# Pool all George data
fishData[ grep("GEORGE", WATERWAY, ignore.case = T), DOWID := "2009100"]
loonBlood[ grep("george", Lake, ignore.case = T), LakeID := "2009100" ]
# Pool all clearwater data (CUSTOM ID)
fishData[grep("clearwater", WATERWAY, ignore.case=T), DOWID := "18003800"]
loonBlood[grep("clearwater", Lake, ignore.case=T), LakeID := "18003800"]
## pool fox lake
loonBlood[ grep("East Fox", Lake), LakeID := "18029700"]
loonBlood[ grep("West Fox", Lake), LakeID := "18029700"]
# Pool all east rabbit data
loonBlood[grep("east rabbit", Lake, ignore.case=T), LakeID := "18009301"]
fishData[grep("(east.*rabbit)|(rabbit.*east)", WATERWAY, ignore.case=T), DOWID := "18009301"]
# pool all west rabbit data
loonBlood[grep("west rabbit", Lake, ignore.case=T), LakeID := "18009302"]
fishData[grep("18009300|18009302", DOWID, ignore.case=T), DOWID := "18009302"]
#pool all burntside data
loonBlood[grep("burntside", Lake, ignore.case=T), LakeID := "69011800" ]
#pool all little burch
loonBlood[grep("little birch", Lake, ignore.case=T), LakeID := "77008900"]
#pool all big burch
loonBlood[grep("77008401", LakeID, ignore.case=T), LakeID := "77008400"]
#pool wild rice data
loonBlood[grep("wild|rice", Lake, ignore.case=T), LakeID := "69037100"]
#pool south turtle data
loonBlood[grep("(south.*turtle)|(turtle.*south)", Lake, ignore.case = T), LakeID := "56037700"]
# mantrap
loonBlood[grep("Mantrap", Lake, ignore.case = T), LakeID := "29015100"]
# east vermilion
loonBlood[grep("East Vermilion", Lake, ignore.case = T), LakeID := "69037800"]
# monongalia
loonBlood[grep("Monongalia", Lake, ignore.case = T), LakeID := "34015800"]
## Reformat data structure
fishData[ , DOWID := as.character(DOWID)]
fishData[ , HGppmLog := log(HGppm + 1)]
fishData[ , Lgthcm := Lgthin * 2.54]
fishData[ , LgthinLog := log(Lgthin + 1)]
fishData[ , LgthcmLog := log(Lgthcm + 1)]
fishData[ , SppCut := paste(Spec, Anat, sep = "_")]
fishData[ , sampleEvent := paste(DOWID, YEAR, sep = "_")]
## Enter in non-detect codes
ndCodes <- c("K", "KM", "ND")
fishData[ , ND := "no"]
fishData[ Hgcode %in% ndCodes, ND := "yes"]
## create non-detect lables
fishData[ , NDyes := 0]
fishData[ ND == "yes", NDyes := 1]
fishData[ , NDno := 0]
fishData[ ND == "no", NDno := 1]
## merge data
loonBloodLakeIDs <- loonBlood[ , unique(fishLakeID)]
missingLoonLakes <- loonBloodLakeIDs[!loonBloodLakeIDs %in% fishData[ , unique(DOWID)]]
missingLoonLakes
loonBlood[ !LakeID %in% missingLoonLakes, unique(Lake)]
## Remove data that has too small of number
## Total of 7 fish
dropSpp <- c("SLT_WHORG", "SF_WHORG")
dropSpp
dim(fishData)
fishData <- copy(fishData[ ! SppCut %in% dropSpp, ])
dim(fishData)
## Remove lake evens that have fewer than 5 fish, results in removing 1362 fish
minEvents <- 5
eventDT <- fishData[ , list(Det = sum(NDno), ND = sum(NDyes), total = length(HGppm)),
by = sampleEvent]
eventDT[ , prop := round(ND/total, 2)]
eventDT[ , lakeID := gsub("(\\d+)_(\\d{4})", "\\1", sampleEvent)]
eventDT[ order(total, decreasing = FALSE), ][ total < minEvents,]
dim(eventDT)
sampleEventRemove <- eventDT[ total < minEvents, sampleEvent]
## sampleEventRemove
length(sampleEventRemove)
## sampleEventRemove
## May need line in code to save loon lakes
eventDT[ total < minEvents & lakeID %in%loonBloodLakeIDs, ]
fishData
fishData <- copy(fishData[ ! sampleEvent %in% sampleEventRemove, ])
print(eventDT[ prop > 0, ][order(prop, decreasing = TRUE)], 110)
## Remove sppCuts now that have >= 5 observaitons, also, make sure
## any observaiton with more than 50% ND should be removed
sppCutDT <- fishData[ , list(Det = sum(NDno), ND = sum(NDyes), total = length(HGppm)),
by = SppCut]
sppCutDT[ , prop := round(ND/total, 2)]
sppCutDT[ order(prop, decreasing = TRUE)]
sppCutDT[ order(total, decreasing = TRUE)]
sppCutRemove <- sppCutDT[ total <= 5, SppCut]
sppCutRemove
fishData <- copy(fishData[ ! SppCut %in% sppCutRemove, ])
## Need repeate removing lakes with fewer than 5 fish
eventDT <- fishData[ , list(Det = sum(NDno), ND = sum(NDyes), total = length(HGppm)),
by = sampleEvent]
eventDT[ , prop := round(ND/total, 2)]
eventDT[ , lakeID := gsub("(\\d+)_(\\d{4})", "\\1", sampleEvent)]
eventDT[ order(total, decreasing = FALSE), ][ total <5,]
sampleEventRemove <- eventDT[ total < minEvents, sampleEvent]
sampleEventRemove
eventDT[ total < minEvents & lakeID %in%loonBloodLakeIDs, ]
fishData
fishData <- copy(fishData[ ! sampleEvent %in% sampleEventRemove, ])
fishData
## How many samples per
fishData[ , length(HGppm), by = SppCut][ order(V1, decreasing = FALSE),]
fishData[ , length(HGppm), by = sampleEvent][ order(V1, decreasing = FALSE),][ V1 <=5,]
fishData[ , Censor := FALSE]
fishData[ NDyes == 1, Censor := TRUE]
########################################
########################################
####### RUN THE MODEL OR LOAD IT #######
########################################
########################################
#### RUN ####
# Then we run our model (must create named lists to avoid programming issues)
HGppbLog = log(fishData$HGppm*1000 + 1)
Censor = fishData$Censor
LengthInchesLog = fishData$LgthinLog
SppCut = fishData$SppCut
SampleEvent = fishData$sampleEvent
st <- system.time(
modelOut <-
cenreg( Cen(HGppbLog, Censor) ~
LengthInchesLog : SppCut +
SampleEvent - 1, dist = 'gaussian')
)
# save the model
save(modelOut, file = "fishHGmodelHgPpb20180302.rda")
#### OR ####
# load the model
load("fishHGmodelHgPpb20180302.rda")
############
# Get coefficients from the model
coefEst = coef(modelOut)
#load library for parallel computing
library(snow)
#create clusters
cl <- makeSOCKcluster(5)
#Add data to clusters
clusterExport(cl, "fishData")
clusterExport(cl, "coefEst")
#calculate predicted Hg for a 12 cm perch each lake
cluster_results = parSapply(cl, 1:nrow(fishData), function(i) {
lakeyear <- paste(fishData$DOWID[i], fishData$YEAR[i], sep="_")
lakeCoef <- coefEst[grep(paste0("SampleEvent",lakeyear), names(coefEst), perl=T)]
if(length(lakeCoef) != 0) {
return(as.numeric(lakeCoef) + as.numeric(coefEst[grep("YP_WHORG", names(coefEst))]*log(4.7 + 1)))
} else {
return(NA)
}
})
# Stop the clusters
stopCluster(cl)
#bind predictions with data
all_results = cbind(fishData, perchHG = cluster_results)
#write results to file
write.csv(all_results, "perchHGPredictData2018_03_09.csv", row.names = F)
# Find nearest fish sample year (UseYear) to loon sample year for each lake
loonBlood[, UseYear := 0]
for(id in unique(loonBlood$LakeID)) {
subLoon = subset(loonBlood, LakeID == id)
subFishData = subset(fishData, DOWID == id)
if(nrow(subFishData) > 0) {
for(year in subLoon$Year) {
subLoonYear = subset(subLoon, Year == year)
closestYear = subFishData$YEAR[which(abs(subFishData$YEAR-year) == min(abs(subFishData$YEAR-year)))[1]]
#update loonblood UseYear with closest year
loonBlood[LakeID == id & Year == year, "UseYear"] = closestYear
}
}
}
# Remove lakes with no match
loonBlood = loonBlood[UseYear != 0, ]
loonBlood[, perchHG := NULL]
for(i in 1:nrow(loonBlood)) {
lakeyear <- paste(loonBlood$LakeID[i], loonBlood$UseYear[i], sep="_")
lakeCoef <- coefEst[grep(lakeyear, names(coefEst))]
if(length(lakeCoef) != 0) {
loonBlood[i, "perchHG"] <- lakeCoef + coefEst[grep("YP_WHORG", names(coefEst))]*log(4.7 + 1)
} else {
loonBlood[i, "perchHG"] <- NA
}
}
# Change year for fish
fishData[, FISHYEAR := YEAR]
# Remove columns
fishData[, YEAR := NULL]
#join loonblood data (with predicted perch Hg) to fishData
results=sqldf("SELECT * FROM fishData AS a JOIN loonBlood AS b ON a.DOWID = b.LakeID AND a.FISHYEAR = b.UseYear")
# Fix loon lake id's
results[results$Lake == "East Fox", "LakeID"] = "18029800"
results[results$Lake == "South Tamarack", "LakeID"] = "3024101"
results[ grep("North Tamarac", results$Lake), "LakeID"] = "3024102"
results[ grep("george", results$Lake, ignore.case = T), "LakeID"] = "2009100"
results[grep("clearwater", results$Lake, ignore.case=T), "LakeID"] = "18003800"
results[ grep("East Fox", results$Lake), "LakeID"] = "18029800"
results[grep("77008400", results$LakeID, ignore.case=T), "LakeID"] = "77008401" #big birch
results[grep("29015100", results$LakeID, ignore.case = T), "LakeID"] = "29015104" #mantrap
results[grep("69037800", results$LakeID, ignore.case = T), "LakeID"] = "69037801" #east vermilion
results[grep("Monongalia - main", results$Lake, ignore.case = T), "LakeID"] = "34015801"
results[grep("Monongalia.*crow", results$Lake, ignore.case = T), "LakeID"] = "34015802"
#remove duplicate column
results <- results[, -c(1)]
# Write perch & loon joined data/results for loon analysis
write.csv(results, "perchLoonHGData2018_03_09.csv", row.names = FALSE)
#remove NA
results <- results[!is.na(results$perchHG), ]
# # Get mean perchHG by id-year combinations
perchHGAvg = data.frame(sqldf("SELECT LakeID, Year, AVG(perchHG) perchHG FROM results GROUP BY LakeID, Year"))
# Write loonData
write.csv(perchHGAvg, "perchHGAvg2018_03_09.csv", row.names = F)