Announcement

Collapse
No announcement yet.
X
  • Filter
  • Time
  • Show
Clear All
new posts

  • Identifying network components

    Here's a toy example of my problem. The dataset below describes 3 horse races, in which a total of 10 horses (numbered 1 to 10) were ridden by a total of 6 jockeys, labeled A to F. Jockeys and horses constitute a two-mode network, with jockeys connected by horses that they have in common.

    Not every jockey is connected, though. For example, jockey B is not connected because (s)he rode horse 2, which no other jockey rode. I believe all the other jockeys are connected, though. So from the jockeys' point of view there are two network components, one consisting of jockeys A, C, D, E, F, and one consisting of jockey B.

    What I'd like to do is add a column that indicates which component each jockey is part of.

    What's the simplest way to do this? I waded into the nwcommands suite, and quickly got lost. I don't actually need to do any network analysis beyond identifying the components, so a simple solution will do.

    This is the input dataset:
    Code:
    input Race Horse str1 Jockey
    1 1 "A"
    1 2 "B"
    1 3 "C"
    1 4 "D"
    1 5 "E"
    2 3 "A"
    2 4 "C"
    2 5 "D"
    2 6 "E"
    2 7 "F"
    3 5 "A"
    3 7 "C"        
    3 8 "D"
    3 9 "E"
    3 10 "F"
    end
    This is the desired output dataset:
    Code:
    input Race Horse Jockey Component
    1 1 "A" 1
    1 2 "B" 2
    1 3 "C" 1
    1 4 "D" 1
    1 5 "E" 1
    2 3 "A" 1
    2 4 "C" 1
    2 5 "D" 1
    2 6 "E" 1
    2 7 "F" 1
    3 5 "A" 1
    3 7 "C" 1
    3 8 "D" 1
    3 9 "E" 1
    3 10 "F" 1
    end




  • #2
    I am nearly certain there is a better way to do this, but the following gets the job done, however inefficiently:

    Code:
    clear*
    input Race Horse str1 Jockey
    1 1 "A"
    1 2 "B"
    1 3 "C"
    1 4 "D"
    1 5 "E"
    2 3 "A"
    2 4 "C"
    2 5 "D"
    2 6 "E"
    2 7 "F"
    3 5 "A"
    3 7 "C"        
    3 8 "D"
    3 9 "E"
    3 10 "F"
    end
    
    
    replace Jockey = strtoname(Jockey)
    levelsof Jockey, local(jockeys)
    local n_jockeys: word count `jockeys'
    
    matrix Network = I(`n_jockeys')
    matrix rownames Network = `jockeys'
    matrix colnames Network = `jockeys'
    
    matrix list Network
    
    
    capture program drop one_horse
    program define one_horse
        sort Jockey
        local N = _N
        forvalues i = 1/`N' {
            forvalues j = `=`i'+1'/`N' {
                local r: rownumb Network `"`=Jockey[`i']'"'
                local c: colnumb Network `"`=Jockey[`j']'"'
                matrix Network[`r', `c'] = 1
                matrix Network[`c', `r'] = 1
            }
        }
        exit
    end
    
    runby one_horse, by(Horse) verbose
    matrix list Network
    
    forvalues n = 1/`n_jockeys' {
        matrix Network = Network * Network
        forvalues i = 1/`n_jockeys' {
            forvalues j = `i'/`n_jockeys' {
                matrix Network[`i', `j'] = !!Network[`i', `j']
                matrix Network[`j', `i'] = !!Network[`j', `i']
            }
        }
    }
    
    matrix list Network
    
    gen component = .
    local c = 1
    foreach j of local jockeys {
        local n_changes = 0
        foreach  k of local jockeys {
            count if Network[`"`j'"', `"`k'"'] & missing(component) & Jockey == `"`k'"'
            local n_changes = `n_changes'  + `r(N)'
            replace component = `c' if Network[`"`j'"', `"`k'"'] & missing(component) ///
                & Jockey == `"`k'"'
        }
        if `n_changes' > 0 {
            local ++c
        }
    }
    
    browse
    Added: -runby- is written by Robert Picard and me, and is available from SSC.
    Last edited by Clyde Schechter; 16 Jan 2022, 23:33.

    Comment


    • #3
      Thanks for your very thorough answer, Clyde!
      Maybe there's an easier way, Thomas Grund ?

      Comment


      • #4
        Thanks for your very thorough answer, Clyde!
        Maybe there's an easier way using -nwcommands-, Thomas Grund ?

        Comment

        Working...
        X