*! version 3 15Oct2014
program define reg_distdicho, rclass byable(recall)

	syntax varname [, /*
	*/ tail(string)  cp(real 0)]

	if (e(cmd)=="regress"){
		*Std.Dev. of model fitted
		local std_mod = e(rmse)

	}
	else if (e(cmd)=="xtmixed"){
		*Std.Dev. of model fitted
		matrix b = e(b)
		matrix ln_std = b[1, "lnsig_e:"]
		local std_mod = exp(ln_std[1,1])

	}
	else if (e(cmd)=="xtreg"){
		*Std.Dev. of model fitted
		local std_mod = e(sigma_e)
	}
	else if (e(cmd)==.){
		error 301
	}
	else{
		di as error "cannot perform reg_distdicho for used function"
	}

	*no. observations
	local dicho = "`varlist'"
	quietly tabulate `dicho' if e(sample), matcell(m)
	tempvar nobs
	matrix `nobs' = m
	local obs1 = `nobs'[1,1]
	local obs2 = `nobs'[2,1]

	*margins of dicho. variable
	quietly margins `1'

	*marginal means
	tempvar mmean
	matrix `mmean' = r(b)
	local mean1 = `mmean'[1,1]
	local mean2 = `mmean'[1,2]

	*remarks
	di as text ""
	di as text "Comparisons of proportions based on marginal effects of "as input e(cmdline)
	di as text ""

	*distributional method
		distdichoi `obs2' `mean2' `std_mod' `obs1' `mean1' `std_mod' `cp' `tail'
	ret add
end

program reg_dist, eclass byable(onecall)
	reg_distdicho `0'
end
exit