|
Examples / OLSMENU.RPF |
OLSMENU.RPF is an example of a user-defined menu doing using USERMENU. It creates a menu to allow the user to select the dependent variables and regressors for a least squares regression.
This sets up the data that will be available:
calendar(a) 1922
allocate 1941:1
open data fooddata.dat
data(format=free,org=obs) / $
foodcons prretail dispinc foodprod prfarm
set trend = t
set avgprices = (prfarm+prfarm{1})/2.0
set logconsump = log(foodcons)
set logincome = log(dispinc)
set logprice = log(prretail)
The following code is the main part of the program. After executing these lines, a menu called “OLS” is added to the menu bar. The user must select a dependent variable and a list of independent variables before they can choose the “Run Regression” operation, which does the OLS regression. The user can change the variables and re-run the regression as desired. Select “Done” to remove the menu from the menu bar and return control to “ready” mode.
compute depvar=0,regressors=0
compute dep_item = 1, ind_item = 2, run_item = 3, done_item = 4
usermenu(action=define,title="OLS") $
1>>"Select Dependent Variable" $
2>>"Select Regressors" $
3>>"Run Regression" $
4>>"Done"
This initiallly disables the "Run Regression" item:
usermenu(action=modify,enable=no) run_item
Start a LOOP and activate the menu
loop
usermenu
Inside the loop, we process %MENUCHOICE values that are returned by the USERMENU. (The RATS program won't continue until the user selects from the user menu).
This is the handling of the value 1 choice ("Select Dependent Variable"). This does a SELECT instruction to pick one of the SERIES. If the user makes a selection (the OK value in the STATUS option will be non-zero), we modify the menu item to show a check.
if %menuchoice==1 {
select(series,status=ok) depvar
if ok
usermenu(action=modify,check=yes) dep_item
}
This is the handling of the value 2 choice ("Select Regressors"). With two parameters, SELECT allows multiple selections, returning the number of choices in REGRESSORS and the VECTOR of choices in REGLIST. Again, if the STATUS returns a non-zero OK value, we check the menu item.
if %menuchoice==2
{
select(series,status=ok) regressors reglist
if ok
usermenu(action=modify,check=yes) ind_item
}
This is the handling of the value 3 choice ("Run Regression"). This runs a regression of the chosen dependent variable on CONSTANT (assumed in all regressions) and the variables in the current REGLIST. This is only active if the user has chosen both the dependent variable and the regressors.
if %menuchoice==3 {
linreg depvar
# constant reglist
}
This is the handling of the value 4 choice ("Done"). This does a BREAK out of the LOOP and is the only way out of the user menu (other than quitting RATS completely).
if %menuchoice==4
break
This is inside the loop, but is executed after any choice. It checks to see if the user has both chosen a dependent variable and regressors. If so, it enables choice 3 (the run_item):
if depvar>0.and.regressors>0
usermenu(action=modify,enable=yes) run_item
This is the end of the loop. Outside the loop, this will remove the USERMENU.
end loop
usermenu(action=remove)
This will allow the user to run regressions with various combinations of regressors and dependent variables until they select the “Done” item from the OLS menu.
Full Program
***
*** The first few lines of this program are specific to the data
*** set being used. The define the frequency, starting and ending date,
*** read in the data, and do the necessary transformations.
*** To use the OLS menu program with your own data, simply modify
*** or add to these lines as needed for your data set
***
calendar(a) 1922
open data fooddata.dat
data(format=free,org=obs) 1922:1 1941:1 foodcons prretail dispinc foodprod prfarm
set trend = t
set avgprices = (prfarm+prfarm{1})/2.0
set logconsump = log(foodcons)
set logincome = log(dispinc)
set logprice = log(prretail)
***
*** The following code is the main part of the OLSMENU program.
*** After executing these lines, a menu called "OLS" is added to
*** the menu bar. The user must select a dependent variable
*** and a list of independent variables before they can choose
*** the "Run Regression" operation, which does the OLS regression.
*** The user can change the variables and re-run the regression as
*** desired. Select "Done" to remove the menu from the menu bar
*** and return control to "ready" mode.
***
compute depvar=0,regressors=0
compute dep_item = 1
compute ind_item = 2
compute run_item = 3
compute done_item = 4
USERMENU(ACTION=DEFINE,TITLE='OLS') $
1>>'Select Dependent Variable' $
2>>'Select Regressors' $
3>>'Run Regression' $
4>>'Done'
*
* Disable the 'Run Regression' item
*
usermenu(action=modify,enable=no) run_item
*
* Go into a loop around the menu
*
loop
usermenu
if %menuchoice==1
{
select(series,status=ok) depvar
*
* If user made a selection, check the DEP_ITEM
*
if ok
usermenu(action=modify,check=yes) dep_item
}
if %menuchoice==2
{
select(series,status=ok) regressors reglist
if ok
usermenu(action=modify,check=yes) ind_item
}
*
* The user can only run the regression if she has already chosen both
* regressors and dependent variable
*
if %menuchoice==3
{
linreg depvar
# constant reglist
}
if %menuchoice==4
break
*
* If we have both a dependent variable and regressors
* enable the 'Run' item.
*
if depvar>0.and.regressors>0
usermenu(action=modify,enable=yes) run_item
end loop
usermenu(action=remove)
Copyright © 2026 Thomas A. Doan