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