Regression Analysis Software (RAS 2.1)
Platform: HP48G, G+, or GX
Author: MacDonald R. Phillips
Email: mrp100@erols.com  phillipsm.ggd@gao.gov 
April 1999


RAS is regression analysis software that allows you to fit data by the least squares method.  It supports OLS (ordinary least squares), NLLS (non-linear least squares that are linear in its coefficients), and TSLS (two-stage least squares).  The software allows you to enter your data, label your variables, and specify a model to be fit using all or any combination of variables.  Once you enter a dataset, all sorts of models can be fitted to the data.

NEW: RAS 2.1 adds, hopefully, complete validation of variable and dataset names so they do not conflict with any HP commands, functions, operations, etc., or with any of your created variables.  If you attempt to enter as a variable name any HP or MATHLIB function, any variable name in the HOME path, or any duplicate variable name, you will get a "Name conflict.." error message.  Press OK and enter another variable name.  Also, you will be able to delete only variables that are in the current variable list.  I also added a new data manipulation function called LAG.  With LAG you can create lagged variables for any number of periods of any of your current variables.

NEW: RAS 2.0 combines OLS and NLLS under one menu - LSQ.  Since OLS is just a subset of NLLS, the two are now entered and processed the same.  RAS 2.0 also allows you to use the functions of MATHLIB (HP48SX Engineering Mathematics Library) by John F. Holland as well as your own user-defined functions.  However, RAS does not use the MATHLIB routines for regression, so you do not need MATHLIB to use RAS.  RAS does regression in the complex plane also; at least I get the same answers from worked out problems I've seen, but don't ask me how to interpret the results.

RAS 1.6 corrects a "feature" in the NLLS program that no one has yet to complain about.  In NLLS you can enter functions of variables, including multiplying, dividing, or subtracting variables.  However, due to the HP's manner of list processing, you could not add variables to form a new variable because the HP interprets a plus when list processing as concatenation.  I have programmed around this "feature" so that in your model statement you can now enter such things as 'X+Y+2', although I'm not sure why anyone would want to add variables together since it is usually the influence of separate variables one is testing for.

RAS 1.5 adds the ability to do weighted regressions in OLS and NLLS.  Simply enter the weight variable along with the other variables.  In OLS and NLLS there is an option to have a weight or not; if you choose yes, simply enter the name of the weight variable.  Doing weighted regressions eats up a lot of memory while processing.  Not only is the data matrix used, but an n x n matrix of the weights is also created, where n is the number of observations in the data set.  When you are predicting or forecasting y-hat, and asked to enter the values of the variables, you will also be asked to enter a value for the observation's weight.  The weight is used in calculating the standard errors of the predicted values.




The output for least squares analysis is:

* Regression equation
* Standard error of the residuals
* Standard errors of the estimated coefficients
* ANOVA table (Analysis of Variance Table)
* t statistics and probabilities for the estimated coefficients
* F statistic and probability for the estimated equation
* R^2 and adjusted R^2
* Durbin Watson statistic
* Predicted values with standard errors of the mean predicted value and point predicted value along with user specified confidence intervals
* F statistic and probability associated with adding 1 or more variables to a model

The output for TSLS is:

* Regression equation
* Standard error of the residuals
* Standard error of the estimated coefficients
* t statistics and probabilities for the estimated coefficients
* Predicted values

Installing the Software

Simply download the RAS file to your calculator in binary mode. A directory named RAS will be set up.  All variables except MAIN, and the DATA and TEMP subdirectories have been hidden using GxTools 6.0.  The TEMP subdirectory is used for some intermediate processing.  If the program crashes for any reason, check the TEMP subdirectory and purge any variables before starting a new regression analysis.  If you want access to the (many) hidden variables in order to modify and/or add routines, use or get a copy of GxTools 6.0 and follow instructions.

The program was developed and tested on a HP48GX, Version R with a HP 128KB RAM card merged with main memory.  If there are problems on any other ROM version, please let me know.  If you press R-S RCL RAS and then BYTES, the checksum is  #A37h and the size is 18,318.50 bytes.

The software is freeware.  It is presented as is with no warranty as to usefulness for any purpose.  You, the user, must be the judge.  I also assume a basic understanding of regression analysis and use of the HP48G(X) calculator.  My purpose is to explain how to use the software, not to teach statistics or how to operate your calculator.  If something, however, is not as clear as you think it should be, please feel free to email me any questions you have.  Output from the least squares routines has been compared with output from SAS 6.12.  Except for rounding errors, they are the same.

Using RAS

Enter the RAS directory and press MAIN.  This sets up the main menu.  From here you can select one of 3 menus: DATA, LSQ, or TSLS.  In many places you will be prompted to enter data; if you enter nothing and simply press the ENTER key you will exit the routine and get a message to try again.  The exception to this rule occurs when you are prompted to enter variable names for your data; you will be continuously prompted to enter the names until you do so.  This is because, once the data is entered, the program cannot work without the variable names.

The DATA Menu

Pressing DATA puts you in the data entry menu.  The first six menu items are NEW, CONT, SIZE, VARS, CORM, and MM.

You enter a new dataset by pressing NEW.  You will first be prompted to save the existing dataset (data and variable names).  Select Yes to save or No to not save.  If you select Yes, you will be prompted for a dataset name.  Enter a name and the dataset will be saved in the DATA subdirectory.

You will now be prompted to enter the first observation.  The data for each observation is entered in a vector.  Simply enter each value separated by a space.  Press ENTER to enter the observation; you will now be prompted for the 2nd observation, etc.  When all observations have been entered, press ENTER again.

You will now be prompted to enter the variables' names, one by one.  I suggest using one or two letter names in capital letters so there will be NO conflicts with the program variables.  If there is a name conflict with any of the HP commands, a message saying so will appear, and you will be prompted to enter the variable name again.  NOTE: This is the only place where variable names are checked for conflict with HP functions and commands.

Once the variables' names are entered, you will be ready to do your analysis.

The CONT key allows you enter more observations to the existing data set.  If there are n observations already in, you will be prompted to enter the n + 1 observation.  Simply proceed as above.  You will not, however, be prompted to enter variable names once you are done entering the additional observations.

The SIZE key tells you the number of variables, number of observations and the size of the dataset in bytes.

The VARS key gives you a list of variable names in the current dataset.

The CORM key calculates the correlation matrix for all variables.

The MM key takes you back to the main menu.

The second set of menu keys, accessed by pressing NXT, allow you to manipulate your data set.  The keys are TRSF, CMPT, AVAR, DVAR, MMAT, and TIME.

TRSF allows you to transform a variable using a HP function(s).  You may change the values of a variable to their log, or sin, or any set of operations that act on one number.  Press TRFS and you will be prompted for a variable name and put in alpha mode.  Simply enter the name.  Press ENTER when done.  You will then be prompted to enter the operations to transform the variable.  What you are doing is entering a program in RPL.  For instance, pressing the LN key would enter that operation and compute the natural log of the variable.  Entering 2 / would divide each value by 2.  The sequence of operations must operate on only one value and leave only one value after the computations are done.

CMPT allows you to create a new variable from existing variables. It uses the DOLIST routine.  Press CMPT and you will be prompted to enter the variable names to be operated on.  Enter the names in the order you want them to appear on the stack for processing.  Press ENTER when done.  You will then be prompted for the operations.  As above, enter the operations in RPL form.  The sequence of operations must use the values from all the specified variables and leave only one value after the computations are done.  Press ENTER when done.  At the end you will be prompted for a name of the new variable.

AVAR allows you to add a new variable to your dataset.  You will be prompted to add the values one by one.  When done, press ENTER.  You will then be prompted for a variable name.  Press ENTER when done.

DVAR allows you to delete a variable and its values from the current dataset. You will be prompted for the name of the variable to delete.  Press ENTER when done and the variable will be deleted.  If you try to delete a variable that is not in the variable list, you will get a "Invalid variable.." message.

MMAT allows you to modify the entire dataset at once.  If you want to have the natural log of every value in the dataset, simply press MMAT, enter LN when prompted for the operations, and then press ENTER.  The RPL program to modify the dataset must operate on only one value at a time and return only one result.

TIME allows you to enter a time variable to your dataset.  Pressing TIME prompts you for a beginning value and then a step size.  For instance, if you want to add a time variable beginning at year 2 and increasing by 5 until the end of the data, i.e., 2, 7, 12, 17, etc., then enter a beginning value of 2 and step size of 5.  You will then be prompted to enter a variable name.

The next set of menus accessed by pressing NXT is LAG, SigmaDat, SDAT, GDAT, and DDAT.

LAG allows you to lag a current variable any number of periods.  For instance, if a regression equation is based on income in the current period, Yt, and income from one year ago, Yt-1, you can enter Yt in your dataset and then automatically enter Yt-1 with LAG.  Press LAG and you are prompted for the lag period, 1 in this case.  Enter 1 and press ENTER.  You are then prompted to enter the variable name, Y in this case.  You will then be prompted to enter the t-1 value of Y.  Enter the value and press ENTER.  LAG will create a new variable with a .Ln extension.  "L" stands for lagged and n is the number of periods.  In this example, the new variable would be Y.L1.

If you do not know the prior values of the variable you want to lag, then you will have to edit the data and take out the number of top rows that you want to lag the variable by.  Editing can be done with the next command.

Pressing SigmaDAT returns the dataset to the stack for viewing or editing with your favorite matrix editor.  If you change the dataset, save it by pressing L-S (left-shift) SigmaDAT.

SDAT saves the current dataset in the DATA subdirectory.  You will be prompted to give the dataset a name.  Press ENTER when done.

GDAT gets a saved dataset and makes it the current dataset.  You will first be asked if you want to save the current dataset.  Choose Yes or No.  After you make your choice and save it or not, you will be given a list of datasets to choose from.  Highlight your choice and press OK.  Press CANCEL to cancel the operation and retain the current dataset.

DDAT deletes a saved dataset.  Press DDAT and you will be presented with a list of datasets.  Scroll to the one you want to delete and press ENTER.  The dataset will be deleted.

Using User-Defined Functions

To use user-defined functions with RAS, I strongly recommend the local variable, program structure.  An example would be:

		<< -> v1 v2 v3 << program >> >>

In the program, any addition (use of the "+" sign) must be replaced with the ADD function in the MATH LIST directory.  This is because most of the routines in RAS use list based processing.

The LSQ Menu

The LSQ menu allows you to solve basic multiple regression problems that are linear in its coefficients but not necessarily linear in its variables. For instance, given variables X, Y, and Z, you could estimate an equation such as the following:

            LN(Y) = a + b*X^2 +c*(1/Z) + d*X*Z

The equation is linear in the coefficients (a, b, c, d), but not in its variables (X, Y, Z).  For LSQ to work, the number of observations must be more than the number of coefficients estimated (including the y-intercept).  In statistical terms, the degrees of freedom must be 1 or more.

Pressing LSQ in the main menu sets up the LSQ menu.  There are seven menu keys available: YINT?, WGT?, MODL, LSQ, STATS, MB, and MM.

The current dataset is SMOK; it is also saved in the DATA subdirectory along with the datasets TS and GAMa.  (Pressing SIZE, in the DATA subdirectory, shows that SMOK has 4 variables, 20 observations, and is 655 bytes.  Pressing VARS shows that the variables are named R, A, BP, and S.)

YNT? presents a choose box asking whether or not you want a y-intercept computed.  Choose Yes or No and press OK.  The selected option remains in effect until it is changed.

WGT? presents a choose box asking whether or not you want to use a weight in the regression.  Choose Yes or No and press OK.  If you choose Yes, you will be prompted for the name of the weight variable.  Enter the name and press ENTER. The selected option remains in effect until it is changed.

Use MODL to set up your model.  On pressing MODL you will first be prompted to enter the dependent variable.  Enter the name of the variable (you have to press the alpha key first) and press ENTER. You will then be prompted to enter a list of the independent variables or functions of independent variables you want included in the model.  The functions must be preceded with the ' mark; e.g., 'LN(X)'.

To see what model you have entered, press LS MODL.  A list of the variables in the model is put on the stack.  The first variable in the list is the dependent variable.  Press LS MODL now and see { R A BP S }.  R is the risk of having a stroke within ten years multiplied by 100; A is age, BP is blood pressure, and S is a dummy variable for smoker (1) or non-smoker (0).  The model to be fitted is that risk is a linear function of age, blood pressure, and smoking.  YINT? is already set to Yes and WGT? is already set to No.

Press LSQ to do the regression.  The output is:

     3:  5.76
     2:  {15.22 0.17 0.05 3.00}
     1:  'R=-91.76+1.08*A+0.25*BP+8.74*S'

On level 1 is the fitted equation.  On level 2 is a list of the standard errors of the coefficients.  For instance, the standard error of the intercept is 15.22.  On level 3 is the standard error of the residuals.  Note: If the number of coefficients to be estimated is equal to or greater than the number of observations, you will get an error message saying so.

Before going to the STATS menu, a word about the MB key.  MB stands for model building.  If you compute a model and the next model you compute is the same except for one or more added variables, pressing MB computes the F statistic and probability of the F statistic that measures whether the added variables significantly contributed to the model.  A high value of F and low probability indicated that the added variables contributed to the explanatory power of the model.

Pressing the final key, MM, takes you back to the main menu.

The STATS Menu

Press STATS to enter the statistics menu.  There are nine choices here: ANOV, TTEST, FTEST, R^2, YF, P->CI, DW, BACK, MM.

ANOV recalls the ANOVA table (Analysis of Variance table) to the stack as a 3 by 3 matrix.  The elements of the matrix are:

          DFR   SSR    MSR
          DFE   SSE    MSE
          DFT   SST     F

DFR is the regression degrees of freedom.  DFE is the error degrees of freedom.  And DFT is the total degrees of freedom.  SSR is the regression sum of squares; SSE is the error sum of squares; and SST is the total sum of squares.  MSR is the regression mean sum of squares; MSE is the error mean sum of squares; and F is the F statistic.  F = MSR/MSE with DFR and DFE degrees of freedom.
For the problem at hand the ANOVA matrix is:

          3     3660.74     1220.25
         16      530.21       33.14
         19     4190.95       36.82


Press TTEST to get the t statistics on the regression coefficients.  The output is:

          2:  P(t): { 1.76E-5  7.49E-6  4.24E-5  0.01 }
          1:  t: { -6.03  6.49  5.57  2.91 }

On level 1 are the t statistics of the regression coefficients.  On level 2 are the two-tail probabilities of the t statistics.
Press FTEST to get the F statistic for the model.  The output is:

          2:  P(F): 2.06E-7
          1:  F: 36.82

On level 1 is the F statistic.  On level 2 is the probability of getting an F value this high or higher.

Press R^2.  The output is:

           2:  adjR^2:  0.85
           1:  R^2:  0.87

To forecast values press YF.  You will be prompted to enter values for the independent variables.  Enter 75 for A, 175 for BP, and 1 for S.  (Note: If you had computed a weighted regression, you would also be prompted to enter a value for the weight.)  The output is:



           3:  SE(R0):  6.08
           2:  SE(R_mean):  1.96
           1:  'R=41.80'

On level 1 is the estimated value of R.  On level 2 is the standard error of the mean value of R and on level 3 is the standard error of an individual value of R.  SEp measures the standard error of predicting R for all persons age 75 with BP of 175 and who smoke.  SEf measures the standard error of forecasting R for one person with those characteristics.

To calculate confidence intervals around the estimated value, R in this case, the output of YF MUST remain on the stack.  P->CI computes the two confidence intervals for a given P.  For instance, if you want a 95 percent confidence interval, enter 95 and press P->CI.  The output is:

           2:  CI(R0): { 28.91  54.70 }
           1:  CI(R_mean): { 37.64  45.96 }

Thus, a 95 percent CI around the average value of R goes from a low of 37.64 to a high of 45.96.  The 95 percent CI for R0 goes from a low of 28.91 to a high of 54.70.

DW computes the Durbin-Watson statistic to test for autocorrelation in time series data.  While it makes no sense to compute it for this dataset, pressing DW gives:
           2:  DW(3,20):  1.36
           1:  p:  0.30

On level 1 is a first round estimate of rho (funny p), the correlation between the first order differences in the residuals.  On level 2 is the Durbin-Watson d statistic for 3 variables and 20 observations.

Example 2:

You may, for instance, want to test for an interaction between age and blood pressure in the model.  Press MODL and enter R as the dependent variable (be sure to press the alpha key before pressing R).  Then enter the independent
variables { A BP S 'A*BP' }.  Be sure to press the alpha and ' keys where appropriate.  NOTE: You may navigate through the HP's menus to get to any of the built-in functions.  You cannot do so for the MATHLIB functions.  The MATHLIB functions must be typed in.

Press YINT? and choose Yes.

(Note: If you wanted to compute a weighted regression, you would now press WGT?, choose Yes, and enter the name of the weight variable.)

Press LSQ to compute the model.  The output is:

          3:  5.88
          2:  { 56.94  0.78  0.35  3.07  4.81E-3 }
          1:  'R=-123.16+1.51*A+0.45*BP+8.87*S-2.76E-3*(A*B)'

Before going to STATS, press MB.  This gives the F value and probability associated with adding the interaction variable 'A*BP' to the model you computed previously.  The output is:

           2:  P(F):  0.57
           1:  F:  0.33

The interaction does not significantly add to the explanatory power of the model.  This can also be seen by going into the STATS menu and running a t test on the variables by pressing TTEST.  The output is:

           2:  P(t):  { 0.05  0.07  0.21  0.01  0.57 }
           1:  t:  { -2.16  1.94  1.30  2.88  -0.57 }

Pressing FTEST gives:

           2:  P(F):  1.19E-6
           1:  F:  26.54

Pressing R^2 gives:

           2:  adjR^2:  0.84
           1:  R^2:  0.88
Pressing YF and entering 75 for A, 175 for BP, and 1 for S, yields:  (Note: If you had computed a weighted regression, you would also be prompted to enter a value for the weight.)

           3:  SE(R0):  6.24
           2:  SE(R_mean):  2.10
           1:  'R=41.45'

Without dropping the last output, enter 95 and press P->CI; the 95 percent confidence intervals are:

           2:  CI(R0):  { 28.14  54.76 }
           1:  CI(R_mean):  { 36.97  45.92 }

Two-Stage Least Squares

Press NXT and then MM to go back to the main menu.  Two-stage least squares is next.  But first you need to load the dataset I use for the example.  Clear the display and go into the data menu.  Press NXT two times and then GDAT to get the dataset.  You will be prompted to save the existing dataset.  If you want to save it choose Yes and give it a name; if not, choose No.  A list of saved datasets will now appear.  Scroll to TS, highlight it and press OK to select it.

Press NXT and then VARS to see the names of the variables in the dataset.  The variables are { Q P D F A }.  Press MM to return to the main menu.

Press TSLS to bring up the two-stage least squares menu.  There are only five menu keys available.  They are: MODL, TSLS, TTEST, YF, and MM.

Pressing MODL asks for 4 different set(s) of variables.  You will be prompted first to enter the dependent variable (the endogenous variable to be estimated).  Enter Q.  You will then be prompted to enter the other endogenous variables (the dependent variables you will not be estimating with this model).  Enter P.  You will then be prompted to enter the exogenous variables for this model.  Enter D.  You will then be asked to enter the excluded exogenous variables.  Enter F and A.

Press TSLS to estimate the model.  The output is:

           3:  3.87
           2:  { 7.92  0.10  0.05 }
           1:  'Q=94.63-0.24*P+0.31*D'

As with LSQ, the second and third lines are the standard errors of the variables and residuals.

Press TTEST and the output is:

           2:  P(t):  { 1.08E-9  0.02  3.81E-6 }
           1:  t:  { 11.95  -2.52  6.69 }

Press YF to estimate Q.  You will be prompted to enter a value for P; enter 100.  Enter 100 also for D.  The estimated value of Q is 101.68.

Estimate another model for Q.  Erase the stack first.  Then press MODL and enter Q for the dependent variable, P for the other endogenous variable, F and A for the exogenous variables, and D for the excluded exogenous variable.  Press TSLS and the output is:

           3:  6.04
           2:  { 12.01  0.10  0.05  0.10 }
           1:  'Q=49.53+0.24*P+0.26*F+0.25*A'

The output of the t test is:

           2:  P(t):  {7.95E-4  0.03  5.79E-5  0.02 }
           1:  t:  { 4.12  2.40  5.41  2.54 }

A Final Example (Using MATHLIB)

This example fits a forth-order Legendre polynomial with only even terms to the data in GAM.  The Legendre function (POFX(v,n)) is in the Orthogonal Polynomial Menu in MATHLIB.  Load the dataset GAMA with the GDAT command in the DATA menu.  The variables are D and C.  D is an angle measured in degrees, so set your calculator to degree mode.  The Legendre polynomial requires the cosine of the angle.  Press LSQ to enter the least squares menu.

In this model there is no y-intercept and no weighting.  Press YINT? and WGT? and select NO for both.  Press MODL to enter the model.  The dependent variable is C.  Remember to press the alpha key first.  The independent variables are: {'POFX(COS(D),0)' 'POFX(COS(D),2)' 'POFX(COS(D),4)'}.  Press LSQ.  The results are (this takes some time):

		3:  37.10
		2:  { 9.72 19.24 23.54 }
		1:  C = 907.35*POFX(COS(D),0) + 260.78*POFX(COS(D),2) + 201.73*POFX(COS(D),4)

1


10


