*! version 1.0.0 12sep2022 Matteo Bottai
cap program drop m_mipool
program m_mipool
	tempvar b v
	tempname tfh
	tempfile tf of
	qui save `of'
	postfile `tfh' b v using `tf'
	qui sum $poolsimid, meanonly
	local M = r(max)
	qui forv i = 1/`M' {
		use `of' if $poolsimid==`i', clear
		`0'
		post `tfh' ($poolestimate) (($poolse)^2)
		}
	postclose `tfh'
	use `tf', clear
	qui summ b
	local bbar = r(mean)
	local bvar = r(sd)^2
	qui summ v, meanonly
	local sebar = sqrt(r(mean) + (1 + 1/`M')*`bvar')
	di as txt _n "Pooled inference from imputed datasets" _n
	di as txt "No. imputed datasets  =  " as res "`M'"
	di as txt "Pooled estimate       =  " as res `bbar'
	di as txt "Standard error        =  " as res `sebar'
	di as txt "P-value               =  " as res chi2tail(1,(`bbar'/`sebar')^2)
	di as txt "95% Conf. Int.        =  " as res "(" `bbar' - invnormal(.975)*`sebar' ", " `bbar' + invnormal(.975)*`sebar' ")"
	qui use `of', clear
end