Date: Fri, 26 Dec 2008 09:44:55 +0100
Reply-To: Marta García-Granero <mgarciagranero@gmail.com>
Sender: "SPSSX(r) Discussion" <SPSSX-L@LISTSERV.UGA.EDU>
From: Marta García-Granero <mgarciagranero@gmail.com>
Subject: Re: Matrix Algebra
In-Reply-To: <da1bdb9a0812240344s714bd8bdt5cbcdf1cb1b4f9ac@mail.gmail.com>
Content-Type: text/plain; charset=ISO-8859-1; format=flowed
J Sutar wrote:
> I'd like to know if the below code can be generalised for any number of
> cases? (I tried LOOP but with little success).
>
> *Create Sample Data*.
> DATA LIST FREE /id x1 x2 x3.
> BEGIN DATA
> 1 1 5 1
> 2 1 2 1
> 3 2 3 1
> 4 4 3 1
> 5 3 4 2
> END DATA.
>
> MATRIX.
> GET x /VARIABLES = x1 x2 x3 /FILE=* /NAMES=xvarnam.
> COMPUTE tmp1 =(T(X(1,:))*X(1,:)).
> COMPUTE tmp2 =(T(X(2,:))*X(2,:)).
> COMPUTE tmp3 =(T(X(3,:))*X(3,:)).
> COMPUTE tmp4 =(T(X(4,:))*X(4,:)).
> COMPUTE tmp5 =(T(X(5,:))*X(5,:)).
> PRINT tmp1.
> PRINT tmp2.
> PRINT tmp3.
> PRINT tmp4.
> PRINT tmp5.
> COMPUTE tmp6=tmp1 + tmp2 + tmp3 + tmp4 + tmp5.
> PRINT tmp6.
> COMPUTE tmp7=INV(tmp6).
> PRINT tmp7.
> END MATRIX.
>
>
First, I modified your sample dataset, the matrix code could not work
with only 3 variables8it works with five). Here is my modified code:
* Sample dataset (modified) *.
DATA LIST FREE /id x1 x2 x3 x4 x5.
BEGIN DATA
1 1 5 1 2 5
2 1 2 1 2 2
3 2 3 1 2 3
4 4 3 1 2 1
5 3 4 2 1 4
END DATA.
MATRIX.
GET x /VARIABLES = x1 TO x5 /FILE=* /NAMES=xvarnam.
COMPUTE nvars=NCOL(x).
COMPUTE ndata=NROW(x).
COMPUTE Final=MAKE(ndata,nvars,0).
LOOP i=1 TO nvars.
- COMPUTE tmp =(T(X(i,:))*X(i,:)).
- PRINT tmp.
- COMPUTE Final=Final+tmp.
END LOOP.
PRINT Final.
COMPUTE InvFinal=INV(Final).
PRINT InvFinal.
END MATRIX.
HTH,
Marta García-Granero
For miscellaneous statistical stuff, visit:
http://gjyp.nl/marta/
=====================
To manage your subscription to SPSSX-L, send a message to
LISTSERV@LISTSERV.UGA.EDU (not to SPSSX-L), with no body text except the
command. To leave the list, send the command
SIGNOFF SPSSX-L
For a list of commands to manage subscriptions, send the command
INFO REFCARD
|