# st: RE: Histogram with weights

 From "Nick Cox" To Subject st: RE: Histogram with weights Date Thu, 3 Apr 2008 18:49:21 +0100

```Because nobody implemented that yet, perhaps because frequency is no longer defined for non-integer weights. But fraction, percent and density do make sense.

Doing it yourself is within range, e.g.:

sysuse auto, clear
* midpoints of bins of width 2, starting at 0
gen mpg2 = 1 + 2 * floor(mpg/2)
* use price as aw
egen binheight = sum(price) , by(mpg2)
* need to use each bin just once
egen tag = tag(mpg2)
* get total of weights
su binheight if tag, meanonly
di r(sum)
replace binheight = binheight/r(sum)
twoway bar binheight mpg2 if tag, bstyle(histogram) barw(2) ///
xtitle(`: var label mpg') ytitle(fraction)

Nick
n.j.cox@durham.ac.uk

Tunga Kantarci

Why I cannot use weights with a histogram?
Why my weights should be integers?
(SPPS can do this)

histogram ab071 [fweight = weging], frequency
may not use noninteger frequency weights

histogram ab071 [iweight = weging]
iweight not allowed

histogram ab071 [aweight = weging]
aweight not allowed

. tab weging

weegfactor |      Freq.     Percent        Cum.
------------+-----------------------------------
.7643276 |        694        1.43        1.43
.8073236 |        745        1.53        2.96
.808844 |        609        1.25        4.21
.8225074 |        667        1.37        5.58
.8710093 |        656        1.35        6.93
.8844799 |        432        0.89        7.82
.8849404 |        722        1.48        9.31
.88766 |        544        1.12       10.42
.888154 |        568        1.17       11.59
.8946 |        479        0.99       12.58
.9158224 |        348        0.72       13.29
.9165806 |        622        1.28       14.57
.9171282 |        618        1.27       15.84
.9188034 |        384        0.79       16.63
.9256861 |        558        1.15       17.78
.9471917 |        503        1.03       18.82
1 |     35,453       72.91       91.73
1.001637 |         87        0.18       91.91
1.050009 |        560        1.15       93.06
1.090102 |        487        1.00       94.06
1.090514 |        337        0.69       94.75
1.183675 |        479        0.99       95.74
1.190906 |        524        1.08       96.82
1.236765 |        376        0.77       97.59
1.246083 |        332        0.68       98.27
1.457202 |         60        0.12       98.40
1.460718 |        195        0.40       98.80
1.642645 |        186        0.38       99.18
1.948541 |        136        0.28       99.46
2.047319 |        232        0.48       99.94
2.527611 |         31        0.06      100.00
------------+-----------------------------------
Total |     48,624      100.00

. sum  weging

Variable |       Obs        Mean    Std. Dev.       Min        Max
-------------+--------------------------------------------------------
weging |     48624    .9989959    .1312408   .7643276   2.527611

*
*   For searches and help try:
*   http://www.stata.com/support/faqs/res/findit.html
*   http://www.stata.com/support/statalist/faq
*   http://www.ats.ucla.edu/stat/stata/
```