This repository was archived by the owner on Sep 14, 2022. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy path.Rhistory
More file actions
512 lines (512 loc) · 28.3 KB
/
Copy path.Rhistory
File metadata and controls
512 lines (512 loc) · 28.3 KB
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
ggplot() +
geom_rect(data=test, aes(xmin=easting, xmax=easting + 30, ymin=northing, ymax=northing + 30, fill = as.factor(unlist(pred))), color="black") +
labs(title=paste("Population Abundance Prediction"), x="X (30m increment)", y="Y (30m increment)", fill="Cover")
ggplot() +
geom_rect(data=species.map, aes(xmin=easting, xmax=easting + 20, ymin=northing, ymax=northing + 20, fill = as.factor(unlist(species.map[1]))), color=NA) +
labs(title=paste("Population Abundance"), x="X (30m increment)", y="Y (30m increment)", fill = "Cover") +
geom_rect(data=training, aes(xmin=easting, xmax=easting + 30, ymin=northing, ymax=northing + 30), color="black", fill = NA)
ggplot() +
geom_rect(data=test, aes(xmin=easting, xmax=easting + 30, ymin=northing, ymax=northing + 30, fill = as.factor(unlist(pred))), color="black") +
labs(title=paste("Population Abundance Prediction"), x="X (30m increment)", y="Y (30m increment)", fill="Cover")
min(test$easting)
max(test$easting)
min(test$northing)
max(test$northing)
4303800
ggplot() +
geom_rect(data=test, aes(xmin=easting, xmax=easting + 30, ymin=northing, ymax=northing + 30, fill = as.factor(unlist(pred))), color="black") +
labs(title=paste("Population Abundance Prediction"), x="X (30m increment)", y="Y (30m increment)", fill="Cover") +
scale_x_continuous(limits = c(365430, 366240)) +
scale_y_continuous(limits = c(4303800, 4304430))
ggplot() +
geom_rect(data=test, aes(xmin=easting, xmax=easting + 30, ymin=northing, ymax=northing + 30, fill = as.factor(unlist(pred))), color="black") +
labs(title=paste("Population Abundance Prediction"), x="X (30m increment)", y="Y (30m increment)", fill="Cover") +
scale_x_continuous(limits = c(365430, 366280)) +
scale_y_continuous(limits = c(4303800, 4304470))
ggplot() +
geom_rect(data=species.map, aes(xmin=easting, xmax=easting + 20, ymin=northing, ymax=northing + 20, fill = as.factor(unlist(species.map[1]))), color=NA) +
labs(title=paste("Population Abundance"), x="X (30m increment)", y="Y (30m increment)", fill = "Cover") +
geom_rect(data=training, aes(xmin=easting, xmax=easting + 30, ymin=northing, ymax=northing + 30), color="black", fill = NA) + +
scale_x_continuous(limits = c(365430, 366280)) +
scale_y_continuous(limits = c(4303800, 4304470))
ggplot() +
geom_rect(data=species.map, aes(xmin=easting, xmax=easting + 20, ymin=northing, ymax=northing + 20, fill = as.factor(unlist(species.map[1]))), color=NA) +
labs(title=paste("Population Abundance"), x="X (30m increment)", y="Y (30m increment)", fill = "Cover") +
geom_rect(data=training, aes(xmin=easting, xmax=easting + 30, ymin=northing, ymax=northing + 30), color="black", fill = NA) +
scale_x_continuous(limits = c(365430, 366280)) +
scale_y_continuous(limits = c(4303800, 4304470))
# edit scale for validation
species.map[which(is.na(species.map[,1])),1] <- "none"
species.map[which(species.map[,1] == 0),1] <- "few"
species.map[which(species.map[,1] == 1),1] <- "few more"
species.map[which(species.map[,1] == 2),1] <- "little"
species.map[which(species.map[,1] == 3),1] <- "some"
species.map[which(species.map[,1] == 4),1] <- "most"
species.map[which(species.map[,1] == 5),1] <- "all"
species.map[,z] <- as.factor(species.map[,1])
ggplot() +
geom_rect(data=species.map, aes(xmin=easting, xmax=easting + 20, ymin=northing, ymax=northing + 20, fill = as.factor(unlist(species.map[1]))), color=NA) +
labs(title=paste("Population Abundance"), x="X (30m increment)", y="Y (30m increment)", fill = "Cover") +
geom_rect(data=training, aes(xmin=easting, xmax=easting + 30, ymin=northing, ymax=northing + 30), color="black", fill = NA) +
scale_x_continuous(limits = c(365430, 366280)) +
scale_y_continuous(limits = c(4303800, 4304470))
ggplot() +
geom_rect(data=species.map, aes(xmin=easting, xmax=easting + 20, ymin=northing, ymax=northing + 20, fill = as.factor(unlist(species.map[1]))), color=NA) +
labs(title=paste("Population Abundance"), x="X (30m increment)", y="Y (30m increment)", fill = "Cover") +
geom_rect(data=training, aes(xmin=easting, xmax=easting + 30, ymin=northing, ymax=northing + 30), color="black", fill = NA) +
scale_x_discrete(limits=c("none", "few", "few more", "little", "some", "most", "all")) +
scale_x_continuous(limits = c(365430, 366280)) +
scale_y_continuous(limits = c(4303800, 4304470))
species.map$scam <- factor(species.map$scam, levels = c("none", "few", "few more", "little", "some", "most", "all"))
ggplot() +
geom_rect(data=species.map, aes(xmin=easting, xmax=easting + 20, ymin=northing, ymax=northing + 20, fill = as.factor(unlist(species.map[1]))), color=NA) +
labs(title=paste("Population Abundance"), x="X (30m increment)", y="Y (30m increment)", fill = "Cover") +
geom_rect(data=training, aes(xmin=easting, xmax=easting + 30, ymin=northing, ymax=northing + 30), color="black", fill = NA) +
scale_x_continuous(limits = c(365430, 366280)) +
scale_y_continuous(limits = c(4303800, 4304470))
test$pred <- factor(test$pred, levels = c("none", "few", "few more", "little", "some", "most", "all"))
test$pred <- factor(test$pred, levels = c("none", "few", "few more", "little", "some", "most"))
pred <- factor(pred, levels = c("none", "few", "few more", "little", "some", "most"))
pred <- factor(pred, levels = c("none", "few", "few more", "little", "some", "most", "all"))
ggplot() +
geom_rect(data=test, aes(xmin=easting, xmax=easting + 30, ymin=northing, ymax=northing + 30, fill = as.factor(unlist(pred))), color="black") +
labs(title=paste("Population Abundance Prediction"), x="X (30m increment)", y="Y (30m increment)", fill="Cover") +
scale_x_continuous(limits = c(365430, 366280)) +
scale_y_continuous(limits = c(4303800, 4304470))
ggplot() +
geom_rect(data=species.map, aes(xmin=easting, xmax=easting + 20, ymin=northing, ymax=northing + 20, fill = as.factor(unlist(species.map[1]))), color=NA) +
labs(title=paste("Population Abundance of SERC under Landsat Plots"), x="Easting", y="Northing", fill = "Cover") +
geom_rect(data=training, aes(xmin=easting, xmax=easting + 30, ymin=northing, ymax=northing + 30), color="black", fill = NA) +
scale_x_continuous(limits = c(365430, 366280)) +
scale_y_continuous(limits = c(4303800, 4304470))
ggplot() +
geom_rect(data=species.map, aes(xmin=easting, xmax=easting + 20, ymin=northing, ymax=northing + 20, fill = as.factor(unlist(species.map[1]))), color=NA) +
labs(title=paste("Population Abundance of SERC"), x="Easting", y="Northing", fill = "Cover") +
geom_rect(data=training, aes(xmin=easting, xmax=easting + 30, ymin=northing, ymax=northing + 30), color="black", fill = NA) +
scale_x_continuous(limits = c(365430, 366280)) +
scale_y_continuous(limits = c(4303800, 4304470))
ggplot() +
geom_rect(data=species.map, aes(xmin=easting, xmax=easting + 20, ymin=northing, ymax=northing + 20, fill = as.factor(unlist(species.map[1]))), color=NA) +
labs(title=paste("Population Abundance of SERC Plots"), x="Easting", y="Northing", fill = "Cover") +
geom_rect(data=training, aes(xmin=easting, xmax=easting + 30, ymin=northing, ymax=northing + 30), color="black", fill = NA) +
scale_x_continuous(limits = c(365430, 366280)) +
scale_y_continuous(limits = c(4303800, 4304470))
ggplot() +
geom_rect(data=test, aes(xmin=easting, xmax=easting + 30, ymin=northing, ymax=northing + 30, fill = as.factor(unlist(pred))), color="black") +
labs(title=paste("Population Abundance Prediction"), x="Easting", y="Northing", fill="Cover") +
scale_x_continuous(limits = c(365430, 366280)) +
scale_y_continuous(limits = c(4303800, 4304470))
pred.graph <- ggplot() +
geom_rect(data=test, aes(xmin=easting, xmax=easting + 30, ymin=northing, ymax=northing + 30, fill = as.factor(unlist(pred))), color="black") +
labs(title=paste("Population Abundance Prediction"), x="Easting", y="Northing", fill="Cover") +
scale_x_continuous(limits = c(365430, 366280)) +
scale_y_continuous(limits = c(4303800, 4304470))
setwd("~/Documents/Junior_Year/DISC_REU/DISC_chesapeake/")
ggsave("plots/prediction_visual.png")
ggplot() +
geom_rect(data=species.map, aes(xmin=easting, xmax=easting + 20, ymin=northing, ymax=northing + 20, fill = as.factor(unlist(species.map[1]))), color=NA) +
labs(title=paste("Population Abundance of SERC Plots"), x="Easting", y="Northing", fill = "Cover") +
geom_rect(data=training, aes(xmin=easting, xmax=easting + 30, ymin=northing, ymax=northing + 30), color="black", fill = NA) +
scale_x_continuous(limits = c(365430, 366280)) +
scale_y_continuous(limits = c(4303800, 4304470))
ggsave("plots/prediction_validation.png")
# model with plot proportion as a value
model2 = randomForest(scam ~ .,data = train[,-c(1:3)], keep.forest=TRUE)
model2
plot(model2)
varImpPlot(model2)
pred2 <- predict(model2, newdata = test)
table(pred2, test$scam)
pred2 <- factor(pred, levels = c("none", "few", "few more", "little", "some", "most", "all"))
pred2.graph <- ggplot() +
geom_rect(data=test, aes(xmin=easting, xmax=easting + 30, ymin=northing, ymax=northing + 30, fill = as.factor(unlist(pred2))), color="black") +
labs(title=paste("Population Abundance Prediction w/ Overlap"), x="Easting", y="Northing", fill="Cover") +
scale_x_continuous(limits = c(365430, 366280)) +
scale_y_continuous(limits = c(4303800, 4304470))
pred2.graph
pred.graph
ggplot() +
geom_rect(data=species.map, aes(xmin=easting, xmax=easting + 20, ymin=northing, ymax=northing + 20, fill = as.factor(unlist(species.map[1]))), color=NA) +
labs(title=paste("Population Abundance of SERC Plots"), x="Easting", y="Northing", fill = "Cover") +
geom_rect(data=training, aes(xmin=easting, xmax=easting + 30, ymin=northing, ymax=northing + 30), color="black", fill = NA) +
scale_x_continuous(limits = c(365430, 366280)) +
scale_y_continuous(limits = c(4303800, 4304470))
model2 = randomForest(scam ~ .,data = train[,-c(1:4, 9, 10, 18)], keep.forest=TRUE)
model2
plot(model2)
varImpPlot(model2)
pred2 <- predict(model2, newdata = test)
table(pred2, test$scam)
pred2 <- factor(pred, levels = c("none", "few", "few more", "little", "some", "most", "all"))
pred2.graph <- ggplot() +
geom_rect(data=test, aes(xmin=easting, xmax=easting + 30, ymin=northing, ymax=northing + 30, fill = as.factor(unlist(pred2))), color="black") +
labs(title=paste("Population Abundance Prediction w/ Overlap"), x="Easting", y="Northing", fill="Cover") +
scale_x_continuous(limits = c(365430, 366280)) +
scale_y_continuous(limits = c(4303800, 4304470))
pred2.graph
model = randomForest(scam ~ .,data = train[,-c(1:4)], keep.forest=TRUE)
model
plot(model)
varImpPlot(model)
pred <- predict(model, newdata = test)
table(pred, test$scam)
pred <- factor(pred, levels = c("none", "few", "few more", "little", "some", "most", "all"))
pred.graph <- ggplot() +
geom_rect(data=test, aes(xmin=easting, xmax=easting + 30, ymin=northing, ymax=northing + 30, fill = as.factor(unlist(pred))), color="black") +
labs(title=paste("Population Abundance Prediction"), x="Easting", y="Northing", fill="Cover") +
scale_x_continuous(limits = c(365430, 366280)) +
scale_y_continuous(limits = c(4303800, 4304470))
pred.graph
pred2.graph
ggplot() +
geom_rect(data=species.map, aes(xmin=easting, xmax=easting + 20, ymin=northing, ymax=northing + 20, fill = as.factor(unlist(species.map[1]))), color=NA) +
labs(title=paste("Population Abundance of SERC Plots"), x="Easting", y="Northing", fill = "Cover") +
geom_rect(data=training, aes(xmin=easting, xmax=easting + 30, ymin=northing, ymax=northing + 30), color="black", fill = NA) +
scale_x_continuous(limits = c(365430, 366280)) +
scale_y_continuous(limits = c(4303800, 4304470))
pred2.graph <- ggplot() +
geom_rect(data=test, aes(xmin=easting, xmax=easting + 30, ymin=northing, ymax=northing + 30, fill = as.factor(unlist(pred2))), color="black") +
labs(title=paste("Population Abundance Prediction w/ less vars"), x="Easting", y="Northing", fill="Cover") +
scale_x_continuous(limits = c(365430, 366280)) +
scale_y_continuous(limits = c(4303800, 4304470))
pred2.graph
pred2.graph <- ggplot() +
geom_rect(data=test, aes(xmin=easting, xmax=easting + 30, ymin=northing, ymax=northing + 30, fill = as.factor(unlist(pred2))), color="black") +
labs(title=paste(curr.species, "Population Abundance Predictions"), x="Easting", y="Northing", fill="Cover") +
scale_x_continuous(limits = c(365430, 366280)) +
scale_y_continuous(limits = c(4303800, 4304470))
pred2.graph
pred.graph <- ggplot() +
geom_rect(data=test, aes(xmin=easting, xmax=easting + 30, ymin=northing, ymax=northing + 30, fill = as.factor(unlist(pred))), color="black") +
labs(title=paste(curr.species, "Population Abundance Prediction"), x="Easting", y="Northing", fill="Cover") +
scale_x_continuous(limits = c(365430, 366280)) +
scale_y_continuous(limits = c(4303800, 4304470))
pred.graph
pred.graph <- ggplot() +
geom_rect(data=test, aes(xmin=easting, xmax=easting + 30, ymin=northing, ymax=northing + 30, fill = as.factor(unlist(pred))), color="black") +
labs(title=paste(curr.species, "Population Abundance Prediction w/ all vars"), x="Easting", y="Northing", fill="Cover") +
scale_x_continuous(limits = c(365430, 366280)) +
scale_y_continuous(limits = c(4303800, 4304470))
pred.graph
pred2.graph <- ggplot() +
geom_rect(data=test, aes(xmin=easting, xmax=easting + 30, ymin=northing, ymax=northing + 30, fill = as.factor(unlist(pred2))), color="black") +
labs(title=paste(curr.species, "Population Abundance Predictions"), x="Easting", y="Northing", fill="Cover") +
scale_x_continuous(limits = c(365430, 366280)) +
scale_y_continuous(limits = c(4303800, 4304470))
pred2.graph
ggsave("plots/prediction_visual2.png")
pred.graph <- ggplot() +
geom_rect(data=test, aes(xmin=easting, xmax=easting + 30, ymin=northing, ymax=northing + 30, fill = as.factor(unlist(pred))), color="black") +
labs(title=paste(curr.species, "Population Abundance Prediction w/ all vars"), x="Easting", y="Northing", fill="Cover") +
scale_x_continuous(limits = c(365430, 366280)) +
scale_y_continuous(limits = c(4303800, 4304470))
pred.graph
ggsave("plots/prediction_visual1.png")
par(mfrow=c(1,2))
pred.graph
pred2.graph
grid.arrange(pred.graph, pred2.graph, ncol=2)
library(gridExtra)
grid.arrange(pred.graph, pred2.graph, ncol=2)
serc.plots <- ggplot() +
geom_rect(data=species.map, aes(xmin=easting, xmax=easting + 20, ymin=northing, ymax=northing + 20, fill = as.factor(unlist(species.map[1]))), color=NA) +
labs(title=paste("Population Abundance of SERC Plots"), x="Easting", y="Northing", fill = "Cover") +
geom_rect(data=training, aes(xmin=easting, xmax=easting + 30, ymin=northing, ymax=northing + 30), color="black", fill = NA) +
scale_x_continuous(limits = c(365430, 366280)) +
scale_y_continuous(limits = c(4303800, 4304470))
serc.plots
grid.arrange(serc.plots, pred2.graph, ncol=2)
ggsave("plots/plot_comparison.png")
ggsave("plots/plot_comparison.png", arrangeGrob(serc.plots, pred2.graph, ncol=2))
ggsave("plots/plot_comparison.png", arrangeGrob(serc.plots, pred2.graph, ncol=2), width = 10)
ggsave("plots/plot_comparison.png", arrangeGrob(serc.plots, pred2.graph, ncol=2), width = 11)
pred2 <- factor(pred, levels = c("none", "few", "few more", "little", "some", "most", "all"))
cols = c("none"="CCCCCC", "few" = "FFFFCC", "few more" = "CCFF99", "little" = "99FF00", "some" = "33CC33", "most" = "006600", "all" = "003300")
pred2.graph <- ggplot() +
geom_rect(data=test, aes(xmin=easting, xmax=easting + 30, ymin=northing, ymax=northing + 30, fill = as.factor(unlist(pred2))), color="black") +
labs(title=paste(curr.species, "Population Abundance Predictions"), x="Easting", y="Northing", fill="Cover") +
scale_x_continuous(limits = c(365430, 366280)) +
scale_y_continuous(limits = c(4303800, 4304470)) +
scale_colour_manual(values = cols)
pred2.graph
pred2.graph <- ggplot() +
geom_rect(data=test, aes(xmin=easting, xmax=easting + 30, ymin=northing, ymax=northing + 30, fill = as.factor(unlist(pred2))), color="black") +
labs(title=paste(curr.species, "Population Abundance Predictions"), x="Easting", y="Northing", fill="Cover") +
scale_x_continuous(limits = c(365430, 366280)) +
scale_y_continuous(limits = c(4303800, 4304470)) +
scale_colour_discrete(guide=guide_legend(override.aes=aes(values = cols)))
pred2.graph <- ggplot() +
geom_rect(data=test, aes(xmin=easting, xmax=easting + 30, ymin=northing, ymax=northing + 30, fill = as.factor(unlist(pred2))), color="black") +
scale_colour_discrete(guide=guide_legend(override.aes=aes(values = cols))) +
labs(title=paste(curr.species, "Population Abundance Predictions"), x="Easting", y="Northing", fill="Cover") +
scale_x_continuous(limits = c(365430, 366280)) +
scale_y_continuous(limits = c(4303800, 4304470))
pred2.graph <- ggplot(data=test) +
geom_rect(aes(xmin=easting, xmax=easting + 30, ymin=northing, ymax=northing + 30, fill = as.factor(unlist(pred2))), color="black") +
labs(title=paste(curr.species, "Population Abundance Predictions"), x="Easting", y="Northing", fill="Cover") +
scale_x_continuous(limits = c(365430, 366280)) +
scale_y_continuous(limits = c(4303800, 4304470))
pred2.graph <- ggplot(data=test) +
geom_rect(aes(xmin=easting, xmax=easting + 30, ymin=northing, ymax=northing + 30, fill = as.factor(unlist(pred2))), color="black") +
labs(title=paste(curr.species, "Population Abundance Predictions"), x="Easting", y="Northing", fill="Cover") +
scale_x_continuous(limits = c(365430, 366280)) +
scale_y_continuous(limits = c(4303800, 4304470)) +
scale_fill_manual(values = cols)
pred2.graph
pred2 <- factor(pred, levels = c("none", "few", "few more", "little", "some", "most", "all"))
cols = c("none"="red", "few" = "FFFFCC", "few more" = "CCFF99", "little" = "99FF00", "some" = "33CC33", "most" = "006600", "all" = "003300")
pred2.graph <- ggplot(data=test) +
geom_rect(aes(xmin=easting, xmax=easting + 30, ymin=northing, ymax=northing + 30, fill = as.factor(unlist(pred2))), color="black") +
labs(title=paste(curr.species, "Population Abundance Predictions"), x="Easting", y="Northing", fill="Cover") +
scale_x_continuous(limits = c(365430, 366280)) +
scale_y_continuous(limits = c(4303800, 4304470)) +
scale_fill_manual(values = cols)
pred2.graph
ggsave("plots/prediction_visual2.png")
pred2 <- factor(pred, levels = c("none", "few", "few more", "little", "some", "most", "all"))
cols = c("none"="#ceb467", "few" = "#ace5b2", "few more" = "#7aef87", "little" = "#57d165", "some" = "#25c637", "most" = "#197f24", "all" = "#115118")
pred2.graph <- ggplot(data=test) +
geom_rect(aes(xmin=easting, xmax=easting + 30, ymin=northing, ymax=northing + 30, fill = as.factor(unlist(pred2))), color="black") +
labs(title=paste(curr.species, "Population Abundance Predictions"), x="Easting", y="Northing", fill="Cover") +
scale_x_continuous(limits = c(365430, 366280)) +
scale_y_continuous(limits = c(4303800, 4304470)) +
scale_fill_manual(values = cols)
pred2.graph
pred2 <- factor(pred, levels = c("none", "few", "few more", "little", "some", "most", "all"))
cols = c("none"="#ceb467", "few" = "#ace5b2", "few more" = "#7aef87", "little" = "#57d165", "some" = "#1f961b", "most" = "#197f24", "all" = "#115118")
pred2.graph <- ggplot(data=test) +
geom_rect(aes(xmin=easting, xmax=easting + 30, ymin=northing, ymax=northing + 30, fill = as.factor(unlist(pred2))), color="black") +
labs(title=paste(curr.species, "Population Abundance Predictions"), x="Easting", y="Northing", fill="Cover") +
scale_x_continuous(limits = c(365430, 366280)) +
scale_y_continuous(limits = c(4303800, 4304470)) +
scale_fill_manual(values = cols)
pred2.graph
pred2 <- factor(pred, levels = c("none", "few", "few more", "little", "some", "most", "all"))
cols = c("none"="#ceb467", "few" = "#ace5b2", "few more" = "#7aef87", "little" = "#57d165", "some" = "#21a31d", "most" = "#197f24", "all" = "#115118")
pred2.graph <- ggplot(data=test) +
geom_rect(aes(xmin=easting, xmax=easting + 30, ymin=northing, ymax=northing + 30, fill = as.factor(unlist(pred2))), color="black") +
labs(title=paste(curr.species, "Population Abundance Predictions"), x="Easting", y="Northing", fill="Cover") +
scale_x_continuous(limits = c(365430, 366280)) +
scale_y_continuous(limits = c(4303800, 4304470)) +
scale_fill_manual(values = cols)
pred2.graph
species.map$scam <- factor(species.map$scam, levels = c("none", "few", "few more", "little", "some", "most", "all"))
cols = c("none"="#ceb467", "few" = "#ace5b2", "few more" = "#7aef87", "little" = "#57d165", "some" = "#21a31d", "most" = "#197f24", "all" = "#115118")
serc.plots <- ggplot() +
geom_rect(data=species.map, aes(xmin=easting, xmax=easting + 20, ymin=northing, ymax=northing + 20, fill = as.factor(unlist(species.map[1]))), color=NA) +
labs(title=paste("Population Abundance of SERC Plots"), x="Easting", y="Northing", fill = "Cover") +
geom_rect(data=training, aes(xmin=easting, xmax=easting + 30, ymin=northing, ymax=northing + 30), color="black", fill = NA) +
scale_x_continuous(limits = c(365430, 366280)) +
scale_y_continuous(limits = c(4303800, 4304470)) +
scale_fill_manual(values = cols)
serc.plots
grid.arrange(serc.plots, pred2.graph, ncol=2)
ggsave("plots/plot_comparison.png", arrangeGrob(serc.plots, pred2.graph, ncol=2), width = 11)
serc.plots <- ggplot() +
geom_rect(data=species.map, aes(xmin=easting, xmax=easting + 20, ymin=northing, ymax=northing + 20, fill = as.factor(unlist(species.map[1]))), color=NA) +
labs(title=paste("Scam Population Abundance in SERC Plots"), x="Easting", y="Northing", fill = "Cover") +
geom_rect(data=training, aes(xmin=easting, xmax=easting + 30, ymin=northing, ymax=northing + 30), color="black", fill = NA) +
scale_x_continuous(limits = c(365430, 366280)) +
scale_y_continuous(limits = c(4303800, 4304470)) +
scale_fill_manual(values = cols)
serc.plots
ggsave("plots/prediction_validation.png")
pred2.graph <- ggplot(data=test) +
geom_rect(aes(xmin=easting, xmax=easting + 30, ymin=northing, ymax=northing + 30, fill = as.factor(unlist(pred2))), color="black") +
labs(title=paste(curr.species, "Population Abundance Predictions"), x="Easting", y="Northing", fill="Cover") +
scale_x_continuous(limits = c(365430, 366280)) +
scale_y_continuous(limits = c(4303800, 4304470)) +
scale_fill_manual(values = cols)
pred2.graph
ggsave("plots/prediction_visual2.png")
# need to run training.R concurrently
grid.arrange(serc.plots, pred2.graph, ncol=2)
ggsave("plots/plot_comparison.png", arrangeGrob(serc.plots, pred2.graph, ncol=2), width = 11)
ggplot(data = full.data, aes(x=x, y=y)) + geom_point(aes(color = ndvi))
ggsave("plots/score_test.png")
ggsave("plots/score_test.png")
ggsave("plots/ndvi_test.png")
int("1")
as.integer("1")
load(lake.inputs)
lake.inputs = paste("InputsCV", lake.name, ".rda",sep="")
lake.name <- chopper
lake.name <- "chopper"
lake.inputs = paste("InputsCV", lake.name, ".rda",sep="")
load(lake.inputs)
setwd("~/Documents/Junior_Year/DISC_REU/DISC_bayesian_model/")
lake.inputs = paste("InputsCV", lake.name, ".rda",sep="")
load(lake.inputs)
run.cv = function(x){
attach(x)
out = try(adaptMetropGibbs(ltd=ltd,starting=starting,tuning=tune,batch=2400,report=400,
inputs=inputs,priors=priors))
n.sim = 2400*25
n.burn = 50000
n.step = 5
save.idx = seq(n.burn+1,n.sim,n.step)
if (class(out)=="try-error"){
samps = array(NA,dim=c(length(save.idx),length(starting)))
} else {
samps = out$p.theta.samples[save.idx,]
}
return(samps)
detach(x)
}
####### END OF Malcolm's CODE ######
results = vector("list", ((stop - start) + 1))
for (i in start+1 : stop){
results[[i]] = run.cv(mod.inputs[i])
}
return(results)
start <- 1
stop <- 3
run.cv = function(x){
attach(x)
out = try(adaptMetropGibbs(ltd=ltd,starting=starting,tuning=tune,batch=2400,report=400,
inputs=inputs,priors=priors))
n.sim = 2400*25
n.burn = 50000
n.step = 5
save.idx = seq(n.burn+1,n.sim,n.step)
if (class(out)=="try-error"){
samps = array(NA,dim=c(length(save.idx),length(starting)))
} else {
samps = out$p.theta.samples[save.idx,]
}
return(samps)
detach(x)
}
####### END OF Malcolm's CODE ######
results = vector("list", ((stop - start) + 1))
for (i in start+1 : stop){
results[[i]] = run.cv(mod.inputs[i])
}
return(results)
library(spBayes)
library(mgcv)
run.cv = function(x){
attach(x)
out = try(adaptMetropGibbs(ltd=ltd,starting=starting,tuning=tune,batch=2400,report=400,
inputs=inputs,priors=priors))
n.sim = 2400*25
n.burn = 50000
n.step = 5
save.idx = seq(n.burn+1,n.sim,n.step)
if (class(out)=="try-error"){
samps = array(NA,dim=c(length(save.idx),length(starting)))
} else {
samps = out$p.theta.samples[save.idx,]
}
return(samps)
detach(x)
}
####### END OF Malcolm's CODE ######
results = vector("list", ((stop - start) + 1))
for (i in start+1 : stop){
results[[i]] = run.cv(mod.inputs[i])
}
return(results)
results = vector("list", ((stop - start) + 1))
for (i in start+1 : stop){
results[[i]] = run.cv(mod.inputs[[i]])
}
start+1
for (i in start : stop){
results[[i]] = run.cv(mod.inputs[[i]])
}
start <- 0
for (i in start+1 : stop){
results[[i]] = run.cv(mod.inputs[[i]])
}
results = vector("list", ((stop - start) + 1))
for (i in start+1 : stop){
results[[i]] = run.cv(mod.inputs[[i]])
}
View(training)
min(training$overlap)
rm(list=ls())
# import libraries
library(spBayes)
library(ggplot2)
library(coda)
library(gridExtra)
library(mgcv)
library(zoo)
library(Matrix)
library(stringr)
library(snow)
library(rlecuyer)
library(snowfall)
library(readxl)
#STEP 1: Setup and Data Manipulation
setwd("~/Documents/Junior_Year/DISC_REU/DISC_bayesian_model/")
# specify sheet number
curr.lake <- read_excel("bigwoods.xls", sheet = 5, col_names = TRUE)
char.dat <- data.frame(age=rep(NA,length(curr.lake$Date)))
# define variables from lake
char.dat$age <- with(curr.lake, round(Date) - 2018) # calculate YBP
char.dat$sed.rate <- with(curr.lake, Flux / Count) # NOTE: do not have age of sediment core so we left it fixed
char.dat$influx <- curr.lake$`Char Flux`
char.dat$count <- curr.lake$Count
qplot(age,count,data=char.dat) + geom_smooth(method="loess", span=0.08) + theme_bw()
# specify sheet number
curr.lake <- read_excel("bigwoods.xls", sheet = 7, col_names = TRUE)
char.dat <- data.frame(age=rep(NA,length(curr.lake$Date)))
# define variables from lake
char.dat$age <- with(curr.lake, round(Date) - 2018) # calculate YBP
char.dat$sed.rate <- with(curr.lake, Flux / Count) # NOTE: do not have age of sediment core so we left it fixed
char.dat$influx <- curr.lake$`Char Flux`
char.dat$count <- curr.lake$Count
qplot(age,count,data=char.dat) + geom_smooth(method="loess", span=0.08) + theme_bw()
# specify sheet number
curr.lake <- read_excel("bigwoods.xls", sheet = 5, col_names = TRUE)
char.dat <- data.frame(age=rep(NA,length(curr.lake$Date)))
# define variables from lake
char.dat$age <- with(curr.lake, round(Date) - 2018) # calculate YBP
char.dat$sed.rate <- with(curr.lake, Flux / Count) # NOTE: do not have age of sediment core so we left it fixed
char.dat$influx <- curr.lake$`Char Flux`
char.dat$count <- curr.lake$Count
qplot(age,count,data=char.dat) + geom_smooth(method="loess", span=0.08) + theme_bw()
#STEP 2: Approximate background and foreground intensity (using cubic base spline) and set offset
char.dat$age.c = with(char.dat,age-min(age))
char.dat$age.s = with(char.dat,age.c/max(age.c))
n = nrow(char.dat)
n.knots = 31 # this variable will change for each model based on time interval
# note: smoothCon is part of the mgcv package for constructing the smooth terms in a GAM model
CRbasis = smoothCon(s(age.s,k=n.knots,bs="cr"),data=char.dat,knots=NULL,absorb.cons=TRUE,
scale.penalty=TRUE)
Sb = CRbasis[[1]]$S[[1]]
X = CRbasis[[1]]$X
knots = as.numeric(CRbasis[[1]]$xp)
S.scale = CRbasis[[1]]$S.scale
TT <- 1/lake.dat$sed.rate # sets offset (inverse of sedimentation rate from STEP 1)
char.dat$t.hold = rollapply(char.dat$count,50,function(x)quantile(x,0.9),fill="extend")
# Separate charcoal counts
y.back = y.fore = char.dat$count
y.back[char.dat$count>char.dat$t.hold] = NA # removes count value for background if the value is distributed in the 90% quantile
y.fore[char.dat$count<char.dat$t.hold] = NA # removes count value for background if the value is NOT distributed in the 90% quantile
#approximate the missing values (background = na.approx, foreground = randomly pulls value from binomial distribution)
y.back = round(na.approx(y.back))
y.fore[is.na(y.fore)] = rbinom(sum(is.na(y.fore)),1,0.1) # ask about why we add a bunch of zeros instead of NAs
# Plot background and foreground counts
plot(char.dat$age,y.back)
# Plot background and foreground counts
plot(char.dat$age,y.back)
# Plot background and foreground counts
plot(char.dat$age,y.back)
plot(char.dat$age,y.fore)
# Add separate counts to data.frame
char.dat$count.b = y.back
char.dat$count.f = y.fore
# Fit GAM Model to background (gam is part of mgcv package)
m1 = gam(y.back~X,family=poisson,offset=log(TT),paraPen=list(X=list(Sb,sp=0.001*S.scale)))
# plots the points and our fitted GAM model for background
plot(char.dat$age,y.back)
lines(char.dat$age,fitted(m1),col="blue",lwd=2)
# Fit GAM Model to foreground
m2 = gam(y.fore~X,family=poisson,offset=log(TT),paraPen=list(X=list(Sb,sp=(1e-7)*S.scale)))
# plots the points and our fitted GAM model for foreground
plot(char.dat$age,y.fore)
lines(char.dat$age,fitted(m2),col="blue",lwd=2)
rm(list=ls())