/*
  mkspline

*! VERSION 2.0.4  23apr2007

*/

VERSION 10.0

INCLUDE _std_large
INCLUDE _ht340
INCLUDE header

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

DIALOG main, label("mkspline - Linear and restricted cubic spline construction") tabtitle("Main")
BEGIN
  /*--+----1----+----2----+----3----+----4----+----5----+----6----+----7----+*/
  TEXT tx_oldvar	_lft	_top	_iwd	.,			///
	label("Make spline of variable:")
  VARNAME vn_oldvar	@	_ss	_vnwd	.,			///
	label("Make spline of variable")
  GROUPBOX gb_spl	@	_ls	_iwd	_ht23h,			///
	label("Create spline by specifying")
  RADIO rb_var		_indent	_ss	_ibwd	.,			///
	label("New variables and knots for linear spline:")		///
	clickon("script var_on") first
  DEFINE holdx @x
  EDIT ed_var		_indent2 _ss	_inwd	.,			///
	label("New variables and knots for linear spline")
  EDIT ed_var2		@	_ms	@	.,			///
	label("New variables and knots for linear spline")
  EDIT ed_var3		@	_ms	@	.,			///
	label("New variables and knots for linear spline")
  TEXT tx_var		@	_ss	_inwd	.,			///
	label(`"Example: "mild 10 moderate 20 heavy 30 extreme""')
  TEXT tx_var2		@	_vss	@	.,			///
	label("Note: the last new variable is not followed by a knot.")
  RADIO rb_stub		holdx	_ls	_ibwd	.,			///
	clickon("script stub_on")					///
label("New variable stub and number of variables to create for linear spline:")
  EDIT ed_stub		_indent2 _ss	_en14wd	.,			///
	max(31)								///
	label("New variable stub for linear spline")
  SPINNER sp_stub	_en14sep @	_spwd	.,			///
	min(2) max(1000000000) default(2)				///
	label("Number of knots")

  RADIO rb_stub2	holdx	_ls	_ibwd	.,			///
	label("New variable stub for restricted cubic spline:")		///
 	clickon("program stub2_on") last
  EDIT ed_stub2		_indent2 _ss	_en14wd	.,			///
	max(31)								///
  	label("New variable stub for restricted cubic spline")
END

INCLUDE ifin
INCLUDE weights_f

DIALOG opt, label("") tabtitle("Options")
BEGIN
  /*--+----1----+----2----+----3----+----4----+----5----+----6----+----7----+*/
  GROUPBOX gb_marg		_lft	_top	_iwd	_ht4,		///
label("Construct new variables so that when they are used in estimation")
  RADIO rb_nomarg		_indent	_ss	_inwd	.,		///
	option(NONE) first						///
	label("Coefficients measure the slopes for the intervals")
  RADIO rb_marg			@	_ss	@	.,		///
	option(marginal) last						///
label("Coefficients represent the change in slope from the preceding interval")
  CHECKBOX ck_displayknots	_lft	_xls	_iwd	.,		///
	option(displayknots)						///
	label("Display the values of the knots used in creating the spline")
  CHECKBOX ck_pctile		_lft	_ms	_iwd	.,		///
	option(pctile)							///
label("Place knots at percentiles rather than equally spaced over the range")

  GROUPBOX gb_knots		_lft	_ls	_iwd	_ht6,		///
	label("Knot specification for restricted cubic spline")
  RADIO rb_nknots		_indent	_ss	_rbwd	.,		///
	label("")							///
	option(NONE)							///
	onclickon(program nknots_on)					///
	first
  SPINNER sp_nknots		_rbsep	@	_spwd	.,		///
	label("Number of knots to use")					///
	option(nknots)							///
	default(5)							///
	min(3)								///
	max(7)
DEFINE holdx @x
  TEXT tx_nknots		_spsep	@	150	.,		///
	label("Number of knots to use")
  RADIO rb_knots		_ilft	_ss	_ibwd	.,		///
	label("Location of knots")					///
	option(NONE)							///
	onclickon(script nknots_off)					///
	last
  EDIT ed_knots			holdx	_ss	_en14wd	.,		///
	label("Location of knots")					///
	option(knots)
END

SCRIPT var_on
BEGIN
	main.ed_var.enable
	main.ed_var2.enable
	main.ed_var3.enable
	main.tx_var.enable
	main.tx_var2.enable
	main.ed_stub.disable
	main.sp_stub.disable
	main.ed_stub2.disable
	script weights_disable
	opt.gb_marg.enable
	opt.rb_nomarg.enable
	opt.rb_marg.enable
	opt.ck_pctile.disable
	opt.gb_knots.disable
	opt.rb_nknots.disable
	opt.sp_nknots.disable
	opt.tx_nknots.disable
	opt.rb_knots.disable
	opt.ed_knots.disable
END

SCRIPT stub_on
BEGIN
	main.ed_var.disable
	main.ed_var2.disable
	main.ed_var3.disable
	main.tx_var.disable
	main.tx_var2.disable
	main.ed_stub.enable
	main.sp_stub.enable
	main.ed_stub2.disable
	script weights_enable
	opt.gb_marg.enable
	opt.rb_nomarg.enable
	opt.rb_marg.enable
	opt.ck_pctile.enable
	opt.gb_knots.disable
	opt.rb_nknots.disable
	opt.sp_nknots.disable
	opt.tx_nknots.disable
	opt.rb_knots.disable
	opt.ed_knots.disable
END

PROGRAM stub2_on
BEGIN
	call main.ed_var.disable
	call main.ed_var2.disable
	call main.ed_var3.disable
	call main.tx_var.disable
	call main.tx_var2.disable
	call main.ed_stub.disable
	call main.sp_stub.disable
	call main.ed_stub2.enable
	call script weights_enable
	call opt.gb_marg.disable
	call opt.rb_nomarg.disable
	call opt.rb_marg.disable
	call opt.ck_pctile.disable
	call opt.gb_knots.enable
	call opt.rb_nknots.enable
	if opt.rb_nknots {
		call opt.sp_nknots.enable
	}
	call opt.tx_nknots.enable
	call opt.rb_knots.enable
	if opt.rb_knots {
		call opt.ed_knots.enable
	}
END

PROGRAM nknots_on
BEGIN
	if opt.rb_nknots.isenabled() {
		call opt.sp_nknots.enable
	}
	call opt.ed_knots.disable
END

SCRIPT nknots_off
BEGIN
	opt.ed_knots.enable
	opt.sp_nknots.disable
END

PROGRAM command
BEGIN
	put "mkspline "
	if main.rb_var {
		if !(main.ed_var | main.ed_var2 | main.ed_var3) {
			require main.ed_var
		}
		put main.ed_var
		put " "
		put main.ed_var2
		put " "
		put main.ed_var3
	}
	if main.rb_stub {
		require main.ed_stub
		put main.ed_stub " "
		put main.sp_stub " "
	}
	if main.rb_stub2 {
		put main.ed_stub2 " "
	}
	put "="
	varlist main.vn_oldvar
	INCLUDE _ifin_pr
	put /program weights_output
	beginoptions
		option radio(opt rb_nomarg rb_marg)
		if main.rb_stub {
			option opt.ck_pctile
		}
		if main.rb_stub2 {
			put " cubic"
			optionarg /hidedefault opt.sp_nknots
			optionarg opt.ed_knots
		}
		option opt.ck_displayknots
	endoptions
END

