-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathfunctions_migration_KS.R
595 lines (483 loc) · 18.6 KB
/
functions_migration_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
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
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
# functions for the migration/spatial stats
# data for the tests
library(geoR)
library(testthat)
source("RanalysisFunctions.R")
source("maps_basic_regression.R") # gives maps
genIntervals <- seq(0, 250, 15) # distance classes for the general variogram
varioTest <- variog(coords = maps[,c("X","Y")], data = maps$infest1, breaks = genIntervals)
#======================
# Generating probability matrix
#======================
generate_prob_mat <- function(halfDistJ, halfDistH, useDelta, delta, rateHopInMove, rateSkipInMove, rateJumpInMove, threshold, sp, dist_mat, SB, cumul=FALSE){
### Generating hop, skip, jump matrices
# decreasing spatial link hop/skip
weightMat<-exp(-dist_mat/halfDistH)
diag(weightMat)<- rep(0,dim(weightMat)[1]) # emigrants; can't go from house to same house
# hop
hopMat<-SB*weightMat
# skip
DB <- abs(SB-1)
skipMat<-DB*weightMat
if(useDelta){
hopMat <- hopMat + skipMat*delta
}
# jump
jumpMat<-exp(-dist_mat/halfDistJ)
diag(jumpMat)<- rep(0,dim(jumpMat)[1]) # emigrants; can't go from house to same house
# normalize hops
rsumQ<- hopMat %*% rep(1,num_obs)
probMatH <- as.matrix(hopMat * as.vector(1/rsumQ))
# normalize skips
rsumQ <- skipMat %*% rep(1,num_obs)
probMatS <- as.matrix(skipMat * as.vector(1/rsumQ))
# normalize jumps
rsumQ<- jumpMat %*% rep(1,num_obs)
probMatJ <- as.matrix(jumpMat * as.vector(1/rsumQ))
#======================
# put together
#======================
probMat <- rateHopInMove*probMatH + rateSkipInMove*probMatS + rateJumpInMove*probMatJ
diag(probMat) <- rep(0, dim(probMat)[1]) # emigrants; can't go from house to same house
# #multiply the probMat by the probability that the infestations spreads
# #rateMove from param.r
# probMat <- probMat*rateMove
if(cumul){
#redefine probMat as the cumulative sum on each line
for(row in 1:L)
{
probMat[row, ]<-cumsum(probMat[row, ])
}
# # transpose so that redeable by lines in C
probMat<-t(probMat)
}
return(probMat)
}
# fast function keeping varibles for skip/hop/jumps but not as fix rates
fast_prob_mat <- function(halfDistJ, halfDistH, useDelta, delta, rateHopInMove, rateSkipInMove, rateJumpInMove, threshold, sp, dist_mat, SB, cumul=FALSE){
### Generating hop, skip, jump matrices
# decreasing spatial link hop/skip
weightMat<-exp(-dist_mat/halfDistH)
diag(weightMat)<- rep(0,dim(weightMat)[1]) # emigrants; can't go from house to same house
# hop
hopMat<-SB*weightMat
# skip
DB <- abs(SB-1)
skipMat<-DB*weightMat
hopMat <- hopMat + skipMat*delta
# jump
jumpMat<-exp(-dist_mat/halfDistJ)
diag(jumpMat)<- rep(0,dim(jumpMat)[1]) # emigrants; can't go from house to same house
probMat<-jumpMat+hopMat
# # normalize hops
# rsumQ<- hopMat %*% rep(1,num_obs)
# probMatH <- as.matrix(hopMat * as.vector(1/rsumQ))
# # normalize skips
# rsumQ <- skipMat %*% rep(1,num_obs)
# probMatS <- as.matrix(skipMat * as.vector(1/rsumQ))
# # normalize jumps
# rsumQ<- jumpMat %*% rep(1,num_obs)
# probMatJ <- as.matrix(jumpMat * as.vector(1/rsumQ))
# #======================
# # put together
# #======================
# probMat <- rateHopInMove*probMatH + rateSkipInMove*probMatS + rateJumpInMove*probMatJ
# diag(probMat) <- rep(0, dim(probMat)[1]) # emigrants; can't go from house to same house
# #multiply the probMat by the probability that the infestations spreads
# #rateMove from param.r
# probMat <- probMat*rateMove
if(cumul){
#redefine probMat as the cumulative sum on each line
for(col in 1:L){
probMat[,col]<-cumsum(probMat[,col])
}
}
# # transpose so that redeable by lines in C
# probMat<-t(cumulProbMat)
# }
return(probMat)
}
#=============================
# Migration functions
#=============================
## define the migration functions
# semi-gillespie: draw the next time of event for each house
# not nearly as fast as the gillespie but may
# be easier to use with differing probabilities of events
# for different nodes
loopMethod <- function(events_time, events_loc, probMat, infestH, timeH, currentTime, endTime)
{
if(currentTime >= endTime)
return(list(infestH, timeH))
#remove the house/time responsible for the current event from the queue
events_time <- events_time[-1]
current_loc <- events_loc[1]
events_loc <- events_loc[-1]
#need to pick the newly infested house from current_loc
newInfested <- sample(x = dim(probMat)[1], size = 1, prob = probMat[current_loc, ])
#put the new infested house and the current infested house back into the pile
timeToNext <- currentTime + rexp(2, rate = 1/scale)
#timeToNext <- currentTime + rweibull(2, shape = 1, scale = scale)
if(!(newInfested %in% infestH))
{
infestH <- c(infestH, newInfested)
timeH <- c(timeH, currentTime)
events_time <- c(events_time, timeToNext)
events_loc <- c(events_loc, current_loc, newInfested)
}else
{
events_time <- c(events_time, timeToNext[1])
events_loc <- c(events_loc, current_loc)
}
sortTime <- order(events_time)
events_time <- events_time[sortTime]
events_loc <- events_loc[sortTime]
#cat("current time", currentTime, "infested", length(infestH), "\n")
#cat("time: ", events_time, "\n")
#cat("locations: ", events_loc, "\n\n")
return(loopMethod(events_time, events_loc, probMat, infestH, timeH, events_time[1], endTime))
}
# # gillespie with multiple snapshots possible
# useless, just use ages from normal gillespie as demonstrated in basic_regression.R and infestSerieToMaps() in this file
# gillespie.mo<-function(probMat, # matrix with probability to end up in given house given "departure" from an initial house
# cumulProbMat, # same but cumulative probability for one house of departure
# infestH, # vector of infestation
# timeH, # vector of time of infestation
# snapTimes,# time of snapshots, the last one being the end time of simulation
# scale, # 1/(rate of departure per house)
# toNextEvent=rweibull(1, shape = 1, scale = scale/length(infestH)), # time to the next event
# currentTime=0 # initial time
# ){
# snapshots<-list()
# for(itime in 1:length(snapTimes)){
# snapshots[[itime]]<-gillespie(probMat,cumulProbMat,infestH,timeH,snapTimes[itime],scale,toNextEvent,currentTime=currentTime)
# snapshots[[itime]]$time<-snapTimes[itime]
# currentTime <- snapTimes[itime]
# infestH<-snapshots[[itime]]$infestOrder
# timeH<-snapshots[[itime]]$infestTime
# toNextEvent<-snapshots[[itime]]$toNextEvent
# currentTime<-snapshots[[itime]]$time
# }
# return(snapshots)
# }
# test
# set.seed(777)
# out <- gillespie.mo(probMat, cumulProbMat, infestH, timeH, snapTimes=snapTimes, scale)
#
# par(mfcol=c(2,length(snapTimes)))
# for(itime in 1:length(snapTimes)){
# outT<-out[[itime]]
# maps<-infestSerieToMaps(outT,sp)
# plot_reel(maps$X,maps$Y,maps$infest,base=0,main=paste(outT$time,"time unit (weeks)"))
# plot_reel(maps$X,maps$Y,log(maps$ages+1),base=0,top=max(log(maps$ages+1)))
# }
# with one start, one stop
gillespie <- function(probMat, # matrix with probability to end up in given house given "departure" from an initial house
cumulProbMat, # same but cumulative probability for one house of departure
infestH, # vector of infestation
timeH, # vector of time of infestation
endTime, # end time of simulation
scale, # 1/(rate of departure per house)
toNextEvent=rweibull(1, shape = 1, scale = scale/length(infestH)), # time to the next event
currentTime=0 # initial time
){
#cumulProbMat is not used in this function (is used in .C function)
#pass as parameter for homogeneity
#toNextEvent - the time to the toNextEvent
# cat("begin:", currentTime,"first event:",currentTime+toNextEvent,"end:",endTime,"\n")
while(currentTime + toNextEvent <= endTime)
{
currentTime <- currentTime + toNextEvent
#pick a location to be the infesting house
current_loc <- as.integer(runif(1, min = 1, max = length(infestH)+1))
current_loc <- infestH[current_loc]
#pick a new house to become infested from the infesting house
newInfested <- sample(x = dim(probMat)[1], size = 1, prob = probMat[current_loc, ])
if(!(newInfested %in% infestH))
{
infestH <- c(infestH, newInfested)
timeH <- c(timeH, currentTime)
}
#calculate the time to the next event again
toNextEvent <- rweibull(1, shape = 1, scale = scale/length(infestH))
}
return(list(infestOrder=infestH,infestTime=timeH,toNextEvent=toNextEvent,time=endTime))
}
# import C functions if possible
importOk<-try(dyn.load("functions_migration.so"), silent=TRUE)
# define migration functions and
# change all entries in A bigger than maxtobesetnull to 0
if(class(importOk)!="try-error"){
gillespie<- function(probMat, cumulProbMat, infestH, timeH, endTime, scale,seed=runif(1, 1, 2^31-1)){
#probMat is not used here, is used in R function
#pass for homogeneity
#for proper seeding of stochastic simulation
L<-dim(cumulProbMat)[1]
indexInfest <- rep(-1, L)
timeI <- rep(-1, L)
infested <- rep(0, L)
indexInfest[1:length(infestH)] <- infestH - 1
timeI[1:length(timeH)] <- timeH
infested[infestH] <- 1
endIndex<-length(infestH)-1
out<- .C("gillespie",
infested = as.integer(infested),
endIndex = as.integer(endIndex),
L = as.integer(L),
probMat = as.numeric(cumulProbMat),
endTime = as.numeric(endTime),
indexInfest = as.integer(indexInfest),
timeI = as.numeric(timeI),
scale = as.numeric(1/scale),
seed = as.integer(seed))
infestH<-out$indexInfest
infestH <- infestH[which(infestH != -1)] + 1
timeH <- out$timeI
timeH <- timeH[which(infestH != -1)]
# return(list(infestH, timeH))
return(list(infestOrder=infestH,infestTime=timeH,time=out$endTime))
}
#### statistics functions
#### with their tests
# get the indexes of distance class for each pair
makeDistClasses<-function(X,Y,breaks){
cbin<-rep(0,length(breaks)-1)
CClassIndex<-dists<-rep(0,length(X)^2)
out<-.C("makeDistClasses",
xc=as.numeric(X),
L=as.integer(length(X)),
yc=as.numeric(Y),
cbin=as.integer(cbin),
CClassIndex=as.integer(CClassIndex),
dists=as.numeric(dists),
nbbreaks=as.integer(length(breaks)),
breaks=as.numeric(breaks),
maxdist = as.numeric(max(breaks))
)
return(list(dists=out$dists,
CClassIndex=out$CClassIndex,
classSize=out$cbin))
}
# make distance classes taking into account streets
makeDistClassesWithStreets<-function(X,Y, breaks, blockIndex){
# checks to avoid segfault
if(length(X)!=length(Y) || length(X) != length(blockIndex))
stop(paste("makeDistClassesWithStreets Abort\n
length(X)=",length(X),
"length(Y):",length(Y),
"length(blockIndex):",length(blockIndex)))
# declare outputs
cbin<-rep(0,length(breaks)-1)
cbinas<-rep(0, length(breaks)-1)
cbinsb<-rep(0, length(breaks)-1)
CClassIndex<-dists<-rep(0,length(X)^2)
out<-.C("makeDistClassesWithStreets",
xc=as.numeric(X),
L=as.integer(length(X)),
yc=as.numeric(Y),
cbin=as.integer(cbin),
cbinas=as.integer(cbinas),
cbinsb=as.integer(cbinsb),
CClassIndex=as.integer(CClassIndex),
dists=as.numeric(dists),
nbbreaks=as.integer(length(breaks)),
breaks=as.numeric(breaks),
maxdist = as.numeric(max(breaks)),
blockIndex = as.integer(blockIndex)
)
return(list(dists=out$dists,
CClassIndex=out$CClassIndex,
classSize=out$cbin,
classSizeSB=out$cbinsb,
classSizeAS=out$cbinas))
}
# NB: dist_indices are in C convention beginning at 0
# see makeDistClasses()
variogFromIndices<-function(CClassIndex,vectData,classSize){
stats<-rep(0,2*length(classSize))
out<-.C("modBinIt"
,n=as.integer(length(vectData))
,dist_index=as.integer(CClassIndex)
,inf_data=as.numeric(vectData)
,cbin=as.integer(classSize)
,stats=as.double(stats)
,nbins=as.integer(length(classSize)+1) # nb of breaks
)
# cat("stats",out$stats,"\n")
variog<-out$stats[1:length(classSize)]
sdvariog<-out$stats[(length(classSize)+1):length(stats)]
return(list(variog=variog,sdvariog=sdvariog))
}
# pass cumulProbMat and dist_out to make this method faster
multiGilStat<-function(cumulProbMat, blockIndex, infestH, timeH, endTime, rateMove, Nrep, coords, breaksGenVar, seed=1, simul=TRUE, getStats=TRUE, halfDistJ = -1, halfDistH = -1, useDelta = -1, delta = -1, rateHopInMove = -1, rateSkipInMove = -1, rateJumpInMove = -1, breaksStreetVar = breaksGenVar, dist_out = NULL){
# seed <- runif(1, 1, 2^31-1)
#for random seeding of stochastic simulation
indexInfest <- rep(-1, L)
timeI <- rep(-1, L)
infested <- rep(0, L)
indexInfest[1:length(infestH)] <- infestH - 1
timeI[1:length(timeH)] <- timeH
infested[infestH] <- 1
infestedDens<-rep(0,length(infested))
if(is.null(dist_out))
dist_out <- makeDistClassesWithStreets(as.vector(coords[, 1]), as.vector(coords[, 2]), breaksGenVar, blockIndex)
dist_mat <- dist_out$dists
dist_indices <- dist_out$CClassIndex
cbin <- dist_out$classSize
cbinas <- dist_out$classSizeAS
cbinsb <- dist_out$classSizeSB
useProbMat <- TRUE
# if cumulProbMat is not passed, create blank cumulProbMat for C computation
# set useProbMat to FALSE
if(is.null(cumulProbMat)){
cumulProbMat <- mat.or.vec(L, L)
useProbMat <- FALSE
}else{ # else pass dummy dist_mat so as not to take up memory space
dist_mat <- -1
}
# stats selection
# need to implement system where we can add and remove stats
###===================================
## CURRENT STATS:
## General Semivariance
## General Semivariance Std. Dev.
## Interblock Semivariance
## Interblock Semivariance Std. Dev.
## Intrablock Semivariance
## Intrablock Semivariance Std. Dev.
## By block Semivariance
## By block Semivariance Std. Dev.
## = 8 * length(cbin)
## Number Infested Houses
## Number Infested Blocks
## (Infested Houses)/(Infested Blocks)
## = 8 * length(cbin) + 3
###===================================
nbStats<- 8*length(cbin) + 3
statsTable<-mat.or.vec(nbStats,Nrep)
out<- .C("multiGilStat",
# simulation parameters
probMat = as.numeric(cumulProbMat),
useProbMat = as.integer(useProbMat),
distMat = as.numeric(dist_mat),
halfDistJ = as.numeric(halfDistJ),
halfDistH = as.numeric(halfDistH),
useDelta = as.integer(useDelta),
delta = as.numeric(delta),
rateHopInMove = as.numeric(rateHopInMove),
rateSkipInMove = as.numeric(rateSkipInMove),
rateJumpInMove = as.numeric(rateJumpInMove),
blockIndex = as.integer(blockIndex),
simul = as.integer(simul),
infested = as.integer(infested),
infestedDens = as.numeric(infestedDens),
endIndex = as.integer(length(infestH) - 1),
L = as.integer(L),
endTime = as.numeric(endTime),
indexInfest = as.integer(indexInfest),
timeI = as.numeric(timeI),
rateMove = as.numeric(rateMove),
seed = as.integer(seed),
Nrep = as.integer(Nrep),
# stats
getStats=as.integer(getStats),
nbins = as.integer(length(breaksGenVar)),
cbin = as.integer(cbin),
cbinas = as.integer(cbinas),
cbinsb = as.integer(cbinsb),
indices = as.integer(dist_indices),
statsTable = as.numeric(statsTable),
nbStats = as.integer(nbStats)
)
out$infestedDens<-out$infestedDens/Nrep;
# make matrix out of statsTable and clean away NANs introduced in C
out$statsTable<-matrix(out$statsTable,byrow=FALSE,ncol=Nrep)
# remove interblock and intrablock stats, they're causing problems
out$statsTable<-out$statsTable[c(1:11, 12:22, 67:77, 78:88, 89, 90, 91), ]
keep <- which(!is.nan(out$statsTable[, 1]))
out$statsTable<-out$statsTable[keep, ]
infestH <- out$indexInfest
infestH <- infestH[which(infestH != -1)] + 1
out$indexInfest <- infestH
timeH <- out$timeI
timeH <- timeH[which(infestH != -1)]
out$timeI <- timeH
return(out)
}
getPosteriorMaps<-function(Fit,Data,repByTheta=1){
Data$Nrep<-repByTheta
meanMap<-0*rep(0,dim(Data$maps)[1])
thetas<-as.matrix(Fit$Posterior2)
nbThetas<-dim(thetas)[1]
for(numTheta in 1:nbThetas){
theta<-thetas[numTheta,]
ModelOutBest<-Model(theta,Data,postDraw=TRUE)
meanMap<-meanMap+ModelOutBest$yhat
}
meanMap<-meanMap/nbThetas
attributes(meanMap)$nbThetas<-nbThetas
attributes(meanMap)$repByTheta<-repByTheta
return(meanMap)
}
generate_prob_mat_C <- function(halfDistJ, halfDistH, useDelta, delta, rateHopInMove, rateSkipInMove, rateJumpInMove, dist_mat, blockIndex, L, cumul=FALSE )
{
prob_mat <- mat.or.vec(L, L)
out <- .C("generateProbMat",
halfDistJ = as.numeric(halfDistJ),
halfDistH = as.numeric(halfDistH),
useDelta = as.integer(useDelta),
delta = as.numeric(delta),
rateHopInMove = as.numeric(rateHopInMove),
rateSkipInMove = as.numeric(rateSkipInMove),
rateJumpInMove = as.numeric(rateJumpInMove),
dist_mat = as.numeric(dist_mat),
prob_mat = as.numeric(prob_mat),
blockIndex = as.integer(blockIndex),
cumul = as.integer(cumul),
L = as.integer(L))
return(matrix(out$prob_mat, L, L, byrow = TRUE))
}
}
# take the out from gillespie and transform it in normal maps
# (create a binary list INFEST where 1 = infested, 0 = uninfested)
infestSerieToMaps<-function(outGillespie, sp, mapTime=outGillespie$time){
infestH <- unlist(outGillespie[1])
timeH <- unlist(outGillespie[2])
# reset times above mapTime
indLastInf<-max(which(timeH<=mapTime))
timeH<-timeH[1:indLastInf]
infestH<-infestH[1:indLastInf]
# make the map
map<-as.data.frame(sp)
names(map)<-c("X","Y")
map$ages <- map$infest<-rep(0, length(sp[,1]))
map$infest[infestH] <- 1
map$ages[infestH]<-mapTime-timeH
return(map)
}
updatePredict<-function(Fit,Data,infestHints,repByTheta=1000){
# make a lot of simulations and select the ones with same house
# initmap
# or same block (to add: ,sameBlockApprox=FALSE)
Data$Nrep<-repByTheta
meanMap<-0*rep(0,dim(Data$maps)[1])
thetas<-as.matrix(Fit$Posterior2)
nbThetas<-dim(thetas)[1]
for(numTheta in 1:nbThetas){
theta<-thetas[numTheta,]
ModelOutBest<-Model(theta,Data,postDraw=TRUE,infestHints)
meanMap<-meanMap+ModelOutBest$yhat
}
meanMap<-meanMap/nbThetas
attributes(meanMap)$nbThetas<-nbThetas
attributes(meanMap)$repByTheta<-repByTheta
outBase <- multiGilStat(cumulProbMat=cumulProbMat, blockIndex, infestH, timeH=rep(-1,length(infestH)), endTime = nbit, rateMove, Nrep, coords = maps[, c("X", "Y")], breaks = genIntervals, simul=TRUE)
return(meanMap)
}
getBestPredict<-function(maps,cumulProbMat,init,nbit=52*2,Nrep=1000){
infestH<-which(init==1)
outBase <- multiGilStat(cumulProbMat=cumulProbMat, blockIndex=maps$blockIndex, infestH, timeH=rep(-1,length(infestH)), endTime = nbit, rateMove, Nrep, coords = maps[, c("X", "Y")], breaks = genIntervals, simul=TRUE,getStats=FALSE)
return(postMap)
}
# Tests
test_file("test-functions_migration.R")