Converting to Structures: Example with ols and olsmt

Converting to Structures: Example with ols and olsmt

In the last section of this tutorial series, A gentle introduction to using structures, we learned the steps for using a structure:

  1. Define the structure
  2. Declare the structure instance
  3. Assign the structure members

Here we will go through the steps for converting a simple example from using the GAUSS procedure “ols” which uses many individual variables for input and output as well as global control variables to the GAUSS procedure olsmt which employs structures instead.

Both of these procedures calculate least squares estimates and an identical set of inferential statistics. There will be three steps to creating our simple example program:

  1. Create random data for the regression
  2. Apply the settings for the “ols” or” olsmt” procedure
  3. Call the procedure that performs the calculations.

Create data

For both examples, the data creation will be identical and look like this:

//Create data for example
rndseed 2134124;
indVar = rndn(100, 4);
depVar = rndn(100,1);

Here we are creating some random normal numbers to fill in our dependent and independent variables so that we have some data on which to run the regression. The “rndseed” keyword allows us to repeat the same random inputs for each program.

Apply settings

In this step, we see the first difference between the two different programs. The main difference will arise from the fact that we need to create a structure instance to hold our control variables instead of simply assigning to global control variables. The code for the example using “ols” will set the global control variables like this:

//Apply settings
__altnam = { GDP, GNP, Income, TBill, Growth };
__con = 1;
__output = 1;

Applying the settings for the “olsmt” example will be very similar. The names for the settings will be the same; however, they will be structure members instead of global variables. Since we are using structures we have to define them, declare them and assign their values.

//Define structure
#include olsmt.sdf

//Declare structures
struct olsmtControl control;
struct olsmtOut out;

//Fill in default values
control = olsmtControlCreate();

//Apply settings
control.altnam = { GDP, GNP, Income, TBill, Growth };
control.con = 1;
control.output = 1;

Since the olsmt structures have already been created, you do not have to create a file that defines the structure. However, you do need to execute the file that contains this definition so that GAUSS knows the structure and its members. This is accomplished by the statement “#include olsmt.sdf”. The “#include” statement tells GAUSS to copy and paste the code from the specified file, in this case olsmt.sdf,  into your current program at the location of the “#include”.

The next step is declaring that “control” and “out” will be instances of their respective structure type. Then the default values for each member variable are filled in. GAUSS offers a convenient function, “olsmtControlCreate” that will apply the default values for each member variable of the olsmtControl structure. The final step above is applying the new settings to the “altnam”, “con” and “output” member variables. The “out” structure will be filled in by the call to the “olsmt” procedure.

Calling the procedure

The call to “ols” looks like this:

//Perform calculation
{ vnam, m, b, stb, 
  vc, stderr, sigma, 
  cx, rsq, resid, dwstat } = ols("", y, x);

Here is the call to “olsmt”:

//Perform calculation
out = olsmt(control, "", y, x);

Complete code for both versions

The complete code for each version of the program is included below. Copy and paste it into the GAUSS editor, run the files and try altering the settings to become comfortable with what you have learned in this short tutorial section.

/***** OLS WITH GLOBAL VARIABLES *****/

//Create data for example
rndseed 2134124;
x = rndn(100, 4);
y = rndn(100, 1);

//Apply settings
__altnam = { GDP, GNP, Income, TBill, Growth };
__con = 1;
__output = 1;

//Perform calculation
{ vnam, m, b, stb,
  vc, stderr, sigma,
  cx, rsq, resid, dwstat } = ols("", y, x);
/***** OLSMT WITH STRUCTURES *****/

//Define structures
#include olsmt.sdf

//Create data for example
rndseed 2134124;
x = rndn(100, 4);
y = rndn(100, 1);

//Declare structures
struct olsmtControl control;
struct olsmtOut out;

//Apply settings
control = olsmtControlCreate();
control.altnam = { GDP, GNP, Income, TBill, Growth };
control.con = 1;
control.output = 1;

//Perform calculation
out = olsmt(control, "", y, x);