/*
  igencox

*!  VERSION 1.0.0  12dec2011

*/

VERSION 12.0

INCLUDE _std_large
DEFINE _dlght 335
INCLUDE header

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

SCRIPT PREINIT
BEGIN
	script se_createAsNoML
	script se_setRobust_off
	script se_setClust_off
	se_hasJackknife.setfalse
	program _se_rebuild_listbox
	script se_setTitleSE
	create STRING matrixResult
END

DIALOG main, tabtitle("Model")						///
	title("igencox - Generalized Cox model")
BEGIN
  DEFINE _x _xsetbu
  DEFINE _y _top
  INCLUDE _bu_stset

  TEXT tx_iv			_lft	_topph	_comb4_3 .,		///
	label("Independent variables:")
  VARLIST vl_iv			@	_ss	_iwd	.,		///
	fv								///
	label("Independent variables")

  GROUPBOX gb_transform		@	_xls	_cwd1	_ht4h,		///
	label("Transformation")
  TEXT tx_method		_ilft	_ss	120	.,		///
	label("Method:")
  DEFINE holdy @y
  COMBOBOX cb_method		@	_ss	@	.,		///
	dropdownlist							///
	contents(main_transform_list)					///
	values(main_transform_values)					///
	label("Method")
  TEXT tx_parameter		+130	holdy	@	.,		///
	label("Parameter:")
  EDIT ed_parameter		@	_ss	_spwd	.,		///
	numonly								///
	default(1)							///
	label("Parameter")

  TEXT tx_baseq			_lft	+45	_iwd	.,		///
	label("Save jump sizes of the Lambda function in varname:")
  EDIT ed_baseq			@	_ss	_vnwd	.,		///
	option(baseq)							///
	label("Save jump sizes of the Lambda function in varname")

  CHECKBOX ck_savesigma		_lft	_ls	_iwd	_ht6h,		///
	groupbox							///
	onclickon(script main_savesigma_on)				///
	onclickoff(script main_savesigma_off)				///
	label("Save Sigma matrix to dataset")
  TEXT tx_savesigma		_indent	_ss	_ibwd	.,		///
	label("Filename:")
  FILE fi_savesigma		@	_ss	@	.,		///
	label("Browse...")						///
	option(savesigmalf)						///
	filter("Stata Dataset (*.dta)|*.dta|All (*.*)|*.*")		///
	save								///
	error("Filename")
  CHECKBOX ck_replace	@	_ms	@	.,			///
	option(replace)							///
	label("Overwrite file if it already exists")
END

LIST main_transform_list
BEGIN
	Box-Cox
	Logarithmic
END

LIST main_transform_values
BEGIN
	boxcox
	logarithmic
END

SCRIPT main_savesigma_on
BEGIN
	main.tx_savesigma.enable
	main.fi_savesigma.enable
	main.ck_replace.enable
END

SCRIPT main_savesigma_off
BEGIN
	main.tx_savesigma.disable
	main.fi_savesigma.disable
	main.ck_replace.disable
END

PROGRAM main_savesigma_output
BEGIN
        put `"""' main.fi_savesigma `"""'
        beginoptions
                option main.ck_replace
        endoptions
END

INCLUDE ifin
INCLUDE se

SCRIPT rpt_POSTINIT
BEGIN
	create STRING rpt_bu_fmtcoefResults
	program rpt_bu_fmtcoef_ckResults
END

DIALOG rpt, tabtitle("Reporting")
BEGIN
  DEFINE _x _lft
  DEFINE _cx _spr2b
  DEFINE _y _top
  INCLUDE _sp_level
  
  CHECKBOX ck_noshow		_lft	_ls	_iwd	.,		///
	option(noshow)							///
	label("Do not show st setting information")

  DEFINE _x _lft
  DEFINE _y _ms
  DEFINE _cx _iwd
  INCLUDE _noomitted

  DEFINE _x _lft
  DEFINE _y _ms
  DEFINE _cx _iwd
  INCLUDE _vsquish

  DEFINE _x _lft
  DEFINE _y _ms
  DEFINE _cx _iwd
  INCLUDE _factor_vars_reporting

  DEFINE _x _lft
  DEFINE _y _xxxls
  INCLUDE _bu_coef_table_reporting
END

INCLUDE fmt_coef_table_reporting_pr

DIALOG em, tabtitle("EM options")
BEGIN
  SPINNER sp_iterate		_lft	_top	_spwd	.,		///
	default(1000)							///
	min(0)								///
	max(16000)							///
	option(iterate)							///
	label("Maximum number of iterations")
  TEXT tx_iterate		_spsep	@	_spr	.,		///
	label("Maximum number of iterations")

  EDIT ed_tolerance		_lft	_ls	_en7wd	.,		///
	default(1e-6) 							///
	option(tolerance)						///
	label("Tolerance for the coefficient vector")
  TEXT tx_tolerance		_en7sep	@	_en7r	.,		///
	label("Tolerance for the coefficient vector")

  CHECKBOX ck_nolog		_lft	_ls	_cwd1	.,		///
	option(nolog)							///
	label("Supress the iteration log")

  TEXT tx_from			_lft	_ls	_iwd	.,		///
	label("Initial values for the coefficients (matrix):")
  COMBOBOX cb_from		_lft	_ss	150	.,		///
	option(from)							///
	contents(matrix)						///
	dropdown							///
	error("Initial values for the coefficients")
  BUTTON bu_matinput		+160	@	100	.,		///
	onpush(program show_matrix_input)				///
	tooltip("Input matrix by hand")					///
	label("Input matrix...")
END

PROGRAM show_matrix_input
BEGIN
	call create CHILD matrix_input
	if ! _rc {
		call matrix_input.setExitString matrixResult
		call matrix_input.setExitAction "program getMatrixResult"
		call matrix_input.callthru "isChildContext.settrue"
	}
END
PROGRAM getMatrixResult
BEGIN
	call em.cb_from.repopulate
	call em.cb_from.setvalue class matrixResult.value
END

DIALOG adv, tabtitle("Advanced")
BEGIN
  CHECKBOX ck_savespace		_lft	_top	_iwd	.,		///
	option(savespace)						///
	label("Conserve memory during estimation")
END

PROGRAM command
BEGIN
	put "igencox "
	varlist main.vl_iv
	put " " /program ifin_output
	beginoptions
		if ((main.cb_method.isneq("boxcox") |			///
			main.ed_parameter.isneq("1")) & main.vl_iv.isneq("")) {
			put "transform("
				put main.cb_method " "
				put main.ed_parameter
			put ")"
		}
		optionarg main.ed_baseq
		if main.ck_savesigma {
			require main.fi_savesigma
			put "savesigma("
			put /program main_savesigma_output
			put ") "
		}

		put " " /program se_output
		optionarg /hidedefault rpt.sp_level
		option rpt.ck_noshow
		INCLUDE _noomitted_pr
		INCLUDE _vsquish_pr
		INCLUDE _factor_vars_reporting_pr
		put " " rpt_bu_fmtcoefResults
		optionarg /hidedefault em.sp_iterate
		optionarg /hidedefault em.ed_tolerance
		option em.ck_nolog
		optionarg em.cb_from
		option adv.ck_savespace
	endoptions
END