/*
  pwcompare

*!  VERSION 1.0.8  28may2011

*/

VERSION 12.0

INCLUDE _std_large
DEFINE _dlght 310
INCLUDE header

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

PROGRAM ON_DOTPROMPT
BEGIN
	call main.cb_marlist.repopulate
END

PROGRAM POSTINIT_PROGRAM
BEGIN
	call create STRING ecmd
	call ecmd.setvalue e(cmd)		
	if ecmd.iseq("regress") | ecmd.iseq("cnsreg") |		///
		ecmd.iseq("mvreg") | ecmd.iseq("anova") | ecmd.iseq("manova") {
		call main.cb_mcompare.hide
		call main.cb_mcompare_2.show
	}
	else {
		call main.cb_mcompare.show
		call main.cb_mcompare_2.hide
	}
END

DIALOG main, tabtitle("Main")						///
title("pwcompare - Pairwise comparisons of marginal linear predictions")
BEGIN
  TEXT tx_marlist	_lft	_top	_iwd	.,			///
	label("Factor terms to compute pairwise comparisons for:")
  COMBOBOX cb_marlist	@	_ss	_iwd	.,			///
	append								///
	dropdown							///
	contents(margins_mlist)						///
	label("Factor terms to compute pairwise comparisons for")

  GROUPBOX gb_opts		_lft	_ls	_iwd	_ht7,		///
	label("Options")
  TEXT tx_mcompare		_ilft	_ss	_inwd	.,		///
	label("Multiple comparisons:")
  COMBOBOX cb_mcompare		@	_ss	280	.,		///
	option("mcompare")						///
	onselchange("program main_mcompare_change")			///
	dropdownlist							///
	contents(main_mcompare_list)					///
	values(main_mcompare_values)					///
	label("Multiple comparisons")
  COMBOBOX cb_mcompare_2	@	@	280	.,		///
	option("mcompare")						///
	onselchange("program main_mcompare_change")			///
	dropdownlist							///
	contents(main_mcompare_list_2)					///
	values(main_mcompare_values_2)					///
	label("Multiple comparisons")
  CHECKBOX ck_asobserved 	@	_ls	@	.,		///
	option("asobserved")						///
	label("Treat all factor variables as observed")
END

PROGRAM main_mcompare_change
BEGIN
	if main.cb_mcompare.iseq("bonferroni adjustall") |		///
		main.cb_mcompare.iseq("sidak adjustall") {
		call rpt.ck_groups.disable
	}
	else {
		call rpt.ck_groups.enable
	}
END

LIST main_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 main_mcompare_values
BEGIN
	""
	bonferroni
	"bonferroni adjustall"
	sidak
	"sidak adjustall"
	scheffe
END

LIST main_mcompare_list_2
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"
	"Tukey's method"
	"Student Newman Keuls' method"
	"Duncan's method"
	"Dunnett's method"
END

LIST main_mcompare_values_2
BEGIN
	""
	bonferroni
	"bonferroni adjustall"
	sidak
	"sidak adjustall"
	scheffe
	tukey
	snk
	duncan
	dunnett
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 comparisons of the margins within each equation")
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 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 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

  CHECKBOX ck_table		_lft	_ls	_iwd	_ht11h,		///
	groupbox							///
	onclickon("script rpt_table_on")				///
	onclickoff("script rpt_table_off")				///
	label("Specify additional tables (default is effects table with confidence intervals)")
  CHECKBOX ck_cieffects		_indent	_ss	_inwd	.,		///
	option("cieffects")						///
	label("Show effects table with confidence intervals")
  CHECKBOX ck_pveffects		@	_ms	@	.,		///
	option("pveffects")						///
	label("Show effects table with p-values")
  CHECKBOX ck_effects		@	_ms	@	.,		///
	option("effects")						///
	label("Show effects table with confidence intervals and p-values")
  CHECKBOX ck_cimargins		@	_ms	@	.,		///
	option("cimargins")						///
	label("Show table of margins and confidence intervals")
  CHECKBOX ck_groups		@	_ms	@	.,		///
	option("groups")						///
	label("Show table of margins and group codes")

  CHECKBOX ck_sort		_lft	_xls	@	.,		///
	option("sort")							///
	label("Sort the margins/differences in each term")
  CHECKBOX ck_post		@	_ms	@	.,		///
	option("post")							///
	label("Post margins 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

SCRIPT rpt_table_on
BEGIN
	rpt.ck_cieffects.enable
	rpt.ck_pveffects.enable
	rpt.ck_effects.enable
	rpt.ck_cimargins.enable
	rpt.ck_groups.enable
END

SCRIPT rpt_table_off
BEGIN
	rpt.ck_cieffects.disable
	rpt.ck_pveffects.disable
	rpt.ck_effects.disable
	rpt.ck_cimargins.disable
	rpt.ck_groups.disable
END

INCLUDE fmt_coef_table_reporting_pr
INCLUDE eform_option_reporting_pr

PROGRAM rpt_output
BEGIN
	optionarg /hidedefault rpt.sp_level

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

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

PROGRAM command
BEGIN
	put "pwcompare "
	require main.cb_marlist
	put main.cb_marlist
	beginoptions
		if main.cb_mcompare.isvisible() {
			optionarg main.cb_mcompare
		}
		if main.cb_mcompare_2.isvisible() {
			optionarg main.cb_mcompare_2
		}
		option main.ck_asobserved
		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

