-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy path2-read.qmd
More file actions
291 lines (215 loc) · 7.89 KB
/
2-read.qmd
File metadata and controls
291 lines (215 loc) · 7.89 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
---
title: "Reading Data"
author:
- Thomas W. Valente
- George G. Vega Yon
- Aníbal Olivera M.
date: "2025-06-24"
date-modified: "2025-06-24"
---
```{r setup, echo=FALSE, warning=FALSE, message=FALSE}
knitr::opts_chunk$set(comment = "#")
library(netdiffuseR)
library(igraph)
library(networkDynamic)
```
# Basic Diffusion Network (as_diffnet)
* To create `diffnet` objects we only need a network and times of adoption:
```{r 2-static-net}
set.seed(9)
# Network
net <- rgraph_ws(500, 4, .2)
# Times of adoption
toa <- sample(c(NA, 2010:2014), 500, TRUE)
diffnet_static <- as_diffnet(net, toa)
summary(diffnet_static)
```
# Dynamic survey (survey_to_diffnet)
The package can also read dynamic survey data, i.e., data that has a time variable and a time of adoption variable. The function `survey_to_diffnet` is used to convert survey data into a `diffnet` object.
```{r 2-static-dynsurvey}
data("fakesurveyDyn")
fakesurveyDyn
```
```{r}
#| label: 2-static-dynsurvey-cont1
#| warning: false
#| message: false
diffnet_dynsurvey <- survey_to_diffnet(
dat = fakesurveyDyn,
idvar = "id",
netvars = c("net1", "net2", "net3"),
groupvar = "group",
toavar = "toa",
timevar = "time"
)
plot_diffnet(diffnet_dynsurvey)
```
# Datasets in netdiffuseR (surveys)
- **netdiffuseR** has the three classic Diffusion Network Datasets (as surveys):
- `medInnovations` Doctors and the innovation of Tetracycline (1955).
- `brfarmers` Brazilian farmers and the innovation of Hybrid Corn Seed (1966).
- `kfamily` Korean women and Family Planning methods (1973).
Let's have a look to `kfamily`:
```{r}
#| label: kfamily-survey
#| warning: false
#| message: false
data(kfamily)
# The data contains adoption information of 25 villages:
unique(kfamily$village)
# across 10 time steps (toa = 11 means 'no adoption')
sort(unique(kfamily$toa))
```
We can construct a `diffnet` object from from those survey data:
```{r}
#| label: kfamily-survey-to-diffnet
#| warning: false
#| message: false
kfamily_diffnet <- survey_to_diffnet(
dat = kfamily,
idvar = "id",
netvars = c(
# Neighbors talk to about FP
"net11", "net12", "net13", "net14", "net15",
# Closest neighbor most frequently met
"net21", "net22", "net23", "net24", "net25",
# Advice on FP sought from
"net31", "net32", "net33", "net34", "net35"),
toavar = "toa",
groupvar = "village"
)
kfamily_diffnet
summary(kfamily_diffnet)
```
We can calculate direct exposure (cohesion) to an innovation using `exposure()`,
```{r}
#| label: kfamily-exposure
#| warning: false
#| message: false
# Computing exposure
coh <- exposure(kfamily_diffnet)
# See results
head(round(coh, 2))
```
and also indirect influence (structural equivalence):
```{r}
#| label: kfamily-se
#| warning: false
#| message: false
# Computing structural equivalence
se <- exposure(kfamily_diffnet,
alt.graph="se", # select 'structural equivalence'
groupvar="village", # separately by community
valued=TRUE # to account for weights
)
# See results
head(round(se, 2))
```
The `diffnet` object also contains attributes of the vertices, which can be retrieved using `diffnet.attrs()`:
```{r}
#| label: kfamily-diffnet-attrs
#| warning: false
#| message: false
# Retrieving attributes as data.frame
kfamily_diffnet.df <- diffnet.attrs(kfamily_diffnet, as.df = TRUE)
# Subset to relevant variables
kfamily_relevant_vars <- kfamily_diffnet.df[, c("per", "toa", "village")]
# Select 10 random rows
kfamily_relevant_vars[sample(nrow(kfamily_relevant_vars), 10), ]
```
# Problems
1. Using the rda file [read.rda](files/read.rda), read in the edgelist `net_edgelist` and the adjacency
matrix `net_list` as a diffnet objects. In both cases you should use the data.frame `X`
which has the time of adoption variable.
(<a href="files/read-solutions.r" target="_blank">solution script</a>)
```{r 2-generates-data, echo=FALSE, cache=TRUE, warning=FALSE, message=FALSE, results='hide'}
library(igraph)
library(networkDynamic)
# source("diffnet_to_network.r")
set.seed(81231)
# Generating random data
net_list <- rgraph_er(20, 5)
net_edgelist <- adjmat_to_edgelist(net_list)
X <- data.frame(
idnum = rep(1:20, 5),
var1 = rnorm(20*5),
YearAdopt = rep(sample(c(NA, 1:5), 20, TRUE), 5),
year = sort(rep(1:5, 20))
)
# Storing the data
save(X, net_edgelist, net_list, file = "files/read.rda")
```
<!-- 2. With the new diffnet object, apply the same analysis as before. -->
<!-- Which strategy maximizes adoption? -->
# Appendix
## Diffusion Network Object (diffnet)
- Most of the package's functions accept different types of graphs:
* Static: `matrix`, `dgCMatrix` (from the __Matrix__ pkg),
* Dynamic: `list` + `dgCMatrix`, `array`, `diffnet`
- __netdiffuseR__ has its own class of objects: `diffnet`, from which you get the most.
- From __netdiffuseR__'s perspective, network data comes in three classes:
1. Raw R network data: Datasets with edgelist, attributes, survey data, etc.
2. Already R data: already read into R using igraph, statnet, etc. (`igraph_to_diffnet`, `network_to_diffnet`, etc.)
3. Graph files: DL, UCINET, pajek, etc. (`read_pajek`, `read_dl`, `read_ucinet`, etc.)
- In this presentation we will show focus on 1.
## What is a (diffnet) object
A diffusion network, a.k.a. `diffnet` object, is a `list` that holds the following objects:
- `graph`: A `list` with $t$ `dgCMatrix` matrices of size $n\times n$,
- `toa`: An integer vector of length $n$,
- `adopt`: A matrix of size $n\times t$,
- `cumadopt`: A matrix of size $n\times t$,
- `vertex.static.attrs`: A `data.frame` of size $n\times k$,
- `vertex.dyn.attrs`: A list with $t$ dataframes of size $n\times k$,
- `graph.attrs`: Currently ignored..., and
- `meta`: A list with metadata about the object.
These are created using `new_diffnet` (or its wrappers).
## Static survey (survey_to_diffnet)
* netdiffuseR can also read survey (nomination) data:
```{r 2-static-survey}
data("fakesurvey")
fakesurvey
```
* In group one, id 4 nominates id 6, who does not show in the data, and in group two id 1 nominates 3, 4, and 8, also individuals who don't show up in the survey.
```{r 2-static-survey-cont1}
d1 <- survey_to_diffnet(
dat = fakesurvey, # Dataset
idvar = "id", # The name of the idvar
netvars = c("net1", "net2", "net3"), # Name of the nomination variables
groupvar = "group", # Group variable (if any)
toavar = "toa" # Name of the time of adoption variable
); d1
```
* If you want to include those, you can use the option `no.unsurveyed`
```{r}
d2 <- survey_to_diffnet(
dat = fakesurvey,
idvar = "id",
netvars = c("net1", "net2", "net3"),
groupvar = "group",
toavar = "toa",
no.unsurveyed = FALSE
); d2
```
* We can also check the difference
```{r}
d2 - d1
rownames(d2 - d1)
```
## Other network formats
* The package also supports working with other network formats.
* Besides of `.net` (Pajek), and `ml` (UCINET), netdiffuseR can actually
convert between classes: `igraph`, `network`, and `networkDynamic`.
```{r foreign, warnings=FALSE, messages=FALSE, cache=TRUE}
data("medInnovationsDiffNet")
dn_ig <- diffnet_to_igraph(medInnovationsDiffNet)
# dn_ig # For some issue with lazy eval, knitr won't print this
dn_net <- diffnet_to_network(medInnovationsDiffNet)
dn_net[[1]]
dn_ndy <- diffnet_to_networkDynamic(medInnovationsDiffNet)
dn_ndy
```
First two examples it creates a list of objects, the later actually
creates a single object
```{r networkDyn}
networkDynamic_to_diffnet(dn_ndy, toavar = "toa")
```