/
teaching1.Rmd
930 lines (783 loc) · 28.1 KB
/
teaching1.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
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
---
title: "DyNAM: How to start"
subtitle: "Dynamic Network Actor Models using the goldfish package"
author: "Christoph Stadtfeld, James Hollway, Marion Hoffman, Alvaro Uzaheta,
Kieran Mepham, Timon Elmer, Mirko Reul"
output: rmarkdown::html_vignette
vignette: >
%\VignetteIndexEntry{DyNAM: How to start}
%\VignetteEngine{knitr::rmarkdown}
%\VignetteEncoding{UTF-8}
---
Example script analyzing the MIT Social Evolution data with `R`'s `goldfish`
package.
Models inspired by section 7 of:
> Stadtfeld & Block (2017), "Interactions, Actors and Time: Dynamic Network
> Actor Models for Relational Events", Sociological Science, 2017, 4(1):
> 318-352. DOI: 10.15195/v4.a14
# Step 0: Load package and data
First, we load the `goldfish` package and load the data.
The data is loaded using lazy loading, i.e., the objects are only 'promised'
for the moment, but are available in the environment to be used, and
more information will appear as you use them.
You can find out more about this dataset, its format, and its origins in
a couple of ERGM papers by callings its documentation:
```r
library(goldfish)
data("Social_Evolution")
# ?Social_Evolution
head(calls)
#> time sender receiver increment
#> 1 1220733470 Actor 72 Actor 50 1
#> 2 1221102974 Actor 43 Actor 51 1
#> 3 1221784293 Actor 43 Actor 51 1
#> 4 1221785882 Actor 43 Actor 22 1
#> 5 1221787264 Actor 43 Actor 55 1
#> 6 1221848443 Actor 43 Actor 51 1
head(actors)
#> label present floor gradeType
#> 1 Actor 1 TRUE 3 5
#> 2 Actor 2 TRUE 5 1
#> 3 Actor 3 TRUE 5 2
#> 4 Actor 4 TRUE 2 2
#> 5 Actor 5 TRUE 4 1
#> 6 Actor 6 TRUE 4 2
```
# Preamble: Run a quick DyNAM in five lines
We use an `R` version higher than 4.2.0 to compile the vignettes.
The native pipe operator is available in `R` from version 4.1.0.
```r
callNetwork <- defineNetwork(nodes = actors, directed = TRUE) |> # 1
linkEvents(changeEvent = calls, nodes = actors) # 2
# 3
callsDependent <- defineDependentEvents(
events = calls, nodes = actors,
defaultNetwork = callNetwork
)
# 4
mod00Rate <- estimate(
callsDependent ~ indeg + outdeg,
model = "DyNAM", subModel = "rate"
)
summary(mod00Rate)
#>
#> Call:
#> estimate(x = callsDependent ~ indeg + outdeg, model = "DyNAM",
#> subModel = "rate")
#>
#>
#> Coefficients:
#> Estimate Std. Error z-value Pr(>|z|)
#> indeg 0.551445 0.066344 8.3119 < 2.2e-16 ***
#> outdeg 0.263784 0.028386 9.2927 < 2.2e-16 ***
#> ---
#> Signif. codes: 0 '***' 0.001 '**' 0.01 '*' 0.05 '.' 0.1 ' ' 1
#>
#> Converged with max abs. score of 2e-05
#> Log-Likelihood: -1750.9
#> AIC: 3505.8
#> AICc: 3505.9
#> BIC: 3514
#> model: "DyNAM" subModel: "rate"
mod00Choice <- estimate(
callsDependent ~ inertia + recip + trans,
model = "DyNAM", subModel = "choice"
)
summary(mod00Choice)
#>
#> Call:
#> estimate(x = callsDependent ~ inertia + recip + trans, model = "DyNAM",
#> subModel = "choice")
#>
#>
#> Coefficients:
#> Estimate Std. Error z-value Pr(>|z|)
#> inertia 5.19690 0.17397 29.8725 < 2.2e-16 ***
#> recip 1.39802 0.17300 8.0812 6.661e-16 ***
#> trans -0.23036 0.21554 -1.0687 0.2852
#> ---
#> Signif. codes: 0 '***' 0.001 '**' 0.01 '*' 0.05 '.' 0.1 ' ' 1
#>
#> Converged with max abs. score of 7e-05
#> Log-Likelihood: -696.72
#> AIC: 1399.4
#> AICc: 1399.5
#> BIC: 1411.7
#> model: "DyNAM" subModel: "choice"
```
# Step 1: Create data objects
## Step 1a: Define node set(s) and attributes
We've loaded a dataset that defines its nodes and their attributes as
a data frame. Let's check what we have first.
```r
class(actors)
#> [1] "data.frame"
head(actors)
#> label present floor gradeType
#> 1 Actor 1 TRUE 3 5
#> 2 Actor 2 TRUE 5 1
#> 3 Actor 3 TRUE 5 2
#> 4 Actor 4 TRUE 2 2
#> 5 Actor 5 TRUE 4 1
#> 6 Actor 6 TRUE 4 2
```
Note that there are four column variables: `label` the identifier, `present`,
the `floor` of residence, and `gradeType` their educational level
(1 = freshmen to 5 = graduate).
All of these columns will be recognized as individual attributes by goldfish.
We need to define them as nodes so that `goldfish` knows what to do with them.
```r
actors <- defineNodes(actors)
actors
#> Number of nodes: 84
#> Number of present nodes: 84
#>
#> First 6 rows
#> label present floor gradeType
#> 1 Actor 1 TRUE 3 5
#> 2 Actor 2 TRUE 5 1
#> 3 Actor 3 TRUE 5 2
#> 4 Actor 4 TRUE 2 2
#> 5 Actor 5 TRUE 4 1
#> 6 Actor 6 TRUE 4 2
```
As you can see, the structure is the same, so we can still treat it like
a data frame, but the added class helps goldfish interpret the data frame
correctly.
## Step 1b: Define networks
Next we want to define the dyadic or network elements: calls between our actors.
```r
head(calls)
#> time sender receiver increment
#> 1 1220733470 Actor 72 Actor 50 1
#> 2 1221102974 Actor 43 Actor 51 1
#> 3 1221784293 Actor 43 Actor 51 1
#> 4 1221785882 Actor 43 Actor 22 1
#> 5 1221787264 Actor 43 Actor 55 1
#> 6 1221848443 Actor 43 Actor 51 1
```
Note there are columns for `time`, `sender`, and `receiver`. `increment`
is a reserved column.
To tell goldfish this is a network, we must define it as such:
```r
?defineNetwork
```
```r
callNetwork <- defineNetwork(nodes = actors, directed = TRUE)
```
The argument `directed` is `TRUE` by default, but we need to specify
the nodes so that `goldfish` can check for consistency and relate it to
that nodeset as needed.
```r
callNetwork
#> Dimensions: 84 84
#> Number of ties (no weighted): 0
#> Nodes set(s): actors
#> It is a one-mode and directed network
#>
#> First 6 rows and columns
#> receiver
#> sender Actor 1 Actor 2 Actor 3 Actor 4 Actor 5 Actor 6
#> Actor 1 0 0 0 0 0 0
#> Actor 2 0 0 0 0 0 0
#> Actor 3 0 0 0 0 0 0
#> Actor 4 0 0 0 0 0 0
#> Actor 5 0 0 0 0 0 0
#> Actor 6 0 0 0 0 0 0
```
Note that we have not added any network data yet. By default, `defineNetwork()`
just constructs an empty matrix with dimensions defined by the length of the
nodeset(s).
So we have an empty network as a starting state.
Now that goldfish recognizes the matrix as a network, we can also associate
an event list that updates it.
To do this we use the `linkEvents()` function, which requires us to identify
a goldfish object to be updated, the events that update it and, in this case,
also the nodes that the events should relate to.
`goldfish` checks the consistency of all this information and relates these
objects to one another so that information can be called as needed.
```r
?linkEvents
```
```r
callNetwork <- linkEvents(x = callNetwork, changeEvent = calls, nodes = actors)
callNetwork
#> Dimensions: 84 84
#> Number of ties (no weighted): 0
#> Nodes set(s): actors
#> It is a one-mode and directed network
#> Linked events: calls
#>
#> First 6 rows and columns
#> receiver
#> sender Actor 1 Actor 2 Actor 3 Actor 4 Actor 5 Actor 6
#> Actor 1 0 0 0 0 0 0
#> Actor 2 0 0 0 0 0 0
#> Actor 3 0 0 0 0 0 0
#> Actor 4 0 0 0 0 0 0
#> Actor 5 0 0 0 0 0 0
#> Actor 6 0 0 0 0 0 0
```
### Task
You should now be able to do the same with the friendship nomination
network/event list.
See the familiar columns `time`, `sender`, and `receiver`.
The new column, `replace`, is an alternative treatment to `increment`.
When an event occurs, goldfish will replace the value in
the relevant cell with the value in this column instead of incrementing it.
Friendship is thus a binary network.
```r
head(friendship)
#> time sender receiver replace
#> 1 1220918400 Actor 47 Actor 2 1
#> 3 1220918400 Actor 57 Actor 2 1
#> 4 1220918400 Actor 9 Actor 38 1
#> 5 1220918400 Actor 68 Actor 40 1
#> 6 1220918400 Actor 23 Actor 40 1
#> 7 1220918400 Actor 49 Actor 40 1
friendshipNetwork <- defineNetwork(nodes = actors, directed = TRUE)
friendshipNetwork <- linkEvents(
x = friendshipNetwork,
changeEvents = friendship,
nodes = actors
)
friendshipNetwork
#> Dimensions: 84 84
#> Number of ties (no weighted): 0
#> Nodes set(s): actors
#> It is a one-mode and directed network
#> Linked events: friendship
#>
#> First 6 rows and columns
#> receiver
#> sender Actor 1 Actor 2 Actor 3 Actor 4 Actor 5 Actor 6
#> Actor 1 0 0 0 0 0 0
#> Actor 2 0 0 0 0 0 0
#> Actor 3 0 0 0 0 0 0
#> Actor 4 0 0 0 0 0 0
#> Actor 5 0 0 0 0 0 0
#> Actor 6 0 0 0 0 0 0
```
## Step 1c: Define dependent events
The final step in defining the data objects is to identify the dependent events.
Here we would like to model as the dependent variable the calls
between individuals.
We specify the event list and the node list.
```r
?defineDependentEvents
```
```r
callsDependent <- defineDependentEvents(
events = calls, nodes = actors,
defaultNetwork = callNetwork
)
callsDependent
#> Number of events: 439
#> Nodes set(s): actors
#> Default network: callNetwork
#>
#> First 6 rows
#> time sender receiver increment
#> 1 1220733470 Actor 72 Actor 50 1
#> 2 1221102974 Actor 43 Actor 51 1
#> 3 1221784293 Actor 43 Actor 51 1
#> 4 1221785882 Actor 43 Actor 22 1
#> 5 1221787264 Actor 43 Actor 55 1
#> 6 1221848443 Actor 43 Actor 51 1
```
### Intermediate step: Visualization
While not a required part of the modeling process,
we highly recommend the visualization of your data for analytic and
diagnostic purposes.
`goldfish` includes wrappers for base `R` commands to help extract monadic
and dyadic information for certain time points, `?as.data.frame.nodes.goldfish`
and `?as.matrix.network.goldfish`.
We can use these functions to visually compare our network at two (or more)
different time periods using `migraph`. See the `migraph` package documentation
for additional information about network visualization.
```r
library(igraph)
library(ggraph)
library(migraph)
# The network at the beginning
callNetworkBgn <- as.matrix(callNetwork)
autographr(callNetworkBgn, labels = FALSE, layout = "fr")
```
![plot of chunk plot-teaching1](teaching/plot-teaching1-1.png)
```r
# The network at half time
callNetworkHlf <- as.matrix(
callNetwork,
time = calls$time[floor(nrow(calls) / 2)]
) |>
as_igraph() |>
add_node_attribute("floor", actors$floor)
autographr(callNetworkHlf, labels = FALSE, layout = "fr") +
geom_node_point(aes(color = as.factor(floor)), size = 2, show.legend = FALSE)
```
![plot of chunk plot-teaching1](teaching/plot-teaching1-2.png)
```r
# The network at the end
callNetworkEnd <- as.matrix(callNetwork, time = max(calls$time) + 1) |>
as_igraph() |>
add_node_attribute("floor", actors$floor)
autographr(callNetworkEnd, labels = FALSE, layout = "fr") +
geom_node_point(aes(color = as.factor(floor)), size = 2, show.legend = FALSE)
```
![plot of chunk plot-teaching1](teaching/plot-teaching1-3.png)
```r
# The tie strength at the end
table(as.matrix(callNetwork, time = max(calls$time) + 1))
#>
#> 0 1 2 3 4 6 7 8 9 10 11 13 15 64 115
#> 6972 36 13 10 8 1 5 3 1 1 1 2 1 1 1
```
# Step 2: Specify and estimate model
The second step is to specify and fit a model to this data.
This step can be broken up into several stages:
- **Step 2a**. *Formula*: Specify a model formula from the effects and
variables available
- **Step 2b**. *Preprocessing*: Calculate the change statistics associated
with these effects
- **Step 2c**. *Estimation*: Fit an appropriate model to these statistics
However, in goldfish we also have the option of accelerating this process and
using memory more efficiently by combining these three sub-steps in one.
Nonetheless, it can be helpful to think of 2a separately, and
recognize steps 2b and 2c as goldfish does them.
## Step 2a. Formula
We specify our model using the standard R formula format like:
`goldfish_dependent ~ effects(process_state_element)`
We can see which effects are currently available and how to specify them here:
```r
vignette("goldfishEffects")
```
Let's start with the simplest model we can imagine:
```r
simpleFormulaChoice <- callsDependent ~ tie(friendshipNetwork)
```
What are we testing here?
Do individuals call their friends more than non-friends?
## Step 2b and 2c. Preprocessing and Estimation
Now to estimate this model, we use the `?estimate` function.
For now, only need to worry about the `formula` and the `model`,
`subModel` type (DyNAM-choice).
```r
mod01Choice <- estimate(
simpleFormulaChoice,
model = "DyNAM", subModel = "choice"
)
summary(mod01Choice)
#>
#> Call:
#> estimate(x = callsDependent ~ tie(friendshipNetwork), model = "DyNAM",
#> subModel = "choice")
#>
#>
#> Coefficients:
#> Estimate Std. Error z-value Pr(>|z|)
#> tie 4.02538 0.12513 32.17 < 2.2e-16 ***
#> ---
#> Signif. codes: 0 '***' 0.001 '**' 0.01 '*' 0.05 '.' 0.1 ' ' 1
#>
#> Converged with max abs. score of 0
#> Log-Likelihood: -1288.7
#> AIC: 2579.4
#> AICc: 2579.4
#> BIC: 2583.5
#> model: "DyNAM" subModel: "choice"
```
Ok, as fascinating as that was, perhaps we can test how robust
this finding is in the presence of plausible controls.
```r
complexFormulaChoice <-
callsDependent ~ inertia(callNetwork) + recip(callNetwork) +
tie(friendshipNetwork) + recip(friendshipNetwork) +
same(actors$gradeType) + same(actors$floor)
mod02Choice <- estimate(
complexFormulaChoice,
model = "DyNAM", subModel = "choice"
)
summary(mod02Choice)
#>
#> Call:
#> estimate(x = callsDependent ~ inertia(callNetwork) + recip(callNetwork) +
#> tie(friendshipNetwork) + recip(friendshipNetwork) + same(actors$gradeType) +
#> same(actors$floor), model = "DyNAM", subModel = "choice")
#>
#>
#> Effects details:
#> Object
#> inertia callNetwork
#> recip callNetwork
#> tie friendshipNetwork
#> recip friendshipNetwork
#> same actors$gradeType
#> same actors$floor
#>
#> Coefficients:
#> Estimate Std. Error z-value Pr(>|z|)
#> inertia 4.42057 0.19225 22.9936 < 2.2e-16 ***
#> recip 0.27151 0.19735 1.3758 0.168895
#> tie 1.28779 0.25673 5.0160 5.275e-07 ***
#> recip 0.68625 0.22930 2.9928 0.002765 **
#> same 0.69847 0.17607 3.9670 7.279e-05 ***
#> same -0.28700 0.16618 -1.7271 0.084155 .
#> ---
#> Signif. codes: 0 '***' 0.001 '**' 0.01 '*' 0.05 '.' 0.1 ' ' 1
#>
#> Converged with max abs. score of 0
#> Log-Likelihood: -637.96
#> AIC: 1287.9
#> AICc: 1288.1
#> BIC: 1312.4
#> model: "DyNAM" subModel: "choice"
```
### Rate model
How do individual properties affect the rate of action of individuals?
(Step 1 of the model)
Let us again define a simple formula that only depends on individuals' degree
in the friendship network
```r
simpleFormulaRate <- callsDependent ~ indeg(friendshipNetwork)
mod01Rate <- estimate(
simpleFormulaRate,
model = "DyNAM", subModel = "rate"
)
```
#### Remark:
Sometimes, the default values for the algorithm are not enough
to reach convergence.
We can remedy this by increasing the number of iterations:
```r
mod01Rate <- estimate(
simpleFormulaRate,
model = "DyNAM", subModel = "rate",
estimationInit = list(maxIterations = 40)
)
summary(mod01Rate)
#>
#> Call:
#> estimate(x = callsDependent ~ indeg(friendshipNetwork), model = "DyNAM",
#> subModel = "rate", estimationInit = list(maxIterations = 40))
#>
#>
#> Coefficients:
#> Estimate Std. Error z-value Pr(>|z|)
#> indeg 0.157370 0.012699 12.392 < 2.2e-16 ***
#> ---
#> Signif. codes: 0 '***' 0.001 '**' 0.01 '*' 0.05 '.' 0.1 ' ' 1
#>
#> Converged with max abs. score of 0
#> Log-Likelihood: -1868.1
#> AIC: 3738.3
#> AICc: 3738.3
#> BIC: 3742.3
#> model: "DyNAM" subModel: "rate"
```
What if we include additional structural effects?
Let us add the in and out degree of the nodes in the call network
```r
complexFormulaRate <-
callsDependent ~ indeg(callNetwork) + outdeg(callNetwork) +
indeg(friendshipNetwork)
mod02Rate <- estimate(complexFormulaRate, model = "DyNAM", subModel = "rate")
summary(mod02Rate)
#>
#> Call:
#> estimate(x = callsDependent ~ indeg(callNetwork) + outdeg(callNetwork) +
#> indeg(friendshipNetwork), model = "DyNAM", subModel = "rate")
#>
#>
#> Effects details:
#> Object
#> indeg callNetwork
#> outdeg callNetwork
#> indeg friendshipNetwork
#>
#> Coefficients:
#> Estimate Std. Error z-value Pr(>|z|)
#> indeg 0.454676 0.070918 6.4113 1.443e-10 ***
#> outdeg 0.240231 0.029494 8.1452 4.441e-16 ***
#> indeg 0.060809 0.015464 3.9324 8.409e-05 ***
#> ---
#> Signif. codes: 0 '***' 0.001 '**' 0.01 '*' 0.05 '.' 0.1 ' ' 1
#>
#> Converged with max abs. score of 2e-05
#> Log-Likelihood: -1743.2
#> AIC: 3492.5
#> AICc: 3492.5
#> BIC: 3504.7
#> model: "DyNAM" subModel: "rate"
```
#### Right-censored intervals
Recall that it is important to add a time intercept when estimating models with
right-censored intervals (as discussed in Stadtfeld & Block, 2017).
Adding an intercept is as easy as including a 1 to the formula:
```r
interceptFormulaRate <-
callsDependent ~ 1 + indeg(callNetwork) + outdeg(callNetwork) +
indeg(friendshipNetwork)
mod03Rate <- estimate(interceptFormulaRate, model = "DyNAM", subModel = "rate")
summary(mod03Rate)
#>
#> Call:
#> estimate(x = callsDependent ~ 1 + indeg(callNetwork) + outdeg(callNetwork) +
#> indeg(friendshipNetwork), model = "DyNAM", subModel = "rate")
#>
#>
#> Effects details:
#> Object
#> Intercept
#> indeg callNetwork
#> outdeg callNetwork
#> indeg friendshipNetwork
#>
#> Coefficients:
#> Estimate Std. Error z-value Pr(>|z|)
#> Intercept -14.380373 0.095669 -150.3135 < 2.2e-16 ***
#> indeg 0.695555 0.063115 11.0204 < 2.2e-16 ***
#> outdeg 0.234633 0.030153 7.7814 7.105e-15 ***
#> indeg 0.054792 0.015049 3.6409 0.0002716 ***
#> ---
#> Signif. codes: 0 '***' 0.001 '**' 0.01 '*' 0.05 '.' 0.1 ' ' 1
#>
#> Converged with max abs. score of 0
#> Log-Likelihood: -6003.1
#> AIC: 12014
#> AICc: 12014
#> BIC: 12031
#> model: "DyNAM" subModel: "rate"
```
Note that $1 / \exp({\beta_{intercept}})$ is the waiting time
without any covariates, or how long it takes to make the next phone call
if everything else is set to 0.
Therefore, the larger the intercept the shorter the waiting time.
For example, an intercept of -14 means a waiting time of
$1 / e^{-14} = 334$ hours.
The baseline waiting time between two events in hours:
```r
mod03RateCoef <- coef(mod03Rate)
1 / exp(mod03RateCoef[["Intercept"]]) / 3600
#> [1] 488.6682
# or days:
1 / exp(mod03RateCoef[["Intercept"]]) / 86400
#> [1] 20.36118
# But what if it is not just a random call?
# Expected waiting time of those who have five outgoing call ties
# (five different actors)
1 / exp(
mod03RateCoef[["Intercept"]] + mod03RateCoef[["outdeg"]] * 5
) / 3600
#> [1] 151.1872
# Expected waiting time of those who have five outgoing and incoming call ties
# (five different actors)
1 / exp(
mod03RateCoef[["Intercept"]] +
mod03RateCoef[["outdeg"]] * 5 +
mod03RateCoef[["indeg"]] * 5
) / 3600
#> [1] 4.66806
```
### Windows
Remember our `callNetwork` process state accumulates actions in time,
but some older actions may no longer be relevant to current action.
Let's see whether it is really just recent partners that matter
by adding extra effects with a window of... how long?
Let us try 5 minutes $= 5 * 60 = 300s$
```r
windowFormulaRate <-
callsDependent ~ 1 + indeg(callNetwork) + outdeg(callNetwork) +
indeg(callNetwork, window = 300) +
outdeg(callNetwork, window = 300) +
indeg(friendshipNetwork)
mod04Rate <- estimate(windowFormulaRate, model = "DyNAM", subModel = "rate")
summary(mod04Rate)
#>
#> Call:
#> estimate(x = callsDependent ~ 1 + indeg(callNetwork) + outdeg(callNetwork) +
#> indeg(callNetwork, window = 300) + outdeg(callNetwork, window = 300) +
#> indeg(friendshipNetwork), model = "DyNAM", subModel = "rate")
#>
#>
#> Effects details:
#> Object window
#> Intercept
#> indeg callNetwork
#> outdeg callNetwork
#> indeg callNetwork 300
#> outdeg callNetwork 300
#> indeg friendshipNetwork
#>
#> Coefficients:
#> Estimate Std. Error z-value Pr(>|z|)
#> Intercept -14.530750 0.101676 -142.9125 < 2.2e-16 ***
#> indeg 0.245045 0.070682 3.4669 0.0005266 ***
#> outdeg 0.364576 0.032556 11.1985 < 2.2e-16 ***
#> indeg 5.295709 0.139463 37.9722 < 2.2e-16 ***
#> outdeg -0.767499 0.116642 -6.5800 4.706e-11 ***
#> indeg 0.083772 0.015289 5.4794 4.268e-08 ***
#> ---
#> Signif. codes: 0 '***' 0.001 '**' 0.01 '*' 0.05 '.' 0.1 ' ' 1
#>
#> Converged with max abs. score of 1e-05
#> Log-Likelihood: -5475.9
#> AIC: 10964
#> AICc: 10964
#> BIC: 10992
#> model: "DyNAM" subModel: "rate"
```
Of course, you can also add windows to the choice formula!
```r
windowFormulaChoice <-
callsDependent ~ inertia(callNetwork) + recip(callNetwork) +
inertia(callNetwork, window = 300) +
recip(callNetwork, window = 300) +
tie(friendshipNetwork) + recip(friendshipNetwork) +
same(actors$gradeType) + same(actors$floor)
mod03Choice <- estimate(windowFormulaChoice,
model = "DyNAM", subModel = "choice")
summary(mod03Choice)
#>
#> Call:
#> estimate(x = callsDependent ~ inertia(callNetwork) + recip(callNetwork) +
#> inertia(callNetwork, window = 300) + recip(callNetwork, window = 300) +
#> tie(friendshipNetwork) + recip(friendshipNetwork) + same(actors$gradeType) +
#> same(actors$floor), model = "DyNAM", subModel = "choice")
#>
#>
#> Effects details:
#> Object window
#> inertia callNetwork
#> recip callNetwork
#> inertia callNetwork 300
#> recip callNetwork 300
#> tie friendshipNetwork
#> recip friendshipNetwork
#> same actors$gradeType
#> same actors$floor
#>
#> Coefficients:
#> Estimate Std. Error z-value Pr(>|z|)
#> inertia 4.29828 0.20193 21.2863 < 2.2e-16 ***
#> recip -0.48574 0.23205 -2.0932 0.0363293 *
#> inertia 1.39628 0.36093 3.8685 0.0001095 ***
#> recip 5.02256 0.73879 6.7983 1.058e-11 ***
#> tie 1.50593 0.27206 5.5354 3.105e-08 ***
#> recip 0.62314 0.24975 2.4951 0.0125930 *
#> same 0.44865 0.19815 2.2641 0.0235659 *
#> same -0.20953 0.18139 -1.1551 0.2480445
#> ---
#> Signif. codes: 0 '***' 0.001 '**' 0.01 '*' 0.05 '.' 0.1 ' ' 1
#>
#> Converged with max abs. score of 9e-05
#> Log-Likelihood: -565.9
#> AIC: 1147.8
#> AICc: 1148.1
#> BIC: 1180.5
#> model: "DyNAM" subModel: "choice"
```
All this shows you that you can specify different formula
for the rate and choice components of the model.
This is a key advantage of the DyNAM!
When comparing the information criteria (AIC / BIC) of the different models,
we see that the time windows explain a lot
```r
# Compare different specifications of the subModel = "choice"
AIC(mod02Choice, mod03Choice)
#> df AIC
#> mod02Choice 6 1287.912
#> mod03Choice 8 1147.804
# Compare different specifications of the subModel = "rate"
AIC(mod03Rate, mod04Rate)
#> Warning in AIC.default(mod03Rate, mod04Rate): models are not all fitted to the
#> same number of observations
#> df AIC
#> mod03Rate 4 12014.20
#> mod04Rate 6 10963.75
```
### REM with `goldfish`
`goldfish` does not only run DyNAMs; it also runs REMs (Butts, 2008).
We can now also run REMs using the right-censored intervals as introduced in Stadtfeld & Block (2017).
An equivalent model specification to the DyNAM model we estimated above,
including the rate and choice sub-models, is:
```r
allFormulaREM <-
callsDependent ~
1 + indeg(callNetwork, type = "ego") + outdeg(callNetwork, type = "ego") +
indeg(friendshipNetwork, type = "ego") +
inertia(callNetwork) + recip(callNetwork) +
inertia(callNetwork, window = 300) + recip(callNetwork, window = 300) +
tie(friendshipNetwork) + recip(friendshipNetwork) +
same(actors$gradeType) + same(actors$floor)
```
And we can estimate this model, to speed up estimation, we can use any of the
two `C` implementations of the estimation algorithm.
Setting the option `engine` on the `estimationInit` argument, we can choose
which version of the `C` code to use.
`"default_c"` implements the same algorithm as
the default one implemented in plain `R` code that reduces the memory use
by exploiting the sparsity of the dyads that change the effects' statistics
after each event.
`"gather_compute"` implements a version with an overhead of memory use,
representing the data in a more usual tabular way but reducing time estimation.
```r
mod01REM <- estimate(
allFormulaREM, model = "REM",
estimationInit = list(initialDamping = 40, engine = "default_c")
)
```
```r
mod01REM <- estimate(
allFormulaREM, model = "REM",
estimationInit = list(engine = "gather_compute")
)
summary(mod01REM)
#>
#> Call:
#> estimate(x = callsDependent ~ 1 + indeg(callNetwork, type = "ego") +
#> outdeg(callNetwork, type = "ego") + indeg(friendshipNetwork,
#> type = "ego") + inertia(callNetwork) + recip(callNetwork) +
#> inertia(callNetwork, window = 300) + recip(callNetwork, window = 300) +
#> tie(friendshipNetwork) + recip(friendshipNetwork) + same(actors$gradeType) +
#> same(actors$floor), model = "REM", estimationInit = list(engine = "gather_compute"))
#>
#>
#> Effects details:
#> Object type window
#> Intercept "" "" ""
#> indeg "callNetwork" "ego" ""
#> outdeg "callNetwork" "ego" ""
#> indeg "friendshipNetwork" "ego" ""
#> inertia "callNetwork" "" ""
#> recip "callNetwork" "" ""
#> inertia "callNetwork" "" "300"
#> recip "callNetwork" "" "300"
#> tie "friendshipNetwork" "" ""
#> recip "friendshipNetwork" "" ""
#> same "actors$gradeType" "" ""
#> same "actors$floor" "" ""
#>
#> Coefficients:
#> Estimate Std. Error z-value Pr(>|z|)
#> Intercept -19.763568 0.136731 -144.5434 < 2.2e-16 ***
#> indeg 0.086900 0.071528 1.2149 0.2243968
#> outdeg -0.222330 0.040445 -5.4971 3.860e-08 ***
#> indeg 0.010742 0.017245 0.6229 0.5333320
#> inertia 6.203483 0.189796 32.6850 < 2.2e-16 ***
#> recip -0.313425 0.154555 -2.0279 0.0425690 *
#> inertia -1.589903 0.179494 -8.8577 < 2.2e-16 ***
#> recip 7.013842 0.170730 41.0814 < 2.2e-16 ***
#> tie 0.853373 0.206297 4.1366 3.525e-05 ***
#> recip 0.930856 0.194742 4.7800 1.753e-06 ***
#> same 0.470688 0.132528 3.5516 0.0003829 ***
#> same -0.670748 0.123206 -5.4441 5.207e-08 ***
#> ---
#> Signif. codes: 0 '***' 0.001 '**' 0.01 '*' 0.05 '.' 0.1 ' ' 1
#>
#> Converged with max abs. score of 0
#> Log-Likelihood: -5639
#> AIC: 11302
#> AICc: 11302
#> BIC: 11359
#> model: "REM" subModel: "choice"
```