*! version 1.1 April 28, 2018
* Authors: Richard Williams & Paul Allison
* Support: Richard Williams, rwilliam@nd.edu
* Takes chi-square statistics from multiple imputations and combines them
* to get a single F statistic and p-value

program combchi
* Example: combchi, df(3) chi(5.8 7.2 6.1 8.5)
	version 9
	syntax, df(numlist min = 1 max = 1 integer >=1) chi(numlist min = 2 >= 0)

	* Need to sum the chi-square values and the sqrt of the chi-square values
	foreach chival of local chi {
		local m = `m' + 1
		local gsum = `gsum' + `chival'
		local gsqsum = `gsqsum' + `chival' ^ .5
	}
	local mg2 = `gsum' / `m'
	local r = (1 + 1/`m') * (`gsum' - `gsqsum'^2/`m') / (`m' - 1)
	local F = (`mg2'/`df' - `r' * (`m' + 1)/ (`m' - 1))/ (1 + `r')
	local DDF = (`m' - 1) * (1 + 1/`r') ^ 2 /`df' ^ (3/`m')
	local P = Ftail(`df', `DDF', `F')
	di
	di "m = " `m'
	di "F = "`F'
	di "df = " `df'
	di "DDF = " `DDF'
	di "P = " `P'
end