Matrix Algebra

classic Classic list List threaded Threaded
4 messages Options
Reply | Threaded
Open this post in threaded view
|

Matrix Algebra

Jignesh Sutar
Hello,
I'd like to know if the below code can be generalised for any number of
cases? (I tried LOOP but with little success).

Help would be much appreciated. Many thanks in advance.

Cheers
Jigs



/***********************************/.
*Sample Code*.
/***********************************/.
DATASET CLOSE ALL.
NEW FILE.
OUTPUT CLOSE ALL.

*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.
/***********************************/.
/*End of Sample Code*.
/***********************************/.

=====================
To manage your subscription to SPSSX-L, send a message to
[hidden email] (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
Reply | Threaded
Open this post in threaded view
|

Re: Matrix Algebra

Marta Garcia-Granero
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
[hidden email] (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
Reply | Threaded
Open this post in threaded view
|

Re: Matrix Algebra

Marta Garcia-Granero
In reply to this post by Jignesh Sutar
J Sutar wrote:
> Hello,
> I'd like to know if the below code can be generalised for any number of
> cases? (I tried LOOP but with little success).
>
Hi

Disregards my previous message (SPSS&flu don't get along very well). I
misunderstood what th code was doing (hence my comment "I have added two
more variables because your code couldn't work", it worked perfectly
OK). This is the final AND fully working version of the extended code:

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.

* Trying 3 variables (it mimics your original code)*.
MATRIX.
GET x /VARIABLES = x1 tO x3 /FILE=* /NAMES=xvarnam.
COMPUTE nx=NCOL(x).
COMPUTE ndata=NROW(x).
COMPUTE Final=MAKE(nx,nx,0).
LOOP i=1 TO ndata.
- 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.

* Trying with 5 variables *.
MATRIX.
GET x /VARIABLES = x1 tO x3 /FILE=* /NAMES=xvarnam.
COMPUTE nx=NCOL(x).
COMPUTE ndata=NROW(x).
COMPUTE Final=MAKE(nx,nx,0).
LOOP i=1 TO ndata.
- 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.

Marta

=====================
To manage your subscription to SPSSX-L, send a message to
[hidden email] (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
Reply | Threaded
Open this post in threaded view
|

Re: Matrix Algebra

Jignesh Sutar
Thanks Marta, that works fine.
Just a follow up question regarding printing formats under MATRIX command.

Is it possible to print output in comma format, I tried /FORMAT="COMMA8.0"
but it still outputted as F8.0 (without commas). Is it not possible to
specify comma under the FORMAT subcommand?

Many thanks
Jigs


2008/12/27 Marta García-Granero <[hidden email]>

> J Sutar wrote:
>
>> Hello,
>> I'd like to know if the below code can be generalised for any number of
>> cases? (I tried LOOP but with little success).
>>
>>  Hi
>
> Disregards my previous message (SPSS&flu don't get along very well). I
> misunderstood what th code was doing (hence my comment "I have added two
> more variables because your code couldn't work", it worked perfectly
> OK). This is the final AND fully working version of the extended code:
>
> 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.
>
> * Trying 3 variables (it mimics your original code)*.
> MATRIX.
> GET x /VARIABLES = x1 tO x3 /FILE=* /NAMES=xvarnam.
> COMPUTE nx=NCOL(x).
> COMPUTE ndata=NROW(x).
> COMPUTE Final=MAKE(nx,nx,0).
> LOOP i=1 TO ndata.
> - 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.
>
> * Trying with 5 variables *.
> MATRIX.
> GET x /VARIABLES = x1 tO x3 /FILE=* /NAMES=xvarnam.
> COMPUTE nx=NCOL(x).
> COMPUTE ndata=NROW(x).
> COMPUTE Final=MAKE(nx,nx,0).
> LOOP i=1 TO ndata.
> - 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.
>
> Marta
>
>
> =====================
> To manage your subscription to SPSSX-L, send a message to
> [hidden email] (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
>

====================To manage your subscription to SPSSX-L, send a message to
[hidden email] (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