Interactive Procedures |
RATS offers several ways to control an interactive procedure: a set of steps controlled by a user through dialogs and prompts. These range from using simple prompts and informational dialogs to simplify routine or oft-repeated tasks, to implementing complex menu-driven procedures like the CATS cointegration analysis package.
•The SOURCE instruction can be extremely helpful when you are designing procedures where the user picks one of a set of (RATS) programs to run. Put the programs on separate files and use separate SOURCE instructions for each.
•MESSAGEBOX and INFOBOX display information in pop-up dialog boxes. MESSAGEBOX is used for messages which require a user-response (such as Yes, No, or Cancel), while INFOBOX is used to display informational messages (which can be updated) and graphical “progress” bars.
•USERMENU defines a pull-down menu, which can be used to control the program. The CATS cointegration analysis procedure is the most sophisticated implementation of USERMENUs.
•SELECT displays a dialog box that allows the user to select from a list of series, strings, or integers.
•QUERY obtains information (as a line of text) from the user. The input text is then interpreted.
•MEDIT displays the contents of an array in a spreadsheet style window, which makes it easy to view, enter, or edit the values in the array.
•MENU and CHOICE display a dialog prompting the user to choose one action from a single list of choices.
•DBOX allows you to create custom user-defined dialog boxes. It provides all the capabilities of MENU/CHOICE, SELECT, and QUERY and a lot more.
Using MESSAGEBOX
MESSAGEBOX displays one of a limited set of standard dialog boxes to give information to the user or request answers to simple “Yes/No” type questions.
messagebox(style=alert) "Matrix is Singular"
messagebox(style=yesno,status=ok) "Save to File?"
The first of these is an “alert,” which simply displays the message and waits for the user to dismiss the dialog before continuing. The second includes “Yes” and “No” buttons. The variable OK will be 1 if the user clicked “Yes” and 0 if “No.”
Using USERMENU and SELECT
The example OLSMENU.RPF uses the USERMENU and SELECT instructions to create a menu-driven program for specifying and estimating a least squares regression model. The USERMENU instruction creates the menu and controls program flow, while the SELECT command allows the user to select dependent and independent variables from all series currently available.
This creates a menu titled "OLS", which will be added to the RATS menu bar just to the left of the standard Windows and Help menus:
usermenu(action=define,title="OLS") $
1>>"Select Dependent Variable" $
2>>"Select Regressors" $
3>>"Run Regression" $
4>>"Done"
The pseudo-code for using this is
loop
usermenu
do operations based upon value of %menuchoice
if %menuchoice==done value (here 4)
break
end loop
usermenu(action=remove)
Note the ACTION=REMOVE operation. That's what takes the menu out of the menu bar.
The code for handling the "Select Dependent Variable" (choice value 1) inside the loop is
if %menuchoice==1
{
select(series,status=ok) depvar
if ok
usermenu(action=modify,check=yes) dep_item
}
which uses SELECT to allow the user to pick the dependent variable. If the STATUS option returns 1 into OK (it's 1 if the user properly selects a series and 0 if the user cancels), it checks the dependent variable menu item to show the user has done that.
QUERY and MEDIT are useful for obtaining numerical or character information from a user. QUERY is used to get a limited amount of information, usually a single number or string. MEDIT gets an entire array. MEDIT is also useful for displaying a set of results in matrix form.
QUERY pops up a dialog box with a prompt for the user. This is a “modal” dialog, so the program will wait until the user is done with it before continuing. QUERY can be set to accept real values, integers or strings. You need to make sure that any variable you want filled in has the type that you want. If you need an integer, DECLARE it as an INTEGER. With the VERIFY option, you can check that the user has entered valid information, and prevent him from exiting the dialog until the input is corrected, or he cancels. You should always include a STATUS variable, and check it to see if the user has cancelled.
MEDIT pops up a “spreadsheet” with cells for a (real-valued) matrix. If you are using it for input, you should use the options EDIT and MODAL to ensure that the user fills in the matrix before the program continues. The matrix should be DECLARE’d and DIMENSION’ed first. You can’t request an array with variable dimensions—they must be known in advance. It’s usually a good idea to zero out the array first.
The following takes the most recent regression and requests information to do a set of linear restrictions on it. (Similar to what the Regression Tests wizard does). The QUERY asks for the number of restrictions. The user then fills in the restrictions in rows of a matrix using MEDIT. The HLABELS option on the MEDIT is set up to label each column with the regressor label. The final column is labeled with "=".
procedure TestRestrict
local integer nrestr i
local vect[strings] hlabels rlabels
local rect r capr
local vect lowr
local integer i
*
* Ask for the number of restrictions, make sure the
* value is positive and no bigger than %NREG.
*
query(prompt="How Many Restrictions?",status=ok,$
verify=nrestr>0.and.nrestr<= %nreg,errmessage="Illegal Value") $
nrestr
if .not.ok
return
dim r(nrestr,%nreg+1)
compute r = %const(0.0)
dim hlabels(%nreg+1)
compute rlabels = %eqnreglabels(0)
do i=1,%nreg+1
if i<=%nreg
compute hlabels(i)=rlabels(i)
else
compute hlabels(i)="="
end do i
medit(modal,edit,hlabels=hlabels) r
compute capr=%xsubmat(r,1,nrestr,1,%nreg)
compute lowr=%xcol(r,%nreg+1)
mrestrict nrestr capr lowr
end test
MENU was originally designed to handle program flow under user control, but USERMENU now does that in a more flexible way. However, MENU still has its uses. It displays a dialog box with radio buttons for selecting one and only one of the choices provided by the CHOICE blocks. The string on the MENU instruction is the overall prompt for the choices. Each CHOICE instruction is followed by the instruction or block of instructions to be executed if that is chosen. If there is more than one instruction controlled by a CHOICE, enclose the group within { }.
Combining MENU with SOURCE is an easy way of controlling a set of steps in a procedure. For example:
loop
menu "What now?"
choice "Identification"
source identify.src
choice "Estimation"
source estimate.src
choice "Diagnostics"
source diagnost.src
choice "Forecast"
source forecast.src
choice "end"
halt
end menu
end loop
will keep executing the menu until "end" is chosen, with each other choice running the instructions on a source file.
The QUERY command discussed earlier is useful for getting a single piece of user input via a simple dialog box. The DBOX command is a much more powerful instruction for creating custom dialog boxes. It can generate dialogs with multiple fields, and supports many different types of dialog box elements, including simple text labels (used to identify fields or present information), text fields to input numbers or strings, check boxes to turn settings on or off, radio buttons to select from a list of choices, and several kinds of “list boxes” allowing the user to select one or many items from lists of series or other information.
To create a dialog box, you need at least three DBOX instructions. The first command uses the option ACTION=DEFINE to begin the definition of the dialog box:
dbox(action=define)
This is followed by one or more additional DBOX commands with the (default) ACTION=MODIFY option to add the desired fields to the dialog. For example, this adds a checkbox field titled “Graph?”. The returned value (1 for on, 0 for off) will be stored in GRAPHYESNO:
dbox(action=modify, checkbox="Graph?") graphyesno
Finally, use the ACTION=RUN option to display the dialog box:
dbox(action=run)
Below is a simple example, taken from the @RegHeteroTests procedure.
procedure RegHeteroTests
local integer lm exlm
local vect[integer] selreg
local series usq logusq
local rect[integer] fulltable seltable
local integer nselect i j
Initiate the dialog box, and supply a title:
dbox(action=define,title="Regression-Based Tests")
Add a static text field to the dialog (note that we omit the ACTION option because ACTION=MODIFY is the default).
dbox(stext="Which Tests Do You Want?")
Add two check boxes, one for each of the two available tests. The user can turn on either or both boxes. The settings of the check boxes (1 for on, 0 for off) will be saved in LM and EXLM, respectively.
dbox(checkbox="u^2 form") lm
dbox(checkbox="log u^2 form") exlm
Now add a title for the list of regressors field. The SPECIAL option puts an extra space between the previous field and this new text field.
dbox(stext="Select Regressors",special=gap)
Display a scrolling list of the regressors from the most recent regression. The user can select one or more of these.
dbox(scroll,regressors) selreg
Activate the dialog box. If the user selects “Cancel” in the dialog box, the status check routine will exit from the procedure.
dbox(action=run,status=ok)
if .not.ok
return
The remaining commands use the information collected from the dialog box to perform the selected tests.
* How many regressors were selected?
compute nselect=%rows(selreg)
* Pull them out from the regressor table
compute fulltable=%eqntable(0)
dim seltable(2,nselect)
ewise seltable(i,j)=fulltable(i,selreg(j))
* Create the transformed residuals series
set usq = %resids^2
set logusq = log(usq)
if lm {
linreg(noprint) usq
# %rlfromtable(seltable)
cdf(title="LM Test on U^2") chisqr %trsquared nselect-1
}
if exlm {
linreg(noprint) logusq
# %rlfromtable(seltable)
cdf(title="LM Test on log(u^2)") chisqr %trsquared nselect-1
}
end RegHeteroTests
See the description of DBOX for complete details on this instruction.
Copyright © 2025 Thomas A. Doan