'Program for generating a table containing the coefficient estimates and standard errors of multiple equations.  It provides options on which results to collate and some basic formatting options.

logmode +addin

!doGUI = 1     'this sets whether to show dialog options or not.  Set to zero to show no dialogs, and simply use the settings below.  Set to 1 to use dialogs, with the defaults being whatever is in the settings below.


%eqnames = "e*"   'equations to be collated.  Enter * for a wildcard, or enter a name of an equation, or a list of equation names (separated by spaces), or the name of a string object in the workfile containing a list of equation names.

!doEqTitles = 1			'Add equation titles
!doEqMethod = 0			'Add equation estimation method
!doDepVar = 1			'Add equation's dependent variable.

!doSEs = 1			'Add SEs below Coefs
!doTStats = 0			'Add TStats below Coefs
!doObs = 1			'Add number of observations
!doR2 = 1			'Add R-squared
!doFStat = 1			'Add F-statistic
!doFProb = 0			'Add F-prob

!SEStyle = 2			'SE Style - 1 for just number, 2 for surround in parenthesis, 3 for surround in brackets
!TSStyle = 3			'TStat Style - 1 for just number, 2 for surround in parenthesis, 3 for surround in brackets
!SEasterisk = 1			'Add asterix to SEs for significance
!TSasterisk = 1			'Add asterix to Tstats for significance

%CoefDP = "6"			'Number of decimal places to display the Coefficient estimates
%SeDP = "4"			'Number of decimal places to display the Standard Errors
%TSDP = "4"			'Number of decimal places to display the T-Stats
%R2DP = "4"			'Number of decimal places to display the R-squared
%FSDP = "4"			'Number of decimal places to display the F-statistic
%FPDP = "4"			'Number of decimal plaecs to display the F P-val


!SaveTable = 0			'Save table as an RTF file to disk
%SaveName = "EqResults"	'Name of file to be saved
%SavePath = @wfpath		'Folder to save file in.  @wfpath will result in the same folder as the workfile.


'-------------------------------------------Process command line options-------------------------
if @len(%args)>0 then
	%eqnames = %args
	!doGUI=0
endif
if @len(@option(1)) then
	!doGUI = @hasoption("prompt")
	
	!doEqTitles = @hasoption("names")
	!doEqMethod = @hasoption("methods")
	!doDepVar = @hasoption("deps")
	
	%temp = @equaloption("coef")
	if @len(%temp) then
		%coefdp = %temp
	endif
	!doObs = @hasoption("obs")
	
	%temp = @equaloption("SE")
	if @len(%temp) then
		!doSEs=1
		%SeDP=%temp
	else
		!doSEs=0
	endif
	%temp = @equaloption("tstat")
	if @len(%temp) then
		!doTStats=1
		%TSDP =%temp
	else
		!doTStats=0
	endif
	%temp = @equaloption("R2")
	if @len(%temp) then
		!doR2=1
		%R2DP =%temp
	else
		!doR2=0
	endif
	%temp = @equaloption("FStat")
	if @len(%temp) then
		!doFStat=1
		%FSDP =%temp
	else
		!doFStat=0
	endif
	%temp = @equaloption("FProb")
	if @len(%temp) then
		!doFProb=1
		%FPDP =%temp
	else
		!doFProb=0
	endif

	%temp = @equaloption("sestyle")
	if @upper(%temp) = "n" then
		!sestyle=1
	endif
	if @upper(%temp) = "p" then
		!sestyle=2
	endif
	if @upper(%temp) = "b" then
		!sestyle=3
	endif
	%temp = @equaloption("tstyle")
	if @upper(%temp) = "n" then
		!tsstyle=1
	endif
	if @upper(%temp) = "p" then
		!tsstyle=2
	endif
	if @upper(%temp) = "b" then
		!tsstyle=3
	endif

	!SEasterisk = @hasoption("se*")
	!TSasterisk = @hasoption("ts*")

endif

'------------------------------------------- GUI --------------------------------------------------------


if !doGUI = 1 then
	if @len(%args)=0 then
 		!result = @uidialog("edit",%eqnames,"Enter a list of equation names", "text"," (use * as a wildcard)")
		if !result=-1 then
			stop
		endif
	endif

	!result = @uidialog("Text", "Table Headers:","check",!doEqTitles,"Equation Titles","check",!doEqMethod,"Equation Methods","check",!doDepVar,"Dependent Variables")

	if !result=-1 then
		stop
	endif

	!result = @uidialog("text","Equation Statistics:","check",!doSEs,"Include Standard Errors","check",!dotstats,"Include t-Statistics","check",!doobs,"Include Number of Observations","check",!dor2,"Include R-Squared","check",!dofstat,"Include F-statistic","check",!dofprob,"Include F P-value")

	if !result=-1 then
		stop
	endif

	%radioopts = """No Surround"" ""Surround in Parenthesis"" ""Surround in Brackets"""
	!result = @uidialog("caption","Table Formating","radio",!sestyle,"Standard Error Style",%radioopts,"check",!SEasterisk,"Add asterix to SEs to indicate significance","radio",!tsstyle,"t-Statistic Style",%radioopts,"check",!TSasterisk,"Add asterix to t-Stats to indicate signficance","check",!SaveTable,"Save Table to Disk?","colbreak","edit",%coefDP,"Coefficient decimal places",4,"edit",%sedp,"Standard Error decimal places",4,"edit",%tsdp,"t-Statistic decimal places",4,"edit",%r2dp,"R-Squared decimal places",4,"edit",%fsdp,"F-Statistic decimal places",4,"edit",%fpdp,"F P-Value decimal places",4)

	if !result=-1 then
		stop
	endif


	if !SaveTable=1 then
		!result= @uidialog("edit",%SaveName,"File name for output","edit",%SavePath,"Folder to put file",128)
		if !result=-1 then
			stop
		endif
	endif

endif


'---------------------------------------------------------------Table creation--------------------------------------------------------------
'create decimal place format strings
%coefformat = "f." + %coefdp
%seformat = "f." + %sedp
%TSformat = "f." + %tsdp
%R2format = "f." + %r2dp
%FSformat = "f." + %fsdp
%FPformat = "f." + %fpdp

'check to see whether entered list of equations is a string object in the workfile:
if @isobject(%eqnames) = 1 then
	if {%eqnames}.@type = "STRING" then
		%eqnames = {%eqnames}  'set %eqnames to the string contained within the inputted %eqnames
	else 
		if @neqna({%eqnames}.@type,"EQUATION") = 1 then
			%error = %eqnames + " is not a string object or equation in your workfile"       'object name wasn't a string or single equation, so error.
			@uiprompt(%error)
			stop
		endif
	endif
endif

'check to see whether a wild card was entered in the eqnames list, and process it if it was.
if @instr(%eqnames,"*")>0 then
	%eqnames = @wlookup(%eqnames,"equation")
endif


'Create list of all regressors
%allregs = ""
for !i = 1 to @wcount(%eqnames)

	%eqname = @word(%eqnames,!i) 'get ith equation name

	'error check for object existance and type
	if @isobject(%eqname) = 0 then  
		%error = %eqname + " is not an equation in your workfile"
		@uiprompt(%error)
		stop
	else
		if @neqna({%eqname}.@type,"EQUATION") = 1 then
			%error = %eqname + " is not an equation in your workfile"
			@uiprompt(%error)
			stop
		endif
	endif

	
	%vars = {%eqname}.@varlist
	%vars = @wmid(%vars,2) 'drop dependent variable
	%allregs = %allregs + " " + %vars
next

'remove duplicates from regressor list
%allregs = @wunique(%allregs)


'create table object
%tabname = "results"
if @isobject(%tabname) = 1 then
%tabname = @getnextname(%tabname)
endif
table {%tabname}

'add headers
!rowcounter=1
if !doEqTitles = 1 then
	{%tabname}(!rowcounter,1) = "Eq Name:"
	{%tabname}.setfont(!rowcounter,1) +i
	!rowcounter=!rowcounter+1
endif
if !doEqMethod = 1 then
	{%tabname}(!rowcounter,1) = "Method:"
	{%tabname}.setfont(!rowcounter,1) +i
	!rowcounter=!rowcounter+1
endif
if !doDepVar = 1 then
	{%tabname}(!rowcounter,1) = "Dep. Var:"
	{%tabname}.setfont(!rowcounter,1) +i
	!rowcounter=!rowcounter+1
endif
!headerline = !rowcounter

!rowcounter = !rowcounter+1

'adjust width of first column
{%tabname}.setwidth(1) 15


'add regressors to table
for !i=1 to @wcount(%allregs)
	{%tabname}(!rowcounter,1) = @word(%allregs,!i)
	!rowcounter = !rowcounter+2+!doSEs+!doTStats  '+2 (rather than +1) to add an extra line between each regressor.
next

!rowcounter=!rowcounter-1 'adjust rowcounter to take account of the extra space added after last regressor 

'cycle through equations
!colcounter=2
for !i=1 to @wcount(%eqnames)
	%eqname = @word(%eqnames,!i)
	if !doEqTitles = 1 then
		{%tabname}(!doEqTitles ,!colcounter) = %eqname  'add equation name to table
	endif
	%vars = {%eqname}.@varlist
	%dep = @word(%vars,1)  'get dependent variable
	%regs = @wmid(%vars,2)  'get regressors
	%method = {%eqname}.@method
	if !doEqMethod = 1 then
		{%tabname}(!doEqTitles +!doEqMethod,!colcounter) = {%eqname}.@method  'add equation method to table
	endif
	if !doDepVar = 1 then 
		{%tabname}(!doEqTitles +!doEqMethod+!doDepVar,!colcounter) = %dep  'add dependent var to table
	endif
	!df =  ({%eqname}.@regobs-{%eqname}.@ncoef)

	'add coefs and SEs to table
	for !j=1 to @wcount(%allregs)  'cycle through all regressors in %allregs
		%reg = @word(%allregs,!j)
		!posreg = @wfind(%regs,%reg)  'find the position of the current regessor in the equation's regressor list
		if !posreg>0 then

			'ADD COEFS
			!coefpos = !headerline +(2+!doSEs+!doTStats)*!j-(!doSEs+!doTStats+1)
			{%tabname}(!coefpos,!colcounter) = {%eqname}.@coef(!posreg) 'add coef to table
			{%tabname}.setformat(!coefpos,!colcounter) {%coefformat}  'format coef decimal places

			!doZDist = 0   'does equation use z-stats or t-stats?
			if %method="ARCH" or %method="GLM" or %method="BINARY" or %method="TOBIT" or %method="COUNT" or %method = "ORDERED" then
				!doZDist=1
			endif		

			'CALCULATE PVALS
			!tstat = {%eqname}.@tstat(!posreg) ' get tstat/zstat
			!pval = @tdist(!tstat,!df)
			if !doZDist =1 then  'if equation p-values require normal distribution, calcluate them differently.
				!pval = 2*(1-@cnorm(@abs(!tstat)))
			endif
			!sig10 = 0
			!sig5 = 0
			!sig1 = 0
			if !pval < 0.01 then
				!sig1=1
			endif
			if !pval < 0.05 then
				!sig5=1
			endif
			if !pval < 0.10 then
				!sig10=1
			endif

			'ADD SEs
			!se = {%eqname}.@stderr(!posreg)  'get se
			%se = @str(!se,%seformat)  'convert to string
			if !SEStyle = 2 then
				%se = "(" + %se + ")"
			endif
			if !SEStyle = 3 then
				%se = "[" + %se + "]"
			endif
			if !SEasterisk then  'add asterix if significant
				if !sig10=1 then
					%se = %se + "*"
				endif
				if !sig5=1 then
					%se = %se + "*"
				endif
				if !sig1=1 then
					%se = %se + "*"
				endif
			endif
			if !doSEs = 1 then
				{%tabname}(!coefpos+1,!colcounter) = %se  'add to table
			endif

			'ADD TSTATS
			%tstat = @str(!tstat,%tsformat) ' convert sto string 
			if !TSStyle = 2 then
				%tstat = "(" + %tstat + ")"
			endif
			if !TSStyle = 3 then
				%tstat = "[" + %tstat + "]"
			endif
			if !TSasterisk then  'add asterix if significant
				if !sig10=1 then
					%tstat = %tstat + "*"
				endif
				if !sig5=1 then
					%tstat = %tstat + "*"
				endif
				if !sig1=1 then
					%tstat = %tstat + "*"
				endif
			endif			
			if !doTStats = 1 then
				{%tabname}(!coefpos+1+!doSEs,!colcounter) = %tstat
			endif

			'ADD OBS, R2 and F-stat
			if !doObs = 1 then
				%obs = @str({%eqname}.@regobs,"f.0") ' convert to string 
				{%tabname}(!rowcounter+1,!colcounter) = %obs
			endif
			if !doR2 = 1 then
				%r2 = @str({%eqname}.@r2,%r2format) ' convert to string 
				{%tabname}(!rowcounter+1+!doObs,!colcounter) = %r2
			endif
			if !doFstat = 1 then
				%fstat = @str({%eqname}.@f,%fsformat) ' convert to string 
				{%tabname}(!rowcounter+1+!doObs+!dor2,!colcounter) = %fstat
			endif
			if !doFProb = 1 then
				!fpval = 1-@cfdist(@abs({%eqname}.@f),{%eqname}.@ncoef-1,!df)  'calculate f pvalue
				%fpval = @str(!fpval,%fpformat)  'convert to string
				{%tabname}(!rowcounter+1+!doObs+!dor2+!dofstat,!colcounter) = %fpval
			endif
	
		endif
	next
	
	!colcounter = !colcounter+1
next

{%tabname}.setlines(!headerline) +d 'add double line separator after titles.  Note this has to be done after all the equations have been added, otherwise the table will be the wrong size when adding the lines.

{%tabname}.setlines(!rowcounter) +d  'add double lines after last regressor
!rowcounter=!rowcounter+1

if !doObs =1 then  'add observations title
	{%tabname}(!rowcounter,1) = "Observations:"
	{%tabname}.setfont(!rowcounter,1) +i
	!rowcounter=!rowcounter+1
endif

if !doR2 =1 then  'add R2 title
	{%tabname}(!rowcounter,1) = "R-squared:"
	{%tabname}.setfont(!rowcounter,1) +i
	!rowcounter=!rowcounter+1
endif
	

if !doFstat then   'add F-Stat
	{%tabname}(!rowcounter,1) = "F-statistic:"
	{%tabname}.setfont(!rowcounter,1) +i
	!rowcounter=!rowcounter+1	
endif

if !doFProb then    'add F p-value
	{%tabname}(!rowcounter,1) = "Prob(F-stat):"
	{%tabname}.setfont(!rowcounter,1) +i
	!rowcounter = !rowcounter+1
endif

{%tabname}.setlines(!rowcounter) +d  'add double lines after last statistic
!rowcounter=!rowcounter+1

show {%tabname}

if !SaveTable=1 then
	%savepath = @trim(%savepath) 'remove spaces from the end of savepath
	
	if @length(%savepath) = 0 then  'if no path was given, use default working path
		%savepath = @temppath
	endif

	%last = @right(%savepath,1) 
	if @neqna(%last,"\") then 'workout if the last character of the path is a slash, if not add one.
		%savepath = %savepath + "\"
	endif
	
	%fullpath = %savepath + @trim(%savename) + ".rtf"  'create full path and file name
	%fullpath = @trim(%fullpath)
	{%tabname}.save(t=rtf) %fullpath 
endif
