Notice: On March 31, it was announced that Statalist is moving from an email list to a forum. The old list will shut down on April 23, and its replacement, statalist.org is already up and running.
[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]
Re: st: collapse and weights
Austin Nichols <email@example.com>
Re: st: collapse and weights
Mon, 26 Nov 2012 19:23:50 +0100
OP contacted me off-list to say:
On Mon, Nov 26, 2012 at 7:16 PM, Tomáš Houška <firstname.lastname@example.org> wrote:
> Dear Mr. Nicholson,
> thank you for your help on Stata collapse. But your command is exactly
> what i have tried and what fails to accomplish my goal.
> Lets say you have variables "quantity" and "price", each one has two
> observations, (2,4) and (3,6) respectively. Now my aim is to get two
> new variables, that would say "sum of quantity" and "average price
> weighted by quantity". If you use the command
> --collapse (sum) sum_q=quantity (mean) wavg_price=price [fw=quantity] --
> you get wavg_price = 5 (which is correct; (2*3+4*6)/(2+4)), but for
> sum_q you get "20" => which is the weighted sum (2*2+4*4). the
> "fweight" bracket applies to the sum as well as to the mean. That is
> the problem which I am trying to solve. I would be greatfull for any
> tips on this.
Obviously, the OP did not read the two lines of code I posted, nor my
last name, very carefully.
More generally, do you think it is better to repost off-list emails to
the list, or to completely ignore them? In the past, I have ignored
them as akin to spam, and in clear violation of the Statalist FAQ
guidelines, but I am unsure as to which is a good general rule.
On Mon, Nov 26, 2012 at 6:36 PM, Austin Nichols <email@example.com> wrote:
> Tomáš Houška <firstname.lastname@example.org>:
> Your goal is a bit hard to understand from the description--are you
> saying you want the sum of q and the q-weighted mean of price?
> g one=1
> collapse (sum) sum=one (mean) price [fweight=q], by(prod_group)
> On Sun, Nov 25, 2012 at 9:44 AM, Tomáš Houška <email@example.com> wrote:
>> Dear Statalist,
>> I have data containing weekly information on prices and quantities of
>> sales of several products. These products can be divided into few
>> groups and I would like to create for each group 1) an weighted
>> average weekly price for the group where weight is the amounts of sale
>> of each product in the group and 2) sum of sales of the products that
>> belong to particular group. I will end up with average price and total
>> sales for the given product group.
>> But if I use
>> -- collapse (sum) q (mean) price [fweight=q], by(prod_group) --
>> the weights get applied both to the average and the sum, which is not
>> correct. I havent been able to come up with a solution how to bracket
>> the stata command. Could someone please give me some guidance?
>> Thank you!
>> Tomas (PhD student)
* For searches and help try: