-
Notifications
You must be signed in to change notification settings - Fork 0
/
LDsynLikSpat_KS.R
323 lines (275 loc) · 10.9 KB
/
LDsynLikSpat_KS.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
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
library(sl)
library(LaplacesDemon)
library(testthat)
library(verification)
source("RanalysisFunctions.R")
source("logLik.r") # override some sl functions and add synLik
source("param.r")
## load data
## use simulated data as in base_regression.R
source("maps_basic_regression.R") # gives maps
source("functions_migration_KS.R")
#==================
# Params, if modifying stats: look for multiGilStat
#==================
Nrep=300;
n.mc=30000
set.seed(1)
genIntervals <- c(seq(10, 100, 15), seq(130, 250, 30)) # distance classes for the general variogram
#===================
# Prep for simulations
# declare Data for LD
#===================
threshold <- 2000
dist_mat <- nearest.dist(x=sp, y=NULL, method="euclidian", delta=threshold, upper=NULL);
dist_mat <- as.matrix(dist_mat)
# Rprof()
# cumulProbMat <- fast_prob_mat(halfDistJ, halfDistH, useDelta, delta, rateHopInMove, rateSkipInMove, rateJumpInMove, threshold, sp, dist_mat,SB,cumul=TRUE)
cumulProbMat <- generate_prob_mat(halfDistJ, halfDistH, useDelta, delta, rateHopInMove, rateSkipInMove, rateJumpInMove, threshold, sp, dist_mat,SB,cumul=TRUE)
# Rprof(NULL)
# test cumulProbMat
test_that("cumulProbMat is cumulative, ending in ones",{
expect_equal(sum(cumulProbMat[dim(cumulProbMat)[1],]),dim(cumulProbMat)[1])
expect_true(all.equal(cumulProbMat[dim(cumulProbMat)[1],],rep(1,dim(cumulProbMat)[1])))
})
### the vector of stats for the data
infestH<-which(maps$infest3==1)
stats <- multiGilStat(cumulProbMat=cumulProbMat, blockIndex = blockIndex, infestH = infestH, timeH=maps$ages[infestH], endTime = 1, rateMove = rateMove, Nrep = Nrep, coords = maps[, c("X", "Y")], breaksGenVar = genIntervals, simul=FALSE)
statsData <- stats$statsTable[, 1]
### the standard call to multiGilStat
snapTimes<-attributes(maps)$snapTimes
nbit<-snapTimes[3]-snapTimes[2]
infestH<-which(maps$infest2==1)
outBase <- multiGilStat(cumulProbMat=cumulProbMat, blockIndex = blockIndex, infestH = infestH, timeH=rep(-1,length(infestH)), endTime = nbit, rateMove = rateMove, Nrep = Nrep, coords = maps[, c("X", "Y")], breaksGenVar = genIntervals, simul=TRUE)
# test
test_that("multiGilStat with true params -> stats ~= statsData",{
expect_true(count(apply(outBase$statsTable,1,max)<statsData)==0)
expect_true(count(apply(outBase$statsTable,1,min)>statsData)==0)
})
stop()
### starting point for simulations
infestH <- which(maps$infest2 > 0)
timeH <- maps$ages[infestH]
### Priors (also the place to change the parameters)
priorMeans<-log(c(0.03, 0.04, 0.35))
realMeans<-log(c(rateMove, rateJumpInMove, delta))
names(priorMeans)<-c("rateMove", "rateJumpInMove", "delta")
names(realMeans)<-names(priorMeans)
### LD formalism for Data (no setting should be made past this declaration)
PGF<-function(Data){ # parameters generating functions (for init etc...)
priorMeans<-Data$priorMeans
values<-rnorm(length(priorMeans),mean=priorMeans,sd=0.15)
return(values)
}
### Set cumulProbMat to NULL <- we're now using priorMeans from 3 valuables
### Must calculate probMat each time
MyData <- list(y=statsData,
trans=NULL,
cumulProbMat=NULL,
blockIndex=blockIndex,
dist_out = makeDistClassesWithStreets(X = as.vector(maps[, "X"]), Y = as.vector(maps[, "Y"]), genIntervals, blockIndex),
infestH=infestH,
timeH=timeH,
endTime=nbit,
maps=maps,
nbit=nbit,
Nrep=Nrep,
priorMeans=priorMeans,
genIntervals=genIntervals,
PGF=PGF,
mon.names=c("ll","lp"), # monitored variables (like in Model)
parm.names=names(priorMeans)# parameters names (like in Model and Initial.Values)
)
rm(cumulProbMat,dist_mat)
#==================================
## declare Model for LD
#==================================
Model <- function(theta,Data,postDraw=FALSE){
# coerce theta, a priori all positive
theta<-exp(theta)
names(theta)<-Data$parm.names
# set intervals for the variables
# if value above the interval, move to closest value
theta["rateMove"]<-interval(theta["rateMove"],a=0.001, b=0.22)
theta["rateJumpInMove"]<-interval(theta["rateJumpInMove"],a=0.0001, b = 0.25)
theta["delta"]<-interval(theta["delta"], a=0.0001, b = 0.5)
cat("theta:",theta, "\n")
seed<-runif(1,min=0,(2^16-1)) # NB: for repeatability, R seed
# Should be set
# simulations
start <- Sys.time()
if(postDraw){
getStats<-FALSE
# then in wrapper use infested if Nrep =1 and infested$Dens if Nrep=a few
# to show a posterior predictive map
}else{
getStats<-TRUE
}
# if only optimization is rateMove
if(length(theta) == 1 & all.equal(Data$parm.names[1], "rateMove")){
out <- multiGilStat(cumulProbMat = Data$cumulProbMat, blockIndex = Data$blockIndex,
infestH = Data$infestH, timeH = Data$timeH, endTime = Data$nbit,
rateMove = theta["rateMove"],
Nrep = Data$Nrep,
coords = Data$maps[, c("X", "Y")],
breaksGenVar = Data$genIntervals,
getStats=getStats,
seed=seed,
dist_out = Data$dist_out)
}else{
#pass all variables correctly!
#need to make probMat everytime, this is more time consuming.
#note rateHopInMove = 1-rateJumpInMove
out <- multiGilStat(cumulProbMat = Data$cumulProbMat, blockIndex = Data$blockIndex,
infestH = Data$infestH, timeH = Data$timeH, endTime = Data$nbit,
rateMove = theta["rateMove"],
Nrep = Data$Nrep,
coords = Data$maps[, c("X", "Y")],
breaksGenVar = Data$genIntervals,
getStats=getStats,
seed=seed,
halfDistJ = ifelse(is.na(theta["halfDistJ"]), halfDistJ, theta["halfDistJ"]),
halfDistH = ifelse(is.na(theta["halfDistH"]), halfDistH, theta["halfDistH"]),
useDelta = ifelse(is.na(theta["useDelta"]), useDelta, theta["useDelta"]),
delta = ifelse(is.na(theta["delta"]), delta, theta["delta"]),
rateHopInMove = ifelse(is.na(theta["rateJumpInMove"]), 1-rateJumpInMove, 1-theta["rateJumpInMove"]),
rateSkipInMove = ifelse(is.na(theta["rateSkipInMove"]), rateSkipInMove, theta["rateSkipInMove"]),
rateJumpInMove = ifelse(is.na(theta["rateJumpInMove"]), rateJumpInMove, theta["rateJumpInMove"]),
dist_out = Data$dist_out)
}
end <- Sys.time()
cat("t multiGil:",end-start,"\n")
if(postDraw){
yhat<-out$infestedDens
LL<-NA
LP<-NA
}else{
yhat<-out$statsTable[,1]
# synthetic likelihood
ll<-synLik(out$statsTable,Data$y,Data$trans)
# get likelihood with priors
LL<-ll
attributes(LL)<-NULL
LP<-LL+sum(dnorm(log(theta),mean=Data$priorMeans,sd=1))
# LP<-LL+sum(dlnorm(theta,meanlog=log(Data$priorMeans),sdlog=1))
cat("LL:",LL,"LP:",LP,"\n")
}
# return
Modelout <- list(LP=LP, # joint posterior
Dev=-2*LL, # deviance, probably not to be changed
Monitor=c(LL,LP), # to be monitored/ploted
yhat=yhat, # data generated for that set of parameter
# will be used for posterior check
parm=log(theta) # the parameters, possibly constrained by the model
)
}
#===========================
# Testing Model/Data
#===========================
ModelOutGood<-Model(priorMeans,MyData)
ModelOutBest<-Model(realMeans,MyData)
expect_true(ModelOutGood$Dev>ModelOutBest$Dev)
signedLog<-function(signedBigNums){
signNum<-sign(signedBigNums)
signedLog<-log(abs(signedBigNums))*signNum
return(signedLog)
}
# theta<-realMeans
# theta["rateMove"]<-log(0.5)
# ModelOutBest<-Model(theta,MyData)
# testlogRateMove<-seq(-7,-1.5,0.1)
# testLL<-0*testlogRateMove
# for(i in 1:length(testlogRateMove)){
# rM<-exp(testlogRateMove[i])
# cat("RateMove:",rM," ")
# theta["rateMove"]<-log(rM)
# ModelOutGood<-Model(theta,MyData)
# testLL[i]<-ModelOutGood$LP
# }
# plot(testlogRateMove,signedLog(testLL),type="l",ylab="Log(Log(likelihood)",xlab="Log(rateMove)",main="Synthetic likelihood profile for rateMove \n blue is true");
# abline(v=log(rateMove),col="blue")
# dev.print(device=pdf,"SynLikProfileRateMove.pdf")
# weibull order plotting to check if stats are normal
for(i in 1:dim(outBase$statsTable)[1])
{
order <- order(outBase$statsTable[i, ])
plot((1:dim(outBase$statsTable)[2])/(dim(outBase$statsTable)[2]+1), outBase$statsTable[i, order], main = i)
Sys.sleep(0.5)
}
stop()
#===========================
# Init values
#===========================
Initial.Values <- GIV(Model, MyData, PGF=TRUE,n=100) #GIV: generate initial values
#===========================
## launch LD
#===========================
start1<-Sys.time()
n.mc <- 200
Fit1 <- LaplacesDemon(Model,
Data=MyData,
Initial.Values,
Covar=NULL, # proposal covariance matrix
Iterations=n.mc, # total number of iterations
Status=10, # how often status plotted
Thinning=1, # allow to save memory...
Algorithm="", # see details for this and specs in ?LaplacesDemon
Specs = list(SIV = NULL, n1 = 4, at = 6, aw = 1.5) # standard recommended specifications for twalk
)
stop1<-Sys.time()
cat("chain time:",stop1-start1)
#===========================
## results
#===========================
print(Fit1)
Consort(Fit1)
# PosteriorChecks(Fit1)
MyData$infestH<-which(maps$infest3==1)
postMap<-getPosteriorMaps(Fit1,MyData,repByTheta=10)
# Brier scores
predReg<-maps$predict4
predReg[which(maps$infest3==1)]<-1
verWood<-verify(maps$infest4,maps$postMap)
# b<-verify(mapsholds<-exp(seq(log(0.01),log(1),0.2))
verReg<-verify(maps$infest4,maps$predict4)
dev.new()
probthresholds<-exp(seq(log(0.01),log(1),0.2))
par(mfrow=c(2,3))
plot(verReg)
rocReg<-roc.plot(maps$infest4,maps$predict4,main="predict4",CI=TRUE,n.boot=100)
valueReg<-value(maps$infest4,maps$predict4,thresholds=probthresholds,all=TRUE)
plot(verWood)
rocWood<-roc.plot(maps$infest4,maps$postMap,main="postMap",CI=TRUE,n.boot=100)
valueReg<-value(maps$infest4,maps$postMap,all=TRUE,thresholds=probthresholds)
dev.new()
discrimination.plot(maps$infest4,maps$predict4,main="Reg")
dev.new()
discrimination.plot(maps$infest4,maps$postMap,main="Wood")
dev.new()
par(mfrow=c(3,3))
plot_reel(maps$X,maps$Y,maps$infest3,base=0)
plot_reel(maps$X,maps$Y,maps$infest3,base=0,main="Init")
plot_reel(maps$X,maps$Y,maps$infest3,base=0)
plot_reel(maps$X,maps$Y,maps$postMap,base=0,main=paste("Posterior Map \nBrier:",verWood$bs,"Skill:",verWood$ss))
plot_reel(maps$X,maps$Y,maps$infest4,base=0,main="Observed")
plot_reel(maps$X,maps$Y,maps$predict4,base=0,main=paste("Prediction regression\nBrier:",verReg$bs,"Skill:",verReg$ss))
plot_reel(maps$X,maps$Y,maps$postMap,base=0,top=0.1)
plot_reel(maps$X,maps$Y,maps$infest4,base=0,top=0.1,main="idem topped at 0.1")
plot_reel(maps$X,maps$Y,maps$predict4,base=0,top=0.1)
caterpillar.plot(Fit1, Parms="rateMove")
BurnIn <- Fit1$Rec.BurnIn.Thinned
plot(Fit1, BurnIn, MyData, PDF=FALSE)
hist(exp(Fit1$Posterior1))
Pred <- predict(Fit1, Model, MyData)
summary(Pred, Discrep="Chi-Square")
plot(Pred, Style="Covariates", Data=MyData)
plot(Pred, Style="Density", Rows=1:9)
plot(Pred, Style="ECDF")
plot(Pred, Style="Fitted")
plot(Pred, Style="Jarque-Bera")
plot(Pred, Style="Predictive Quantiles")
plot(Pred, Style="Residual Density")
plot(Pred, Style="Residuals")
Levene.Test(Pred)
Importance(Fit1, Model, MyData, Discrep="Chi-Square")
# prediction