Six-hour workshop
A workshop in two sessions of three hours, for researchers who know network
analysis and often work in R (statnet/ergm, RSiena, igraph), but may be new to Stata or to nwcommands.
The workshop therefore spends no time on what a network or a centrality measure is. It shows how the work is
done in Stata: networks as objects next to the data, measures as variables, Stata’s own commands on them, and the
model packages nwergm and nwsaom.
- Session 1 (3 hours): the nwcommands core.
- Session 2 (3 hours): ERGMs with
nwergmand SAOMs withnwsaom. - Session 3 (3 hours, optional): relational events with
nwremandnwdynam.
Slides. PowerPoint decks for all three sessions, with speaker notes. Instructors are free to use, adapt and share them.
- Session 1, the nwcommands core: PowerPoint · PDF
- Session 2, ERGMs and SAOMs: PowerPoint · PDF
- Session 3 (optional), relational events: PowerPoint · PDF
Every demonstration uses the data and the commands of the book’s chapters. Exercises are cited as “Ch. 8, Ex. 3”, numbered as in the book. The R column of Appendix C of the book (Commands by task, with R and Python equivalents) gives the R functions quoted below.
- Setup
- Session 1: the nwcommands core (3 hours)
- Session 2: ERGMs and SAOMs (3 hours)
- Session 3 (optional): relational events with nwrem and nwdynam (3 hours)
- Run times
Setup
The software needs Stata 17 or later. Stata/BE, the smallest edition, runs every example in the book. Install the complete collection as described in Getting started:
. net install nwcommands, from(https://nwcommands.org/net/) replace
. nwinstall, everything
The first command installs a small starter package; nwinstall fetches the rest, including the seven model
packages nwergm, nwsaom, nwrem, nwdynam, nwalaam, nwlsm and nwsbm. Appendix B of the book covers installation problems.
All data sets are loaded over the internet with nwwebuse, so the room needs a working internet connection. The
do-files of the book contain every printed command, chapter by chapter, and can be handed out
as the demonstration scripts.
Before the workshop
Send participants this list:
- Install Stata 17 or later (any edition).
- Run the two installation commands above.
- Check that the model packages are there:
which nwergmandwhich nwsaom. - Check that data can be loaded and drawn:
nwwebuse florentine, nwclearand thennwplot flomarriage. - Participants new to Stata: be able to open and run a do-file and to use
use,generate,tabulate,summarizeandregress. The book assumes that readers know Stata. - Optional: download the do-files of Chapters 4, 6, 8, 18 to 21 and 25.
Session 1: the nwcommands core (3 hours)
| Time | Block |
|---|---|
| 0:00–0:15 | 1. Networks next to the data |
| 0:15–0:50 | 2. Getting networks in and out |
| 0:50–1:25 | 3. Pictures and whole-network description |
| 1:25–1:40 | Break |
| 1:40–2:15 | 4. Measures as variables, and regression |
| 2:15–2:50 | 5. Permutation tests, QAP and CUG |
| 2:50–3:00 | 6. Reproducible scripts, questions |
1. Networks next to the data (15 min)
- Goal: see the basic design. A network is declared once and kept as an object beside the data set in memory; node attributes are ordinary variables, one row per node; every command finds a network by name.
- Demo:
. nwwebuse florentine, nwclear . nwplot flomarriage . nwwebuse lazega, nwclear . nwsummarize lazega_* . describe - For R users: where
igraphkeeps attributes inside the graph object, here they are variables of the data set, sotabulate,regressand the rest of Stata work on them directly. - Read afterwards: Getting started; Chapter 4.
2. Getting networks in and out (35 min)
- Goal: declare a network from an edge list or a matrix, build one from a wide survey export, keep several
networks of the same actors, save and reload them. Know the two traps:
cleardoes not remove networks (nwcleardoes), and an edge list loses isolates unless they get a row of their own. - Demo (Chapter 4):
nwsetwith an edge list and withmat(); the survey example fromreshape longtonwfromedge ..., undirected;nwimport; wildcards such aslazega_*and*_adv;nwsave,nwuse,nwload;nwsubset lazega_adv if office == "Hartford", name(hartford);nwsymmetrizewithmode(min)andmode(max). - R equivalents (Appendix C):
graph_from_data_frame(),graph_from_adjacency_matrix(),read_graph(),write_graph(). - Hands-on: Ch. 4, Ex. 1 (declare a five-person network from a matrix); Ch. 4, Ex. 4 (wildcards,
nwsave,nwuseandnwname). - Read afterwards: Chapter 4; Chapter 3 on construction decisions (the OR and AND rules).
3. Pictures and whole-network description (35 min)
- Goal: draw a network with attributes mapped to color and symbol, fix coordinates to compare pictures, write an interactive page; describe a network with the dyad and triad census and mixing tables.
- Demo (Chapters 5 and 6):
. nwplot lazega_adv, color(office) . nwplotmatrix lazega_adv, group(office) scheme(s1mono) . nwdyads lazega_adv . nwtriads lazega_adv . nwmixing lazega_adv, attribute(office) . nwassortativity lazega_adv, attribute(age)and from Chapter 5 an interactive page with
nwplot ..., interactiveand coordinates kept withgenerate(x y). - R equivalents:
plot(),reciprocity(),dyad_census(),triad_census(),assortativity_nominal(). - Hands-on: Ch. 5, Ex. 4 (status as color, practice as symbol); Ch. 6, Ex. 5 (the work network beside advice and friendship).
- Read afterwards: Chapters 5 and 6.
4. Measures as variables, and regression (35 min)
- Goal: compute node-level measures into variables and use them with Stata’s own commands.
- Demo (Chapters 8 and 11):
. nwdegree lazega_adv, generate(adv_out adv_in) silent . tabstat adv_in adv_out, by(partner) statistics(mean sd n) . nwcloseness lazega_adv, generate(clo far near) . nwbetween lazega_adv, generate(_between) . nwburt lazega_adv . nwbrokerage lazega_adv, generate(brk) group(officenum) . nwego lazega_adv, sizevar(egosize) densvar(egodens)then
regresson the new variables; from Chapter 11, exposure to smoking friends in the Glasgow data withnwaltergen frsmoke = proportion(alter.smoker1==1), net(glasgow1), after codingsmoker1fromsmoke1as the chapter does, and a regression of later smoking on it. - Points to make: say whether ties are counted in their direction (
nwclosenesssymmetrizes unlessnosymis given); closeness needs a rule for unreachable pairs (unconnected());nwkatzsums over shortest-path distances unlesswalksis given. - R equivalents:
degree(),closeness(),betweenness(),constraint(),sna::brokerage(),make_ego_graph(). - Hands-on: Ch. 8, Ex. 7 (tenure as a rival source of a high in-degree); Ch. 8, Ex. 10 (does the status difference in constraint survive?); if time allows, Ch. 11, Ex. 6 (the exposure regression one wave later).
- Read afterwards: Chapters 7, 8 and 11.
5. Permutation tests, QAP and CUG (35 min)
- Goal: test a statistic against random networks of the same size and density, a correlation of two networks against relabeling, and a dyadic regression with permutation p-values.
- Demo (Chapters 18 to 20):
. nwcug lazega_adv, stat(nwdyads) rname(reciprocity) reps(1000) seed(12345) condition(density) . nwcorrelate lazega_adv lazega_fr, permutations(1000) . nwmixing lazega_adv, attribute(office) permutations(1000) . set seed 12345 . nwqap lazega_adv lazega_fr office practice status, permutations(500) plotthen
condition(census)andcondition(degrees)for the same test, andqapsppfor the QAP model. - Points to make: the conditioning choice defines the question; the plus-one two-sided p-value cannot be smaller than 2/(R+1); a QAP table has no z column, because its p-values come from refitting the model on permuted networks.
- R equivalents:
sna::cug.test(),sna::qaptest(),sna::netlm(). - Hands-on: Ch. 19, Ex. 4 (advice and co-work); Ch. 20, Ex. 5 (the advice model without friendship); Ch. 18, Ex. 3 (the friendship network’s null mean).
- Read afterwards: Chapters 18, 19 and 20.
6. Reproducible scripts, questions (10 min)
- Goal: set seeds, save layout coordinates, and chain commands through stored results (
return listafternwcug,ereturn listafternwqap) instead of retyping numbers. - Read afterwards: Chapter 30; Appendix C for the full table of commands with R and Python equivalents.
Session 2: ERGMs and SAOMs (3 hours)
| Time | Block |
|---|---|
| 0:00–0:10 | 1. The model packages |
| 0:10–0:55 | 2. Specifying and estimating an ERGM |
| 0:55–1:30 | 3. Diagnosing an ERGM, and simulating from it |
| 1:30–1:45 | Break |
| 1:45–2:25 | 4. Specifying, estimating and checking an SAOM |
| 2:25–2:50 | 5. Score tests and co-evolution |
| 2:50–3:00 | 6. What else the packages do |
1. The model packages (10 min)
- Goal: place the packages.
nwergmfollows the R packageergmof the statnet project and implements 125 of its 139 terms, most with R’s names, arguments and defaults; the book callsergmthe reference and the more complete program.nwsaomis derived fromRSienaandmultiSienaand checks its results againstRSiena::siena(). Both are installed bynwinstall, everything. - Read afterwards: the opening sections of Chapters 21 and 25.
2. Specifying and estimating an ERGM (45 min)
- Goal: build the advice model of the law firm in stages, and read a coefficient as the change in the conditional log-odds of one tie, given the rest of the network.
- Demo (Chapter 21):
. nwwebuse lazega, nwclear . nwergm lazega_adv, edges seed(12345) . nwergm lazega_adv, edges nodematch(office practice) /// nodeofactor(status) nodeifactor(status) seed(12345) . nwergm lazega_adv, edges mutual nodematch(office practice) /// nodeofactor(status) nodeifactor(status) seed(12345) . nwergm lazega_adv, edges mutual nodematch(office practice) /// nodeofactor(status) nodeifactor(status) gwesp(.5) seed(12345)With
edgesalone the coefficient is the log-odds of the density, ln(0.1795/0.8205) = −1.52. Models without dyad-dependent terms are fitted by pseudolikelihood; withmutualorgwespthe estimates come from MCMLE. - R equivalent:
ergm::ergm(). - Hands-on: Ch. 21, Ex. 1 (what the office coefficient says about a pair); Ch. 21, Ex. 3 (homophily on gender).
- Read afterwards: Chapter 21.
3. Diagnosing an ERGM, and simulating from it (35 min)
- Goal: check the MCMC simulation, compare models, test fit on statistics the model does not contain, and see what a model implies by simulating from it.
- Demo (Chapters 21 and 22):
. estat mcmcdiag . estat mcmcdiag, plot . estimates store closure . estat gof, gof(model distance odegree idegree espartners) seed(12345) plot . predict, frame(pr) prestat mcmcdiaguses the settings of R’sergm, and its optionreportadds the report of R’ssummary()for the simulated sample. From Chapter 22:matrix b = e(b)after the final model, thennwergm simulate, coef(b) nsim(100) output(network) seed(1), and the same with one coefficient changed; andcapture noisily nwergm mesa, edges nodematch(grade race) triangle seed(12345)against the same model withgwesp(.25). - Points to make: a converged MCMLE is not a verdict on the model, so read the effective sample sizes; good fit on the model’s own statistics is guaranteed, so look at the other panels (the final advice model misses the spread of the out-degrees and the pairs at distance 2); a coefficient does not move only its own statistic.
- Hands-on: Ch. 21, Ex. 9 (co-work as a rival to closure); Ch. 22, Ex. 4 (simulate with the gwesp coefficient at 0.3).
- Read afterwards: Chapter 22.
4. Specifying, estimating and checking an SAOM (40 min)
- Goal: look at the amount of change, fit a model between waves, tell the convergence check from the significance test, restart, and check fit.
- Demo (Chapter 25):
. nwwebuse glasgow, nwclear . nwsaom describe, waves(glasgow1 glasgow2 glasgow3) . nwsaom, wave1(glasgow1) wave2(glasgow2) outdegree reciprocity seed(1) . nwsaom, wave1(glasgow1) wave2(glasgow2) outdegree reciprocity transtrip /// nodematch(smoke1) seed(12345) . nwsaom, waves(glasgow1 glasgow2 glasgow3) outdegree reciprocity /// transtrip seed(12345) . nwsaom refit, seed(12346) . estat gof, nsim(100) seed(1) - Points to make: the z column tests effects; the convergence t-ratios and the overall maximum convergence
ratio below the table check the Method-of-Moments search.
nwsaom refitrestarts from the last estimates, as RSiena’sprevAnsdoes. Estimation is conditional by default, as in RSiena. For comparison with R:nwwebuse s50loads the 50-actor example that RSiena callss50(networkss50_w1tos50_w3), the same pupils asglasgow. - R equivalent:
RSiena::siena(). - Hands-on: Ch. 25, Ex. 2 (the second period and a restart); Ch. 25, Ex. 3 (homophily in sport, and goodness of fit with two seeds).
- Read afterwards: Chapter 25.
5. Score tests and co-evolution (25 min)
- Goal: decide which effect to add next without fitting every candidate, and separate selection from influence in a model where behavior changes too.
- Demo (Chapters 26 and 25): the restricted fit of Chapter 26, with three candidate effects held at 0 by
fix(), followed byestat score:. quietly nwsaom, waves(glasgow1 glasgow2 glasgow3) outdegree reciprocity /// transtrip seed(12345) . quietly nwsaom refit, seed(12346) . matrix b = e(b) . quietly nwsaom, waves(glasgow1 glasgow2 glasgow3) outdegree reciprocity /// transtrip indegpopularity outactivity cycle3 /// theta0(`=b[1,1]' `=b[1,2]' 0 0 `=b[1,3]' 0) /// fix(indegpopularity outactivity cycle3) seed(1) . estat scorethen the co-evolution model of Chapter 25:
. nwsaom, waves(glasgow1 glasgow2 glasgow3) outdegree reciprocity transtrip /// behsim behavior(alcohol1 alcohol2 alcohol3) linear quadratic avalt /// seed(12345) - Points to make: each score test is conditional on the effects already in the model, so free one effect at a time; a behavior must change often enough to identify a co-evolution model (alcohol use works in these data, smoking gives very imprecise behavior estimates).
- Hands-on: Ch. 26, Ex. 3 (free outdegree activity, then rerun
estat scoreandestat gof); Ch. 25, Ex. 5 (avsimin place ofavalt). - Read afterwards: Chapter 26.
6. What else the packages do (10 min)
- Goal: point to what the session left out. Chapter 26 covers the time test (
estat timetest), the importance of effects (estat importance), maximum likelihood and Bayesian estimation, two networks that evolve together (nwsaom multinet), several groups in a joint model (groups()) and meta-analysis (nwsaom meta, asRSiena::siena08()). Chapters 27 and 28 treat time-stamped events withnwrem(R:relevent::rem.dyad()) andnwdynam(R:goldfish). Chapter 23 fits hidden groups and positions withnwsbmandnwlsm, and Chapter 24 an attribute as the outcome withnwalaam. - Read afterwards: Chapters 23, 24 and 26 to 29.
Session 3 (optional): relational events with nwrem and nwdynam (3 hours)
An optional third session for groups that work with time-stamped data (e-mails, calls, messages). It needs
Session 1 and no part of Session 2. It uses the e-mail log of the manufacturing company, the first 14 days
(keep if t < 14: 2,738 events among 129 employees), as Chapters 27 and 28 do. The commands were run with
nwrem 1.5.0 and nwdynam 1.9.0.
| Time | Block |
|---|---|
| 0:00–0:15 | 1. Events and event data |
| 0:15–1:00 | 2. The relational event model: nwrem |
| 1:00–1:40 | 3. The DyNAM: who acts, and whom they choose |
| 1:40–1:55 | Break |
| 1:55–2:15 | 4. Comparing the two models |
| 2:15–2:40 | 5. Postestimation: fit, outliers, change points |
| 2:40–3:00 | 6. Limits, pitfalls and where to go |
1. Events and event data (15 min)
- Goal: an event is a dated act with a sender and a receiver; the question is which event comes next, given
the history. Declare the log with
nwset sender receiver, eventtime(t); a ten-event toy log shows the output. - Read afterwards: Chapter 27.
2. The relational event model: nwrem (45 min)
- Goal: the model compares the observed pair with all 129 × 128 = 16,512 ordered pairs at each event; there
is no intercept. Fit
nodsnd nidrec, add inertia (frpsndsnd), compare frequency with recency (rsndsnd) by AIC, add a pair covariate (covevent(), same supervisor), and read the display (ci). - Hands-on: Ch. 27, Ex. 2 (the risk set and the log likelihood of equal weights); Ch. 27, Ex. 4 (recency of receipt).
- Read afterwards: Chapter 27.
3. The DyNAM: who acts, and whom they choose (40 min)
- Goal: two sub-models. The rate model says who acts next (with an intercept: how long it takes), the choice
model whom the actor chooses. The same effect name means different things in the two. Fit
nwdynam(choice),submodel(rate),same()on a branch variable, and the rate with an intercept. - Hands-on: Ch. 28, Ex. 3 (an expected waiting time); Ch. 28, Ex. 7 (
choice_coordinationon the undirected log). - Read afterwards: Chapter 28.
4. Comparing the two models (20 min)
- Goal: the same log in
nwremandnwdynam; the coefficients are not comparable in size; which model answers which question (Ch. 28, Ex. 8).
5. Postestimation: fit, outliers, change points (25 min)
- Goal:
estat ic,estat eventprob(350,464 rows for 2,738 events and 128 candidates),estat outliersandestat changepoints, as goldfish’sexamineOutliers()andexamineChangepoints(). - Hands-on: list the five least likely events and compare change-point methods and windows.
6. Limits, pitfalls and where to go (20 min)
- Goal: no intercept in the REM;
indegandoutdegmean different things in rate and choice; declare the log first; what the packages refuse; the R equivalents. - Read afterwards: Chapters 27 to 29.
Run times
All the model runs above are short. Appendix A of the book reports that an ERGM of the advice network with seven
terms took about three seconds in Stata/BE on a Mac. On a laptop with Stata/BE, each fit of this session (the
staged ERGMs, estat gof, 100 simulated networks, the Glasgow SAOMs, the score test and the co-evolution model; in Session 3 each nwrem and nwdynam fit takes seconds)
finished within a few seconds, and in Session 1 nwcug with reps(1000) took about ten seconds. Chapter 26 notes that a Bayesian run takes a few minutes,
and that fitting the 19 Dutch schools one by one took minutes; show those from prepared output rather than run
them live.