Alternatively, this reproduces
the example in the original Goodman
and Kruskal paper in JASA 1954.
The syntax would be something like
lambda rowvar colvar
or
lambda rowvar colvar [w=freq]
Somebody tell me a better way to do it!
*! NJC 1.0.0 19 Nov 2003
program lambda, rclass
version 8
syntax varlist(min=2 max=2 numeric) [if] [in] [fweight aweight]
marksample touse
qui count if `touse'
if r(N) == 0 error 2000
tempname cell row col total rowmax colmax sumrowmax sumcolmax
tab `varlist' [`weight' `exp'] if `touse', matcell(`cell')
local r = r(r)
local c = r(c)
matrix `row' = `cell' * J(`c',1,1)
matrix `col' = J(1,`r',1) * `cell'
matrix `total' = J(1,`r',1) * `row'
local maxrow = `row'[1,1]
forval i = 2/`r' {
local maxrow = max(`maxrow',`row'[`i',1])
}
local maxcol = `col'[1,1]
forval j = 2/`c' {
local maxcol = max(`maxcol',`col'[1,`j'])
}
mat `rowmax' = `cell'[1..`r',1]
mat `colmax' = `cell'[1,1..`c']
forval i = 1/`r' {
forval j = 1/`c' {
if `cell'[`i',`j'] > `rowmax'[`i',1] {
matrix `rowmax'[`i',1] = `cell'[`i',`j']
}
if `cell'[`i',`j'] > `colmax'[1,`j'] {
matrix `colmax'[1,`j'] = `cell'[`i',`j']
}
}
}
mat `sumrowmax' = J(1,`r',1) * `rowmax'
mat `sumcolmax' = `colmax' * J(`c',1,1)
local a = `sumcolmax'[1,1] - `maxrow'
local b = `total'[1,1] - `maxrow'
local c = `sumrowmax'[1,1] - `maxcol'
local d = `total'[1,1] - `maxcol'
local lambda_a = `a' / `b'
di as txt "lambda_a" as res %10.4f `lambda_a'
local lambda_b = `c' / `d'
di as txt "lambda_b" as res %10.4f `lambda_b'
local lambda = (`a' + `c') / (`b' + `d')
di as txt "lambda " as res %10.4f `lambda'
return scalar lambda_b = `lambda_b'
return scalar lambda_a = `lambda_a'
return scalar lambda = `lambda'
end
Nick
[email protected]
Nick Cox
> This sounds like Goodman and Kruskal's lambda.
>
> Stata programmers have shown relatively little
> interest in more than a few of the measures of
> association that proliferate in the literature.
Brad Lockerbie
> > I have what I hope is a very simple question. I've looked in the
> > documentation and searched online for a way to calculate a
> > lambda. I am
> > trying to look at the relationship between two nominal
> > variables for a
> > class. I can do it by hand, but I would like to be able to
> > get Stata to do it for me.
*
* 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/