/*
  binreg

*!  VERSION 1.2.0  23aug2010

*/

VERSION 12.0

INCLUDE glm_header
DEFINE _dlght 455
INCLUDE header
HELP hlp1, view("help binreg")
RESET res1

DIALOG main, /*
*/ label("binreg - Generalized linear models: Extensions to the binomial family") tabtitle("Model")
BEGIN
  TEXT tx_dv 		_lft	_top	_vnwd	.,			///
	label("Dependent variable:")
  VARNAME vn_dv		@ 	_ss	@	.,			///
	ts								///
	label("Dependent variable")

  TEXT tx_iv		_vlx	_top	160	.,			///
	label("Independent variables:")
  VARLIST vl_iv		@	_ss	_vlwd	.,			///
	fv ts								///
	allowcat							///
	label("Independent variables")
  CHECKBOX ck_nocons @         _ms       @         .,			/*
		*/ label("Suppress constant term")			/*
		*/ option("noconstant")					/*
		*/
  GROUPBOX gb_opts   _lft      _ms       _iwd      _ht4,		/*
		*/ label("Link choices")				/*
		*/
  RADIO    rb_or     _ilft     _ss       _cwd2     .,			/*
		*/ label("Logit link/odds ratios")			/*
		*/ first						/*
		*/ option("or")						/*
		*/
  RADIO    rb_rr     _lft2     @         @         .,			/*
		*/ label("Log link/risk ratios")			/*
		*/ option("rr")						/*
		*/
  RADIO    rb_hr     _ilft     _ss       @         .,			/*
		*/ label("Log-complement link/health ratios")		/*
		*/ option("hr")						/*
		*/
  RADIO    rb_rd     _lft2     @         @         .,			/*
		*/ label("Identity link/risk differences")		/*
		*/ last							/*
		*/ option("rd")						/*
		*/

  GROUPBOX gb_n      _lft      _xls      _iwd      _ht4h,		/*
		*/ label("Number of trials")				/*
		*/
  RADIO    rb_sp_n   _ilft     _ss       _rbwd     .,			/*
		*/ clickon("script sp_n")				/*
		*/ first						/*
		*/ option(NONE)	 					/*
		*/
  SPINNER  sp_n      _rbsep    @         _spwd     .,			/*
		*/ label("Constant")					/*
		*/ min(1)						/*
		*/ max(10000000)					/*
		*/ default(1)						/*
		*/ option("n")	 					/*
		*/
  TEXT     tx_sp_n   _spsep    @         _rbsprb   .,			/*
		*/ label("Constant")					/*
		*/
  RADIO    rb_vn_n   _ilft     _ms       _rbwd     .,			/*
		*/ clickon("script vn_n")				/*
		*/ last							/*
		*/ option(NONE)						/*
		*/
  VARNAME  vn_n      _rbsep    @         _vnwd     .,			/*
		*/ label("Variable")					/*
		*/ option("n")						/*
		*/
  TEXT     tx_vn_n   _vnsep    @         _rbvnrb   .,			/*
		*/ label("Variable")					/*
		*/

  GROUPBOX gb_opt    _lft      _xls      _iwd      _ht17,		/*
		*/ label("Options")					/*
		*/

  RADIO    rb_expose _ilft     _ss       _cwd4_1   .,			/*
		*/ label("Exposure variable:")				/*
		*/ first						/*
		*/ clickon("script expose")				/*
		*/
  RADIO    rb_offset _ilft4_2  @         @         .,			/*
		*/ label("Offset variable:")				/*
		*/ last							/*
		*/ clickon("script offset")				/*
		*/

  VARNAME  vn_expose _ilft     _ss       @         .,			/*
		*/ label("Exposure variable")				/*
		*/ option("exposure")					/*
		*/
  VARNAME  vn_offset _ilft4_2  @         @         .,			/*
		*/ label("Offset variable")				/*
		*/ option("offset")					/*
		*/

  DEFINE _x _ilft
  DEFINE _y _ls
  DEFINE _cx _ilw80
  DEFINE _bux _islw80
  INCLUDE _constraints

  DEFINE _x _ilft
  DEFINE _xw _ibwd
  INCLUDE _ck_collinear

  TEXT     tx_init   _ilft     _ms      _ibwd      .,			/*
		*/ label("Initial mean estimates variable:")		/*
		*/
/* -init- seems to be equivalent to the -glm- option -mu-  */
  VARNAME  vn_init   @         _ss      _cwd4_1    .,			/*
		*/ label("Initial mean estimates variable")		/*
		*/ option("init")					/*
		*/
  TEXT     tx_note   _lft4_2   @        _comb4_3   .,			/*
  		*/ label("Note: this is an advanced option that may help with convergence.")
END

SCRIPT main_PREINIT
BEGIN
	create STRING __const_tab
	__const_tab.setvalue "main"
END

INCLUDE _constraints_sc 

SCRIPT collinear_disable
BEGIN
	main.ck_collinear.disable
END

SCRIPT collinear_enable
BEGIN
	main.ck_collinear.enable
END

SCRIPT sp_n
BEGIN
	main.sp_n.enable
	main.vn_n.disable
END

SCRIPT vn_n
BEGIN
	main.sp_n.disable
	main.vn_n.enable
END


INCLUDE byifin
INCLUDE weights_fpai
INCLUDE se

SCRIPT PREINIT
BEGIN
	program parseMessage
	script se_createAsGLM
END

PROGRAM parseMessage
BEGIN
	if __MESSAGE.contains("__MI__") {
		call script byifin_set_by_off
		call script se_setMI_on
	}
END

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 _spr2
  DEFINE _y _top
  INCLUDE _sp_level

  CHECKBOX ck_coeff  _lft      _ls       _iwd      .,			/*
		*/ label("Report nonexponentiated coefficients")	/*
		*/ option(coeff)					/*
		*/

  DEFINE _x _lft
  DEFINE _y _ms
  DEFINE _cx _iwd
  INCLUDE _nocnsreport

  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 _ls
  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

PROGRAM rpt_output
BEGIN
	optionarg /hidedefault rpt.sp_level
	option rpt.ck_coeff
	INCLUDE _nocnsreport_pr
	INCLUDE _noomitted_pr
	INCLUDE _vsquish_pr
	INCLUDE _factor_vars_reporting_pr
	put " " rpt_bu_fmtcoefResults
END

SCRIPT expose
BEGIN
  main.vn_expose.enable
  main.vn_offset.disable
END

SCRIPT offset
BEGIN
  main.vn_expose.disable
  main.vn_offset.enable
END

INCLUDE max_glm

SCRIPT POSTINIT
BEGIN
	program max_create_binreg
END

PROGRAM command
BEGIN
	INCLUDE _by_pr
	put "binreg "
	varlist main.vn_dv [main.vl_iv]
	if !main.vl_iv & main.ck_nocons {
	  stopbox stop `""Suppress constant term" is selected without independent variables."'
	}
	INCLUDE _weights_pr
	INCLUDE _ifin_pr
	beginoptions
		option main.ck_nocons
		option radio(main rb_or rb_rr rb_hr rb_rd)
		if main.rb_sp_n {
			optionarg /hidedefault main.sp_n
		}
		if main.rb_vn_n {
			require main.vn_n
			optionarg main.vn_n
		}
		optionarg main.vn_expose
		optionarg main.vn_offset
		optionarg main.cb_constr
		option main.ck_collinear
		optionarg main.vn_init

		put " " /program se_output
		put " " /program rpt_output
		put " " /program max_glm_output
	endoptions
END
