0
votes

I'm doing an analysis of the Current Population Survey. I have a wage variable (wage), a time-series variable (qtr), and an observational weight (pworwgt). Each quarter has thousands of observations.

I can easily make a table showing the weighted average wage in each quarter:

table qtr [iw=pworwgt], contents(mean wage)

What I want to do, however, is graph this easily within Stata. I tried to use egen to make a variable containing the mean by qtr, but egen mean() does not allow for weights.

1
It may be easy just to use egen's total() function to get the numerator and denominator of a weighted average, and then divide. There is a user-written egen function for this purpose, but doing it from first principles should be easy and instructive. Or even easier is to use collapse to get a reduced dataset of averages. - Nick Cox

1 Answers

1
votes

One of the many ways this can be done is with a regression followed by two margins* commands:

webuse hanley
table rating [iw=pop], contents(mean disease)
reg disease i.rating [iw=pop]
margins rating
marginsplot, noci

This has the advantage of not altering your data in any way.