@@ -320,7 +320,8 @@ rdiffnet <- function(
320
320
exposure.args = list (),
321
321
name = " A diffusion network" ,
322
322
behavior = " Random contagion" ,
323
- stop.no.diff = TRUE
323
+ stop.no.diff = TRUE ,
324
+ disadopt = NULL
324
325
) {
325
326
326
327
# Checking options
@@ -451,6 +452,8 @@ rdiffnet <- function(
451
452
# Step 3.0: Running the simulation -------------------------------------------
452
453
453
454
for (i in 2 : t ) {
455
+
456
+ # 3.1 Computing exposure
454
457
if (exists(" attrs_arr" )){
455
458
exposure.args [c(" attrs" )] <- list (attrs_arr [,i , ,drop = FALSE ])
456
459
}
@@ -460,24 +463,62 @@ rdiffnet <- function(
460
463
461
464
for (q in 1 : num_of_behaviors ) {
462
465
466
+ # 3.2 Identifying who adopts based on the threshold
463
467
whoadopts <- which( (expo [,,q ] > = thr [,q ]) )
468
+
469
+ # 3.3 Updating the cumadopt
464
470
cumadopt [whoadopts , i : t , q ] <- 1L
465
- # ADD SOMETHING TO DISADOPT
466
471
472
+ # 3.4` Updating the toa
473
+ # toa[cbind(whoadopts, q)] <- i
467
474
toa [, q ] <- apply(cumadopt [,, q ], 1 , function (x ) {
468
475
first_adopt <- which(x == 1 )
469
476
if (length(first_adopt ) > 0 ) first_adopt [1 ] else NA
470
477
})
471
478
472
479
}
480
+
481
+ if (length(disadopt )) {
482
+
483
+ # Run the disadoption algorithm. This will return the following:
484
+ # - A list of length q with the nodes that disadopted
485
+ disadopt_res <- disadopt(expo , cumadopt , i )
486
+
487
+ for (q in seq_along(disadopt_res )) {
488
+
489
+ # So only doing this is there's disadoption
490
+ if (length(disadopt_res [[q ]]) == 0 )
491
+ next
492
+
493
+ # Checking this makes sense (only adopters can disadopt)
494
+ q_adopters <- which(! is.na(toa [, q ]))
495
+
496
+ if (length(setdiff(disadopt_res [[q ]], q_adopters )) > 0 )
497
+ stop(" Some nodes that disadopted were not adopters." )
498
+
499
+ # Updating the cumadopt
500
+ cumadopt [disadopt_res [[q ]], i : t , q ] <- 0L
501
+
502
+ # Updating toa
503
+ toa [cbind(disadopt_res [[q ]], q )] <- NA
504
+
505
+ }
506
+
507
+
508
+ }
473
509
}
474
510
475
511
for (i in 1 : num_of_behaviors ) {
476
512
reachedt <- max(toa [,i ], na.rm = TRUE )
477
513
478
514
if (reachedt == 1 ) {
479
515
if (stop.no.diff )
480
- stop(paste(" No diffusion in this network for behavior" , i , " (Ups!) try changing the seed or the parameters." ))
516
+ stop(
517
+ paste(
518
+ " No diffusion in this network for behavior" , i ,
519
+ " (Ups!) try changing the seed or the parameters."
520
+ )
521
+ )
481
522
else
482
523
warning(paste(" No diffusion for behavior" , i , " in this network." ))
483
524
}
0 commit comments