[Date Prev][Date Next][Thread Prev][Thread Next][Date index][Thread index]

From |
"Nick Cox" <n.j.cox@durham.ac.uk> |

To |
<statalist@hsphsun2.harvard.edu> |

Subject |
st: RE: Re: Save -tabulate- output as matrix |

Date |
Thu, 14 Oct 2004 01:33:10 +0100 |

Is this closer to what you want? Not very efficient, so can be much improved. program friedrich2 version 8 syntax varname [if] [in] [fweight aweight] , matrix(string) preserve tempvar freq gen `freq' = 1 collapse (count) `freq' `if' `in' [`weight' `exp'], by(`varlist') rename `freq' freq egen percent = pc(freq) mkmat freq percent, mat(`matrix') forval i = 1/`= rowsof(`matrix')' { local name : label (`varlist') `= `varlist'[`i']' if `"`name'"' == "" local name = `varlist'[`i'] if `"`name'"' == "." local name "missing" local names `"`names' `"`name'"' "' } matrix rownames `matrix' = `names' end Nick n.j.cox@durham.ac.uk > -----Original Message----- > From: owner-statalist@hsphsun2.harvard.edu > [mailto:owner-statalist@hsphsun2.harvard.edu]On Behalf Of Friedrich > Huebler > Sent: 14 October 2004 00:48 > To: statalist@hsphsun2.harvard.edu > Subject: st: Re: Save -tabulate- output as matrix > > > Thank you for your suggestions, Nick. -contract- does not work with > my data because it only accepts frequency weights. I looked at > -groups- but I am afraid I don't have the skills to modify it. > > Instead, I found a solution that involves -egen- and -tabstat- so > that -tabstatmat- can be used to create the matrix. I have to do some > additional work to normalize the weight so that the results are > identical to the -tabulate- output. > > . sysuse auto > . sum weight > . gen normwgt = weight/r(mean) > . bysort foreign: egen wgtcount = sum(normwgt) > . egen wgtsum = sum(normwgt) > . gen wgtshare = 100*wgtcount/wgtsum > . tabstat wgtshare, by(foreign) stat(mean count) nototal save > . tabstatmat share > . tabstat wgtcount, by(foreign) stat(mean count) nototal save > . tabstatmat count > . matrix table = share, count > . matrix list table > > table[2,4] > mean N mean N > Domestic 77.197456 52 57.126118 52 > Foreign 22.802542 22 16.87388 22 > > The matrix contains the same values as this table: > > . tab foreign [aw=weight] > > Car type | Freq. Percent Cum. > ---------+----------------------------------- > Domestic | 57.1261189 77.20 77.20 > Foreign | 16.8738811 22.80 100.00 > > I have a separate question about saving a matrix with correct row > names and I will start a new thread for this. > > Friedrich Huebler > > --- Nick Cox <n.j.cox@durham.ac.uk> wrote: > > Various things come close. The easiest way to do this > > I can think of is > > > > preserve > > contract ... , freq(freq) percent(percent) > > mkmat freq percent, matrix(mymatrix) > > levels ..., local(levels) > > mat rownames mymatrix = `levels' > > restore > > > > You could hack at a clone of -groups- (SSC) > > to make it save matrices optionally. > > > > Friedrich Huebler > > > > > I am looking for a way to save -tabulate- output as a matrix, > > > with row names and preferably without the "Total" row. > > > > > > . sysuse auto > > > . tab foreign > > > > > > Car type | Freq. Percent Cum. > > > ---------+----------------------------------- > > > Domestic | 52 70.27 70.27 > > > Foreign | 22 29.73 100.00 > > > ---------+----------------------------------- > > > Total | 74 100.00 > > > > > > I would like to create a matrix that looks like this: > > > > > > tab[2,2] > > > freq percent > > > Domestic 52 70.27 > > > Foreign 22 29.73 > > > > > _______________________________ > Do you Yahoo!? > Express yourself with Y! Messenger! Free. Download now. > http://messenger.yahoo.com > * > * For searches and help try: > * http://www.stata.com/support/faqs/res/findit.html > * http://www.stata.com/support/statalist/faq > * http://www.ats.ucla.edu/stat/stata/ > * * For searches and help try: * http://www.stata.com/support/faqs/res/findit.html * http://www.stata.com/support/statalist/faq * http://www.ats.ucla.edu/stat/stata/

**Follow-Ups**:**st: Re: Save -tabulate- output as matrix***From:*Friedrich Huebler <huebler@rocketmail.com>

- Prev by Date:
**st: RE: RE: RE: Negative Binomial Models** - Next by Date:
**st: RE: RE: RE: RE: Negative Binomial Models** - Previous by thread:
**st: RE: RE: RE: Negative Binomial Models** - Next by thread:
**st: Re: Save -tabulate- output as matrix** - Index(es):

© Copyright 1996–2014 StataCorp LP | Terms of use | Privacy | Contact us | What's new | Site index |