/*
  stcox_estat

*!  VERSION 1.1.6  13apr2010

*/

VERSION 11.0

INCLUDE _std_large
DEFINE _dlght 400	//dialog length for bootstrap options
INCLUDE header

HELP hlp1, view("help stcox postestimation")
RESET res1

SCRIPT PREINIT
BEGIN
	create BOOLEAN has_FactorVars
	has_FactorVars.settrue
	program sum_bu_facvars_ckResults
	program vce_bu_facvars_ckResults
	create STRING phtestResult
END

SCRIPT POSTINIT
BEGIN
	program phtestCheckResult
END

DIALOG main, label("estat - Postestimation statistics") tabtitle("Main")
BEGIN
  TEXT     tx_subcmd		_lft	_top	_iwd	.,		///
  	label("Reports and statistics: (subcommand)")
  LISTBOX  lb_subcmd		@	_ss	@	70,		///
  	contents(main_subcommand_contents)				///
  	values(main_subcommand_values)					///
  	onselchangelist(main_subcommand_scripts)

  DEFINE holdy 110

  // Information criteria
  CHECKBOX ck_n			_lft	holdy	_iwd	.,		///
	onclickon(main.sp_n.enable)					///
	onclickoff(main.sp_n.disable)					///
	label("Number of observations for calculating BIC")
  SPINNER sp_n			_indent2 _ss	_spwd	.,		///
	default(e(N)) min(1) max(e(N))					///
	option(n)

  INCLUDE estat_sum
  INCLUDE estat_vce

  // concordance options
  GROUPBOX gb_coef		_lft	holdy	_iwd	_ht6,		///
	label("Concordance measure")
  RADIO rb_harrell		_ilft	_ss	_cwd1	., first	///
	onclickon(main.ck_se.disable)					///
	onclickoff(main.ck_se.enable)					///
	label("Harrell's C")
DEFINE holdy2 @y
  RADIO rb_gheller		@	_ss	@	.,		///
	option(gheller)							///
	label("Gonen and Heller's K")
  RADIO rb_both			@	_ss	@	., last		///
	option(harrell gheller)						///
	label("Harrell's C and Gonen and Heller's K")
  CHECKBOX ck_se		_lft2	holdy2	_cwd2	.,		///
	option(se)							///
	label("Compute standard error")

  GROUPBOX gb_option		_lft	+75 	_iwd	_ht4,		///
	label("Sample")
  RADIO    rb_default2		_ilft	_ss	_inwd	.,		///
	first label("Use the sample from the stcox regression, e(sample)")
  RADIO    rb_all		@	_ss	@	.,		///
	label("Use all available data") option(all) last

  CHECKBOX ck_noshow		_lft	+45	_iwd	.,		///
	label("Suppress st variable description") option(noshow)

  // phtest options
  BUTTON   bu_phtest		_lft	holdy	100	.,		///
  	onpush(script show_dialog_phtest) 				///
  	label("Options  ")						///
  	tooltip("Options for proportional hazard assumptions")

  INCLUDE _estat_bootstrap
END

SCRIPT show_dialog_phtest
BEGIN
	create CHILD stcox_estat_phtest AS phtest, allowsubmit
	phtest.setExitString phtestResult
	phtest.setOkAction "program phtestCheckResult"
	phtest.setSubmitAction "script phtestSubmit"
END
PROGRAM phtestCheckResult
BEGIN
	if phtestResult {
		call main.bu_phtest.setlabel "Options *"
	}
	else {
		call main.bu_phtest.setlabel "Options  "
	}
END
SCRIPT phtestSubmit
BEGIN
	program phtestCheckResult
	Submit
END

INCLUDE estat_sum_pr
INCLUDE _estat_bootstrap_pr
INCLUDE estat_vce_pr

LIST main_subcommand_contents
BEGIN
 	 "Concordance probability (concordance)"
 	 "Proportional-hazards assumption test based on Schoenfeld residuals (phtest)"
 	 "Information criteria (ic)"
 	 "Summarize estimation sample (summarize)"
 	 "Covariance matrix estimates (vce)"
END

LIST main_subcommand_values
BEGIN
	concordance
	phtest
	ic
	summarize
	vce
END

LIST main_subcommand_scripts
BEGIN
	script sel_concordance
	script sel_phtest
	script sel_ic
	script sel_summarize
	script sel_vce
END

SCRIPT sel_ic
BEGIN
	script main_hide_all
	main.ck_n.show
	main.sp_n.show
END

SCRIPT sel_summarize
BEGIN
	script main_hide_all
	program main_summ_on
END

SCRIPT sel_vce
BEGIN
	script main_hide_all
	program vce_on
END

SCRIPT sel_concordance
BEGIN
	script main_hide_all
	script main_concordance_on
	script ifin_enable
END

SCRIPT sel_phtest
BEGIN
	script main_hide_all
	script main_phtest_on
END

SCRIPT main_concordance_on
BEGIN
	main.gb_option.show
	main.rb_default2.show
	main.rb_all.show
	main.gb_coef.show
	main.rb_harrell.show
	main.rb_gheller.show
	main.rb_both.show
	main.ck_se.show
	main.ck_noshow.show
END
SCRIPT main_concordance_off
BEGIN
	main.gb_option.hide
	main.rb_default2.hide
	main.rb_all.hide
	main.gb_coef.hide
	main.rb_harrell.hide
	main.rb_gheller.hide
	main.ck_se.hide
	main.rb_both.hide
	main.ck_noshow.hide
END

SCRIPT main_phtest_on
BEGIN
  	main.bu_phtest.show
END
SCRIPT main_phtest_off
BEGIN
  	main.bu_phtest.hide
END

PROGRAM vl_output
BEGIN
	put main.vl_spec
END

PROGRAM vl_eq_output
BEGIN
	put main.vl_eq
END

SCRIPT main_hide_all
BEGIN
	main.ck_n.hide
	main.sp_n.hide
	script main_summ_off
	program vce_off
	script main_concordance_off
	script main_bootstrap_hide
	script ifin_disable
	script main_phtest_off
END

INCLUDE ifin

PROGRAM command
BEGIN
	put "estat "
	put main.lb_subcmd

	put " " /program summarize_output

	INCLUDE _ifin_pr
	beginoptions
		// concordance options
		option main.rb_all
		option radio(main rb_gheller rb_both)
		option main.ck_se
		option main.ck_noshow

		// ic options
		optionarg main.sp_n

		// Summarize options
		put " " /program summarize_opts_output

		put " " /program vce_output
		put " " /program bootstrap_output
		if !H(main.bu_phtest) {
			put " " phtestResult
		}
	endoptions
END
