R-CMD-check Build status codecov.io CRAN_Status_Badge DOI Dependencies USC's Department of Preventive Medicine

netdiffuseR: Analysis of Diffusion and Contagion Processes on Networks

This package contains functions useful for analyzing network data for diffusion of innovations applications.

The package was developed as part of the paper Thomas W. Valente, Stephanie R. Dyal, Kar-Hai Chu, Heather Wipfli, Kayo Fujimoto, Diffusion of innovations theory applied to global tobacco control treaty ratification, Social Science & Medicine, Volume 145, November 2015, Pages 89-97, ISSN 0277-9536 (available here)

From the description:

Empirical statistical analysis, visualization and simulation of diffusion and contagion processes on networks. The package implements algorithms for calculating network diffusion statistics such as transmission rate, hazard rates, exposure models, network threshold levels, infectiousness (contagion), and susceptibility. The package is inspired by work published in Valente, et al., (2015); Valente (1995), Myers (2000), Iyengar and others (2011), Burt (1987); among others.

Acknowledgements: netdiffuseR was created with the support of grant R01 CA157577 from the National Cancer Institute/National Institutes of Health.

citation(package="netdiffuseR")

News

Changelog can be view here.

Installation

CRAN version

To get the CRAN (stable) version of the package, simple type

install.packages("netdiffuseR")

Bleeding edge version

If you want the latest (unstable) version of netdiffuseR, using the devtools package, you can install netdiffuseR dev version as follows

devtools::install_github('USCCANA/netdiffuseR', build_vignettes = TRUE)

You can skip building vignettes by setting build_vignettes = FALSE (so it is not required).

For the case of OSX users, there seems to be a problem when installing packages depending on Rcpp. This issue, developed here, can be solved by open the terminal and typing the following

curl -O http://r.research.att.com/libs/gfortran-4.8.2-darwin13.tar.bz2
sudo tar fvxz gfortran-4.8.2-darwin13.tar.bz2 -C /

before installing the package through devtools.

Binary versions

For the case of windows and mac users, they can find binary versions of the package here, netdiffuseR_1...zip, and netdiffuseR_1...tgz respectively. They can install this directly as follows (using the 1.16.3.29 version):

  1. Install dependencies from CRAN r > install.packages(c("igraph", "Matrix", "SparseM", "RcppArmadillo", "sna"), dependencies=TRUE)

  2. Download the binary version and install it as follows:

    ``` r

    install.packages("netdiffuseR_1.16.3.29.zip", repos=NULL) ```

    For windows users, and for Mac users:

    ``` r

    install.packages("netdiffuseR_1.16.3.29.tgz", repos=NULL) ```

Tutorials

Since starting netdiffuseR, we have done a couple of workshops at Sunbelt and NASN. Here are the repositories:

Presentations

Examples

This example has been taken from the package's vignettes:

library(netdiffuseR)

Infectiousness and Susceptibility

# Generating a random graph
set.seed(1234)
n <- 100
nper <- 20
graph <- rgraph_er(n, nper, .5)
toa <- sample(c(1:(1+nper-1), NA), n, TRUE)
head(toa)

# Creating a diffnet object
diffnet <- as_diffnet(graph, toa)
diffnet
summary(diffnet)

# Visualizing distribution of suscep/infect
out <- plot_infectsuscep(diffnet, bins = 20,K=5, logscale = FALSE, h=.01)
out <- plot_infectsuscep(diffnet, bins = 20,K=5, logscale = TRUE,
                         exclude.zeros = TRUE, h=1)

Threshold

# Generating a random graph
set.seed(123)
diffnet <- rdiffnet(500, 20,
                    seed.nodes = "random",
                    rgraph.args = list(m=3),
                    threshold.dist = function(x) runif(1, .3, .7))
diffnet

# Threshold with fixed vertex size
plot_threshold(diffnet)

Using more features

data("medInnovationsDiffNet")
set.seed(131)
plot_threshold(
  medInnovationsDiffNet,
  vertex.color     = viridisLite::inferno(4)[medInnovationsDiffNet[["city"]]],
  vertex.sides     = medInnovationsDiffNet[["city"]] + 2,
  sub = "Note: Vertices' sizes and shapes given by degree and city respectively",
  jitter.factor = c(1,1), jitter.amount = c(.25,.025)
)

Adoption rate

plot_adopters(diffnet)

Hazard rate

hazard_rate(diffnet)

Diffusion process

plot_diffnet(medInnovationsDiffNet, slices=c(1,9,8))
diffnet.toa(brfarmersDiffNet)[brfarmersDiffNet$toa >= 1965] <- NA
plot_diffnet2(brfarmersDiffNet, vertex.size = "indegree")
set.seed(1231)

# Random scale-free diffusion network
x <- rdiffnet(1000, 4, seed.graph="scale-free", seed.p.adopt = .025,
                           rewire = FALSE, seed.nodes = "central",
                           rgraph.arg=list(self=FALSE, m=4),
                           threshold.dist = function(id) runif(1,.2,.4))

# Diffusion map (no random toa)
dm0 <- diffusionMap(x, kde2d.args=list(n=150, h=1), layout=igraph::layout_with_fr)

# Random
diffnet.toa(x) <- sample(x$toa, size = nnodes(x))

# Diffusion map (random toa)
dm1 <- diffusionMap(x, layout = dm0$coords, kde2d.args=list(n=150, h=.5))

oldpar <- par(no.readonly = TRUE)
col <- viridisLite::plasma(100)
par(mfrow=c(1,2), oma=c(1,0,0,0), cex=.8)
image(dm0, col=col, main="Non-random Times of Adoption\nAdoption from the core.")
image(dm1, col=col, main="Random Times of Adoption")
par(mfrow=c(1,1))
mtext("Both networks have the same distribution on times of adoption", 1,
      outer = TRUE)
par(oldpar)

Adopters classification

out <- classify(kfamilyDiffNet, include_censored = TRUE)
ftable(out)

# Plotting 
oldpar <- par(no.readonly = TRUE)
par(xpd=TRUE)
plot(out, color=viridisLite::inferno(5), las = 2, xlab="Time of Adoption",
     ylab="Threshold", main="")

# Adding key
legend("bottom", legend = levels(out$thr), fill=viridisLite::inferno(5), horiz = TRUE,
       cex=.6, bty="n", inset=c(0,-.1))
par(oldpar)

Session info

sessionInfo()

To-do list



USCCANA/netdiffuseR documentation built on Sept. 5, 2023, 12:31 a.m.