---
title: "Simulating diffusion networks: Using the `rdiffnet` function"
author: "George G. Vega Yon"
date: "November 17, 2015"
output: rmarkdown::html_vignette
header-includes: \usepackage{graphicx}
vignette: >
  %\VignetteIndexEntry{Simulating diffusion networks: Using the `rdiffnet` function}
  %\VignetteEngine{knitr::rmarkdown}
  %\usepackage[utf8]{inputenc}
---

```{r Setup, echo=FALSE}
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.

```{r Simulating diffnets}
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:

```{r}
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:

```{r Printing the networks}
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

```{r Seed graph and initial adopters, message=FALSE, fig.height=4}
cols <- c("lightblue","green", "blue")

oldpar <- par(no.readonly = TRUE)
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).

```{r}
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

```{r Cumulative adopt count}
plot_adopters(diffnet_ran, bg = cols[1], include.legend = FALSE, what="cumadopt")
plot_adopters(diffnet_cen, bg = cols[2], add=TRUE, what="cumadopt")
plot_adopters(diffnet_mar, bg = cols[3], add=TRUE, what="cumadopt")

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

Comparing hazard rates we can do the following

```{r Hazard rate}
plot_hazard(diffnet_ran, ylim=c(0,1), bg=cols[1])
plot_hazard(diffnet_cen, add=TRUE, bg=cols[2])
plot_hazard(diffnet_mar, add=TRUE, bg=cols[3])

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.

```{r Infection and susceptibility}
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)")
```



```{r Threshold}
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:

```{r Multiple-simulations}
# 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")
```

