Announcement

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

  • Compute a weighted sum

    Dear fellow Stata users,

    I'm trying to assign weights to some variables in my dataset to compute a weighted sum but I can't figure out how. I have a variable of weights. The weight in the first row (1.1) should be assigned to v1, the weight in the second row (3.4) to v2, etc. So then I'm able to compute a weighted sum, which for the first observation would be equal to 4*1.1 + 0*3.4 + 1*3.5 + 2*2.3 , and for the second 3*1.1 + 0*3.4 + 4*3.5 + 3*2.3

    Code:
    * Example generated by -dataex-. For more info, type help dataex
    clear
    input byte(v1 v2 v3 v4 unweighted_sum) float weights
     4  0 1 2  7 1.1
     3  0 4 3 10 3.4
     2  0 7 4 13 3.5
     1 10 5 7 23 2.3
     2  0 1 5  8   .
     1  0 0 9 10   .
     2  3 5 0 10   .
    11  4 6 0 21   .
    end

    I hope I made myself clear.

    Thanks in advance.


  • #2
    Some people would use a vector to hold those weights -- meaning in Stata a matrix with 1 column -- but your data structure is convenient enough


    Code:
    * Example generated by -dataex-. For more info, type help dataex
    clear
    input byte(v1 v2 v3 v4 unweighted_sum) float weights
     4  0 1 2  7 1.1
     3  0 4 3 10 3.4
     2  0 7 4 13 3.5
     1 10 5 7 23 2.3
     2  0 1 5  8   .
     1  0 0 9 10   .
     2  3 5 0 10   .
    11  4 6 0 21   .
    end 
    
    gen double wanted = 0 
    
    forval j = 1/4 { 
        replace wanted = wanted + v`j' * weights[`j']
    }
    
    list 
    
         +----------------------------------------------------+
         | v1   v2   v3   v4   unweig~m   weights      wanted |
         |----------------------------------------------------|
      1. |  4    0    1    2          7       1.1        12.5 |
      2. |  3    0    4    3         10       3.4        24.2 |
      3. |  2    0    7    4         13       3.5        35.9 |
      4. |  1   10    5    7         23       2.3   68.700001 |
      5. |  2    0    1    5          8         .        17.2 |
         |----------------------------------------------------|
      6. |  1    0    0    9         10         .        21.8 |
      7. |  2    3    5    0         10         .        29.9 |
      8. | 11    4    6    0         21         .   46.700001 |
         +----------------------------------------------------+
    I wrote a loop here just in case you or someone else says "OK, but my real problem is much bigger" if I post a one-liner

    Code:
    gen double wanted = v1 * weights[1] + v2 * weights[2] +  ...
    where the dots need to be replaced by equivalent terms.

    Comment

    Working...
    X