-
Notifications
You must be signed in to change notification settings - Fork 0
/
netdiffuseR_sunbelt2016_workshop_reading_data.Rmd
417 lines (320 loc) · 11.6 KB
/
netdiffuseR_sunbelt2016_workshop_reading_data.Rmd
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
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
---
title: |
| Understanding Diffusion with __netdiffuseR__
| _Reading data_
subtitle: Sunbelt 2016 INSNA
author: George Vega Yon \and Thomas Valente
date: |
| Newport Beach, CA
| April 5, 2016
institute: |
| Department of Preventive Medicine
| University of Southern California
header-includes: \usepackage{grffile}
output: beamer_presentation
fontsize: 10pt
---
```{r Config-knitr, echo=FALSE, warning=FALSE, message=FALSE}
knitr::opts_chunk$set(
out.width = ".8\\textwidth", dev = "pdf", fig.align = "center",
fig.width = 7, fig.height = 5)
pdf.options(family="Palatino")
options(width = 100)
library(sna)
library(SparseM)
library(netdiffuseR)
```
# Introduction
## Data in __netdiffuseR__
- __netdiffuseR__ has its own class of objects: `diffnet`.
- Most of the package's functions accept different types of graphs:
* Static: `matrix`, `dgCMatrix` (from the __Matrix__ pkg),
* Dynamic: `list` + `dgCMatrix`, `array`, `diffnet`
- But `diffnet` is the class 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.
# Introduction
## `diffnet` objects
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 `as_diffnet` (or its wrappers).
------
\huge Example 1: Nomination networks (cross-section data) \normalsize
# Example 1: Nomination networks (cross-section data)
- In this part we review the function `survey_to_diffnet`
- This function can use as input either a longitudinal dataset (which should be in long format, this is, one row per individual and time period), or a cross sectional dataset.
- For the first example we will use the `fakesurvey` dataset, which holds cross section data.
# Example 1: Nomination networks (cross-section data)
We start by taking a look at the data.
\tiny
```{r split=FALSE}
# Loading the data
data("fakesurvey")
fakesurvey
```
\normalsize
In group one 4 nominates id 6, who does not show in the data, and in group two 1 nominates 3, 4, and 8, also individuals who don't show up in the survey.
# Example 1: Nomination networks (cross-section data)
## Not including unsurveyed
Reading the data into __netdiffuseR__ with the option `no.unsurveyed = TRUE`
\tiny
```{r}
# Coercing the survey data into a diffnet object
diffnet_wo_unsurveyed <- survey_to_diffnet(
dat = fakesurvey, # The dataset
idvar = "id", # Name of the idvar (must be integer)
netvars = c("net1", "net2", "net3"), # Vector of names of nomination vars
toavar = "toa", # Name of the time of adoption var
groupvar = "group", # Name of the group var (OPTIONAL)
no.unsurveyed = TRUE # KEEP OR NOT UNSURVEYED
)
diffnet_wo_unsurveyed
# Retrieving nodes ids
nodes(diffnet_wo_unsurveyed)
```
\normalsize
# Example 1: Nomination networks (cross-section data)
## ~~Not~~ including unsurveyed
Reading the data into __netdiffuseR__ with the option `no.unsurveyed = FALSE`
\tiny
```{r}
# Coercing the survey data into a diffnet object
diffnet_w_unsurveyed <- survey_to_diffnet(
dat = fakesurvey, # The dataset
idvar = "id", # Name of the idvar (must be integer)
netvars = c("net1", "net2", "net3"), # Vector of names of nomination vars
toavar = "toa", # Name of the time of adoption var
groupvar = "group", # Name of the group var (OPTIONAL)
no.unsurveyed = FALSE # KEEP OR NOT UNSURVEYED
)
diffnet_w_unsurveyed
# Retrieving nodes ids
nodes(diffnet_w_unsurveyed)
```
\normalsize
# Example 1: Nomination networks (cross-section data)
Furthermore, we can compare the two diffusion networks by subtracting one from another:
\footnotesize
```{r}
difference <- diffnet_w_unsurveyed - diffnet_wo_unsurveyed
difference
```
\normalsize
------
\huge Example 2: Nomination networks (longitudinal data) \normalsize
# Example 2: Nomination networks (longitudinal data)
- Keep using `survey_to_diffnet`
- Now we will load panel data!
- For the first example we will use the `fakesurveyDyn` dataset, which holds cross section data.
# Example 2: Nomination networks (longitudinal data)
We start by taking a look at the data.
\tiny
```{r split=FALSE}
# Loading the data
data("fakesurveyDyn")
fakesurveyDyn
```
\normalsize
In group one 4 nominates id 6, who does not show in the data, and in group two 1 nominates 3, 4, and 8, also individuals who don't show up in the survey.
# Example 2: Nomination networks (longitudinal data)
## Reading the data-in
\tiny
```{r}
dyndiffnet <- survey_to_diffnet(
dat = fakesurveyDyn, # The dataset
idvar = "id", # Name of the idvar (must be integer)
netvars = c("net1", "net2", "net3"), # Vector of names of nomination vars
toavar = "toa", # Name of the time of adoption var
groupvar = "group", # Name of the group var (OPTIONAL)
no.unsurveyed = TRUE, # keep or not unsurveyed
timevar = "time" # This is new!
)
dyndiffnet
```
\normalsize
# Example 2: Nomination networks (longitudinal data)
## Quick look at some attributes
\tiny
```{r}
# As a list
dyndiffnet[["age"]]
# As a data frame!
dyndiffnet[["age", as.df=TRUE]]
```
\normalsize
# Example 2: Nomination networks (longitudinal data)
## Quick look at the dynamic graph
\tiny
```{r, fig.width=12, fig.height=4}
plot_diffnet(dyndiffnet, vertex.cex = 1.5, displaylabels = TRUE)
```
\normalsize
------
\huge Example 3: Edgelist (cross-section data) \normalsize
# Example 3: Edgelist (cross-section data)
- Now we will use an edgelist as an input.
- For this example we'll use the `fakeEdgelist` dataset.
- We are also using the `fakesurvey` dataset as it holds the attributes.
- Now is turn of the function `edgelist_to_diffnet` to get into action... But first, we will look at the function `edgelist_to_adjmat` (which is actually what's underthehood)
# Example 3: Edgelist (cross-section data)
Lets take a look at the data
\footnotesize
```{r}
data("fakeEdgelist")
fakeEdgelist
```
\normalsize
# Example 3: Edgelist (cross-section data)
## Edgelists as adjacency matrices
\tiny
```{r}
# Coercing the edgelist to an adjacency matrix
adjmat <- edgelist_to_adjmat(
edgelist = fakeEdgelist[,1:2], # Should be a two column matrix/data.frame
w = fakeEdgelist$value, # An optional vector with weights
undirected = FALSE, # In this case, the edgelist is directed
t = 5) # We use this option to make 5 replicas of it
# nnodes(adjmat)
adjmat[[1]][1:5,1:5]
```
\normalsize
- The problem is with the last edge. It has an `NA` in the column `value`.
- If we want to keep it we have to complete that data
# Example 3: Edgelist (cross-section data)
## Edgelists as adjacency matrices (cont.)
\scriptsize
```{r}
# Fixing
fakeEdgelist[11,"value"] <- 0
# Coercing the edgelist to an adjacency matrix
adjmat <- edgelist_to_adjmat(
edgelist = fakeEdgelist[,1:2], # Should be a two column matrix/data.frame
w = fakeEdgelist$value, # An optional vector with weights
undirected = FALSE, # In this case, the edgelist is directed
t = 5) # We use this option to make 5 replicas of it
# nnodes(adjmat)
adjmat[[1]][1:5,1:5]
```
\normalsize
# Example 3: Edgelist (cross-section data)
## Ids in edgelist and attributes
- Notice that, in this case, the ids are already processed accordingly to groups.
- So we need to make ids from both, attributes and edgelist, to match
\footnotesize
```{r}
# Before
fakesurvey$id
# Changing the id
fakesurvey$id <- with(fakesurvey, group*100 + id)
# After
fakesurvey$id
```
\normalsize
# Example 3: Edgelist (cross-section data)
## Reading the data in
\scriptsize
```{r}
diffnet <- edgelist_to_diffnet(
edgelist = fakeEdgelist[,1:2], # Passed to edgelist_to_adjmat
w = fakeEdgelist$value, # Passed to edgelist_to_adjmat
dat = fakesurvey, # Data frame with -idvar- and -toavar-
idvar = "id", # Name of the -idvar- in -dat-
toavar = "toa", # Name of the -toavar- in -dat-
keep.isolates = TRUE # Passed to edgelist_to_adjmat
)
diffnet
```
\normalsize
------
\huge Example 4: Edgelist (longitudinal data) \normalsize
# Example 4: Edgelist (longitudinal data)
- Very much like before, but now we have a dynamic graph (so we need dynamic attributes as well)
- For this example we'll use the `fakeDynEdgelist` dataset.
- We are also using the `fakesurveyDyn` dataset as it holds the attributes.
- Again, for this data, we need to fix the ids
# Example 4: Edgelist (longitudinal data)
## Ids in edgelist and attributes
\scriptsize
```{r}
# Before
head(fakesurveyDyn$id)
# Changing the id
fakesurveyDyn$id <- with(fakesurveyDyn, group*100 + id)
# After
head(fakesurveyDyn$id)
```
\normalsize
# Example 4: Edgelist (longitudinal data)
## Reading the data in
\tiny
```{r}
diffnet <- edgelist_to_diffnet(
edgelist = fakeDynEdgelist[,1:2], # As usual, a two column dataset
w = fakeDynEdgelist$value, # Here we are using weights
t0 = fakeDynEdgelist$time, # An integer vector with starting point of spell
t1 = fakeDynEdgelist$time, # An integer vector with the endpoint of spell
dat = fakesurveyDyn, # Attributes dataset
idvar = "id",
toavar = "toa",
timevar = "time",
keep.isolates = TRUE, # Keeping isolates (if there's any)
warn.coercion = FALSE
)
diffnet
```
\normalsize
--------
\huge Example 5: What about import/export graphs? \normalsize
# Example 5: What about import/export graphs?
## `diffnet` to `igraph`
- We can use the `igraph_to_diffnet` and `diffnet_to_igraph` functions
\tiny
```{r}
# Back and forth
ig <- diffnet_to_igraph(medInnovationsDiffNet)
# ig (igraph has a bug printing this)
dn <- igraph_to_diffnet(ig[[1]], "toa")
# Comparing
dn; medInnovationsDiffNet
```
\normalsize
# Example 5: What about import/export graphs?
## `diffnet` to `network`
- For `diffnet_to_network`... coming soon (in the meantime, you can use the __intergraph__ package!)
\tiny
```{r}
library(intergraph)
asNetwork(ig[[1]])
```
\normalsize
# Example 5: What about import/export graphs?
## `diffnet` to `RSiena`
\scriptsize
```{r}
library(RSiena)
# Creating an array from the fakeDyn
medInnovationsDiffNet <- medInnovationsDiffNet[,,1:5]
nominationsData <- as.array(medInnovationsDiffNet)
nominationsData <- (nominationsData > 1) + 0L
nominations <- sienaDependent(nominationsData)
# Covariates
dat <- diffnet.attrs(medInnovationsDiffNet)
proage1 <- coCovar(as.numeric(dat[[1]][["proage"]]))
adopts <- lapply(dat, function(x) as.integer(with(x, per==toa)))
adopts <- varCovar(do.call(cbind, adopts))
# Putting all together
mydata <- sienaDataCreate(nominations, proage1, adopts)
myeff <- getEffects( mydata )
print01Report( mydata, myeff,modelname="s50")
```
\normalsize