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

st: RE: Matrix from -tabstat- output


From   "Nick Cox" <[email protected]>
To   <[email protected]>
Subject   st: RE: Matrix from -tabstat- output
Date   Thu, 16 Oct 2003 18:52:54 +0100

Friedrich Huebler
 
> I am looking for a flexible way to create matrices from -tabstat-
> output that does not require previous knowledge of the 
> number of rows
> in the table. Let's take a household survey dataset with some
> indicator and make a table for a subsample of persons aged 20-22
> years.
> 
> . tabstat yes no if age>=20 & age<=22, by(age) save
> 
> age |       yes        no
> ----+--------------------
>  20 |        60        40
>  21 |        50        50
>  22 |        55        45
> ----+--------------------
> Tot |        52        48
> -------------------------
> 
> . return list
> 
> macros:
>              r(name3) : "22"
>              r(name2) : "21"
>              r(name1) : "20"
> 
> matrices:
>              r(Stat3) :  1 x 2
>              r(Stat2) :  1 x 2
>              r(Stat1) :  1 x 2
>            r(StatTot) :  1 x 2
> 
> Now I create a matrix. Note that I have to spell out the 
> names of the
> macros and matrices saved by -tabstat-.
> 
> . matrix sample1 = (r(Stat1) \ r(Stat2) \ r(Stat3) \ r(StatTot))
> . matrix rownames sample1 = `r(name1)' `r(name2)' `r(name3)' Total
> . matrix list sample1
> 
> sample1[4,2]
>        yes   no
>    20   60   40
>    21   50   50
>    22   55   45
> Total   52   48
> 
> Are there alternatives to the commands that create the matrix and
> assign row names? There should be commands that do not require
> spelling out "r(Stat1) ..." and "`r(name1)' ..." These alternative
> commands should work for -tabstat- tables of different lengths, for
> example for persons aged 20-29 or 15-64 years.

*! NJC 1.0.0 16 Oct 2003 
program tabstatmat  
	version 8 
	
	args matout garbage 
	if "`matout'" == "" | "`garbage'" != "" error 198 

	if "`r(name1)'" == "" { 
		di as err "nothing found"
		exit 498
	} 	

	// how many vectors? 
	local I = 1 
	while "`r(name`I')'" != "" { 
		local ++I 
	} 	
	local --I 

	// build up matrix 
	if rowsof(r(Stat1)) == 1 { 
		forval i = 1/`I' { 
			local vectors "`vectors' r(Stat`i') \"
			local names   "`names' `r(name`i')'" 
		} 
		matrix `matout' = `vectors' r(StatTot)
	} 
	else { 
		forval i = 1/`I' { 
			local vectors "`vectors' (r(Stat`i'))' \"
			local names   "`names' `r(name`i')'" 
		} 
		matrix `matout' = `vectors' (r(StatTot))'  
	} 	

	matrix rownames `matout' = `names' Total 
	matrix list `matout'
end 
 
Examples: 

. tabstat mpg , by(rep78) 

Summary for variables: mpg
     by categories of: rep78 (Repair Record 1978)

   rep78 |      mean
---------+----------
       1 |        21
       2 |    19.125
       3 |  19.43333
       4 |  21.66667
       5 |  27.36364
---------+----------
   Total |  21.28986
--------------------

. tabstatmat f1
nothing found
r(498);

. tabstat mpg , by(rep78) save

Summary for variables: mpg
     by categories of: rep78 (Repair Record 1978)

   rep78 |      mean
---------+----------
       1 |        21
       2 |    19.125
       3 |  19.43333
       4 |  21.66667
       5 |  27.36364
---------+----------
   Total |  21.28986
--------------------

. tabstatmat f1

f1[6,1]
             mpg
    1         21
    2     19.125
    3  19.433333
    4  21.666667
    5  27.363636
Total  21.289855

. tabstat mpg , by(rep78) s(n mean sd) save

Summary for variables: mpg
     by categories of: rep78 (Repair Record 1978)

   rep78 |         N      mean        sd
---------+------------------------------
       1 |         2        21  4.242641
       2 |         8    19.125  3.758324
       3 |        30  19.43333  4.141325
       4 |        18  21.66667   4.93487
       5 |        11  27.36364  8.732385
---------+------------------------------
   Total |        69  21.28986  5.866408
----------------------------------------

. tabstatmat f2

f2[6,3]
               N       mean         sd
    1          2         21  4.2426407
    2          8     19.125  3.7583241
    3         30  19.433333  4.1413252
    4         18  21.666667  4.9348699
    5         11  27.363636  8.7323849
Total         69  21.289855  5.8664085

Nick 
[email protected] 

*
*   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–2024 StataCorp LLC   |   Terms of use   |   Privacy   |   Contact us   |   What's new   |   Site index