/*
  contrast.dlg

*!  VERSION 1.0.9  07jun2011

*/

VERSION 12.0

INCLUDE _std_large
DEFINE _dlght 450
INCLUDE header

HELP hlp1, view("help contrast")
RESET res1

SCRIPT PREINIT
BEGIN
	create STRING contrastListString
	create DOUBLE e_k_eq
	e_k_eq.setvalue e(k_eq)
END

PROGRAM POSTINIT_PROGRAM
BEGIN
	call create DOUBLE e_k_eq
	call e_k_eq.setvalue e(k_eq)
/*
	if (e_k_eq.isgt(1)) {
		call script equ_enable_equation
	}
	else {
		call script equ_disable_equation
	}
*/

	call create STRING prefix
	call prefix.setvalue e(prefix)
	if (prefix.iseq("svy")) {
		call rpt.ck_nosvyadjust.enable
	}
	else {
		call rpt.ck_nosvyadjust.disable
	}
END

PROGRAM ON_DOTPROMPT
BEGIN
	call main.cb_contrastlist.repopulate
END

DIALOG main, tabtitle("Main")						///
title("contrast - Contrasts and linear hypothesis tests after estimation")
BEGIN
  TEXT tx_contrastlist		_lft	_top	_cwd1	.,		///
	label("Factor terms to compute contrast for:")
  COMBOBOX cb_contrastlist	@	_ss	_lw20	.,		///
	append								///
	dropdown							///
	contents(margins_mlist)						///
	label("Factor terms to compute pairwise comparisons for")
  BUTTON  bu_contrastlist	_slw20	@	20	.,		///
	onpush(script showContrastListDialog)				///
	tooltip("Create a contrast list")				///
        label("...")

  GROUPBOX gb_opts		_lft	_ls	_iwd	_ht7h,		///
	label("Options")
  CHECKBOX ck_overall		_indent	_ms	_inwd	.,		///
	option("overall")						///
	label("Add a single joint hypothesis test for all specified contrasts")
  CHECKBOX ck_asobserved 	@	_ms	@	.,		///
	option("asobserved")						///
	label("Treat all factor variables as observed")
  CHECKBOX ck_lincom		@	_ms	@	.,		///
	option("lincom")						///
	label("Treat user-defined contrasts as linear combinations")
END

DIALOG equ, tabtitle("Equations")
BEGIN
  CHECKBOX ck_equation		_lft	_top	250	_ht4,		///
	groupbox							///
	onclickon("script equ_equation_on")				///
	onclickoff("script equ_equation_off")				///
	label("Equation")
  TEXT tx_equation		_indent	_ss	_cwd1	.,		///
	label("Equation specification:")
  COMBOBOX cb_equation		@	_ss	@	.,		///
	dropdown							///
	contents(ereturn eqnames)					///
	option("equation")						///
	label("Equation specification")
  CHECKBOX ck_atequations 	_lft	_xls	_ibwd	.,		///
	option("atequations")						///
	label("Perform contrasts on the margins within each equation")
END

SCRIPT equ_enable_equation
BEGIN
	equ.ck_equation.enable
END

SCRIPT equ_disable_equation
BEGIN
	equ.ck_equation.disable
END

SCRIPT equ_equation_on
BEGIN
	equ.tx_equation.enable
	equ.cb_equation.enable
	equ.ck_atequations.disable
END

SCRIPT equ_equation_off
BEGIN
	equ.tx_equation.disable
	equ.cb_equation.disable
	equ.ck_atequations.enable
END

DIALOG adv, tabtitle("Advanced")
BEGIN
  GROUPBOX gb_emptycell		_lft	_top	_iwd	_ht4,		///
	label("Treatment of empty cells for balanced factors")
  RADIO rb_strict		_indent	_ss	_inwd	., first	///
	option(emptycells(strict))					///
	label(`"Declare all margins involving empty cells as "not estimable""')
  RADIO rb_reweight		@	_ss	@	., last		///
	option(emptycells(reweight))					///
	label("Reweight nonempty cells to account for empty cells")

  CHECKBOX ck_noestimcheck	_lft	_xls	_iwd	.,		///
	option("noestimcheck")						///
	label("Suppress estimability checks")
END

SCRIPT showContrastListDialog
BEGIN
        create CHILD _contrast_list
        _contrast_list.setExitString contrastListString
        _contrast_list.setExitAction "main.cb_contrastlist.setvalue class contrastListString.value"
END

SCRIPT rpt_POSTINIT
BEGIN
	create STRING rpt_bu_fmtcoefResults
	program rpt_bu_fmtcoef_ckResults

	create STRING rpt_bu_eformResults
	program rpt_bu_eform_ckResults
END

DIALOG rpt, tabtitle("Reporting")
BEGIN
  DEFINE _x _lft
  DEFINE _cx _spr2b
  DEFINE _y _top
  INCLUDE _sp_level
  
  TEXT tx_mcompare		_lft	_ms	280	.,		///
	label("Multiple comparisons:")
  COMBOBOX cb_mcompare		@	_ss	@	.,		///
	dropdownlist							///
	contents(rpt_mcompare_list)					///
	values(rpt_mcompare_values)					///
	option("mcompare")						///
	label("Multiple comparisons")

  RADIO rb_noeffects		_lft	_ls	_iwd	., first	///
	onclickon("script rpt_effects_off")				///
	option("noeffects")						///
	label("Suppress effects tables")
  RADIO rb_effects		@	_ss	@	., last		///
	onclickon("script rpt_effects_on")				///
	label("Effects tables")
  CHECKBOX ck_cieffects		_indent	_ss	_inwd	.,		///
	option("cieffects")						///
	label("Show effects table with confidence intervals")
  CHECKBOX ck_pveffects		@	_ss	@	.,		///
	option("pveffects")						///
	label("Show effects table with p-values")
  CHECKBOX ck_effects		@	_ss	@	.,		///
	option("effects")						///
	label("Show effects table with confidence intervals and p-values")

  GROUPBOX gb_wald		_lft	_ls	_iwd	_ht9h,		///
	label("Wald test")
  RADIO rb_wald			_indent	_ss	_inwd	., first	///
	label("Show table of Wald tests")
  RADIO rb_nowald		@	_ss	@	., last		///
	option("nowald")						///
	onclickon("script rpt_nowald_on")				///
	onclickoff("script rpt_nowald_off")				///
	label("Suppress the table of Wald tests")
  CHECKBOX ck_noatlevels 	@	_ls	_ibwd	.,		///
	option("noatlevels")						///
	label("Report only the overall Wald test for terms")
  CHECKBOX ck_nosvyadjust 	@	_ms	@	.,		///
	option("nosvyadjust")						///
	label("Compute unadjusted Wald tests for survey results")

  CHECKBOX ck_sort		_lft	_xls	_iwd	.,		///
	option("sort")							///
	label("Sort the individual contrast values in each term")
  CHECKBOX ck_post		@	_ms	@	.,		///
	option("post")							///
	label("Post contrasts and their VCE as estimation results")

  DEFINE _x _lft
  DEFINE _y _ms
  DEFINE _cx _iwd
  INCLUDE _vsquish

  DEFINE _x _lft
  DEFINE _y _ls
  INCLUDE _bu_coef_table_reporting

  DEFINE _x _lft2
  DEFINE _y @
  INCLUDE _bu_eform_opts_reporting

END

INCLUDE fmt_coef_table_reporting_pr
INCLUDE eform_option_reporting_pr

LIST rpt_mcompare_list
BEGIN
	"Do not adjust for multiple comparisons"
	"Bonferroni's method"
	"Bonferroni's method; adjust across all terms"
	"Sidak's method"
	"Sidak's method; adjust across all terms"
	"Scheffe's method"
END

LIST rpt_mcompare_values
BEGIN
	""
	bonferroni
	"bonferroni adjustall"
	sidak
	"sidak adjustall"
	scheffe
END

SCRIPT rpt_nowald_on
BEGIN
	rpt.ck_noatlevels.disable
	rpt.ck_nosvyadjust.disable
END

SCRIPT rpt_nowald_off
BEGIN
	rpt.ck_noatlevels.enable
	program rpt_enable_nosvyadjust
END

PROGRAM rpt_enable_nosvyadjust
BEGIN
	if (prefix.iseq("svy")) {
		call rpt.ck_nosvyadjust.enable
	}
END

SCRIPT rpt_effects_on
BEGIN
	rpt.ck_cieffects.enable
	rpt.ck_pveffects.enable
	rpt.ck_effects.enable
END

SCRIPT rpt_effects_off
BEGIN
	rpt.ck_cieffects.disable
	rpt.ck_pveffects.disable
	rpt.ck_effects.disable
END

PROGRAM rpt_output
BEGIN
	optionarg /hidedefault rpt.sp_level

	optionarg rpt.cb_mcompare

	if (rpt.rb_effects & !rpt.ck_cieffects &			///
		!rpt.ck_pveffects & !rpt.ck_effects) {
		stopbox stop `"You must specify an "Effects table""'	///
				`" on the "Reporting" tab"'
	}
	if (rpt.rb_effects) {
		option rpt.ck_cieffects
		option rpt.ck_pveffects
		option rpt.ck_effects
	}

	option rpt.rb_nowald
	option rpt.ck_noatlevels
	option rpt.ck_nosvyadjust

	option rpt.ck_sort
	option rpt.ck_post
	INCLUDE _vsquish_pr
	put " " rpt_bu_fmtcoefResults
	put " " rpt_bu_eformResults
END

PROGRAM command
BEGIN
	put "contrast "
	require main.cb_contrastlist
	put main.cb_contrastlist
	beginoptions
		option main.ck_asobserved
		option main.ck_overall
		option main.ck_lincom
		if equ.ck_equation {
			require equ.cb_equation
			optionarg equ.cb_equation
		}
		option equ.ck_atequations
		option adv.rb_reweight
		option adv.ck_noestimcheck
		put " " /program rpt_output
	endoptions
END

