|
| 1 | +--- |
| 2 | +title: "Introduction to the package" |
| 3 | +author: "George G. Vega Yon" |
| 4 | +date: "2024-12-05" |
| 5 | +date-modified: "2024-12-05" |
| 6 | +--- |
| 7 | + |
| 8 | +```{r setup, echo=FALSE, message=FALSE, warning=FALSE} |
| 9 | +library(netdiffuseR) |
| 10 | +knitr::opts_chunk$set(comment = "#") |
| 11 | +``` |
| 12 | + |
| 13 | + |
| 14 | +* One of the cannonical concepts is the **network threshold**. Network thresholds (Valente, 1995; 1996), $\tau$, are defined as the required proportion or number of neighbors that leads you to adopt a particular behavior (innovation), $a=1$. In (very) general terms\pause |
| 15 | + |
| 16 | + $$ |
| 17 | + a_i = \left\{\begin{array}{ll} |
| 18 | + 1 &\mbox{if } \tau_i\leq E_i \\ |
| 19 | + 0 & \mbox{Otherwise} |
| 20 | + \end{array}\right. \qquad |
| 21 | + E_i \equiv \frac{\sum_{j\neq i}\mathbf{X}_{ij}a_j}{\sum_{j\neq i}\mathbf{X}_{ij}} |
| 22 | + $$ |
| 23 | + |
| 24 | + Where $E_i$ is i's exposure to the innovation and $\mathbf{X}$ is the adjacency matrix (the network). |
| 25 | + |
| 26 | +* This can be generalized and extended to include covariates and other network weighting schemes (that's what __netdiffuseR__ is all about). |
| 27 | + |
| 28 | +# netdiffuseR |
| 29 | + |
| 30 | +## Overview |
| 31 | + |
| 32 | +__netdiffuseR__ is an R package that: |
| 33 | + |
| 34 | +* Is designed for Visualizing, Analyzing and Simulating network diffusion data (in general). |
| 35 | + |
| 36 | +* Depends on some pretty popular packages: |
| 37 | + |
| 38 | + * _RcppArmadillo_: So it's fast, |
| 39 | + * _Matrix_: So it's big, |
| 40 | + * _statnet_ and _igraph_: So it's not from scratch |
| 41 | + |
| 42 | +* Can handle big graphs, e.g., an adjacency matrix with more than 4 billion elements (PR for RcppArmadillo) |
| 43 | + |
| 44 | +* Already on CRAN with ~6,000 downloads since its first version, Feb 2016, |
| 45 | + |
| 46 | +* A lot of features to make it easy to read network (dynamic) data, making it a nice companion of other net packages. |
| 47 | + |
| 48 | + |
| 49 | +## Datasets |
| 50 | + |
| 51 | +- __netdiffuseR__ has the three classic Diffusion Network Datasets: |
| 52 | + |
| 53 | + - `medInnovationsDiffNet` Doctors and the innovation of Tetracycline (1955). |
| 54 | + - `brfarmersDiffNet` Brazilian farmers and the innovation of Hybrid Corn Seed (1966). |
| 55 | + - `kfamilyDiffNet` Korean women and Family Planning methods (1973). |
| 56 | + |
| 57 | + ```{r printing} |
| 58 | + brfarmersDiffNet |
| 59 | + medInnovationsDiffNet |
| 60 | + kfamilyDiffNet |
| 61 | + ``` |
| 62 | + |
| 63 | +## Visualization methods |
| 64 | + |
| 65 | +```{r viz, cache=TRUE, eval=TRUE} |
| 66 | +set.seed(12315) |
| 67 | +x <- rdiffnet( |
| 68 | + n = 400, t = 6, |
| 69 | + rgraph.args = list(k=6, p=.3), |
| 70 | + seed.graph = "small-world", |
| 71 | + seed.nodes = "central", |
| 72 | + rewire = FALSE, |
| 73 | + threshold.dist = 1/4 |
| 74 | + ) |
| 75 | +x |
| 76 | +``` |
| 77 | + |
| 78 | +Diffusion networks can visualized using many methods included in the package. Here are some of them: |
| 79 | + |
| 80 | + |
| 81 | +```{r} |
| 82 | +#| label: plot-methods |
| 83 | +plot(x) |
| 84 | +plot_diffnet(x) |
| 85 | +plot_diffnet2(x) |
| 86 | +plot_adopters(x) |
| 87 | +plot_threshold(x) |
| 88 | +plot_infectsuscep(x, K=2) |
| 89 | +plot_hazard(x) |
| 90 | +``` |
| 91 | + |
| 92 | + |
| 93 | +# Problems |
| 94 | + |
| 95 | +1. Using the diffnet object in [`intro.rda`](intro.rda), use the function `plot_threshold` specifying shapes and colors according to the variables ItrustMyFriends and Age. Do you see any pattern? (<a href="intro-solutions.r" target="_blank">solution script</a> and <a href="intro-solutions.png" target="_blank">solution plot</a>) |
| 96 | + |
| 97 | +```{r datasim, echo=FALSE, eval=TRUE} |
| 98 | +set.seed(1252) |
| 99 | +dat <- data.frame( |
| 100 | + ItrustMyFriends = sample(c(0,1), 200, TRUE), |
| 101 | + Age = 10 + rpois(200, 4) |
| 102 | + ) |
| 103 | +net <- rgraph_er(200, p = .05) |
| 104 | +# net <- diag_expand(list(net, net)) |
| 105 | +# net[cbind(1:20, 101:120)] <- 1 |
| 106 | +
|
| 107 | +# Generating the process |
| 108 | +diffnet <- rdiffnet( |
| 109 | + threshold.dist = 4 - dat$ItrustMyFriends*3, |
| 110 | + seed.graph = net, |
| 111 | + t=6, |
| 112 | + seed.nodes = c(9:25), |
| 113 | + exposure.args = list(normalized=FALSE), |
| 114 | + rewire = FALSE) |
| 115 | +
|
| 116 | +diffnet[["ItrustMyFriends"]] <- dat$ItrustMyFriends |
| 117 | +diffnet[["Age"]] <- dat$Age |
| 118 | +
|
| 119 | +save(diffnet, file = "intro.rda") |
| 120 | +``` |
| 121 | + |
| 122 | + |
0 commit comments