*! version 1.2.1 07mar2013

Title

group_twoway -- groups observations by the connected components of two variables

Syntax

group_twoway
parent child
,
gen(newwar)

generate(newvar) creates newvar with a numeric ID for each connected component

Description

group_twoway groups observations by the connected components of the parent and child variables.

For instance, given a list of family relationships between people, this grouping will connect any two individuals for whom there is a "family path" connecting them (cousin, of a cousin, of a cousin, etc)
even if there are no direct family relationships between them.

This can be useful when classifications (sectorial, regional) change over time and one wants to create a constant classification over the period.

The command will interpret vertices with the same name appearing the both parent and child variables as being the same and will make the appropriate connections.
If that is not the case, change names in parent or child so that the lists are non overlapping (see example2).

Most algorithms convert the graph into adjacency matrix form to compute the connected components, which, in Stata, limit's the number of edges than can be considered.
We keep the edge-list format and use the power's of Stata's own "group" function. Thus it can handle edge lists of virtually any size fits into memory.

Examples

Example 1: connected components.

. clear

. input str1 parent str1 child

. A T

. B T

. C Y

. C U

. D W

. E W

. E Z

. F Z

. end

. group_twoway parent child , gen(extended_family)

Interpreting this as classifications, or some grouping variable, notice we have 3 cases:

- simple division: A and B merge to form T

- simple union: C divides into Y and U

- complex (division and union): Notice how despite D and F not being connected, the fact that E divides into W, which is formed by merging with D, and Z, which is formed by merging with F, creates an indirect link between D and F.

Example 2: Be careful if vertex names that aprear in both Child and Parent lists.

. clear

. input str1 parent str1 child

. A T

. B T

. C A

. C B

. end

. group_twoway parent child , gen(extended_family)

Note that vertices A and B appear in parent and child and, thus, the group_twoway connects all vertices into the same group.

If vertices of each variable are different by definition, regardless of name repetition, as is the case with most classification changes, then change the names so the name lists are disjoint for both variables, as bellow

. gen child2=child+"child"

. group_twoway parent child2 , gen(extended_family2)

Authors

Lucas F. Mation , Instituto de Pesquisa Econômica Aplicada (IPEA) , Brazil

Aguinaldo N. Maciente , Instituto de Pesquisa Econômica Aplicada (IPEA) , Brazil