Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Some commands appear to ignore [w=weights] #88

Open
erciomunoz opened this issue Nov 25, 2022 · 3 comments
Open

Some commands appear to ignore [w=weights] #88

erciomunoz opened this issue Nov 25, 2022 · 3 comments
Labels

Comments

@erciomunoz
Copy link

I noticed that some commands like gegen take weights but actually do not use them. For example, if you try to generate a count or total of a variable and use weights. Below I provide an example where the total is 5, but I thought it would be 25 if using the weights. Perhaps it would be good to stop allowing the use of weights and give an error if the user uses them for these particular commands.

set obs 5
g units=1
g weight=5
gegen total=total(units) [w=weight]
sum total
//     Variable |        Obs        Mean    Std. Dev.       Min        Max
// -------------+---------------------------------------------------------
//       total |          5           5           0          5          5
@mcaceresb
Copy link
Owner

@erciomunoz This is not a gtools bug per se, so to the extent it's a bug at all it would be on Stata. As documented in the help files, weights in gegen and gcollapse are meant to mimic collapse (see weights section of the collapse help pdf). You can see

clear
set obs 5
g units=1
g weight=5
collapse (sum) units [w = weight]
disp units

Gives 5 as the answer. The reason is that aweights are normalized to sum to the number of observations. Try fweight, pweight, or iweight to see the difference. Also

clear
set obs 5
g units=_n
g weight=_n
gegen total =total(units) [w=weight]
gegen totalu=total(units)
sum total*

Gives a different answer, showing weights are not being ignored (they just work different than how you might intuit). Cheers.

@mcaceresb mcaceresb changed the title Some commands accept [w=weights] but do not use them. Some commands appear to ignore [w=weights] Nov 29, 2022
@mcaceresb
Copy link
Owner

mcaceresb commented Nov 29, 2022

@erciomunoz I've decided to leave this issue up in case others have a similar question about how gegen and gcollapse weights are meant to work. I've changed the title to one I think is more precise but LMK if you disagree. Cheers.

@erciomunoz
Copy link
Author

Thanks for the quick reply.

This makes total sense, now I regret a little bit that I did not spend more time thinking about it before posting.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Projects
None yet
Development

No branches or pull requests

2 participants