nwpermute
Generate a permutation of a network
Syntax
nwpermute
[netname]
[,
generate(newnetname)
replace
nwreplace
xvars
swap
swaps(#)]
generate(newnetname) |
Save permutation as new network; one of generate() or replace is required |
replace |
Replace existing network with permutation; with generate(): overwrite the network named there if it already exists |
nwreplace |
Overwrite the network named in generate() if it already exists; without it the command stops with an error |
xvars |
Generate Stata variables for the network |
swap |
exchange the partners of two ties at random (Maslov and Sneppen) instead of permuting the labels; keeps every node’s degree (directed: out-degree and in-degree; two-mode: both degree sequences) and the tie values |
swaps(#) |
the number of attempted exchanges; implies swap; default = ten times the number of ties |
Description
Produces a random permutation of the network netname. In such a permutation, the nodes are randomly reshuffled, while the overall structure of the network remains the same. Either option replace or generate needs to be specified.
With swap, the ties are rewired instead: two ties a-b and c-d, picked at random, become a-d and c-b (in an undirected network, a-d and c-b or a-c and b-d, at random), unless that would make a self-loop or tie a pair that is tied already. Every node keeps its degree (in a directed network its out-degree and its in-degree), a tie keeps its value, and everything else about the network is randomized. swaps() sets the number of attempted exchanges (some are refused). This is the rewiring of R’s igraph::rewire(g, keeping_degseq()): a random network with the degrees of the observed one, the usual baseline for a pattern that may follow from the degrees alone (see nwcug with condition(degrees), and nwrandom with degree() for a network drawn from a list of degrees). Self-loops and ties with a missing value stay where they are. For a one-mode network, swap uses the native plugin; the exchanges are drawn from the seed (set seed).
. nwuse glasgow, nwclear
. nwpermute glasgow1, swap generate(glasgow1_swapped)
A simple example illustrates what the command does. First, we generate a regular lattice network.
. nwclear
. nwlattice 3 3
. nwsummarize lattice, matonly
1 2 3 4 5 6 7 8 9
+-------------------------------------+
1 | 0 |
2 | 1 0 |
3 | 0 1 0 |
4 | 1 0 0 0 |
5 | 0 1 0 1 0 |
6 | 0 0 1 0 1 0 |
7 | 0 0 0 1 0 0 0 |
8 | 0 0 0 0 1 0 1 0 |
9 | 0 0 0 0 0 1 0 1 0 |
+-------------------------------------+
Now, let us permute the network lattice.
. nwpermute lattice, replace
. nwsummarize lattice, matonly
1 2 3 4 5 6 7 8 9
+-------------------------------------+
1 | 0 |
2 | 1 0 |
3 | 0 0 0 |
4 | 0 0 1 0 |
5 | 1 0 0 0 0 |
6 | 0 1 0 1 0 0 |
7 | 0 0 0 0 1 0 0 |
8 | 0 0 1 0 0 0 1 0 |
9 | 0 1 0 1 1 0 0 1 0 |
+-------------------------------------+
The structure of the network remains exactly the same, however, the nodes have a different order. Often, such a permutation is desired to recalculate network statistics (and derive standard errors and confidence intervals for these statistics) while keeping the overall structure of the network constant (see more nwqap, nwcorrelate).
Network types
Directed: yes (swap keeps out-degrees and in-degrees). Valued:, Signed: the tie values move with their ties. Two-mode: yes - the labels are permuted within the modes (tnet::rg_reshuffling_tm); swap keeps both degree sequences (bipartite::swap.web). See nwnetworktypes.
Limits
A network held as stored ties is relabelled tie by tie, with the same permutation for the same seed. 20,000 nodes 0.2 seconds in Stata/BE.
Example
. nwclear
. nwrandom 10, prob(.1) name(mynet)
. nwplot mynet, lab layout(circle)
. nwpermute mynet, replace
. nwplot mynet, lab layout(circle)
See also
nwcommands version 3.25.0, 14 November 2026 (check your installed version)