Lindo Systems

MODEL:
 ! Multiple linear regression;
 !The output is the set of regression coefficients, COEF,
 for the model:
    Y(i) = COEF0 + COEF(1)*X(i,1) + COEF(2)*X(i,2)+... + error(i)
;

! Keywords: regression, Least squares;

SETS:
  OBS : Y, ERROR;
  VARS: MU;
  DEPVAR( VARS);    ! The dependent variable;
  EXPVAR( VARS): B; ! The explanatory variables;
  OXV( OBS, VARS): DATATAB; ! The data table;
  OXE( OBS, EXPVAR): X;
ENDSETS

DATA:
! Certified Regression Statistics for Longley data:
                                             Standard Deviation
  Parameter         Estimate 	              of Estimate
   beta(0) 	  -3482258.63459582 	     890420.383607373
   beta(1) 	     15.0618722713733 	     84.9149257747669
   beta(2) 	  -0.358191792925910E-01 	     0.334910077722432E-01
   beta(3) 	  -2.02022980381683 	     0.488399681651699
   beta(4) 	  -1.03322686717359 	     0.214274163161675
   beta(5) 	  -0.511041056535807E-01 	     0.226073200069370
   beta(6) 	     1829.15146461355 	     455.478499142212

Residual
Standard Deviation   304.854073561965 		
R-Squared 	     0.995479004577296 		

Certified Analysis of Variance Table

Source of  Degrees of    Sums of              Mean
Variation    Freedom 	Squares               Squares 	       F Statistic
Regression      6      184172401.944494 	   30695400.3240823 	330.285339234588
Residual 	    9 	  836424.055505915 	92936.0061673238 	
;
! Names of the variables;
  VARS = EMPLD PRICE GNP__ JOBLS MILIT POPLN  YEAR_; 

! Dependent variable. Must be exactly 1;
  DEPVAR = EMPLD; 

! Explanatory variables. Should not include DEPVAR;
  EXPVAR = PRICE GNP__ JOBLS MILIT POPLN YEAR_; 

! The dataset;
  DATATAB = 
    60323   83  234289  2356  1590  107608  1947  
    61122  88.5 259426  2325  1456  108632  1948  
    60171  88.2 258054  3682  1616  109773  1949  
    61187  89.5 284599  3351  1650  110929  1950  
    63221  96.2 328975  2099  3099  112075  1951  
    63639  98.1 346999  1932  3594  113270  1952  
    64989  99   365385  1870  3547  115094  1953  
    63761 100   363112  3578  3350  116219  1954  
    66019 101.2 397469  2904  3048  117388  1955  
    67857 104.6 419180  2822  2857  118734  1956  
    68169 108.4 442769  2936  2798  120445  1957  
    66513 110.8 444546  4681  2637  121950  1958  
    68655 112.6 482704  3813  2552  123366  1959  
    69564 114.2 502601  3931  2514  125368  1960  
    69331 115.7 518173  4806  2572  127852  1961  
    70551 116.9 554894  4007  2827  130081  1962;  

ENDDATA

CALC:
    @SET( 'TERSEO', 2);

    !Set up data for the @REGRESS function;
    @FOR( OBS( I): 
       Y( I) = DATATAB( I, @INDEX( EMPLD));
       @FOR( OXE( I, J): X( I, J) = DATATAB( I, J))
    );

    ! Do the regression;
    B, B0, ERROR = @REGRESS( Y, X);

    ! Calculate means;
    NOBS = @SIZE( OBS); 
    @FOR( VARS( K):
       MU( K) = @SUM( OBS( I): DATATAB( I, K)) / NOBS;
    );

    NEXP = @SIZE( EXPVAR);
    SUMSQERR = @SUM( OBS( I): ERROR( I) ^ 2);
    YBAR = @SUM( OBS( I): Y( I)) / NOBS;
    SUMTTLSQERR = @SUM( OBS( I): ( Y( I) - YBAR) ^ 2);
    RSQUARED = 1 - SUMSQERR / SUMTTLSQERR;

    @WRITE( '   Sum Error Measures: ', @FORMAT( SUMSQERR, '23.8g'), @NEWLINE(1));
    @WRITE( '   Explained error/R-Square: ', @FORMAT( RSQUARED, '16.8g'), @NEWLINE(1));
    @WRITE( '   Adjusted Explained error: ',
       @FORMAT( 1 - ( 1 - RSQUARED) * ( NOBS - 1)/( NOBS - NEXP - 1), '18.8g'),@NEWLINE( 2));
    @WRITE( '   B( 0):     ', @FORMAT( B0, '16.8g'), @NEWLINE( 1));
    @FOR( EXPVAR( I):
       @WRITE( '   B( ', EXPVAR( I), '): ', @FORMAT( B( I), '16.8g'), @NEWLINE( 1));
    );



ENDCALC
END