/*
	factor

*!  VERSION 1.1.2  18apr2011

*/

VERSION 12.0

INCLUDE _std_large
DEFINE _dlght 310
INCLUDE header
HELP hlp1, view("help factor")
RESET res1

DIALOG main, label("factor - Factor analysis") tabtitle("Model")
BEGIN
  TEXT     tx_var	_lft	_top		_iwd	.,		///
	label("Variables:")
  VARLIST  vl_var	@	_ss		@	.,		///
	label("Variables")
END

DIALOG model2, tabtitle("Model 2")
BEGIN
GROUPBOX gb_methods	_lft	_top		_iwd	_ht8,   	///
	label("Method")
  RADIO    rb_pf	_indent	_ss		_inwd	.,		///
	first option(NONE)	 					///
	onclickon("script pf_on")					///
	label("Principal factor (default)")
  RADIO    rb_pcf	@	_ss		@	.,		///
	option(pcf)							///
	onclickon("script pcf_on")					///
	label("Principal-component factor")
  RADIO    rb_ipf	@	_ss		@	.,		///
	option(ipf)							///
	onclickon("program ipf_on")					///
	label("Iterated principal component factor")
  RADIO    rb_ml	@	_ss		@	.,		///
	last								///
	option(ml)							///
	onclickon("program ml_on")					///
	label("Maximum likelihood factor")

  CHECKBOX ck_factors	_lft	_xls		_iwd	.,		///
	onclickon("gaction model2.sp_factors.enable")			///
	onclickoff("gaction model2.sp_factors.disable")			///
	label("Maximum number of factors to be retained:")
  SPINNER  sp_factors	_cksep	_ss		_spwd	.,		///
	min(1) default(100) max(100000)					///
	option(factors)							///
	label("Maximum number of factors to be retained")
  CHECKBOX ck_mineigen	_lft	_ls		_iwd	.,		///
	onclickon("gaction model2.ed_mineigen.enable")			///
	onclickoff("gaction model2.ed_mineigen.disable")		///
	label("Minimum value of eigenvalues to be retained:")
  EDIT     ed_mineigen	_cksep	_ss		_en7wd	.,		///
	numonly 							///
	option(mineigen)						///
	label("Minimum value of eigenvalues to be retained")
  GROUPBOX gb_ipf	_lft	_ls		_iwd	_ht4,		///
	label("Iterated principal component factor")
  CHECKBOX ck_citerate  _ilft	_ss		_ibwd	.,		///
	onclickon("script citerate_on")					///
	onclickoff("script citerate_off")				///
	label("Communality re-estimation iterations")
  SPINNER  sp_citerate	_cksep	_ss		_spwd	.,		///
	min(0) default(0) max(100000)					///
	option(citerate)						///
	label("Communality re-estimation iterations:")
END

INCLUDE byifin
INCLUDE weights_fa

DIALOG report, tabtitle("Reporting")
BEGIN

  CHECKBOX ck_blanks	_lft	_top		_iwd	.,		///
  	onclickon(script ck_blanks_on)					///
  	onclickoff(script ck_blanks_off)				///
	label("Display loadings as blank when absolute value of loadings is less than:")
  EDIT     ed_blanks	_indent2  _ss		_en7wd	.,		///
	option(blanks)							///
	label("Value")
  TEXT     tx_blanks	_spsep	@		_cksprb	.,		///
	label("Value")
  CHECKBOX ck_altdivisor  _lft	_ls		_iwd	.,		///
	option(altdivisor)						///
	label("Use trace of correlation matrix as the divisor for reported proportions")
END

SCRIPT pcf_on
BEGIN
  model2.gb_ipf.disable
  model2.ck_citerate.disable
  script citerate_off
  program ml_off
END

SCRIPT pf_on
BEGIN
  model2.gb_ipf.disable
  model2.ck_citerate.disable
  script citerate_off
  program ml_off
END

PROGRAM ipf_on
BEGIN
  call gaction model2.gb_ipf.enable
  call gaction model2.ck_citerate.enable
  if model2.ck_citerate {
	call script citerate_on
  }
  call program ml_off
END

SCRIPT citerate_on
BEGIN
  model2.sp_citerate.enable
END

SCRIPT citerate_off
BEGIN
  model2.sp_citerate.disable
END

PROGRAM ml_on
BEGIN
	call gaction model2.gb_ipf.disable
	call gaction model2.ck_citerate.disable
	call script citerate_off

	call gaction max.sp_iter.enable
	call gaction max.tx_iter.enable
	call gaction max.gb_ml.enable
	call gaction max.ck_protect.enable
	if max.ck_protect {
		call script protect_on
	}
	call gaction max.ck_seed.enable
	if max.ck_seed {
		call script seed_on
	}
	call gaction max.ck_random.enable
	call gaction max.gb_log.enable
	call gaction max.rb_log.enable
	call gaction max.rb_trace.enable
	call gaction max.rb_nolog.enable
	call gaction max.gb_tol.enable
	call gaction max.ed_tol.enable
	call gaction max.tx_tol.enable
	call gaction max.ed_ltol.enable
	call gaction max.tx_ltol.enable
	if max.ck_protect {
		call script protect_on
	}
	if max.ck_seed {
		call script seed_on
	}
	call gaction max.ck_random.enable
END

PROGRAM ml_off
BEGIN
	call gaction max.sp_iter.disable
	call gaction max.tx_iter.disable
	call gaction max.gb_ml.disable
	call gaction max.ck_protect.disable
	if max.ck_protect {
		call script protect_off
	}
	call gaction max.ck_seed.disable
	if max.ck_seed {
		call script seed_off
	}
	call gaction max.ck_random.disable
	call gaction max.gb_log.disable
	call gaction max.rb_log.disable
	call gaction max.rb_trace.disable
	call gaction max.rb_nolog.disable
	call gaction max.gb_tol.disable
	call gaction max.ed_tol.disable
	call gaction max.tx_tol.disable
	call gaction max.ed_ltol.disable
	call gaction max.tx_ltol.disable
	if max.ck_protect {
		call script protect_off
	}
	if max.ck_seed {
		call script seed_off
	}
	call gaction max.ck_random.disable
END

SCRIPT ck_blanks_on
BEGIN
	report.ed_blanks.enable
	report.tx_blanks.enable
END

SCRIPT ck_blanks_off
BEGIN
	report.ed_blanks.disable
	report.tx_blanks.disable
END

DIALOG max, tabtitle("Maximization")
BEGIN
  SPINNER  sp_iter	_lft	_top		_spwd	.,		///
	label("Maximum iterations")			 		///
	min(0) max(16000) default(c(maxiter))				///
	option(iterate)
  TEXT     tx_iter	_spsep	@		_spr2b	.,		///
	label("Maximum iterations")

  GROUPBOX gb_ml	_lft	_ls		_iwd	_ht10,		///
	label("Maximum-likelihood factor")
  CHECKBOX ck_protect	_ilft	_ms		_cwd1	.,		///
	onclickon("script protect_on")					///
	onclickoff("script protect_off")				///
	label(`""Protect" optimizations"')
  DEFINE holdy @y

  SPINNER  sp_protect	_cksep	_ss		_spwd	.,		///
	min(1) default(1) max(100000)					///
	option(protect)							///
	label(`""Protect" optimizations"')

  CHECKBOX ck_random	_lft2	holdy		_cwd2	.,           	///
	option(random)							///
	label("Use random starting values")

  CHECKBOX ck_seed	_ilft	+50		_ibwd	.,		///
	onclickon(script seed_on)					///
	onclickoff(script seed_off)					///
	label("Seed for uniform pseudorandom sequence")
  EDIT     en_seed	_indent2  _ss		_en7wd	.,		///
  	numonly	option(seed) label("Seed code")
  TEXT     tx_seed	_en7sep	@		150	.,		///
	label("Seed code")

  GROUPBOX gb_log	_lft	+45		_cwd1	_ht6,		///
	label("Iteration log")
  DEFINE holdy @y
  DEFINE cx @cx
  DEFINE cy @cy

  RADIO	   rb_log	_indent	_ss		_inwd	.,		///
	label("Show log") first option(NONE)
  RADIO    rb_trace	@	_ss		@	.,		///
	label("Show log and parameter vector")				///
	option(trace)
  RADIO	   rb_nolog	@	_ss		@	.,		///
	label("Suppress log") last option(nolog)

  GROUPBOX gb_tol	_lft2	holdy		cx	cy,		///
	label("Convergence tolerances")

  EDIT     ed_tol	_ilft2	_ms		_en7wd	.,		///
	label("Coefficient vector")					///
	option(tolerance) default(1e-4)
  TEXT     tx_tol	_en7sep	@		_en7r2b	.,		///
	label("Coefficient vector")

  EDIT     ed_ltol	_ilft2	_ls		_en7wd	.,		///
	label("Log likelihood")						///
	option(ltolerance) default(0)
  TEXT     tx_ltol	_en7sep	@		_en7r2b	.,		///
	label("Log likelihood")
END

SCRIPT protect_on
BEGIN
  max.sp_protect.enable
END

SCRIPT protect_off
BEGIN
  max.sp_protect.disable
END

SCRIPT seed_on
BEGIN
	max.en_seed.enable
	max.tx_seed.enable
END

SCRIPT seed_off
BEGIN
	max.en_seed.disable
	max.tx_seed.disable
END

PROGRAM max_output
BEGIN
	optionarg /hidedefault max.sp_iter
	if model2.rb_ml {
		if max.ck_protect {
			optionarg max.sp_protect
		}
		option max.ck_random
		if max.ck_seed {
			require max.en_seed
			optionarg max.en_seed
		}
	}
	option max.rb_log
	option max.rb_trace
	option max.rb_nolog
	optionarg /hidedefault max.ed_tol
	optionarg /hidedefault max.ed_ltol
END

PROGRAM command
BEGIN
INCLUDE _by_pr
put "factor "
varlist main.vl_var
INCLUDE _weights_pr
INCLUDE _ifin_pr
beginoptions
	option radio(model2 rb_pf rb_pcf rb_ipf rb_ml)
	if model2.ck_factors {
		optionarg /hidedefault model2.sp_factors
	}
	if model2.ck_mineigen {
		require model2.ed_mineigen
		optionarg model2.ed_mineigen
	}
	if model2.rb_ipf {
		if model2.ck_citerate {
			optionarg model2.sp_citerate
		}
	}
	if report.ck_blanks {
		require report.ed_blanks
		optionarg report.ed_blanks
	}
	option report.ck_altdivisor
	put " " /program max_output
endoptions
END
