Newer
Older
occuprecode = function(attr) {
recode(attr,
"10" = "0" , "11"= "0" ,
"12"= "1" , "14"= "1" ,
"15" = "2" ,"16"= "2" , "17"= "2" ,
"18" = "3" ,"19"="3", "20" = "3" ,
"21" = "3" ,"22" = "3","
23" = "4" , "24" = "4" ,"25" = "4" ,
"26" = "3" , "27" = "3" ,"28" = "3" ,
# recode(sample2$FR_CLC,
# "10" = "0" , "11"= "0" ,
# "12"= "1" , "14"= "1" ,
# "15" = "2" ,"16"= "2" , "17"= "2" ,
# "18" = "3" ,"19"="3", "20" = "3" ,
# "21" = "3" ,"22" = "3","
# 23" = "4" , "24" = "4" ,"25" = "4" ,
# "26" = "3" , "27" = "3" ,"28" = "3" ,
# "32" = "3",
# .default = "A")
# optimisation of the SMN --------------------
eval= FALSE # should we run the ex ante evaluation
# in case of presence of census data to be kept
print(" Include points from existing surveys (forcing to keep existing points) ")
tab1 <- table(framesamp[,domainvalue])
framesamp <- framesamp[framesamp[,domainvalue] %in% names(tab1[tab1 >= 10]),]
tab1 <- table(framecens[,domainvalue])
framecens <- framecens[framecens[,domainvalue] %in% names(tab1[tab1 >= 10]),]
sample2 <- rbind(framesamp,
framecens)
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
frame1 <- buildFrameDF(df = sample2,
id = "id",
X = X,
Y = Y,
domainvalue = domainvalue)
#Build initial strata
strata1 <- buildStrataDF(frame1, progress=T)
#Find initial solutions for each domain (NUTS2) by kmeans clustering
init_sol3 <- KmeansSolution2(frame=frame1,
errors=cv,
maxclusters = 8, # why 8?
showPlot = F)
nstrata3 <- tapply(init_sol3$suggestions,
init_sol3$domainvalue,
FUN=function(x) length(unique(x)))
initial_solution3 <- prepareSuggestion(init_sol3,frame1,nstrata3)
#Optimize initial solution by Genetic Algorithm
print("optimStrata-------------------")
ind_framecens <- c(rep(T, dim(framesamp)[1]),
rep(F, dim(framecens)[1]))
frame1samp <- frame1[ ind_framecens , ]
frame1cens <- frame1[ !ind_framecens , ]
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
solution3 <- optimStrata(method = "continuous",
errors = cv,
framesamp = frame1samp ,
framecens = frame1cens ,
iter = 20,
pops = 20,
nStrata = nstrata3,
suggestions = initial_solution3,
showPlot=FALSE,writeFiles=FALSE)
framenew3 <- solution3$framenew
outstrata3 <- solution3$aggr_strata
strataStructure3 <- summaryStrata(framenew3,
outstrata3,
progress=T)
#Total number of samples required
print("selectSample-------------------")
sample4 <- selectSample(framenew3,
outstrata3,
writeFiles = F)
} else {
## case when framecens is null
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
frame1 <- buildFrameDF(df = sample2,
id = "id",
X = X,
Y = Y,
domainvalue = domainvalue)
#Build initial strata
strata1 <- buildStrataDF(frame1, progress=T)
#Find initial solutions for each domain (NUTS2) by kmeans clustering
init_sol3 <- KmeansSolution2(frame=frame1,
errors=cv,
maxclusters = 8, # why 8?
showPlot = F)
nstrata3 <- tapply(init_sol3$suggestions,
init_sol3$domainvalue,
FUN=function(x) length(unique(x)))
initial_solution3 <- prepareSuggestion(init_sol3,frame1,nstrata3)
#Optimize initial solution by Genetic Algorithm
print("optimStrata-------------------")
solution3 <- optimStrata(method = "continuous",
errors = cv,
framesamp = frame1,
framecens = framecens ,
iter = 20,
pops = 20,
nStrata = nstrata3,
suggestions = initial_solution3,
showPlot=FALSE,writeFiles=FALSE)
framenew3 <- solution3$framenew
outstrata3 <- solution3$aggr_strata
strataStructure3 <- summaryStrata(framenew3,
outstrata3,
progress=T)
#Total number of samples required
print("selectSample-------------------")
sample4 <- selectSample(framenew3,
outstrata3,
writeFiles = F)
}# End else
print("evalSolution-------------------")
res = eval3 = NA
if(eval == TRUE) {
eval3 <- evalSolution(frame = solution3$framenew,
outstrata = solution3$aggr_strata,
nsampl = 200, # why not the optimised number of sample ?
progress = TRUE,
writeFiles = FALSE)
res = expected_CV(solution3$aggr_strata)
}
return(list(
smlsample = sample4,
optimstrata= solution3,
tableau = framenew3,
eval = eval3,
NbSample = sum(strataStructure3$Allocation),
ExpCV = res)
)
}
## ----------- plot
res, # the output of the function optim , a list of results
nom , # the name of the file to be saved
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
points4 <- sample2[res$smlsample$ID,]
# coordinates(points4)=~x+y
points4 <- st_as_sf(points4,
coords=c("x","y"),
crs = crs
)
# plotStrata2d(res$optimstrata$framenew,
# res$optimstrata$aggr_strata,
# domain = 5,
# vars = c("X1","X2"),
# labels = c("OC","pH"))
#
# dom = 1
# hist(eval3$est$Y1[res$eval3$est$dom == dom], col = "grey", border = "white",
# xlab = expression(hat(Y)[1]),
# freq = FALSE,
# main = paste("Variable Y1 Domain ",dom,sep=""))
# abline(v = mean(eval3$est$Y1[eval3$est$dom == dom]), col = "blue", lwd = 2)
# abline(v = mean(frame1$Y1[frame1$domainvalue==dom]), col = "red")
# legend("topright", c("distribution mean", "true value"),
# lty = 1, col = c("blue", "red"), box.col = NA, cex = 0.8)
tm_raster(style = "quantile",n = 10,
palette = "RdYlGn",
alpha = 0.5) +
tm_shape(points4) +
tm_dots(col = "black",
size = 0.002)+
tm_layout(legend.outside = T)
tmap_save(plNUTS,
filename = paste0("output/",nom,".jpeg" )
)
# points4 <- st_as_sf(sample2,
# coords=c("x","y"),
# crs = crs
# )
#
# plNUTS = tm_shape(stack500["FR_pH"]) +
# tm_raster(style = "quantile",n = 10,
# palette = "RdYlGn",
# alpha = 0.5) +
# tm_shape(points4) +
# tm_dots(col = "black",
# size = 0.002)+
# tm_layout(legend.outside = T)
#
# tmap_save(plNUTS,
# filename = paste0("output/",nom,"Start.jpeg" )
#
#
# )
#