-
Notifications
You must be signed in to change notification settings - Fork 19
/
ModelbasedSample_KED_spsann.R
66 lines (55 loc) · 2.38 KB
/
ModelbasedSample_KED_spsann.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
library(sp)
library(gstat)
library(spsann)
#Load grid with EMdata; the nodes of this grid are the potential sampling locations
grid <- read.csv(file="Data/EMUzbekistan_25m.csv")
gridded(grid) <- ~x1+x2
boundary <- rgeos::gUnaryUnion(as(grid, "SpatialPolygons"))
# residual variogram
variogram <- vgm(nugget=0.1, psill=0.075, "Exp", range=100)
gridded(grid)<-FALSE
candi <- data.frame(x=coordinates(grid)[,1],y=coordinates(grid)[,2])
covars <- as(grid,"data.frame")
covars <- covars[,c(3,4,1)]
names(covars)[c(1,2)] <- c("x","y")
schedule <- scheduleSPSANN(initial.acceptance = 0.8,initial.temperature = 0.002,
temperature.decrease=0.95,
chains=500,
chain.length=5,
stopping=10,
x.min=25,y.min=25,
cellsize=25)
set.seed(321)
res <- optimMKV(
points = 50, candi = candi, covars=covars, vgm = variogram,
eqn = z ~ lnEM1m, plotit = F, track=T, schedule = schedule)
sample<-candi[res$points$id,]
#add covariate to sample
ids <- as.integer(rownames(sample))
sample$EM <- covars[ids,3]
library(ggplot2)
pdf(file = "KEDSample_Uzbekistan.pdf", width = 6, height = 4)
ggplot(data = covars) +
geom_raster(mapping = aes(x = x, y = y, fill = lnEM1m)) +
geom_point(data = sample, mapping = aes(x = x, y = y), size=2,colour = "black") +
# scale_fill_gradient(name="x",low = "skyblue", high = "darkblue") +
scale_fill_continuous(name = "lnEMv1m", low = rgb(0, 0.2, 1), high = rgb(1, 0.2, 0)) +
scale_y_continuous(name = "Northing") +
scale_x_continuous(name = "Easting") +
coord_equal()
dev.off()
pdf(file="TraceMKV_MBSample_SSA_OK_Square.pdf",width=6,height=4)
ggplot(res$objective$energy) +
geom_line(mapping = aes(x=1:nrow(res$objective$energy),y = obj),colour="red") +
scale_y_continuous(name = "Mean Kriging Variance") +
scale_x_continuous(name = "Chain")
dev.off()
pdf(file = "HistogramEMUzbekistan_Sample.pdf", width = 4, height = 3)
ggplot(data = sample) +
geom_histogram(mapping = aes(EM),breaks=seq(from=2.75,to=5,by=0.25),color="orange")
dev.off()
griddf <- as(grid,"data.frame")
pdf(file = "HistogramEMUzbekistan_Population.pdf", width = 4, height = 3)
ggplot(data = griddf) +
geom_histogram(mapping = aes(EM),breaks=seq(from=2.75,to=5,by=0.25),color="orange")
dev.off()