Stata The Stata listserver
[Date Prev][Date Next][Thread Prev][Thread Next][Date index][Thread index]

st: Re: Save -tabulate- output as matrix


From   Friedrich Huebler <huebler@rocketmail.com>
To   statalist@hsphsun2.harvard.edu
Subject   st: Re: Save -tabulate- output as matrix
Date   Thu, 14 Oct 2004 09:27:33 -0700 (PDT)

Nick,

Thank you for your program. I deleted the previous messages from the
thread so let me restate what I would like to do: I am looking for a
way to save -tabulate- output (the frequencies and the percent
values) as a matrix. The program by Nick works with frequency weights
but when analytic weights are used the matrix is different from the
-tabulate- output.

. sysuse auto
. tab foreign [aw=weight]

   Car type |      Freq.     Percent        Cum.
------------+-----------------------------------
   Domestic | 57.1261189       77.20       77.20
    Foreign | 16.8738811       22.80      100.00
------------+-----------------------------------
      Total |         74      100.00

. friedrich2 foreign [aw=weight], matrix(mat)
. mat list mat

mat[2,2]
               freq    percent
Domestic         52  70.270271
 Foreign         22  29.729731

The original program uses -collapse (count)-. I changed this to
-collapse (sum)- but the result is the same. The -collapse-
documentation in the Reference Manual explains that with (count) the
unweighted and analytically weighted results are identical. With
(sum) I expected the unweighted and analytically weighted results to
be different. My copy of the manual has these definitions on page
211:

sum:
unweighted: sum(x_j), the sum of the variable
aweight: sum(v_j*x_j); v_j = (w_j normalized to sum to _N)

Is my interpretation of the definitions wrong or does -collapse
(sum)- with analytic weights not do what the manual states?

Friedrich Huebler

--- Nick Cox <n.j.cox@durham.ac.uk> wrote:
> 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


		
_______________________________
Do you Yahoo!?
Declare Yourself - Register online to vote today!
http://vote.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/



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