Bookmark and Share

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: RE: Save -table- command results


From   Pablo Bonilla <[email protected]>
To   [email protected]
Subject   Re: st: RE: Save -table- command results
Date   Tue, 13 Mar 2012 10:19:18 -0400

This is great, Nick.

Seriously, thank you so much.

Best,


On Mon, Mar 12, 2012 at 9:06 PM, Nick Cox <[email protected]> wrote:
> I'll finish what I started. Weights now supported.
>
> N.B. This is just a demonstration for Pablo of the principle that you
> can write your own program. It stops here unless anyone else wants the
> code for their own.
>
> Examples:
>
>  sysuse auto
> (1978 Automobile Data)
>
> . mysums turn trunk mpg, by(rep78)
>
> . matrix li mysums
>
> mysums[5,3]
>    turn  trunk    mpg
> 1     82     17     42
> 2 347 117 153
> 3   1232    458    583
> 4 693 243 390
> 5 392 126 301
>
> . mysums turn trunk mpg, by(rep78) matname(different)
>
> . mat li different
>
> different[5,3]
>    turn  trunk    mpg
> 1     82     17     42
> 2 347 117 153
> 3   1232    458    583
> 4 693 243 390
> 5 392 126 301
>
> . mysums turn trunk mpg if foreign, by(rep78) matname(different)
>
> . mat li different
>
> different[3,3]
>    turn  trunk    mpg
> 3    106     37     70
> 4    315     93    224
> 5    320    107    237
>
>
> program mysums
>       version 8.2
>       syntax varlist(numeric) [if] [in] [aweight fweight iweight],
> by(varname) [matname(name)]
>
>       quietly {
>               marksample touse
>               markout `touse' `by', strok
>               count if `touse'
>               if r(N) == 0 error 2000
>
>               local J : word count `varlist'
>               tempvar group
>               egen `group' = group(`by') if `touse'
>               su `group', meanonly
>               local I = r(max)
>
>               if "`matname'" == "" local matname "mysums"
>               matrix `matname' = J(`I', `J', .)
>               tokenize `varlist'
>
>               forval i = 1/`I' {
>                       forval j = 1/`J' {
>                               su ``j'' if `group' == `i' [`weight'
> `exp'], meanonly
>                               matrix `matname'[`i', `j'] = r(sum)
>                       }
>               }
>
>               levelsof `by' if `touse'
>               capture matrix rownames `matname' = `r(levels)'
>               matrix colnames `matname' = `varlist'
>       }
> end
>
> On Mon, Mar 12, 2012 at 3:53 PM, Nick Cox <[email protected]> wrote:
>> At this distance -statsmat- doesn't seem a very interesting command to change. Kit may be more enthusiastic.
>>
>> It's perhaps more instructive to show how to write your own program. I didn't add in weights, but that's not difficult, and I have a meeting somewhere else in a few minutes' time.
>>
>> program mysums
>>        version 8.2
>>        syntax varlist(numeric) [if] [in] , by(varname) [matname(name)]
>>
>>        quietly {
>>                marksample touse
>>                markout `touse' `by', strok
>>                count if `touse'
>>                if r(N) == 0 error 2000
>>
>>                local J : word count `varlist'
>>                tempvar group
>>                egen `group' = group(`by') if `touse'
>>                su `group', meanonly
>>                local I = r(max)
>>
>>                if "`matname'" == "" local matname "mysums"
>>                matrix `matname' = J(`I', `J', 1)
>>                tokenize `varlist'
>>
>>                forval i = 1/`I' {
>>                        forval j = 1/`J' {
>>                                su ``j'' if `group' == `i', meanonly
>>                                matrix `matname'[`i', `j'] = r(sum)
>>                        }
>>                }
>>
>>                levelsof `by' if `touse'
>>                capture matrix rownames `matname' = `r(levels)'
>>                matrix colnames `matname' = `varlist'
>>        }
>> end
>>
>> Nick
>> [email protected]
>>
>>
>> -----Original Message-----
>> From: [email protected] [mailto:[email protected]] On Behalf Of Pablo Bonilla
>> Sent: 12 March 2012 15:33
>> To: Stata List
>> Subject: Re: st: RE: Save -table- command results
>>
>> Dear Nick,
>>
>> You'r right, I am sorry for not explain the provenance os -statsmat-.
>>
>> Thank you for the suggestion, but still it is faster and more efficient to use the -statsmat- command  because the preserver and the collapse commands are very slow in huge data bases like the one I am using.
>>
>> That would be great if the -statsmat- command can present results with the by() option for several variables.
>>
>> Best,
>>
>> Pablo.
>> Sent via BlackBerry from T-Mobile
>>
>> -----Original Message-----
>> From: Nick Cox <[email protected]>
>> Sender: [email protected]
>> Date: Mon, 12 Mar 2012 14:58:02
>> To: '[email protected]'<[email protected]>
>> Reply-To: [email protected]: st: RE: Save -table- command results
>>
>> -statsmat- is from SSC (Christopher F. Baum and friend). Please remember to explain the provenance of user-written commands.
>>
>> You don't have to use -statsmat- at all to get a matrix. There are many ways of doing it.
>>
>> Here is one:
>>
>> preserve
>> collapse (sum) var1 var2 var3 var4 var5 [w=factor], by(decil)
>> mkmat var*, matrix(mysums) row(decil)
>> restore
>>
>> Nick
>> [email protected]
>>
>> Pablo Bonilla
>>
>> I would like to know if there is a way to save the results from the
>> -table- command in matrices. I am using the -statsmat- command but It
>> is very inefficient if I need to use it with the by() option.
>>
>> For instance, this is what I want to save:
>> table decil [w = factor], c(sum var1  sum var2 sum var3  sum var4 sum
>> var5)  f(%19.2f) row
>>
>> But I have to do it in this way in order to get a matrix:
>> cap mat drop mat1
>> foreach var in var1 var2 var3 var4 var5 {
>>        statsmat `var' [w = factor], by(decile) stat(sum) format(%16.0f)
>> matrix(`income')
>>        mat mat1 = nullmat(INCOME),`income'
>> }
>>
>
> *
> *   For searches and help try:
> *   http://www.stata.com/help.cgi?search
> *   http://www.stata.com/support/statalist/faq
> *   http://www.ats.ucla.edu/stat/stata/



-- 
----

Pablo Bonilla

*
*   For searches and help try:
*   http://www.stata.com/help.cgi?search
*   http://www.stata.com/support/statalist/faq
*   http://www.ats.ucla.edu/stat/stata/


© Copyright 1996–2018 StataCorp LLC   |   Terms of use   |   Privacy   |   Contact us   |   Site index