Notice: On April 23, 2014, Statalist moved from an email list to a forum, based at statalist.org.
[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]
Re: st: Sum of products by sub-groups in mata.
From
Amadou DIALLO <[email protected]>
To
[email protected]
Subject
Re: st: Sum of products by sub-groups in mata.
Date
Mon, 20 Jan 2014 21:04:19 +0100
Dear Nick,
Sorry I was stuck in a meeting.
Many thanks for your answer. However, I find it difficult to do very
basic tasks in mata (as doing a simple egen sum = sum(x) in stata).
Following your example for instance, and doing minor changes, to
populate another matrix with the number of occurrences, I do the
following:
mata: y = ("frog", "frog", "toad", "newt")
mata: allz = .
mata: x = (1, 4, 2,3)
mata: z = (., ., .,.)
mata: sum= sum(x :* (y :== "frog"))
mata: z=sum:*(y :== "frog")
mata: z
mata: z1 = select(z, z[.,.]:>0)
mata: z1
mata: allz = z1
mata: allz
mata: sum= sum(x :* (y :== "toad"))
mata: z=sum:*(y :== "toad")
mata: z
mata: z2 = select(z, z[.,.]:>0)
mata: z2
mata: allz = allz , z2
mata: allz
mata: sum= sum(x :* (y :== "newt"))
mata: z=sum:*(y :== "newt")
mata: z
mata: z3 = select(z, z[.,.]:>0)
mata: z3
mata: allz = allz , z3
mata: allz
mata: sum= sum(x :* (y :== "blabla"))
mata: z=sum:*(y :== "blabla")
mata: z
mata: z4 = select(z, z[.,.]:>0)
mata: z4
mata: allz = allz , z4
mata: allz
This is not very elegant, I believe. I think mata definitely needs the
same sort of "if" qualifiers, as in Stata.
Many thanks for your usual support.
2014/1/20, Nick Cox <[email protected]>:
> Mata has an -if- but nothing quite like Stata's -if- qualifier. But
> neither is in general a good solution to selective summation.
>
> : y = ("frog", "toad", "newt")
>
> : x = (1, 2,3)
>
> : sum(x :* (y :== "frog"))
> 1
>
> See also -select()- and -panelsetup()-.
>
> Nick
> [email protected]
>
>
> On 20 January 2014 16:47, Amadou DIALLO <[email protected]> wrote:
>> Hi,
>> I want to compute in mata total by subgroup (the equivalent of stata's
>> bys group: egen total = total(expense) ).How to do that?
>>
>> Also, is it possible to use "if" condition with mata? SOmething like:
>>
>> mata:
>> sum = 0
>> for (i=1; i<= maxcountry; i++) {
>> sum = sum + sum(expense) if country == i
>> }
>> sum
>> end
>>
>> --
>> Amadou B. DIALLO, PhD.
>> Senior Economist, AfDB.
>> [email protected]
>> +21671101789
>> *
>> * For searches and help try:
>> * http://www.stata.com/help.cgi?search
>> * http://www.stata.com/support/faqs/resources/statalist-faq/
>> * http://www.ats.ucla.edu/stat/stata/
> *
> * For searches and help try:
> * http://www.stata.com/help.cgi?search
> * http://www.stata.com/support/faqs/resources/statalist-faq/
> * http://www.ats.ucla.edu/stat/stata/
>
--
Amadou B. DIALLO, PhD.
Senior Economist, AfDB.
[email protected]
+21671101789
*
* For searches and help try:
* http://www.stata.com/help.cgi?search
* http://www.stata.com/support/faqs/resources/statalist-faq/
* http://www.ats.ucla.edu/stat/stata/