within-subject correlation

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

within-subject correlation

Richard Ristow
At 03:16 PM 1/28/2014, [hidden email] wrote me, off-list.

As I've stated previously, though not recently, I prefer that
questions be put on-list; if I do respond to an off-list question,
I'll almost always post the response.
First, a question posted on-list can be addressed by any list member,
and others may have better ideas than I do;
Second, the list is for the whole community of SPSS users; questions
and responses on the list may be informative for others than the
original posters; and
Third, I prefer to have my own work on SPSS visible to the community.)

But, anyway, the question was,

>I am Antonio Aquino , a postgraduate studnet at cardiff University.
>I need to calculate within-subjects correlation. How I can calculate
>these correlation? You have written that one first step is to
>transform my file into a long file and then split on the basis of
>the subjects. and then calculate a correlation.

That is indeed one way to do it, possibly the most direct. Below is
an example, using VARSTOCASES to transform the file to long form,
CORRELATIONS to calculate the correlations, OMS to capture the
results, and a little manipulation to attach the results to the original data.
|-----------------------------|---------------------------|
|Output Created               |07-FEB-2014 16:27:36       |
|-----------------------------|---------------------------|
  [TestData]

Subject   X1   X2   X3   X4   X5   Y1   Y2   Y3   Y4   Y5

   1001    37   85   37   33   56   43   54   15   18   53
   1002    43   20   63   76   31   32   15   47   38   16
   1003    65   17   39   41   34   48   10   62   58   39
   1004    61   55   29   43   56   93   58   68   74   43
   1005    75   62   29   40    1   78   99   13   52   37

Number of cases read:  5    Number of cases listed:  5

DATASET COPY     LongForm.
DATASET ACTIVATE LongForm WINDOW=FRONT.
VARSTOCASES
   /MAKE X FROM X1 X2 X3 X4 X5
   /MAKE Y FROM Y1 Y2 Y3 Y4 Y5
   /INDEX = Obs#(5)
   /KEEP  =  Subject
   /NULL  = KEEP.

Variables to Cases
|-----------------------------|---------------------------|
|Output Created               |07-FEB-2014 16:27:36       |
|-----------------------------|---------------------------|
  [LongForm]

Generated Variables
|----|------|
|Name|Label |
|----|------|
|Obs#|<none>|
|----|------|
|X   |<none>|
|Y   |<none>|
|----|------|

Processing Statistics
|-------------|--|
|Variables In |11|
|Variables Out|4 |
|-------------|--|

LIST.

List
|-----------------------------|---------------------------|
|Output Created               |07-FEB-2014 16:27:36       |
|-----------------------------|---------------------------|
  [LongForm]
Subject Obs#    X    Y

   1001     1   37   43
   1001     2   85   54
   1001     3   37   15
   1001     4   33   18
   1001     5   56   53
   1002     1   43   32
   1002     2   20   15
   1002     3   63   47
   1002     4   76   38
   1002     5   31   16
   1003     1   65   48
   1003     2   17   10
   1003     3   39   62
   1003     4   41   58
   1003     5   34   39
   1004     1   61   93
   1004     2   55   58
   1004     3   29   68
   1004     4   43   74
   1004     5   56   43
   1005     1   75   78
   1005     2   62   99
   1005     3   29   13
   1005     4   40   52
   1005     5    1   37

Number of cases read:  25    Number of cases listed:  25


SPLIT FILE BY Subject.

DATASET DECLARE  Correlations.

OMS /SELECT ALL EXCEPT=[LOGS TEXTS WARNINGS]
     /DESTINATION VIEWER=NO.
OMS /SELECT TABLES
     /IF COMMANDS = ['CORRELATIONS']
         SUBTYPES = ['Correlations']
     /DESTINATION FORMAT=SAV
                  OUTFILE=Correlations.
CORRELATIONS
   /VARIABLES=X Y
   /MISSING=PAIRWISE.

  [LongForm]

OMSEND.
DATASET ACTIVATE Correlations WINDOW=FRONT.
LIST.

List
|-----------------------------|---------------------------|
|Output Created               |07-FEB-2014 16:27:37       |
|-----------------------------|---------------------------|
  [Correlations]
Command_     Subtype_     Label_       Var1 Var2 Var3                   X    Y

Correlations Correlations Correlations 1001 X    Pearson Correlation 1.00 .745
Correlations Correlations Correlations 1001 X    Sig. (2-tailed)     .    .149
Correlations Correlations Correlations 1001 X    N                   5.00 5.00
Correlations Correlations Correlations 1001 Y    Pearson Correlation .745 1.00
Correlations Correlations Correlations 1001 Y    Sig. (2-tailed)     .149 .
Correlations Correlations Correlations 1001 Y    N                   5.00 5.00
Correlations Correlations Correlations 1002 X    Pearson Correlation 1.00 .881
Correlations Correlations Correlations 1002 X    Sig. (2-tailed)     .    .049
Correlations Correlations Correlations 1002 X    N                   5.00 5.00
Correlations Correlations Correlations 1002 Y    Pearson Correlation .881 1.00
Correlations Correlations Correlations 1002 Y    Sig. (2-tailed)     .049 .
Correlations Correlations Correlations 1002 Y    N                   5.00 5.00
Correlations Correlations Correlations 1003 X    Pearson Correlation 1.00 .634
Correlations Correlations Correlations 1003 X    Sig. (2-tailed)     .    .250
Correlations Correlations Correlations 1003 X    N                   5.00 5.00
Correlations Correlations Correlations 1003 Y    Pearson Correlation .634 1.00
Correlations Correlations Correlations 1003 Y    Sig. (2-tailed)     .250 .
Correlations Correlations Correlations 1003 Y    N                   5.00 5.00
Correlations Correlations Correlations 1004 X    Pearson Correlation 1.00 .029
Correlations Correlations Correlations 1004 X    Sig. (2-tailed)     .    .963
Correlations Correlations Correlations 1004 X    N                   5.00 5.00
Correlations Correlations Correlations 1004 Y    Pearson Correlation .029 1.00
Correlations Correlations Correlations 1004 Y    Sig. (2-tailed)     .963 .
Correlations Correlations Correlations 1004 Y    N                   5.00 5.00
Correlations Correlations Correlations 1005 X    Pearson Correlation 1.00 .751
Correlations Correlations Correlations 1005 X    Sig. (2-tailed)     .    .143
Correlations Correlations Correlations 1005 X    N                   5.00 5.00
Correlations Correlations Correlations 1005 Y    Pearson Correlation .751 1.00
Correlations Correlations Correlations 1005 Y    Sig. (2-tailed)     .143 .
Correlations Correlations Correlations 1005 Y    N                   5.00 5.00

Number of cases read:  30    Number of cases listed:  30


SELECT IF  Var2 EQ 'X'
        AND Var3 EQ 'Pearson Correlation'.

NUMERIC  Subject (F4).
COMPUTE  Subject=NUMBER(Var1,F4).

*  ...  The following EXECUTE is logically unnecessary,     ... .
*  ...  but without it the preceding SELECT IF and COMPUTE, ... .
*  ...  with the following MATCH FILES, give strange and    ... .
*  ...  incorrect, results.                                 ... .
*  ...  (Running SPSS 14 for Windows; it may be different   ... .
*  ...  in later releases.)                                 ... .

EXECUTE.

MATCH FILES
    /FILE=TestData
    /FILE=Correlations
       /RENAME=(Y = Correlation)
    /BY Subject
    /DROP=Command_ Subtype_ Label_
          Var1      Var2    Var3     X.

LIST.

List
|-----------------------------|---------------------------|
|Output Created               |07-FEB-2014 16:27:37       |
|-----------------------------|---------------------------|
Subject   X1   X2   X3   X4   X5   Y1   Y2   Y3   Y4   Y5 Correlation

   1001    37   85   37   33   56   43   54   15   18   53     .745
   1002    43   20   63   76   31   32   15   47   38   16     .881
   1003    65   17   39   41   34   48   10   62   58   39     .634
   1004    61   55   29   43   56   93   58   68   74   43     .029
   1005    75   62   29   40    1   78   99   13   52   37     .751

Number of cases read:  5    Number of cases listed:  5
=================================
APPENDIX: Test data, and all code
=================================
*  C:\Documents and Settings\Richard\My Documents         .
*    \Technical\spssx-l\Z-2014\                           .
*    2014-01-28 AquinoA - within-subject correlation.SPS  .

*  In response to off-list inquiry           .
*  Date:    Tue, 28 Jan 2014 12:16:05 -0800  .
*  From:    [hidden email]            .
*  To:      [hidden email]          .
*  Subject: within-subject correlation       .

*  "I need to calculate within-subjects correlation. How I can    .
*  calculate these correlation? You have written that one first   .
*  step is to tarsnform my file in a long file and then split on  .
*  the basis of the subjects. ... Can you help me."               .

*  ................................................................. .
*  .................   Test data               ..................... .
SET RNG = MT       /* 'Mersenne twister' random number generator  */ .
SET MTINDEX = 0367.

INPUT PROGRAM.
.  NUMERIC Subject  (F5)
           /X1 TO X5 (F4)
           /Y1 TO Y5 (F4).
.  VECTOR X = X1 TO X5
          /Y = Y1 TO Y5.
.  LOOP Subject = 1001 TO 1005.
.     COMPUTE #rho  = RV.UNIFORM(0.5,0.9).
.     COMPUTE #Xbar = RV.UNIFORM(40,60).
.     COMPUTE #Ybar = RV.UNIFORM(40,60).
.     DO REPEAT   X = X1 TO X5
                  /Y = Y1 TO Y5.
.        COMPUTE  X = RV.NORMAL(0,1).
.        COMPUTE #Y = RV.NORMAL(0,1).
.        COMPUTE  Y = #rho*X
                     + Sqrt( 1 - #rho**2)*#Y.
.        COMPUTE  X = #Xbar + 20*X.
.        COMPUTE  Y = #YBar + 20*Y.
.        COMPUTE  X = RND(X).
.        COMPUTE  Y = RND(Y).
.     END REPEAT.
.     END CASE.
.  END LOOP.
END FILE.
END INPUT PROGRAM.
DATASET NAME     TestData WINDOW=FRONT.
LIST.

DATASET COPY     LongForm.
DATASET ACTIVATE LongForm WINDOW=FRONT.

VARSTOCASES
   /MAKE X FROM X1 X2 X3 X4 X5
   /MAKE Y FROM Y1 Y2 Y3 Y4 Y5
   /INDEX = Obs#(5)
   /KEEP  =  Subject
   /NULL  = KEEP.

LIST.

SPLIT FILE BY Subject.

DATASET DECLARE  Correlations.

OMS /SELECT ALL EXCEPT=[LOGS TEXTS WARNINGS]
     /DESTINATION VIEWER=NO.

OMS /SELECT TABLES
     /IF COMMANDS = ['CORRELATIONS']
         SUBTYPES = ['Correlations']
     /DESTINATION FORMAT=SAV
                  OUTFILE=Correlations.

CORRELATIONS
   /VARIABLES=X Y
   /MISSING=PAIRWISE.

OMSEND.

DATASET ACTIVATE Correlations WINDOW=FRONT.
LIST.

SELECT IF  Var2 EQ 'X'
        AND Var3 EQ 'Pearson Correlation'.

NUMERIC  Subject (F4).
COMPUTE  Subject=NUMBER(Var1,F4).

*  ...  The following EXECUTE is logically unnecessary,     ... .
*  ...  but without it the preceding SELECT IF and COMPUTE, ... .
*  ...  with the following MATCH FILES, give strange and    ... .
*  ...  incorrect, results.                                 ... .
*  ...  (Running SPSS 14 for Windows; it may be different   ... .
*  ...  in later releases.)                                 ... .

EXECUTE.

MATCH FILES
    /FILE=TestData
    /FILE=Correlations
       /RENAME=(Y = Correlation)
    /BY Subject
    /DROP=Command_ Subtype_ Label_
          Var1      Var2    Var3     X.

LIST.

=====================
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: within-subject correlation

David Marso
Administrator
Or roll your own with MATRIX ;-)
Missing data will require adjustment of code.

DATA LIST LIST
 /Subject   X1   X2   X3   X4   X5   Y1   Y2   Y3   Y4   Y5 .
BEGIN DATA
   1001    37   85   37   33   56   43   54   15   18   53
   1002    43   20   63   76   31   32   15   47   38   16
   1003    65   17   39   41   34   48   10   62   58   39
   1004    61   55   29   43   56   93   58   68   74   43
   1005    75   62   29   40    1   78   99   13   52   37
END DATA.
DATASET DECLARE out .
MATRIX .
GET Subject / FILE * / VARIABLES Subject.
GET X / FILE * / VARIABLES X1 TO X5.
GET Y / FILE * / VARIABLES Y1 TO Y5.
COMPUTE R=MAKE(NROW(Subject),1,0).
LOOP #=1 TO NROW(Subject).
COMPUTE CP=SSCP(T({X(#,:)-MSUM(X(#,:))/NCOL(X);
                   Y(#,:)-MSUM(Y(#,:))/NCOL(Y)})).
COMPUTE R(#)=CP(1,2)/SQRT(CP(1,1)*CP(2,2)).
END LOOP.
SAVE {Subject , X, Y, R} / OUTFILE out
  / VARIABLES Subject X1 TO X5 Y1 TO Y5 CorrXY.
PRINT R.
END MATRIX.

Richard Ristow wrote
At 03:16 PM 1/28/2014, [hidden email] wrote me, off-list.

As I've stated previously, though not recently, I prefer that
questions be put on-list; if I do respond to an off-list question,
I'll almost always post the response.
First, a question posted on-list can be addressed by any list member,
and others may have better ideas than I do;
Second, the list is for the whole community of SPSS users; questions
and responses on the list may be informative for others than the
original posters; and
Third, I prefer to have my own work on SPSS visible to the community.)

But, anyway, the question was,

>I am Antonio Aquino , a postgraduate studnet at cardiff University.
>I need to calculate within-subjects correlation. How I can calculate
>these correlation? You have written that one first step is to
>transform my file into a long file and then split on the basis of
>the subjects. and then calculate a correlation.

That is indeed one way to do it, possibly the most direct. Below is
an example, using VARSTOCASES to transform the file to long form,
CORRELATIONS to calculate the correlations, OMS to capture the
results, and a little manipulation to attach the results to the original data.
|-----------------------------|---------------------------|
|Output Created               |07-FEB-2014 16:27:36       |
|-----------------------------|---------------------------|
  [TestData]

Subject   X1   X2   X3   X4   X5   Y1   Y2   Y3   Y4   Y5

   1001    37   85   37   33   56   43   54   15   18   53
   1002    43   20   63   76   31   32   15   47   38   16
   1003    65   17   39   41   34   48   10   62   58   39
   1004    61   55   29   43   56   93   58   68   74   43
   1005    75   62   29   40    1   78   99   13   52   37

Number of cases read:  5    Number of cases listed:  5

DATASET COPY     LongForm.
DATASET ACTIVATE LongForm WINDOW=FRONT.
VARSTOCASES
   /MAKE X FROM X1 X2 X3 X4 X5
   /MAKE Y FROM Y1 Y2 Y3 Y4 Y5
   /INDEX = Obs#(5)
   /KEEP  =  Subject
   /NULL  = KEEP.

Variables to Cases
|-----------------------------|---------------------------|
|Output Created               |07-FEB-2014 16:27:36       |
|-----------------------------|---------------------------|
  [LongForm]

Generated Variables
|----|------|
|Name|Label |
|----|------|
|Obs#|<none>|
|----|------|
|X   |<none>|
|Y   |<none>|
|----|------|

Processing Statistics
|-------------|--|
|Variables In |11|
|Variables Out|4 |
|-------------|--|

LIST.

List
|-----------------------------|---------------------------|
|Output Created               |07-FEB-2014 16:27:36       |
|-----------------------------|---------------------------|
  [LongForm]
Subject Obs#    X    Y

   1001     1   37   43
   1001     2   85   54
   1001     3   37   15
   1001     4   33   18
   1001     5   56   53
   1002     1   43   32
   1002     2   20   15
   1002     3   63   47
   1002     4   76   38
   1002     5   31   16
   1003     1   65   48
   1003     2   17   10
   1003     3   39   62
   1003     4   41   58
   1003     5   34   39
   1004     1   61   93
   1004     2   55   58
   1004     3   29   68
   1004     4   43   74
   1004     5   56   43
   1005     1   75   78
   1005     2   62   99
   1005     3   29   13
   1005     4   40   52
   1005     5    1   37

Number of cases read:  25    Number of cases listed:  25


SPLIT FILE BY Subject.

DATASET DECLARE  Correlations.

OMS /SELECT ALL EXCEPT=[LOGS TEXTS WARNINGS]
     /DESTINATION VIEWER=NO.
OMS /SELECT TABLES
     /IF COMMANDS = ['CORRELATIONS']
         SUBTYPES = ['Correlations']
     /DESTINATION FORMAT=SAV
                  OUTFILE=Correlations.
CORRELATIONS
   /VARIABLES=X Y
   /MISSING=PAIRWISE.

  [LongForm]

OMSEND.
DATASET ACTIVATE Correlations WINDOW=FRONT.
LIST.

List
|-----------------------------|---------------------------|
|Output Created               |07-FEB-2014 16:27:37       |
|-----------------------------|---------------------------|
  [Correlations]
Command_     Subtype_     Label_       Var1 Var2 Var3                   X    Y

Correlations Correlations Correlations 1001 X    Pearson Correlation 1.00 .745
Correlations Correlations Correlations 1001 X    Sig. (2-tailed)     .    .149
Correlations Correlations Correlations 1001 X    N                   5.00 5.00
Correlations Correlations Correlations 1001 Y    Pearson Correlation .745 1.00
Correlations Correlations Correlations 1001 Y    Sig. (2-tailed)     .149 .
Correlations Correlations Correlations 1001 Y    N                   5.00 5.00
Correlations Correlations Correlations 1002 X    Pearson Correlation 1.00 .881
Correlations Correlations Correlations 1002 X    Sig. (2-tailed)     .    .049
Correlations Correlations Correlations 1002 X    N                   5.00 5.00
Correlations Correlations Correlations 1002 Y    Pearson Correlation .881 1.00
Correlations Correlations Correlations 1002 Y    Sig. (2-tailed)     .049 .
Correlations Correlations Correlations 1002 Y    N                   5.00 5.00
Correlations Correlations Correlations 1003 X    Pearson Correlation 1.00 .634
Correlations Correlations Correlations 1003 X    Sig. (2-tailed)     .    .250
Correlations Correlations Correlations 1003 X    N                   5.00 5.00
Correlations Correlations Correlations 1003 Y    Pearson Correlation .634 1.00
Correlations Correlations Correlations 1003 Y    Sig. (2-tailed)     .250 .
Correlations Correlations Correlations 1003 Y    N                   5.00 5.00
Correlations Correlations Correlations 1004 X    Pearson Correlation 1.00 .029
Correlations Correlations Correlations 1004 X    Sig. (2-tailed)     .    .963
Correlations Correlations Correlations 1004 X    N                   5.00 5.00
Correlations Correlations Correlations 1004 Y    Pearson Correlation .029 1.00
Correlations Correlations Correlations 1004 Y    Sig. (2-tailed)     .963 .
Correlations Correlations Correlations 1004 Y    N                   5.00 5.00
Correlations Correlations Correlations 1005 X    Pearson Correlation 1.00 .751
Correlations Correlations Correlations 1005 X    Sig. (2-tailed)     .    .143
Correlations Correlations Correlations 1005 X    N                   5.00 5.00
Correlations Correlations Correlations 1005 Y    Pearson Correlation .751 1.00
Correlations Correlations Correlations 1005 Y    Sig. (2-tailed)     .143 .
Correlations Correlations Correlations 1005 Y    N                   5.00 5.00

Number of cases read:  30    Number of cases listed:  30


SELECT IF  Var2 EQ 'X'
        AND Var3 EQ 'Pearson Correlation'.

NUMERIC  Subject (F4).
COMPUTE  Subject=NUMBER(Var1,F4).

*  ...  The following EXECUTE is logically unnecessary,     ... .
*  ...  but without it the preceding SELECT IF and COMPUTE, ... .
*  ...  with the following MATCH FILES, give strange and    ... .
*  ...  incorrect, results.                                 ... .
*  ...  (Running SPSS 14 for Windows; it may be different   ... .
*  ...  in later releases.)                                 ... .

EXECUTE.

MATCH FILES
    /FILE=TestData
    /FILE=Correlations
       /RENAME=(Y = Correlation)
    /BY Subject
    /DROP=Command_ Subtype_ Label_
          Var1      Var2    Var3     X.

LIST.

List
|-----------------------------|---------------------------|
|Output Created               |07-FEB-2014 16:27:37       |
|-----------------------------|---------------------------|
Subject   X1   X2   X3   X4   X5   Y1   Y2   Y3   Y4   Y5 Correlation

   1001    37   85   37   33   56   43   54   15   18   53     .745
   1002    43   20   63   76   31   32   15   47   38   16     .881
   1003    65   17   39   41   34   48   10   62   58   39     .634
   1004    61   55   29   43   56   93   58   68   74   43     .029
   1005    75   62   29   40    1   78   99   13   52   37     .751

Number of cases read:  5    Number of cases listed:  5
=================================
APPENDIX: Test data, and all code
=================================
*  C:\Documents and Settings\Richard\My Documents         .
*    \Technical\spssx-l\Z-2014\                           .
*    2014-01-28 AquinoA - within-subject correlation.SPS  .

*  In response to off-list inquiry           .
*  Date:    Tue, 28 Jan 2014 12:16:05 -0800  .
*  From:    [hidden email]            .
*  To:      [hidden email]          .
*  Subject: within-subject correlation       .

*  "I need to calculate within-subjects correlation. How I can    .
*  calculate these correlation? You have written that one first   .
*  step is to tarsnform my file in a long file and then split on  .
*  the basis of the subjects. ... Can you help me."               .

*  ................................................................. .
*  .................   Test data               ..................... .
SET RNG = MT       /* 'Mersenne twister' random number generator  */ .
SET MTINDEX = 0367.

INPUT PROGRAM.
.  NUMERIC Subject  (F5)
           /X1 TO X5 (F4)
           /Y1 TO Y5 (F4).
.  VECTOR X = X1 TO X5
          /Y = Y1 TO Y5.
.  LOOP Subject = 1001 TO 1005.
.     COMPUTE #rho  = RV.UNIFORM(0.5,0.9).
.     COMPUTE #Xbar = RV.UNIFORM(40,60).
.     COMPUTE #Ybar = RV.UNIFORM(40,60).
.     DO REPEAT   X = X1 TO X5
                  /Y = Y1 TO Y5.
.        COMPUTE  X = RV.NORMAL(0,1).
.        COMPUTE #Y = RV.NORMAL(0,1).
.        COMPUTE  Y = #rho*X
                     + Sqrt( 1 - #rho**2)*#Y.
.        COMPUTE  X = #Xbar + 20*X.
.        COMPUTE  Y = #YBar + 20*Y.
.        COMPUTE  X = RND(X).
.        COMPUTE  Y = RND(Y).
.     END REPEAT.
.     END CASE.
.  END LOOP.
END FILE.
END INPUT PROGRAM.
DATASET NAME     TestData WINDOW=FRONT.
LIST.

DATASET COPY     LongForm.
DATASET ACTIVATE LongForm WINDOW=FRONT.

VARSTOCASES
   /MAKE X FROM X1 X2 X3 X4 X5
   /MAKE Y FROM Y1 Y2 Y3 Y4 Y5
   /INDEX = Obs#(5)
   /KEEP  =  Subject
   /NULL  = KEEP.

LIST.

SPLIT FILE BY Subject.

DATASET DECLARE  Correlations.

OMS /SELECT ALL EXCEPT=[LOGS TEXTS WARNINGS]
     /DESTINATION VIEWER=NO.

OMS /SELECT TABLES
     /IF COMMANDS = ['CORRELATIONS']
         SUBTYPES = ['Correlations']
     /DESTINATION FORMAT=SAV
                  OUTFILE=Correlations.

CORRELATIONS
   /VARIABLES=X Y
   /MISSING=PAIRWISE.

OMSEND.

DATASET ACTIVATE Correlations WINDOW=FRONT.
LIST.

SELECT IF  Var2 EQ 'X'
        AND Var3 EQ 'Pearson Correlation'.

NUMERIC  Subject (F4).
COMPUTE  Subject=NUMBER(Var1,F4).

*  ...  The following EXECUTE is logically unnecessary,     ... .
*  ...  but without it the preceding SELECT IF and COMPUTE, ... .
*  ...  with the following MATCH FILES, give strange and    ... .
*  ...  incorrect, results.                                 ... .
*  ...  (Running SPSS 14 for Windows; it may be different   ... .
*  ...  in later releases.)                                 ... .

EXECUTE.

MATCH FILES
    /FILE=TestData
    /FILE=Correlations
       /RENAME=(Y = Correlation)
    /BY Subject
    /DROP=Command_ Subtype_ Label_
          Var1      Var2    Var3     X.

LIST.

=====================
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
Please reply to the list and not to my personal email.
Those desiring my consulting or training services please feel free to email me.
---
"Nolite dare sanctum canibus neque mittatis margaritas vestras ante porcos ne forte conculcent eas pedibus suis."
Cum es damnatorum possederunt porcos iens ut salire off sanguinum cliff in abyssum?"
Reply | Threaded
Open this post in threaded view
|

Re: within-subject correlation

Rich Ulrich
In reply to this post by Richard Ristow
I might be wrong - the OP is careless when he asks
for "within subject correlation" as a singular noun,
followed by "these correlation."

Does he was a set of correlations, one for each person?
Or, does he want the overall, "within-subject correlation?

For the latter, you still need to reformat so that each
subject has multiple cases. Then, perform Discriminant Function
while using Subjects as groups, with the two variables (or more)
as predictors.  The DF provides the within-group correlation if
you ask for it.

--
Rich Ulrich

----------------------------------------

> Date: Fri, 7 Feb 2014 16:37:58 -0500
> From: [hidden email]
> Subject: within-subject correlation
> To: [hidden email]
>
> At 03:16 PM 1/28/2014, [hidden email] wrote me, off-list.
>
> As I've stated previously, though not recently, I prefer that
> questions be put on-list; if I do respond to an off-list question,
> I'll almost always post the response.
> First, a question posted on-list can be addressed by any list member,
> and others may have better ideas than I do;
> Second, the list is for the whole community of SPSS users; questions
> and responses on the list may be informative for others than the
> original posters; and
> Third, I prefer to have my own work on SPSS visible to the community.)
>
> But, anyway, the question was,
>
>>I am Antonio Aquino , a postgraduate studnet at cardiff University.
>>I need to calculate within-subjects correlation. How I can calculate
>>these correlation? You have written that one first step is to
>>transform my file into a long file and then split on the basis of
>>the subjects. and then calculate a correlation.
>
> That is indeed one way to do it, possibly the most direct. Below is
> an example, using VARSTOCASES to transform the file to long form,
> CORRELATIONS to calculate the correlations, OMS to capture the
> results, and a little manipulation to attach the results to the original data.
> |-----------------------------|---------------------------|
> |Output Created |07-FEB-2014 16:27:36 |
> |-----------------------------|---------------------------|
> [TestData]
>
> Subject X1 X2 X3 X4 X5 Y1 Y2 Y3 Y4 Y5
>
> 1001 37 85 37 33 56 43 54 15 18 53
> 1002 43 20 63 76 31 32 15 47 38 16
> 1003 65 17 39 41 34 48 10 62 58 39
> 1004 61 55 29 43 56 93 58 68 74 43
> 1005 75 62 29 40 1 78 99 13 52 37
>
> Number of cases read: 5 Number of cases listed: 5
>
> DATASET COPY LongForm.
> DATASET ACTIVATE LongForm WINDOW=FRONT.
> VARSTOCASES
> /MAKE X FROM X1 X2 X3 X4 X5
> /MAKE Y FROM Y1 Y2 Y3 Y4 Y5
> /INDEX = Obs#(5)
> /KEEP = Subject
> /NULL = KEEP.
>
> Variables to Cases
> |-----------------------------|---------------------------|
> |Output Created |07-FEB-2014 16:27:36 |
> |-----------------------------|---------------------------|
> [LongForm]
>
> Generated Variables
> |----|------|
> |Name|Label |
> |----|------|
> |Obs#|<none>|
> |----|------|
> |X |<none>|
> |Y |<none>|
> |----|------|
>
> Processing Statistics
> |-------------|--|
> |Variables In |11|
> |Variables Out|4 |
> |-------------|--|
>
> LIST.
>
> List
> |-----------------------------|---------------------------|
> |Output Created |07-FEB-2014 16:27:36 |
> |-----------------------------|---------------------------|
> [LongForm]
> Subject Obs# X Y
>
> 1001 1 37 43
> 1001 2 85 54
> 1001 3 37 15
> 1001 4 33 18
> 1001 5 56 53
> 1002 1 43 32
> 1002 2 20 15
> 1002 3 63 47
> 1002 4 76 38
> 1002 5 31 16
> 1003 1 65 48
> 1003 2 17 10
> 1003 3 39 62
> 1003 4 41 58
> 1003 5 34 39
> 1004 1 61 93
> 1004 2 55 58
> 1004 3 29 68
> 1004 4 43 74
> 1004 5 56 43
> 1005 1 75 78
> 1005 2 62 99
> 1005 3 29 13
> 1005 4 40 52
> 1005 5 1 37
>
> Number of cases read: 25 Number of cases listed: 25
>
>
> SPLIT FILE BY Subject.
>
> DATASET DECLARE Correlations.
>
> OMS /SELECT ALL EXCEPT=[LOGS TEXTS WARNINGS]
> /DESTINATION VIEWER=NO.
> OMS /SELECT TABLES
> /IF COMMANDS = ['CORRELATIONS']
> SUBTYPES = ['Correlations']
> /DESTINATION FORMAT=SAV
> OUTFILE=Correlations.
> CORRELATIONS
> /VARIABLES=X Y
> /MISSING=PAIRWISE.
>
> [LongForm]
>
> OMSEND.
> DATASET ACTIVATE Correlations WINDOW=FRONT.
> LIST.
>
> List
> |-----------------------------|---------------------------|
> |Output Created |07-FEB-2014 16:27:37 |
> |-----------------------------|---------------------------|
> [Correlations]
> Command_ Subtype_ Label_ Var1 Var2 Var3 X Y
>
> Correlations Correlations Correlations 1001 X Pearson Correlation 1.00 .745
> Correlations Correlations Correlations 1001 X Sig. (2-tailed) . .149
> Correlations Correlations Correlations 1001 X N 5.00 5.00
> Correlations Correlations Correlations 1001 Y Pearson Correlation .745 1.00
> Correlations Correlations Correlations 1001 Y Sig. (2-tailed) .149 .
> Correlations Correlations Correlations 1001 Y N 5.00 5.00
> Correlations Correlations Correlations 1002 X Pearson Correlation 1.00 .881
> Correlations Correlations Correlations 1002 X Sig. (2-tailed) . .049
> Correlations Correlations Correlations 1002 X N 5.00 5.00
> Correlations Correlations Correlations 1002 Y Pearson Correlation .881 1.00
> Correlations Correlations Correlations 1002 Y Sig. (2-tailed) .049 .
> Correlations Correlations Correlations 1002 Y N 5.00 5.00
> Correlations Correlations Correlations 1003 X Pearson Correlation 1.00 .634
> Correlations Correlations Correlations 1003 X Sig. (2-tailed) . .250
> Correlations Correlations Correlations 1003 X N 5.00 5.00
> Correlations Correlations Correlations 1003 Y Pearson Correlation .634 1.00
> Correlations Correlations Correlations 1003 Y Sig. (2-tailed) .250 .
> Correlations Correlations Correlations 1003 Y N 5.00 5.00
> Correlations Correlations Correlations 1004 X Pearson Correlation 1.00 .029
> Correlations Correlations Correlations 1004 X Sig. (2-tailed) . .963
> Correlations Correlations Correlations 1004 X N 5.00 5.00
> Correlations Correlations Correlations 1004 Y Pearson Correlation .029 1.00
> Correlations Correlations Correlations 1004 Y Sig. (2-tailed) .963 .
> Correlations Correlations Correlations 1004 Y N 5.00 5.00
> Correlations Correlations Correlations 1005 X Pearson Correlation 1.00 .751
> Correlations Correlations Correlations 1005 X Sig. (2-tailed) . .143
> Correlations Correlations Correlations 1005 X N 5.00 5.00
> Correlations Correlations Correlations 1005 Y Pearson Correlation .751 1.00
> Correlations Correlations Correlations 1005 Y Sig. (2-tailed) .143 .
> Correlations Correlations Correlations 1005 Y N 5.00 5.00
>
> Number of cases read: 30 Number of cases listed: 30
>
>
> SELECT IF Var2 EQ 'X'
> AND Var3 EQ 'Pearson Correlation'.
>
> NUMERIC Subject (F4).
> COMPUTE Subject=NUMBER(Var1,F4).
>
> * ... The following EXECUTE is logically unnecessary, ... .
> * ... but without it the preceding SELECT IF and COMPUTE, ... .
> * ... with the following MATCH FILES, give strange and ... .
> * ... incorrect, results. ... .
> * ... (Running SPSS 14 for Windows; it may be different ... .
> * ... in later releases.) ... .
>
> EXECUTE.
>
> MATCH FILES
> /FILE=TestData
> /FILE=Correlations
> /RENAME=(Y = Correlation)
> /BY Subject
> /DROP=Command_ Subtype_ Label_
> Var1 Var2 Var3 X.
>
> LIST.
>
> List
> |-----------------------------|---------------------------|
> |Output Created |07-FEB-2014 16:27:37 |
> |-----------------------------|---------------------------|
> Subject X1 X2 X3 X4 X5 Y1 Y2 Y3 Y4 Y5 Correlation
>
> 1001 37 85 37 33 56 43 54 15 18 53 .745
> 1002 43 20 63 76 31 32 15 47 38 16 .881
> 1003 65 17 39 41 34 48 10 62 58 39 .634
> 1004 61 55 29 43 56 93 58 68 74 43 .029
> 1005 75 62 29 40 1 78 99 13 52 37 .751
>
> Number of cases read: 5 Number of cases listed: 5
> =================================
> APPENDIX: Test data, and all code
> =================================
> * C:\Documents and Settings\Richard\My Documents .
> * \Technical\spssx-l\Z-2014\ .
> * 2014-01-28 AquinoA - within-subject correlation.SPS .
>
> * In response to off-list inquiry .
> * Date: Tue, 28 Jan 2014 12:16:05 -0800 .
> * From: [hidden email] .
> * To: [hidden email] .
> * Subject: within-subject correlation .
>
> * "I need to calculate within-subjects correlation. How I can .
> * calculate these correlation? You have written that one first .
> * step is to tarsnform my file in a long file and then split on .
> * the basis of the subjects. ... Can you help me." .
>
> * ................................................................. .
> * ................. Test data ..................... .
> SET RNG = MT /* 'Mersenne twister' random number generator */ .
> SET MTINDEX = 0367.
>
> INPUT PROGRAM.
> . NUMERIC Subject (F5)
> /X1 TO X5 (F4)
> /Y1 TO Y5 (F4).
> . VECTOR X = X1 TO X5
> /Y = Y1 TO Y5.
> . LOOP Subject = 1001 TO 1005.
> . COMPUTE #rho = RV.UNIFORM(0.5,0.9).
> . COMPUTE #Xbar = RV.UNIFORM(40,60).
> . COMPUTE #Ybar = RV.UNIFORM(40,60).
> . DO REPEAT X = X1 TO X5
> /Y = Y1 TO Y5.
> . COMPUTE X = RV.NORMAL(0,1).
> . COMPUTE #Y = RV.NORMAL(0,1).
> . COMPUTE Y = #rho*X
> + Sqrt( 1 - #rho**2)*#Y.
> . COMPUTE X = #Xbar + 20*X.
> . COMPUTE Y = #YBar + 20*Y.
> . COMPUTE X = RND(X).
> . COMPUTE Y = RND(Y).
> . END REPEAT.
> . END CASE.
> . END LOOP.
> END FILE.
> END INPUT PROGRAM.
> DATASET NAME TestData WINDOW=FRONT.
> LIST.
>
> DATASET COPY LongForm.
> DATASET ACTIVATE LongForm WINDOW=FRONT.
>
> VARSTOCASES
> /MAKE X FROM X1 X2 X3 X4 X5
> /MAKE Y FROM Y1 Y2 Y3 Y4 Y5
> /INDEX = Obs#(5)
> /KEEP = Subject
> /NULL = KEEP.
>
> LIST.
>
> SPLIT FILE BY Subject.
>
> DATASET DECLARE Correlations.
>
> OMS /SELECT ALL EXCEPT=[LOGS TEXTS WARNINGS]
> /DESTINATION VIEWER=NO.
>
> OMS /SELECT TABLES
> /IF COMMANDS = ['CORRELATIONS']
> SUBTYPES = ['Correlations']
> /DESTINATION FORMAT=SAV
> OUTFILE=Correlations.
>
> CORRELATIONS
> /VARIABLES=X Y
> /MISSING=PAIRWISE.
>
> OMSEND.
>
> DATASET ACTIVATE Correlations WINDOW=FRONT.
> LIST.
>
> SELECT IF Var2 EQ 'X'
> AND Var3 EQ 'Pearson Correlation'.
>
> NUMERIC Subject (F4).
> COMPUTE Subject=NUMBER(Var1,F4).
>
> * ... The following EXECUTE is logically unnecessary, ... .
> * ... but without it the preceding SELECT IF and COMPUTE, ... .
> * ... with the following MATCH FILES, give strange and ... .
> * ... incorrect, results. ... .
> * ... (Running SPSS 14 for Windows; it may be different ... .
> * ... in later releases.) ... .
>
> EXECUTE.
>
> MATCH FILES
> /FILE=TestData
> /FILE=Correlations
> /RENAME=(Y = Correlation)
> /BY Subject
> /DROP=Command_ Subtype_ Label_
> Var1 Var2 Var3 X.
>
> LIST.
>
> =====================
> 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
Reply | Threaded
Open this post in threaded view
|

Re: within-subject correlation

Ryan
In reply to this post by Richard Ristow
A linear mixed model can certainly be employed to estimate the within-subject correlation. Frankly, it isn't clear to me exactly what the OP desires. 

Ryan


On Fri, Feb 7, 2014 at 4:37 PM, Richard Ristow <[hidden email]> wrote:
At 03:16 PM 1/28/2014, [hidden email] wrote me, off-list.

As I've stated previously, though not recently, I prefer that
questions be put on-list; if I do respond to an off-list question,
I'll almost always post the response.
First, a question posted on-list can be addressed by any list member,
and others may have better ideas than I do;
Second, the list is for the whole community of SPSS users; questions
and responses on the list may be informative for others than the
original posters; and
Third, I prefer to have my own work on SPSS visible to the community.)

But, anyway, the question was,

I am Antonio Aquino , a postgraduate studnet at cardiff University.
I need to calculate within-subjects correlation. How I can calculate
these correlation? You have written that one first step is to
transform my file into a long file and then split on the basis of
the subjects. and then calculate a correlation.

That is indeed one way to do it, possibly the most direct. Below is
an example, using VARSTOCASES to transform the file to long form,
CORRELATIONS to calculate the correlations, OMS to capture the
results, and a little manipulation to attach the results to the original data.
|-----------------------------|---------------------------|
|Output Created               |07-FEB-2014 16:27:36       |
|-----------------------------|---------------------------|
 [TestData]

Subject   X1   X2   X3   X4   X5   Y1   Y2   Y3   Y4   Y5

  1001    37   85   37   33   56   43   54   15   18   53
  1002    43   20   63   76   31   32   15   47   38   16
  1003    65   17   39   41   34   48   10   62   58   39
  1004    61   55   29   43   56   93   58   68   74   43
  1005    75   62   29   40    1   78   99   13   52   37

Number of cases read:  5    Number of cases listed:  5

DATASET COPY     LongForm.
DATASET ACTIVATE LongForm WINDOW=FRONT.
VARSTOCASES
  /MAKE X FROM X1 X2 X3 X4 X5
  /MAKE Y FROM Y1 Y2 Y3 Y4 Y5
  /INDEX = Obs#(5)
  /KEEP  =  Subject
  /NULL  = KEEP.

Variables to Cases
|-----------------------------|---------------------------|
|Output Created               |07-FEB-2014 16:27:36       |
|-----------------------------|---------------------------|
 [LongForm]

Generated Variables
|----|------|
|Name|Label |
|----|------|
|Obs#|<none>|
|----|------|
|X   |<none>|
|Y   |<none>|
|----|------|

Processing Statistics
|-------------|--|
|Variables In |11|
|Variables Out|4 |
|-------------|--|

LIST.

List
|-----------------------------|---------------------------|
|Output Created               |07-FEB-2014 16:27:36       |
|-----------------------------|---------------------------|
 [LongForm]
Subject Obs#    X    Y

  1001     1   37   43
  1001     2   85   54
  1001     3   37   15
  1001     4   33   18
  1001     5   56   53
  1002     1   43   32
  1002     2   20   15
  1002     3   63   47
  1002     4   76   38
  1002     5   31   16
  1003     1   65   48
  1003     2   17   10
  1003     3   39   62
  1003     4   41   58
  1003     5   34   39
  1004     1   61   93
  1004     2   55   58
  1004     3   29   68
  1004     4   43   74
  1004     5   56   43
  1005     1   75   78
  1005     2   62   99
  1005     3   29   13
  1005     4   40   52
  1005     5    1   37

Number of cases read:  25    Number of cases listed:  25


SPLIT FILE BY Subject.

DATASET DECLARE  Correlations.

OMS /SELECT ALL EXCEPT=[LOGS TEXTS WARNINGS]
    /DESTINATION VIEWER=NO.
OMS /SELECT TABLES
    /IF COMMANDS = ['CORRELATIONS']
        SUBTYPES = ['Correlations']
    /DESTINATION FORMAT=SAV
                 OUTFILE=Correlations.
CORRELATIONS
  /VARIABLES=X Y
  /MISSING=PAIRWISE.

 [LongForm]

OMSEND.
DATASET ACTIVATE Correlations WINDOW=FRONT.
LIST.

List
|-----------------------------|---------------------------|
|Output Created               |07-FEB-2014 16:27:37       |
|-----------------------------|---------------------------|
 [Correlations]
Command_     Subtype_     Label_       Var1 Var2 Var3                   X    Y

Correlations Correlations Correlations 1001 X    Pearson Correlation 1.00 .745
Correlations Correlations Correlations 1001 X    Sig. (2-tailed)     .    .149
Correlations Correlations Correlations 1001 X    N                   5.00 5.00
Correlations Correlations Correlations 1001 Y    Pearson Correlation .745 1.00
Correlations Correlations Correlations 1001 Y    Sig. (2-tailed)     .149 .
Correlations Correlations Correlations 1001 Y    N                   5.00 5.00
Correlations Correlations Correlations 1002 X    Pearson Correlation 1.00 .881
Correlations Correlations Correlations 1002 X    Sig. (2-tailed)     .    .049
Correlations Correlations Correlations 1002 X    N                   5.00 5.00
Correlations Correlations Correlations 1002 Y    Pearson Correlation .881 1.00
Correlations Correlations Correlations 1002 Y    Sig. (2-tailed)     .049 .
Correlations Correlations Correlations 1002 Y    N                   5.00 5.00
Correlations Correlations Correlations 1003 X    Pearson Correlation 1.00 .634
Correlations Correlations Correlations 1003 X    Sig. (2-tailed)     .    .250
Correlations Correlations Correlations 1003 X    N                   5.00 5.00
Correlations Correlations Correlations 1003 Y    Pearson Correlation .634 1.00
Correlations Correlations Correlations 1003 Y    Sig. (2-tailed)     .250 .
Correlations Correlations Correlations 1003 Y    N                   5.00 5.00
Correlations Correlations Correlations 1004 X    Pearson Correlation 1.00 .029
Correlations Correlations Correlations 1004 X    Sig. (2-tailed)     .    .963
Correlations Correlations Correlations 1004 X    N                   5.00 5.00
Correlations Correlations Correlations 1004 Y    Pearson Correlation .029 1.00
Correlations Correlations Correlations 1004 Y    Sig. (2-tailed)     .963 .
Correlations Correlations Correlations 1004 Y    N                   5.00 5.00
Correlations Correlations Correlations 1005 X    Pearson Correlation 1.00 .751
Correlations Correlations Correlations 1005 X    Sig. (2-tailed)     .    .143
Correlations Correlations Correlations 1005 X    N                   5.00 5.00
Correlations Correlations Correlations 1005 Y    Pearson Correlation .751 1.00
Correlations Correlations Correlations 1005 Y    Sig. (2-tailed)     .143 .
Correlations Correlations Correlations 1005 Y    N                   5.00 5.00

Number of cases read:  30    Number of cases listed:  30


SELECT IF  Var2 EQ 'X'
       AND Var3 EQ 'Pearson Correlation'.

NUMERIC  Subject (F4).
COMPUTE  Subject=NUMBER(Var1,F4).

*  ...  The following EXECUTE is logically unnecessary,     ... .
*  ...  but without it the preceding SELECT IF and COMPUTE, ... .
*  ...  with the following MATCH FILES, give strange and    ... .
*  ...  incorrect, results.                                 ... .
*  ...  (Running SPSS 14 for Windows; it may be different   ... .
*  ...  in later releases.)                                 ... .

EXECUTE.

MATCH FILES
   /FILE=TestData
   /FILE=Correlations
      /RENAME=(Y = Correlation)
   /BY Subject
   /DROP=Command_ Subtype_ Label_
         Var1      Var2    Var3     X.

LIST.

List
|-----------------------------|---------------------------|
|Output Created               |07-FEB-2014 16:27:37       |
|-----------------------------|---------------------------|
Subject   X1   X2   X3   X4   X5   Y1   Y2   Y3   Y4   Y5 Correlation

  1001    37   85   37   33   56   43   54   15   18   53     .745
  1002    43   20   63   76   31   32   15   47   38   16     .881
  1003    65   17   39   41   34   48   10   62   58   39     .634
  1004    61   55   29   43   56   93   58   68   74   43     .029
  1005    75   62   29   40    1   78   99   13   52   37     .751

Number of cases read:  5    Number of cases listed:  5
=================================
APPENDIX: Test data, and all code
=================================
*  C:\Documents and Settings\Richard\My Documents         .
*    \Technical\spssx-l\Z-2014\                           .
*    2014-01-28 AquinoA - within-subject correlation.SPS  .

*  In response to off-list inquiry           .
*  Date:    Tue, 28 Jan 2014 12:16:05 -0800  .
*  From:    [hidden email]            .
*  To:      [hidden email]          .
*  Subject: within-subject correlation       .

*  "I need to calculate within-subjects correlation. How I can    .
*  calculate these correlation? You have written that one first   .
*  step is to tarsnform my file in a long file and then split on  .
*  the basis of the subjects. ... Can you help me."               .

*  ................................................................. .
*  .................   Test data               ..................... .
SET RNG = MT       /* 'Mersenne twister' random number generator  */ .
SET MTINDEX = 0367.

INPUT PROGRAM.
.  NUMERIC Subject  (F5)
          /X1 TO X5 (F4)
          /Y1 TO Y5 (F4).
.  VECTOR X = X1 TO X5
         /Y = Y1 TO Y5.
.  LOOP Subject = 1001 TO 1005.
.     COMPUTE #rho  = RV.UNIFORM(0.5,0.9).
.     COMPUTE #Xbar = RV.UNIFORM(40,60).
.     COMPUTE #Ybar = RV.UNIFORM(40,60).
.     DO REPEAT   X = X1 TO X5
                 /Y = Y1 TO Y5.
.        COMPUTE  X = RV.NORMAL(0,1).
.        COMPUTE #Y = RV.NORMAL(0,1).
.        COMPUTE  Y = #rho*X
                    + Sqrt( 1 - #rho**2)*#Y.
.        COMPUTE  X = #Xbar + 20*X.
.        COMPUTE  Y = #YBar + 20*Y.
.        COMPUTE  X = RND(X).
.        COMPUTE  Y = RND(Y).
.     END REPEAT.
.     END CASE.
.  END LOOP.
END FILE.
END INPUT PROGRAM.
DATASET NAME     TestData WINDOW=FRONT.
LIST.

DATASET COPY     LongForm.
DATASET ACTIVATE LongForm WINDOW=FRONT.

VARSTOCASES
  /MAKE X FROM X1 X2 X3 X4 X5
  /MAKE Y FROM Y1 Y2 Y3 Y4 Y5
  /INDEX = Obs#(5)
  /KEEP  =  Subject
  /NULL  = KEEP.

LIST.

SPLIT FILE BY Subject.

DATASET DECLARE  Correlations.

OMS /SELECT ALL EXCEPT=[LOGS TEXTS WARNINGS]
    /DESTINATION VIEWER=NO.

OMS /SELECT TABLES
    /IF COMMANDS = ['CORRELATIONS']
        SUBTYPES = ['Correlations']
    /DESTINATION FORMAT=SAV
                 OUTFILE=Correlations.

CORRELATIONS
  /VARIABLES=X Y
  /MISSING=PAIRWISE.

OMSEND.

DATASET ACTIVATE Correlations WINDOW=FRONT.
LIST.

SELECT IF  Var2 EQ 'X'
       AND Var3 EQ 'Pearson Correlation'.

NUMERIC  Subject (F4).
COMPUTE  Subject=NUMBER(Var1,F4).

*  ...  The following EXECUTE is logically unnecessary,     ... .
*  ...  but without it the preceding SELECT IF and COMPUTE, ... .
*  ...  with the following MATCH FILES, give strange and    ... .
*  ...  incorrect, results.                                 ... .
*  ...  (Running SPSS 14 for Windows; it may be different   ... .
*  ...  in later releases.)                                 ... .

EXECUTE.

MATCH FILES
   /FILE=TestData
   /FILE=Correlations
      /RENAME=(Y = Correlation)
   /BY Subject
   /DROP=Command_ Subtype_ Label_
         Var1      Var2    Var3     X.

LIST.

=====================
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: within-subject correlation

Richard Ristow
In reply to this post by David Marso
At 05:29 PM 2/7/2014, David Marso wrote:

>Or roll your own with MATRIX ;-)

Point taken, especially as it *doesn't* require unrolling the file.

=====================
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: within-subject correlation

David Marso
Administrator
Especially since the math is trivial to implement.
OTOH: We still don't have a definitive idea of what OP actually wants.

Richard Ristow wrote
At 05:29 PM 2/7/2014, David Marso wrote:

>Or roll your own with MATRIX ;-)

Point taken, especially as it *doesn't* require unrolling the file.

=====================
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
Please reply to the list and not to my personal email.
Those desiring my consulting or training services please feel free to email me.
---
"Nolite dare sanctum canibus neque mittatis margaritas vestras ante porcos ne forte conculcent eas pedibus suis."
Cum es damnatorum possederunt porcos iens ut salire off sanguinum cliff in abyssum?"
Reply | Threaded
Open this post in threaded view
|

Re: within-subject correlation

Andy W
In reply to this post by Richard Ristow
FYI CORRELATIONS has an OUTFILE subcommand that can export to a declared sav file. If available I typically choose them over OMS, but I especially like the helper OUTFILE commands when you use SPLIT FILE. The new sav file will have the correct type for the split file variable(s) and so is easily matched back to the original dataset. OMS is dependent on local settings for formats in tables and automatically converts the split field to a string.
Andy W
apwheele@gmail.com
http://andrewpwheeler.wordpress.com/
Reply | Threaded
Open this post in threaded view
|

Re: within-subject correlation

Richard Ristow
At 08:09 AM 2/8/2014, Andy W wrote:

>FYI CORRELATIONS has an OUTFILE subcommand that can export to a
>declared sav file. If available I typically choose them over OMS

This point also taken. I explored this route, but decided that
post-processing the file from OMS would be easier than
post-processing the matrix-form OUTFILE file. (Anyhow, I wanted a
little practice with OMS.)

=====================
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: within-subject correlation

Bruce Weaver
Administrator
In reply to this post by David Marso
I don't have SPSS on this machine, so the following is untested.  But I *think* it should also work, assuming that if there is any missing data, it is missing for both members of an X-Y pair.  (And assuming the OP wants the X-Y correlation for each person individually.)

* The following is UNTESTED.
* It assumes that if there is any missing data, it is
* missing for both of the paired X and Y variables.
* If this is not so, the means and SDs will be incorrect.

* It also assumes that the X1 to Xlast and Y1 to Ylast
* variables are contiguous, as in Richard's example.

* For scalability, rename last X and Y variables.

RENAME VARIABLES (X5 Y5 = Xlast Ylast).

COMPUTE #MeanX = Mean(X1 to Xlast).
COMPUTE #MeanY = Mean(Y1 to Ylast).
COMPUTE #N = NVALID(X1 to Xlast).
COMPUTE #SSX = (#N-1)*SD(X1 to Xlast)**2.
COMPUTE #SSY = (#N-1)*SD(Y1 to Ylast)**2.
COMPUTE #SP = 0.
DO REPEAT X = X1 to Xlast / Y = Y1 to Ylast .
- IF NVALID(X,Y) EQ 2 #SP = #SP + (X-#MeanX)*(Y-#MeanY).
END REPEAT.
COMPUTE rWS = #SP / SQRT(#SSX * #SSY).

* Revert to original variable names for last X and Y variables.

RENAME VARIABLES (Xlast Ylast = X5 Y5).



David Marso wrote
Or roll your own with MATRIX ;-)
Missing data will require adjustment of code.

DATA LIST LIST
 /Subject   X1   X2   X3   X4   X5   Y1   Y2   Y3   Y4   Y5 .
BEGIN DATA
   1001    37   85   37   33   56   43   54   15   18   53
   1002    43   20   63   76   31   32   15   47   38   16
   1003    65   17   39   41   34   48   10   62   58   39
   1004    61   55   29   43   56   93   58   68   74   43
   1005    75   62   29   40    1   78   99   13   52   37
END DATA.
DATASET DECLARE out .
MATRIX .
GET Subject / FILE * / VARIABLES Subject.
GET X / FILE * / VARIABLES X1 TO X5.
GET Y / FILE * / VARIABLES Y1 TO Y5.
COMPUTE R=MAKE(NROW(Subject),1,0).
LOOP #=1 TO NROW(Subject).
COMPUTE CP=SSCP(T({X(#,:)-MSUM(X(#,:))/NCOL(X);
                   Y(#,:)-MSUM(Y(#,:))/NCOL(Y)})).
COMPUTE R(#)=CP(1,2)/SQRT(CP(1,1)*CP(2,2)).
END LOOP.
SAVE {Subject , X, Y, R} / OUTFILE out
  / VARIABLES Subject X1 TO X5 Y1 TO Y5 CorrXY.
PRINT R.
END MATRIX.

Richard Ristow wrote
At 03:16 PM 1/28/2014, [hidden email] wrote me, off-list.

As I've stated previously, though not recently, I prefer that
questions be put on-list; if I do respond to an off-list question,
I'll almost always post the response.
First, a question posted on-list can be addressed by any list member,
and others may have better ideas than I do;
Second, the list is for the whole community of SPSS users; questions
and responses on the list may be informative for others than the
original posters; and
Third, I prefer to have my own work on SPSS visible to the community.)

But, anyway, the question was,

>I am Antonio Aquino , a postgraduate studnet at cardiff University.
>I need to calculate within-subjects correlation. How I can calculate
>these correlation? You have written that one first step is to
>transform my file into a long file and then split on the basis of
>the subjects. and then calculate a correlation.

That is indeed one way to do it, possibly the most direct. Below is
an example, using VARSTOCASES to transform the file to long form,
CORRELATIONS to calculate the correlations, OMS to capture the
results, and a little manipulation to attach the results to the original data.
|-----------------------------|---------------------------|
|Output Created               |07-FEB-2014 16:27:36       |
|-----------------------------|---------------------------|
  [TestData]

Subject   X1   X2   X3   X4   X5   Y1   Y2   Y3   Y4   Y5

   1001    37   85   37   33   56   43   54   15   18   53
   1002    43   20   63   76   31   32   15   47   38   16
   1003    65   17   39   41   34   48   10   62   58   39
   1004    61   55   29   43   56   93   58   68   74   43
   1005    75   62   29   40    1   78   99   13   52   37

Number of cases read:  5    Number of cases listed:  5

DATASET COPY     LongForm.
DATASET ACTIVATE LongForm WINDOW=FRONT.
VARSTOCASES
   /MAKE X FROM X1 X2 X3 X4 X5
   /MAKE Y FROM Y1 Y2 Y3 Y4 Y5
   /INDEX = Obs#(5)
   /KEEP  =  Subject
   /NULL  = KEEP.

Variables to Cases
|-----------------------------|---------------------------|
|Output Created               |07-FEB-2014 16:27:36       |
|-----------------------------|---------------------------|
  [LongForm]

Generated Variables
|----|------|
|Name|Label |
|----|------|
|Obs#|<none>|
|----|------|
|X   |<none>|
|Y   |<none>|
|----|------|

Processing Statistics
|-------------|--|
|Variables In |11|
|Variables Out|4 |
|-------------|--|

LIST.

List
|-----------------------------|---------------------------|
|Output Created               |07-FEB-2014 16:27:36       |
|-----------------------------|---------------------------|
  [LongForm]
Subject Obs#    X    Y

   1001     1   37   43
   1001     2   85   54
   1001     3   37   15
   1001     4   33   18
   1001     5   56   53
   1002     1   43   32
   1002     2   20   15
   1002     3   63   47
   1002     4   76   38
   1002     5   31   16
   1003     1   65   48
   1003     2   17   10
   1003     3   39   62
   1003     4   41   58
   1003     5   34   39
   1004     1   61   93
   1004     2   55   58
   1004     3   29   68
   1004     4   43   74
   1004     5   56   43
   1005     1   75   78
   1005     2   62   99
   1005     3   29   13
   1005     4   40   52
   1005     5    1   37

Number of cases read:  25    Number of cases listed:  25


SPLIT FILE BY Subject.

DATASET DECLARE  Correlations.

OMS /SELECT ALL EXCEPT=[LOGS TEXTS WARNINGS]
     /DESTINATION VIEWER=NO.
OMS /SELECT TABLES
     /IF COMMANDS = ['CORRELATIONS']
         SUBTYPES = ['Correlations']
     /DESTINATION FORMAT=SAV
                  OUTFILE=Correlations.
CORRELATIONS
   /VARIABLES=X Y
   /MISSING=PAIRWISE.

  [LongForm]

OMSEND.
DATASET ACTIVATE Correlations WINDOW=FRONT.
LIST.

List
|-----------------------------|---------------------------|
|Output Created               |07-FEB-2014 16:27:37       |
|-----------------------------|---------------------------|
  [Correlations]
Command_     Subtype_     Label_       Var1 Var2 Var3                   X    Y

Correlations Correlations Correlations 1001 X    Pearson Correlation 1.00 .745
Correlations Correlations Correlations 1001 X    Sig. (2-tailed)     .    .149
Correlations Correlations Correlations 1001 X    N                   5.00 5.00
Correlations Correlations Correlations 1001 Y    Pearson Correlation .745 1.00
Correlations Correlations Correlations 1001 Y    Sig. (2-tailed)     .149 .
Correlations Correlations Correlations 1001 Y    N                   5.00 5.00
Correlations Correlations Correlations 1002 X    Pearson Correlation 1.00 .881
Correlations Correlations Correlations 1002 X    Sig. (2-tailed)     .    .049
Correlations Correlations Correlations 1002 X    N                   5.00 5.00
Correlations Correlations Correlations 1002 Y    Pearson Correlation .881 1.00
Correlations Correlations Correlations 1002 Y    Sig. (2-tailed)     .049 .
Correlations Correlations Correlations 1002 Y    N                   5.00 5.00
Correlations Correlations Correlations 1003 X    Pearson Correlation 1.00 .634
Correlations Correlations Correlations 1003 X    Sig. (2-tailed)     .    .250
Correlations Correlations Correlations 1003 X    N                   5.00 5.00
Correlations Correlations Correlations 1003 Y    Pearson Correlation .634 1.00
Correlations Correlations Correlations 1003 Y    Sig. (2-tailed)     .250 .
Correlations Correlations Correlations 1003 Y    N                   5.00 5.00
Correlations Correlations Correlations 1004 X    Pearson Correlation 1.00 .029
Correlations Correlations Correlations 1004 X    Sig. (2-tailed)     .    .963
Correlations Correlations Correlations 1004 X    N                   5.00 5.00
Correlations Correlations Correlations 1004 Y    Pearson Correlation .029 1.00
Correlations Correlations Correlations 1004 Y    Sig. (2-tailed)     .963 .
Correlations Correlations Correlations 1004 Y    N                   5.00 5.00
Correlations Correlations Correlations 1005 X    Pearson Correlation 1.00 .751
Correlations Correlations Correlations 1005 X    Sig. (2-tailed)     .    .143
Correlations Correlations Correlations 1005 X    N                   5.00 5.00
Correlations Correlations Correlations 1005 Y    Pearson Correlation .751 1.00
Correlations Correlations Correlations 1005 Y    Sig. (2-tailed)     .143 .
Correlations Correlations Correlations 1005 Y    N                   5.00 5.00

Number of cases read:  30    Number of cases listed:  30


SELECT IF  Var2 EQ 'X'
        AND Var3 EQ 'Pearson Correlation'.

NUMERIC  Subject (F4).
COMPUTE  Subject=NUMBER(Var1,F4).

*  ...  The following EXECUTE is logically unnecessary,     ... .
*  ...  but without it the preceding SELECT IF and COMPUTE, ... .
*  ...  with the following MATCH FILES, give strange and    ... .
*  ...  incorrect, results.                                 ... .
*  ...  (Running SPSS 14 for Windows; it may be different   ... .
*  ...  in later releases.)                                 ... .

EXECUTE.

MATCH FILES
    /FILE=TestData
    /FILE=Correlations
       /RENAME=(Y = Correlation)
    /BY Subject
    /DROP=Command_ Subtype_ Label_
          Var1      Var2    Var3     X.

LIST.

List
|-----------------------------|---------------------------|
|Output Created               |07-FEB-2014 16:27:37       |
|-----------------------------|---------------------------|
Subject   X1   X2   X3   X4   X5   Y1   Y2   Y3   Y4   Y5 Correlation

   1001    37   85   37   33   56   43   54   15   18   53     .745
   1002    43   20   63   76   31   32   15   47   38   16     .881
   1003    65   17   39   41   34   48   10   62   58   39     .634
   1004    61   55   29   43   56   93   58   68   74   43     .029
   1005    75   62   29   40    1   78   99   13   52   37     .751

Number of cases read:  5    Number of cases listed:  5
=================================
APPENDIX: Test data, and all code
=================================
*  C:\Documents and Settings\Richard\My Documents         .
*    \Technical\spssx-l\Z-2014\                           .
*    2014-01-28 AquinoA - within-subject correlation.SPS  .

*  In response to off-list inquiry           .
*  Date:    Tue, 28 Jan 2014 12:16:05 -0800  .
*  From:    [hidden email]            .
*  To:      [hidden email]          .
*  Subject: within-subject correlation       .

*  "I need to calculate within-subjects correlation. How I can    .
*  calculate these correlation? You have written that one first   .
*  step is to tarsnform my file in a long file and then split on  .
*  the basis of the subjects. ... Can you help me."               .

*  ................................................................. .
*  .................   Test data               ..................... .
SET RNG = MT       /* 'Mersenne twister' random number generator  */ .
SET MTINDEX = 0367.

INPUT PROGRAM.
.  NUMERIC Subject  (F5)
           /X1 TO X5 (F4)
           /Y1 TO Y5 (F4).
.  VECTOR X = X1 TO X5
          /Y = Y1 TO Y5.
.  LOOP Subject = 1001 TO 1005.
.     COMPUTE #rho  = RV.UNIFORM(0.5,0.9).
.     COMPUTE #Xbar = RV.UNIFORM(40,60).
.     COMPUTE #Ybar = RV.UNIFORM(40,60).
.     DO REPEAT   X = X1 TO X5
                  /Y = Y1 TO Y5.
.        COMPUTE  X = RV.NORMAL(0,1).
.        COMPUTE #Y = RV.NORMAL(0,1).
.        COMPUTE  Y = #rho*X
                     + Sqrt( 1 - #rho**2)*#Y.
.        COMPUTE  X = #Xbar + 20*X.
.        COMPUTE  Y = #YBar + 20*Y.
.        COMPUTE  X = RND(X).
.        COMPUTE  Y = RND(Y).
.     END REPEAT.
.     END CASE.
.  END LOOP.
END FILE.
END INPUT PROGRAM.
DATASET NAME     TestData WINDOW=FRONT.
LIST.

DATASET COPY     LongForm.
DATASET ACTIVATE LongForm WINDOW=FRONT.

VARSTOCASES
   /MAKE X FROM X1 X2 X3 X4 X5
   /MAKE Y FROM Y1 Y2 Y3 Y4 Y5
   /INDEX = Obs#(5)
   /KEEP  =  Subject
   /NULL  = KEEP.

LIST.

SPLIT FILE BY Subject.

DATASET DECLARE  Correlations.

OMS /SELECT ALL EXCEPT=[LOGS TEXTS WARNINGS]
     /DESTINATION VIEWER=NO.

OMS /SELECT TABLES
     /IF COMMANDS = ['CORRELATIONS']
         SUBTYPES = ['Correlations']
     /DESTINATION FORMAT=SAV
                  OUTFILE=Correlations.

CORRELATIONS
   /VARIABLES=X Y
   /MISSING=PAIRWISE.

OMSEND.

DATASET ACTIVATE Correlations WINDOW=FRONT.
LIST.

SELECT IF  Var2 EQ 'X'
        AND Var3 EQ 'Pearson Correlation'.

NUMERIC  Subject (F4).
COMPUTE  Subject=NUMBER(Var1,F4).

*  ...  The following EXECUTE is logically unnecessary,     ... .
*  ...  but without it the preceding SELECT IF and COMPUTE, ... .
*  ...  with the following MATCH FILES, give strange and    ... .
*  ...  incorrect, results.                                 ... .
*  ...  (Running SPSS 14 for Windows; it may be different   ... .
*  ...  in later releases.)                                 ... .

EXECUTE.

MATCH FILES
    /FILE=TestData
    /FILE=Correlations
       /RENAME=(Y = Correlation)
    /BY Subject
    /DROP=Command_ Subtype_ Label_
          Var1      Var2    Var3     X.

LIST.

=====================
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
--
Bruce Weaver
bweaver@lakeheadu.ca
http://sites.google.com/a/lakeheadu.ca/bweaver/

"When all else fails, RTFM."

PLEASE NOTE THE FOLLOWING: 
1. My Hotmail account is not monitored regularly. To send me an e-mail, please use the address shown above.
2. The SPSSX Discussion forum on Nabble is no longer linked to the SPSSX-L listserv administered by UGA (https://listserv.uga.edu/).