# Simulating diffusion networks: Using the `rdiffnet` function In netdiffuseR: Analysis of Diffusion and Contagion Processes on Networks

```library(knitr)
knitr::opts_chunk\$set(fig.width=9, fig.height=6, out.width="600px",fig.align = "center")
```

# Simulating diffusion networks: Using the `rdiffnet` function

In this example we compare 3 different simulations that use the same baseline (seed) network, a scale-free generated via `rgraph_ba` (Barabasi-Albert) with parameter `m=4` (number of new ties that each added node includes in the graph). The only difference between the three simulations is that we use a different set of seed adopters, "random", "central" and "marginal". All three cases start with 5% of the network having adopted the innovation.

```library(netdiffuseR)
s <- 11532
set.seed(s)
diffnet_ran <- rdiffnet(200, 20, "random", seed.p.adopt = .1,
seed.graph = "small-world",
rgraph.args = list(undirected=FALSE, k=4, p=.5),
threshold.dist = function(x) 0.3)

set.seed(s)
diffnet_cen <- rdiffnet(200, 20, "central", seed.p.adopt = .1,
seed.graph = "small-world",
rgraph.args = list(undirected=FALSE, k=4, p=.5),
threshold.dist = function(x) 0.3)

set.seed(s)
diffnet_mar <- rdiffnet(200, 20, "marginal", seed.p.adopt = .1,
seed.graph = "small-world",
rgraph.args = list(undirected=FALSE, k=4, p=.5),
threshold.dist = function(x) 0.3)
```

Furthermore, we can take a more detail view of what's going on in each network using the `summary` method. For example, lets take a look at the marginal network:

```summary(diffnet_mar)
```

At a first look, printing the networks, we can see that they differ in the number of adopters, as the adoption rate shows:

```diffnet_ran; diffnet_cen; diffnet_mar
```

So, as expected, the network that used central nodes as first adopters is the one that reached the highest adoption rate, 0.95; meanwhile the network that used marginal nodes as seed has the lowest adoption rate, 0.56. Lets compare the set of initial adopters graphically

```cols <- c("lightblue","green", "blue")

par(mfcol=c(1,3), mai = c(0, 0, 1, 0), mar = rep(1, 4) +  0.1)
set.seed(s);plot(diffnet_ran, main="Random seed")
set.seed(s);plot(diffnet_cen, main="Central seed")
coords <- set.seed(s);plot(diffnet_mar, main="Marginal seed")
par(oldpar)
```

An interesting way of visualizing the diffusion process is using the `plot_diffnet` function. In this case, instead of plotting all the 20 periods (networks), we only focus on a subset (henceforth we use the `slices` argument).

```plot_diffnet(diffnet_ran, slices = c(1,4,8,12,16,20), layout=coords)
```

## Diffusion process

An easy way to compare these three networks is by checking the cumulative adoption counts, in particular, the proportion. Using the function `plot_adopters` we can achieve our goal

```plot_adopters(diffnet_ran, bg = cols[1], include.legend = FALSE, what="cumadopt")

legend("topleft", bty="n",
legend = c("Random","Central", "Marginal"),
fill=cols)
```

Comparing hazard rates we can do the following

```plot_hazard(diffnet_ran, ylim=c(0,1), bg=cols[1])

legend("topleft", bty="n",
legend = c("Random","Central", "Marginal"),
fill=cols)
```

Furthermore, we can calculate infectiousness and susceptibility on each network and see whether both are correlated in each one of the processess.

```plot_infectsuscep(diffnet_ran, bins=15, K=3,
main = "Distribution of Infectiousness and\nSusceptibility (Random)")
plot_infectsuscep(diffnet_cen, bins=15, K=3,
main = "Distribution of Infectiousness and\nSusceptibility (Central)")
plot_infectsuscep(diffnet_mar, bins=15, K=3,
main = "Distribution of Infectiousness and\nSusceptibility (Marginal)")
```
```plot_threshold(diffnet_ran)
```

## Multiple simulations using `rdiffnet_multiple`

The `rdiffnet_multiple` is a wrapper of `rdiffnet` that allows performing simulation studies. In particular, the user can defined a set of shared parameters across simulations and retrieve one or more statistics from each one of them. The followin example is included in the manual of the function:

```# Simulating a diffusion process with all the defaults but setting
# -seed.nodes- to be random
set.seed(1)
ans0 <- rdiffnet_multiple(R=50, statistic=function(x) sum(!is.na(x\$toa)),
n = 100, t = 4, seed.nodes = "random", stop.no.diff=FALSE)

# Simulating a diffusion process with all the defaults but setting
# -seed.nodes- to be central
set.seed(1)
ans1 <- rdiffnet_multiple(R=50, statistic=function(x) sum(!is.na(x\$toa)),
n = 100, t = 4, seed.nodes = "central", stop.no.diff=FALSE)

boxplot(cbind(Random = ans0, Central = ans1),
main="Distribution of number of adopters in\ndifferent seedscenarios",
sub = "(50 simulations each)", ylab="Number of adopters")
```

## Try the netdiffuseR package in your browser

Any scripts or data that you put into this service are public.

netdiffuseR documentation built on Nov. 17, 2017, 3:59 a.m.