Announcement

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

  • multiply vectors; dot product

    This is probably very easy, but I don't know how to do it.

    1) If part1, part2, part3 in the example below are a vector, I need the dot product of the vector for every pair in every year. Said another way, if person 1 and person 2 (in the below example) are the first pair: I need to multiply (0.33 * 0.2) + (0.33 * 0) + (0.34 * 0.8). I need to do this for all pairwise combinations within the same year. And repeat for all years.

    2) Then for each person, within each year, I need to sum their scores from step 1).

    Code:
    clear
    input person year part1 part2 part3
    1 2010 .33 .33 .34
    2 2010 .2 0 .8
    3 2010 .2 0 .8
    1 2011 .4 .3 .4
    2 2011 .5 .3 .2
    3 2011 .1 .2 .7
    end
    The multplying part is easy. Doing it for every pairwise combination and summing across all pairwise combinations for each person within each year is not as easy.

    I checked it by hand (after step 2) for person 1 in 2010 and got 0.676. For persons 2 and 3 in 2010, I got 1.018.

    Thanks in advance.

  • #2
    Code:
    reshape long part, i(person year) j(index)
    preserve
    rename (person part) =_2
    tempfile copy
    save `copy'
    
    restore
    
    joinby year index using `copy'
    
    by person person_2 year (index), sort: egen dot_product = total(part*part_2)
    by person person_2 year: keep if _n == 1
    drop part part_2 index

    Comment


    • #3
      Thanks alot Clyde.

      Comment

      Working...
      X