Skip to main content

SPSS Regression using Matrices

The SPSS syntax below does several linear regression calculations (model parameter values, sum of squares, F value, F test, R, Rsquare, t tests, etc) using matrices ... just change the data file and initialization part as required and off you go ......



/* ####### DATA FILE ################################################################################### */

FILE HANDLE data /NAME='c:\data\ratty2.sav'.


/* ####### INITIALIZATION AND DATA MATRIX ############################################################## */

MATRIX.

COMPUTE N=21. /* no of data rows */
COMPUTE IND_VARS=3. /* no of independet variables */

GET Y /FILE=data /VARIABLES =size. /* puts y-variable into column matrix called Y */
GET X1 /FILE=data /VARIABLES =t,z2,z3 /* put independent variables into a matrix called X1 */

/* ########## MATRIX CALCULATIONS #################################################################### */


COMPUTE j=make(N,1,1). /* Create column vector of 1's */
COMPUTE X={j,X1}. /* joins column of 1's in front of X1 columns */
COMPUTE XT=TRANSPOS(X). /* XT is the transpose of X */
COMPUTE XDX=XT*X. /* X'X */
COMPUTE IXDX=INV(XDX). /* (X'X)^ -1 */
COMPUTE BETA=IXDX*XT*y. /* BETA= ( (X'X) ^ -1)X'y */
COMPUTE BETAT=TRANSPOS(BETA).
COMPUTE YT = TRANSPOS(Y).
COMPUTE JT=TRANSPOS(J).
COMPUTE MODELSS=BETAT*XT*Y-(JT*Y)**2/N.
COMPUTE ERRSS=YT*Y-BETAT*XT*Y.
COMPUTE TOTALSS = YT*Y-(JT*Y)**2/N.
COMPUTE MMS=MODELSS/IND_VARS.
COMPUTE ERRMS=ERRSS/(N-IND_VARS-1).
COMPUTE F=MMS/ERRMS.
COMPUTE FP=1-FCDF(F,IND_VARS,N-IND_VARS-1).
COMPUTE VAR=IXDX*ERRSS.
COMPUTE VAR=VAR/(N-IND_VARS-1).
COMPUTE SE=SQRT(DIAG(VAR)).
COMPUTE TVAL=BETA/SE.
COMPUTE TVALF=TVAL&**2.
COMPUTE PVAL=1-FCDF(TVALF,1,N-IND_VARS-1).
COMPUTE TAB = {BETA,SE,TVAL,PVAL}.

COMPUTE RSQ= MODELSS/TOTALSS.
COMPUTE ADJRSQ=1-ERRMS/TOTALSS*(N-1).

/* ########### OUTPUT ######################################################################################## */

PRINT BETA.
PRINT X.
PRINT Y.
PRINT XT.
PRINT XDX.
PRINT IXDX.
PRINT MODELSS.
PRINT ERRSS.
PRINT TOTALSS.
PRINT MMS.
PRINT ERRMS.
PRINT F.
PRINT FP.
PRINT VAR.
PRINT SE.
PRINT TAB.
PRINT RSQ.
PRINT ADJRSQ.

END MATRIX.

Comments

Popular posts from this blog

Interfacing C# .Net and R - Integrating the best of both worlds

In specific software areas like in quantitative finance or else in other mathematical domains, data centric programming typically requires a good balance between three requirements - (1) a solid platform with rich mathematical/statistical functionality (2) having an easy to use, contemporary, programming environment which permits easy and flexible front end code development and (3) an easy to use interface between the two environments.
In this artcile I am going to explain how such a balance can be attained by using two of the best products in their specific worlds - using the rich R library as the mathematical/statistical component but then interfacing with C# for the front end application design. As an interfacing option I banked on using R (D)COM which provides an easy to use interfacing method which keeps you away from spending hours identifying interfacing problems.
The software required for this tutorial is the following: 1. R software (download from here) 2. R (D)COM Interface (dow…

Interfacing C# .Net and R - Integrating the best of both worlds (Part 3) - Trader Desk Example

In this third and final part of the series (Part 1, Part 2), I am going to continue shaping the examples in the previous posts by quickly building a small application - a simple Trading Desk application. In the example I will use the same C# and R interface method together with a specific Quantitative Financial Modelling R library called Quantmod.
Example Objective In the example the objective is to build a C# Web Form which acts as the main application controller that captures various user option parameters with the main functions being:
1. To automatically kick off an R routine to download data from Yahoo Finance. 2. To select specific dates of interest 3. To add or udpate different charts 4. To add different chart indicators
5. To calculate Period Return Statistics
Most of these functions are provided through the set of R functions exposed by the Quantmod R library.
Additions under the hood
Below I am showing some of the salient additions done to the code over and above the code in the pr…

Interfacing C# .Net and R - Integrating the best of both worlds (Part 2)

This post is a continuation from the previous post (Part I) focusing on interfacing C# with R using the R (D)COM. In this post I am going to enhance my previous exercise by creating a Facade .Net Class which facilitates access to specific functions in R.
Creating the R Facade Class
Creating a Facade Class (or a set of .Net classes) which acts as a .Net wrapper to R functions greatly facilitate the use of R functions and their integration within the .Net programming environment. Below I am showing an excerpt from the class RFacade that I have created in this example.
using System;

using System.Collections.Generic;
using System.Linq;
using System.Text;
using StatConnectorCommonLib;
using STATCONNECTORSRVLib;
using System.Runtime.InteropServices;


namespace R
{


class RFacade : IDisposable
{


     private StatConnector rconn;
     private bool disposed = false;


     public RFacade()
     {
          rconn = new STATCONNECTORSRVLib.StatConnector();
          rconn.Init("R");
     }


     public void D…