Matrix Difficulty

classic Classic list List threaded Threaded
1 message Options
Reply | Threaded
Open this post in threaded view
|

Matrix Difficulty

bdates
Listers...

 

I'm having trouble with a matrix solution, which is intended to create i
x 2 submatrices from an i x j matrix, where j>2 .  The difficulty is not
in creating the submatrices, but in calculating from them subsequent to
their creation.  I'm including the syntax complete with data for a 15 x
3 matrix.  The syntax has two solutions.  Solution 1 calculates for each
submatrix the msum and places it in a 1 x 3 vector consistent with the
column indicated by the counter.  Solution 2 attempts to create another
matrix from each submatrix, compute how many times the variables match,
and place it in the 1 x 3 vector.  Solution 1 works.  The values for the
stored vector, ri, are correct.  Solution 2 seems to get stuck on the
first solution, placing the same value in each column of the vector.
The correct values for Solution 2 are 0.231111, 0.215556, and 0.193333.
I apologize for the length of the syntax.  Any help would be
appreciated.  Thanks.

 

data list list /v1 v2 v3 .

begin data

1 1 1

2 1 2

2 2 2

2 1 1

2 1 2

4 4 3

4 6 4

5 5 5

4 4 4

6 6 6

4 4 3

2 1 6

2 2 3

1 3 1

1 2 2

end data .

preserve.

set printback=off mprint=off.

save outfile='r__tmp1.sav'.

 

define reg (vars=!charend('/')).

set mxloops=1000000000.

count ms__=!vars (missing).

select if ms__=0.

matrix.

get x /var=!vars.

 

/* SECTION 1: This section computes the scalars, vectors, and matrices
necessary for the analysis.                               */

 

/* Compute necessary scalars.
*/

 

compute n=nrow(x) .

compute vbls=ncol(x) .

compute val=mmax(x) .

compute count=0 .

 

/* Compute the vector to receive the r results.
*/

 

compute ri=make(1,(vbls*(vbls-1))/2,0) .

 

/* Compute the matrix to receive the n x 2 submatrices.
*/

 

compute rmatrix=make(n,2,0) .

 

/* SECTION 2: This section computes the overall r's.
*/

 

/* Solution 1.
*/

 

loop j=1 to ncol(x) .

  loop l=1 to ncol(x) .

    do if l>j .

    compute count=1 + count .

    loop i=1 to nrow(x) .

      loop k=1 to nrow(x) .

        compute rmatrix(i,1)=x(i,j) .

        compute rmatrix(k,2)=x(k,l) .

        compute testsum=msum(rmatrix) .

        compute ri(1,count)=testsum .

      end loop .

    end loop .

    end if .

  end loop .

end loop .

save ri/outfile='ri.sav' .

 

end matrix .

!enddefine.

restore.

reg vars = v1 to v3 .

 

/* Solution 2.
*/

 

loop j=1 to ncol(x) .

  loop l=1 to ncol(x) .

    do if l>j .

    compute count=1 + count .

    loop i=1 to nrow(x) .

      loop k=1 to nrow(x) .

        compute rmatrix(i,1)=x(i,j) .

        compute rmatrix(k,2)=x(k,l) .

 

          compute rmatch=make(n,val,0) .

          loop a=1 to nrow(rmatrix).

            loop b=1 to ncol(rmatrix).

                loop c=1 to val.

                  do if x(a,b)=c.

                  compute rmatch(a,c)=rmatch(a,c)+1.

                  end if.

              end loop.

           end loop.

        end loop.

      compute testsum=msum((csum(rmatch)/msum(rmatch))&**2) .

      compute ri(1,count)=testsum .

    end loop .

    end loop .

    end if .

  end loop .

end loop .

save ri/outfile='ri.sav' .

end matrix .

!enddefine.

restore.

reg vars = v1 to v3 .

 

 

 

 

 

Brian G. Dates

Director of Evaluation and Research

Southwest Counseling Solutions

1700 Waterman

Detroit, MI     48209

313-841-7442

[hidden email]

 

====================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