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. Declare the structure instance
  2. 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;
x = rndn(100, 4);
y = 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.

//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 are part of the standard GAUSS package, you do not have to create a file that defines these structures.

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 *****/

//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);