Announcement

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

  • How to get a weighted count of observations using sum

    I want to use sum to get a weighted count of observations. I can weight sum, but the memory, r(N), is unweighted. How can I get a memory of a weighted count?

  • #2
    I think you need to show us the exact code you tried and the exact output you got from Stata (including the results of -return list-). I can't replicate your problem:

    Code:
    . sysuse auto, clear
    (1978 Automobile Data)
    
    . count
      74
    
    . gen wt = rpoisson(3)
    
    . summ price [fweight = wt]
    
        Variable |        Obs        Mean    Std. Dev.       Min        Max
    -------------+---------------------------------------------------------
           price |        214    5839.346    2643.194       3291      15906
    
    . return list
    
    scalars:
                      r(N) =  214
                  r(sum_w) =  214
                   r(mean) =  5839.345794392523
                    r(Var) =  6986474.85639068
                     r(sd) =  2643.194063323895
                    r(min) =  3291
                    r(max) =  15906
                    r(sum) =  1249620
    
    .

    Comment


    • #3
      Thanks for the nudge Clyde. Below is how I corrected what I was doing. I was using data from IPUMS and using their "perwt" as the weighting variable but I had not classified the weight as an fweight. Once I did that it produced an estimate of the population statistic. Before weighting the N was 2718. After fweighting it was 308381. FYI I am counting the number of people working in the auto industry (ind1950==376) in 1950. Thanks again.

      gen numm = _n;

      sum numm;

      sum numm if ind1950==376;

      gen tot376 = r(N);

      display tot376;

      sum numm if ind1950==376 [fweight=perwt];

      gen weighttot376 = r(N);

      display tot376;

      display weighttot376;


      Variable | Obs Mean Std. Dev. Min
      > Max
      -------------+-----------------------------------------------
      > ---------
      numm | 143559 71780 41442.06 1
      > 143559

      Variable | Obs Mean Std. Dev. Min
      > Max
      -------------+-----------------------------------------------
      > ---------
      numm | 2718 69548.55 41451.88 176
      > 143356
      2718

      Variable | Obs Mean Std. Dev. Min
      > Max
      -------------+-----------------------------------------------
      > ---------
      numm | 308381 69279.89 41776.65 176
      > 143356
      2718
      308381

      Comment

      Working...
      X