Announcement

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

  • Collapse simple mean vs weighted mean

    Dear Stata Users

    I estimate the means of the variables (BuyInd SellInd BuyFor SellFor BuyInst SellInst) every day using the following code:
    Code:
    collapse (mean) BuyInd SellInd BuyFor SellFor BuyInst SellInst, by (date)
    The above code generates simple mean or equally weighted mean. I need, however, to obtain weighted mean. How do I modify a code to get a weighted mean?

    Here is my data:

    Code:
    * Example generated by -dataex-. To install: ssc install dataex
    clear
    input float(date plliq BuyInd SellInd BuyFor SellFor BuyInst SellInst)
    16072 5     .0048265   .00514978  .0005249372             0  .0009264596  .0011281166
    16072 5   .026954187  .026954187            0             0            0            0
    16072 5  .0016120513 .0019503017            0             0  .0004185702 .00008031985
    16072 5   .012340236 .0087637985  .0003407517    .003423757 5.635035e-07  .0004939964
    16072 5   .002392859 .0038890305  .0019538396  .00020748073  .0019767915  .0022273413
    16072 5 .00011021517 .0004738432  .0006776199  .00026347156  .0006201588  .0006706791
    16072 5   .002518975  .001352896            0  .00021204563   .001995555  .0029495885
    16072 5   .017506504  .015926434            0             0  .0008038175   .002342354
    16072 5   .000397253  .000397253            0             0            0            0
    16072 5   .006631209  .007130237  .0006869691             0  .0006791243  .0008699472
    16072 5   .000572428  .000572428            0             0            0            0
    16072 5   .005092801 .0043995036 .00020404154             0  .0008944882  .0017924205
    16075 5   .004448192 .0038269504 .00015038408   .0007907743 .00004419271  .0000250441
    16075 5  .0007222078 .0007697624  .0010697992   .0010961189  .0011561238  .0010830406
    16075 5   .008227355  .008100635            0             0            0 5.175026e-06
    16076 5   .016070643  .016070643            0             0            0            0
    16076 5   .000455775 .0004506543            0             0            0 5.120728e-06
    16076 5   .006828757  .015129183   .007152455 1.4877507e-07   .003212899    .00205061
    16076 5      .005772     .005772            0             0            0            0
    16076 5  .0010874784 .0008432461  .0015770105   .0010703243  .0007035305  .0014544488
    16076 5    .02518045  .022424614            0    .002503478 6.089507e-06  .0002584457
    16076 5   .015849909   .02069944            0             0   .005229685  .0003970034
    16076 5    .00975617  .009559203            0             0            0            0
    16076 5   .002576984 .0039177756  .0014731478   .0008038678  .0029307806  .0022532057
    16076 5      .012139  .008288234            0   .0017975065 .00046382615   .002517085
    16076 5   .017540585  .016606633            0             0 .00026425425  .0011982056
    16076 5   .005528524  .004833599            0             0  .0017002648  .0023951896
    16076 5    .07193081   .07179546            0             0 .00028620078  .0004804352
    16076 5    .02056691  .011103123   .004931806   .0012702686   .003156923   .016470553
    16076 5  .0019304045 .0019304045            0             0            0            0
    end
    format %tdNN/DD/CCYY date


    Thank you.


  • #2
    Well, you have to have a variable in the data set that contains the weight you want to apply to each observation. In the example you show, there is no obvious candidate for that: date would be bizarre as a weight, and plliq is just a constant, so the weighted mean wouldn't be any different from the unweighted one. I'll just assume there's another variable you didn't show, which contains the weight. Let's call it my_weight_var.

    Code:
    collapse (mean) BuyInd SellInd BuyFor SellFor BuyInst SellInst [iweight = my_weight_var], by (date)
    This is the same as your version, except for the italicized part.

    Comment

    Working...
    X