# RE: st: tab2 or crosstable with adjusted standardized residuals

 From "Nick Cox" To Subject RE: st: tab2 or crosstable with adjusted standardized residuals Date Tue, 30 May 2006 15:42:11 +0100

```The opening code

contract rep78 for, freq(n) nomiss
fillin rep78 for
replace n = 0 if _fillin
drop _fillin

can be contracted, so to speak, to

contract rep78 for, freq(n) nomiss zero

This is the whole point of the -zero-
option, which was motivated by precisely
the kind of Poisson fitting (fishing?)
in which explicit zeros are needed
for a cross-classification.

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

Dirk Enzmann

> 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:
> gen res_s = res/sqrt(nhat)
>
> * Calculate adjusted standardized residuals:
> 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 for: replace nb = nb[_N]
> gen res_as = res/sqrt(nhat * (1-na/N) * (1-nb/N))
>
> * 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/
```