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

st: RE: Re: chi squared matrix


From   "Nick Cox" <[email protected]>
To   <[email protected]>
Subject   st: RE: Re: chi squared matrix
Date   Mon, 22 Nov 2004 18:57:45 -0000

Always interesting in these cases to 
contrast a first principles solution 
and recourse to a canned program. 

Let's invent some data: 

clear 
set obs 100
forval i = 1/5 { 
	gen i`i' = uniform() > 0.5
} 

A first principles solution is 

(1) set up some matrices 

mat chi2 = J(5,5,0) 
mat pval = J(5,5,0) 

(2) loop over varlists and populate the matrices with 
results: 

local i = 0 

qui foreach x of var i* { 
	local ++i 
	local j = 0 
	foreach y of var i* { 
		local ++j 
		tab `x' `y', chi 
		mat chi2[`i',`j'] = r(chi2) 
		mat pval[`i',`j'] = r(p) 
	}
} 

(3) decorate the matrices 

unab x: i* 
matrix rownames chi2 = `x' 
matrix colnames chi2 = `x' 
matrix colnames pval = `x'
matrix rownames pval = `x'  

(4) show results 

mat li chi2

symmetric chi2[5,5]
           i1         i2         i3         i4         i5
i1        100
i2  .01463915        100
i3  1.1576392  .52040936        100
i4  .36363636  1.4492754   1.999184        100
i5  .55512978  .42413558   .2398025   1.003613        100

mat li pval

pval[5,5]
           i1         i2         i3         i4         i5
i1  1.524e-23  .90369711  .28195568   .5464936  .45622921
i2  .90369711  1.524e-23  .47066711  .22864426  .51488065
i3  .28195568  .47066711  1.524e-23  .15738391   .6243488
i4   .5464936  .22864426  .15738391  1.524e-23  .31643784
i5  .45622921  .51488065   .6243488  .31643784  1.524e-23

============================= 

-makematrix- is two steps: 

(1) spend 30 minutes reading the help file and 
figuring out syntax

(2) 

makematrix , from(r(chi2)) cols(i*) : tab i*, chi

           i1         i2         i3         i4         i5
i1        100
i2  .01463915        100
i3  1.1576392  .52040936        100
i4  .36363636  1.4492754   1.999184        100
i5  .55512978  .42413558   .2398025   1.003613        100

makematrix , from(r(p)) cols(i*) : tab i*, chi

           i1         i2         i3         i4         i5
i1  1.524e-23  .90369711  .28195568   .5464936  .45622921
i2  .90369711  1.524e-23  .47066711  .22864426  .51488065
i3  .28195568  .47066711  1.524e-23  .15738391   .6243488
i4   .5464936  .22864426  .15738391  1.524e-23  .31643784
i5  .45622921  .51488065   .6243488  .31643784  1.524e-23

I am not clear why Stata does not regard the last matrix
as symmetric.  

Nick 
[email protected] 

Michael Blasnik
 
> you may want to check out -makematrix- available from SSC.
 
Daniel Egan

> > I have a large list of indicator variables for which I 
> would like the
> > equivalent of a correlation matrix, but with pearsons chi2 
> and/or the
> > significance of pearsons chi2 as the cell contents instead of the
> > correlation coefficient. Similarly, I would like only the lower half
> > matrix.
> > 
> > Is there a packaged way of doing this? 

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