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

st: RE: tabulate w/ fweights = 0


From   "Nick Cox" <[email protected]>
To   <[email protected]>
Subject   st: RE: tabulate w/ fweights = 0
Date   Mon, 26 May 2003 18:53:31 +0100

Danielle H. Ferry

> I am using -tabulate- to produce 2-way tables of freq
> counts with fweights.
> In some cases, my fweights are equal to zero. -tabulate-
> then excludes that
> part of the table.
>
> More specifically, this is what I get:
>
> . ta year parity [fw=abrtNum] if race==2 & state=="ID"
>
>    Year of |      Parity (1)
> conception |         0         1+ |     Total
> -----------+----------------------+----------
>       1992 |         2          0 |         2
>       1996 |         1          1 |         2
>       1997 |         1          1 |         2
>       1998 |         4          3 |         7
>       1999 |         4          0 |         4
> -----------+----------------------+----------
>      Total |        12          5 |        17
>
> Since the fweight equals zero for the years 1993-1995,
> these years don't
> appear in the resulting table. I would like these years to
> appear w/ just
> zeros as freq counts. Any ideas? -tabulate- is not an
> option here, b/c I use
> the -matcell- option to save these freq counts and then do matrix
> operations.

Jorge Tuesta Egusquiza asked a similar question:

> I am working with databases of agricultural topics. I have serious
problems
> with the construction of tables of frequency counts. I`d like a
two-way table
> with ALL the categories that exist, and not only with the categories
that
> already have frecuencies. For example,  I have two variables:  "a"
> and "b". "a" has four categories (1, 2, 3 and 4) and "b" has three
categories
> (1, 2 and 3), but one category of "b" (1) doesn`t have frecuency
counts
> in "a". The order

> table a b
>
> produce:
>
>       |                  b
>          a |         2          3 |     Total
> -----------+----------------------+----------
>          1 |         1          0 |         1
>          2 |         1          1 |         2
>          3 |         0          1 |         1
>          4 |         1          0 |         1
> -----------+----------------------+----------
>      Total |         3          2 |         5
>
> but i`d like:
>
>
>       |                      b
>          a |    1         2          3 |     Total
> -----------+----------------------+----------
>          1 |    0         1          0 |         1
>          2 |    0         1          1 |         2
>          3 |    0         0          1 |         1
>          4 |    0         1          0 |         1
> -----------+----------------------+----------
>      Total |   0         3          2 |         5
>

Quite what Jorge is expecting is not clear to me. Stata shows strong
reluctance
not to -tabulate- what might exist but does not in fact occur within
the data, metaphysics not being its strong suit. The absence of
a column for b = 1 could, it seems, only arise if there were no
such values. Perhaps Jorge can comment further.

Danielle's problem is clear, but I don't know of a workaround
with -tabulate-.

Note that a thread a while back produced some workarounds from Shannon
Driver, Michael Blasnik and myself for a related problem with one-way
tables.
Danielle's problem seems to require some new code. The main
feature of this is the -matcell()- option.

program tabzero
*! NJC 1.0.0 26 May 2003
	version 8
	syntax varlist(max=2) [fweight/] [if] [in] [, matcell(str)]

	quietly {
		marksample touse, strok zeroweight
		count if `touse'
		if r(N) == 0 error 2000

		preserve
		tempvar freq
		if "`exp'" == "" local exp 1
		keep if `touse'
		bysort `varlist': gen `freq' = sum(`exp')
		by `varlist': keep if _n == _N

		if `: word count `varlist'' == 2 {
			fillin `varlist'
			replace `freq' = 0 if _fillin
			drop _fillin
		}

		local i = 1
		qui foreach v of var `varlist' {
			levels `v', local(v`i')
			local n`i' : word count `v`i++''
		}

		if "`n2'" == "" {
			local n2 = 1
			local v2 "`2'"
		}

		if "`matcell'" != "" {
			mat `matcell' = J(`n1',`n2',0)

			tempvar i j
			egen `i' = seq(), block(`n2')
			egen `j' = seq(), to(`n2')

			forval n = 1/`=_N' {
				matrix `matcell'[`=`i'[`n']',`=`j'[`n']'] = ///
				`freq'[`n']
			}

			mat rownames `matcell' = `v1'
			mat colnames `matcell' = `v2'
		}
	}

  	label var `freq' "Freq."
	tabdisp `varlist', c(`freq')
end

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