Statalist The Stata Listserver


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

RE: st: tab2 or crosstable with adjusted standardized residuals


From   "Nick Cox" <n.j.cox@durham.ac.uk>
To   <statalist@hsphsun2.harvard.edu>
Subject   RE: st: tab2 or crosstable with adjusted standardized residuals
Date   Mon, 29 May 2006 12:05:15 +0100

It may be that you have the Haberman definition 
of adjusted standardized residuals in mind here.
At a first glance, that is what Uli is also 
working with.  

A program -tabchi- from the package -tab_chi-
on SSC produces these. For example, 

. sysuse auto, clear 

.  tabchi for rep78, adj

          observed frequency
          expected frequency
          adjusted residual

--------------------------------------------------
          |           Repair Record 1978          
 Car type |      1       2       3       4       5
----------+---------------------------------------
 Domestic |      2       8      27       9       2
          |  1.391   5.565  20.870  12.522   7.652
          |  0.949   1.990   3.236  -2.098  -4.040
          | 
  Foreign |      0       0       3       9       9
          |  0.609   2.435   9.130   5.478   3.348
          | -0.949  -1.990  -3.236   2.098   4.040
--------------------------------------------------

4 cells with expected frequency < 5
1 cell with expected frequency < 1

          Pearson chi2(4) =  27.2640   Pr = 0.000
 likelihood-ratio chi2(4) =        .   Pr =     .

Thus the problem reduces to (1) installing -tab_chi- 
and (2) cloning -tab2- so that it calls -tabchi-. 

Here is a hack at (2). 

*! tab2_2 1.0.0 NJC 29 May 2006
*! tab2 2.2.4  29sep2004
program tab2_2, byable(recall)
	version 8          
	syntax varlist(min=2) [if] [in] [fweight] [, *]
	tempvar touse
	mark `touse' `if' `in' [`weight'`exp']
	local weight "[`weight'`exp']"
	capture { 
		tokenize `varlist'
		local I : word count `varlist'
		forval i = 1/`I' {
			forval j = `= `i' + 1'/`I' {		
				noisily di _n as res ///
				  `"-> tabulation of ``i'' by ``j'' `if' `in'"' 
				cap noisily tabchi ``i'' ``j'' ///
				  if `touse' `weight' , `options' 
				if _rc != 0 & _rc != 1001 exit _rc
			}
		}
	}
	error _rc 
end

With this also installed, the syntax would be something like

. tab2_2 for rep78 turn, adj 

Nick 
n.j.cox@durham.ac.uk 

Ulrich Kohler 
 
> Dirk Enzmann wrote:
> > The command tab2 generates crosstables displaying different 
> information
> > in its cells (a.o. counts, row- and column-percentages). 
> What can I do
> > to have additionally a display of adjusted standardized 
> residuals? Is it
> > possible to extend the options of tab2 (or writing an ado that uses
> > tab2) such that adjusted standardized residuals can be displayed?
> >
> > Note: Up to now I never did programming in Stata.
> 
> -tab2- is a caller of -tabulate- and can therefore not be 
> rewritten to display 
> quantities that are not part of -tabulate- itself. If you 
> want to write a 
> program you need to start from scratch, I am afraid.
> 
> I don't know whether I found the formula you had in mind, but 
> a  starting 
> point could be a solution that starts with -poisson-:
> 
> -----------------------------
> sysuse auto, clear
> 
> // Make Frequence Data of 2 way table
> contract rep78 for, freq(n) nomiss
> fillin rep78 for
> replace n = 0 if _fillin
> drop _fillin
> 
> // Estimate "Independence Model"
> xi: poisson n i.for i.rep78             
> 
> // Calculate Expected Frequency of Independence Model
> predict nhat                 
> 
> // Calculate Residual 
> gen res = n - nhat                       
> 
> // Calculate standardized residuals (check Formula?)
> gen res_s = res/sqrt(nhat)               
> 
> // Calculate adjusted standardized residuals (check Formula?)
> gen N = sum(N)
> replace N = N[_N]
> by rep78, sort: gen na = sum(n)
> by rep78: replace na = na[_N]
> by for, sort: gen nb = sum(n)
> by rep78: replace nb = nb[_N]
> gen res_as = res/sqrt(na * nb * (1-na)/N * (1-nb)/N) // <- Adjusted?
> 
> // Table with observed freq, expected freq, raw, standard., adjusted 
> tabdisp rep78 for, cellvar(n nhat res res_s res_as)

*
*   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