Re: [R] apply & incompatible dimensions error

2007-07-24 Thread Bernzweig, Bruce \(Consultant\)
Thanks Benilton,

I know what I want to do, just not sure how to do it using R.  The help
documentation is not very clear.

What I am trying to do is calculate correlations on a row against row
basis:  mat1 row1 x mat2 row1, mat1 row1 x mat2 row2, ... mat1 row1 x
mat2 row-n, mat1 row-n, mat2 row-n

- Bruce

-Original Message-
From: Benilton Carvalho [mailto:[EMAIL PROTECTED] 
Sent: Tuesday, July 24, 2007 11:31 AM
To: Bernzweig, Bruce (Consultant)
Cc: r-help@stat.math.ethz.ch
Subject: Re: [R] apply & incompatible dimensions error

are you positive that your function is doing what you expect it to do?

it looks like you want something like:

sapply(1:10, function(i) cor(mat1[i,], mat2[i,]))

b

On Jul 24, 2007, at 11:05 AM, Bernzweig, Bruce ((Consultant)) wrote:

> Hi,
>
> I've created the following two matrices (mat1 and mat2) and a function
> (f) to calculate the correlations between the two on a row by row  
> basis.
>
>   mat1 <- matrix(sample(1:500,50), ncol = 5,
>   dimnames=list(paste("row", 1:10, sep=""),
>   paste("col", 1:5, sep="")))
>
>   mat2 <- matrix(sample(501:1000,50), ncol = 5,
>   dimnames=list(paste("row", 1:10, sep=""),
>   paste("col", 1:5, sep="")))
>
>   f <- function(x,y) cor(x,y)
>
> When the matrices are squares (# rows = # columns) I have no problems.
>
> However, when they are not (as in the example above with 5 columns and
> 10 rows), I get the following error:
>
>> apply(mat1, 1, f, y=mat2)
> Error in cor(x, y, na.method, method == "kendall") :
> incompatible dimensions
>
> Any help would be appreciated.  Thanks!
>
> - Bruce
>
>
>
> **
> Please be aware that, notwithstanding the fact that the pers... 
> {{dropped}}
>
> __
> R-help@stat.math.ethz.ch mailing list
> https://stat.ethz.ch/mailman/listinfo/r-help
> PLEASE do read the posting guide http://www.R-project.org/posting- 
> guide.html
> and provide commented, minimal, self-contained, reproducible code.



**
Please be aware that, notwithstanding the fact that the pers...{{dropped}}

__
R-help@stat.math.ethz.ch mailing list
https://stat.ethz.ch/mailman/listinfo/r-help
PLEASE do read the posting guide http://www.R-project.org/posting-guide.html
and provide commented, minimal, self-contained, reproducible code.


Re: [R] apply & incompatible dimensions error

2007-07-24 Thread Bernzweig, Bruce \(Consultant\)
Thanks for the explanation.

As for the rows/columns thing, the data I receive is given to me in that
way.  I currently read it in using read.csv.  Is there a function I
should look at that can take that and transpose it or should I just
process the data first outside of R?

Thanks,

- Bruce

-Original Message-
From: Gabor Grothendieck [mailto:[EMAIL PROTECTED] 
Sent: Tuesday, July 24, 2007 11:43 AM
To: Bernzweig, Bruce (Consultant)
Cc: r-help@stat.math.ethz.ch
Subject: Re: [R] apply & incompatible dimensions error

Then try this:

cor(t(mat1), t(mat2))

Also note

1. the above implies that mat1 and mat2 must have the same
number of columns since if x and y are vectors cor(x,y) only makes
sense if they have the same length.

2. the usual convention is that variables are stored as columns
andt that rows correspond to cases so typically you would have
(in terms of your mat1 and mat2):

Mat1 <- t(mat1)
Mat2 <- t(mat2)

and then use Mat1 and Mat2, e.g. cor(Mat1, Mat2)



On 7/24/07, Bernzweig, Bruce (Consultant) <[EMAIL PROTECTED]> wrote:
> Thanks Gabor!
>
> You state that my apply is taking rows of mat1 with columns of mat2.
>
> Is this because I have the y=mat2 parameter?
>
> > apply(mat1, 1, f, y=mat2)
>
> Actually, what I would like is to run the correlations on a row
against
> row basis:  mat1 row1 x mat2 row1, etc.
>
> Thanks again,
>
> - Bruce
>
>
> -Original Message-
> From: Gabor Grothendieck [mailto:[EMAIL PROTECTED]
> Sent: Tuesday, July 24, 2007 11:31 AM
> To: Bernzweig, Bruce (Consultant)
> Cc: r-help@stat.math.ethz.ch
> Subject: Re: [R] apply & incompatible dimensions error
>
> Your apply is trying to take the correlations of the rows of mat1 with
> the
> columns of mat2 which, of course, does not work if they have different
> numbers of columns. I think you mean to take the correlations of the
> columns
> of mat1 with the columns of mat2.  For example, to take the
correlations
> of the 5 columns of mat1 with the first 4 columns of mat2 try:
>
> > cor(mat1, mat2[,1:4])
>col1   col2   col3   col4
> col1 -0.34624254 -0.2669519 -0.2705077  0.2183249
> col2 -0.26553255 -0.2687643 -0.0865895  0.1819025
> col3  0.19474613 -0.2334986  0.1746522  0.2326915
> col4  0.09328338  0.5117784  0.2413143 -0.3374916
> col5  0.27519716  0.1605331 -0.4057137  0.3282105
>
>
> On 7/24/07, Bernzweig, Bruce (Consultant) <[EMAIL PROTECTED]> wrote:
> > Hi,
> >
> > I've created the following two matrices (mat1 and mat2) and a
function
> > (f) to calculate the correlations between the two on a row by row
> basis.
> >
> >mat1 <- matrix(sample(1:500,50), ncol = 5,
> >dimnames=list(paste("row", 1:10, sep=""),
> >paste("col", 1:5, sep="")))
> >
> >mat2 <- matrix(sample(501:1000,50), ncol = 5,
> >dimnames=list(paste("row", 1:10, sep=""),
> >paste("col", 1:5, sep="")))
> >
> >f <- function(x,y) cor(x,y)
> >
> > When the matrices are squares (# rows = # columns) I have no
problems.
> >
> > However, when they are not (as in the example above with 5 columns
and
> > 10 rows), I get the following error:
> >
> > > apply(mat1, 1, f, y=mat2)
> > Error in cor(x, y, na.method, method == "kendall") :
> >incompatible dimensions
> >
> > Any help would be appreciated.  Thanks!
> >
> > - Bruce
> >
> >
> >
> >
**
> > Please be aware that, notwithstanding the fact that the
> pers...{{dropped}}
> >
> > __
> > R-help@stat.math.ethz.ch mailing list
> > https://stat.ethz.ch/mailman/listinfo/r-help
> > PLEASE do read the posting guide
> http://www.R-project.org/posting-guide.html
> > and provide commented, minimal, self-contained, reproducible code.
> >
>
>
>
> **
> Please be aware that, notwithstanding the fact that the person sending
> this communication has an address in Bear Stearns' e-mail system, this
> person is not an employee, agent or representative of Bear Stearns.
> Accordingly, this person has no power or authority to represent, make
> any recommendation, solicitation, offer or statements or disclose
> information on behalf of or in any way bind Bear Stearns or any of its
> affiliates.
> **
>



**
Please be aware that, notwithstanding the fact that the pers...{{dropped}}

__
R-help@stat.math.ethz.ch mailing list
https://stat.ethz.ch/mailman/listinfo/r-help
PLEASE do read the posting guide http://www.R-project.org/posting-guide.html
and provide commented, minimal, self-contained, reproducible code.


Re: [R] apply & incompatible dimensions error

2007-07-24 Thread Benilton Carvalho
that's garbor's suggestion then.
sorry for the misunderstanding. :-)
b

On Jul 24, 2007, at 11:35 AM, Bernzweig, Bruce ((Consultant)) wrote:

> Thanks Benilton,
>
> I know what I want to do, just not sure how to do it using R.  The  
> help
> documentation is not very clear.
>
> What I am trying to do is calculate correlations on a row against row
> basis:  mat1 row1 x mat2 row1, mat1 row1 x mat2 row2, ... mat1 row1 x
> mat2 row-n, mat1 row-n, mat2 row-n
>
> - Bruce
>
> -Original Message-
> From: Benilton Carvalho [mailto:[EMAIL PROTECTED]
> Sent: Tuesday, July 24, 2007 11:31 AM
> To: Bernzweig, Bruce (Consultant)
> Cc: r-help@stat.math.ethz.ch
> Subject: Re: [R] apply & incompatible dimensions error
>
> are you positive that your function is doing what you expect it to do?
>
> it looks like you want something like:
>
> sapply(1:10, function(i) cor(mat1[i,], mat2[i,]))
>
> b
>
> On Jul 24, 2007, at 11:05 AM, Bernzweig, Bruce ((Consultant)) wrote:
>
>> Hi,
>>
>> I've created the following two matrices (mat1 and mat2) and a  
>> function
>> (f) to calculate the correlations between the two on a row by row
>> basis.
>>
>>  mat1 <- matrix(sample(1:500,50), ncol = 5,
>>  dimnames=list(paste("row", 1:10, sep=""),
>>  paste("col", 1:5, sep="")))
>>
>>  mat2 <- matrix(sample(501:1000,50), ncol = 5,
>>  dimnames=list(paste("row", 1:10, sep=""),
>>  paste("col", 1:5, sep="")))
>>
>>  f <- function(x,y) cor(x,y)
>>
>> When the matrices are squares (# rows = # columns) I have no  
>> problems.
>>
>> However, when they are not (as in the example above with 5 columns  
>> and
>> 10 rows), I get the following error:
>>
>>> apply(mat1, 1, f, y=mat2)
>> Error in cor(x, y, na.method, method == "kendall") :
>> incompatible dimensions
>>
>> Any help would be appreciated.  Thanks!
>>
>> - Bruce
>>
>>
>>
>> * 
>> *
>> Please be aware that, notwithstanding the fact that the pers...
>> {{dropped}}
>>
>> __
>> R-help@stat.math.ethz.ch mailing list
>> https://stat.ethz.ch/mailman/listinfo/r-help
>> PLEASE do read the posting guide http://www.R-project.org/posting-
>> guide.html
>> and provide commented, minimal, self-contained, reproducible code.
>
>
>
> **
> Please be aware that, notwithstanding the fact that the person sending
> this communication has an address in Bear Stearns' e-mail system, this
> person is not an employee, agent or representative of Bear Stearns.
> Accordingly, this person has no power or authority to represent, make
> any recommendation, solicitation, offer or statements or disclose
> information on behalf of or in any way bind Bear Stearns or any of its
> affiliates.
> **

__
R-help@stat.math.ethz.ch mailing list
https://stat.ethz.ch/mailman/listinfo/r-help
PLEASE do read the posting guide http://www.R-project.org/posting-guide.html
and provide commented, minimal, self-contained, reproducible code.


Re: [R] apply & incompatible dimensions error

2007-07-24 Thread Gabor Grothendieck
Then try this:

cor(t(mat1), t(mat2))

Also note

1. the above implies that mat1 and mat2 must have the same
number of columns since if x and y are vectors cor(x,y) only makes
sense if they have the same length.

2. the usual convention is that variables are stored as columns
andt that rows correspond to cases so typically you would have
(in terms of your mat1 and mat2):

Mat1 <- t(mat1)
Mat2 <- t(mat2)

and then use Mat1 and Mat2, e.g. cor(Mat1, Mat2)



On 7/24/07, Bernzweig, Bruce (Consultant) <[EMAIL PROTECTED]> wrote:
> Thanks Gabor!
>
> You state that my apply is taking rows of mat1 with columns of mat2.
>
> Is this because I have the y=mat2 parameter?
>
> > apply(mat1, 1, f, y=mat2)
>
> Actually, what I would like is to run the correlations on a row against
> row basis:  mat1 row1 x mat2 row1, etc.
>
> Thanks again,
>
> - Bruce
>
>
> -Original Message-
> From: Gabor Grothendieck [mailto:[EMAIL PROTECTED]
> Sent: Tuesday, July 24, 2007 11:31 AM
> To: Bernzweig, Bruce (Consultant)
> Cc: r-help@stat.math.ethz.ch
> Subject: Re: [R] apply & incompatible dimensions error
>
> Your apply is trying to take the correlations of the rows of mat1 with
> the
> columns of mat2 which, of course, does not work if they have different
> numbers of columns. I think you mean to take the correlations of the
> columns
> of mat1 with the columns of mat2.  For example, to take the correlations
> of the 5 columns of mat1 with the first 4 columns of mat2 try:
>
> > cor(mat1, mat2[,1:4])
>col1   col2   col3   col4
> col1 -0.34624254 -0.2669519 -0.2705077  0.2183249
> col2 -0.26553255 -0.2687643 -0.0865895  0.1819025
> col3  0.19474613 -0.2334986  0.1746522  0.2326915
> col4  0.09328338  0.5117784  0.2413143 -0.3374916
> col5  0.27519716  0.1605331 -0.4057137  0.3282105
>
>
> On 7/24/07, Bernzweig, Bruce (Consultant) <[EMAIL PROTECTED]> wrote:
> > Hi,
> >
> > I've created the following two matrices (mat1 and mat2) and a function
> > (f) to calculate the correlations between the two on a row by row
> basis.
> >
> >mat1 <- matrix(sample(1:500,50), ncol = 5,
> >dimnames=list(paste("row", 1:10, sep=""),
> >paste("col", 1:5, sep="")))
> >
> >mat2 <- matrix(sample(501:1000,50), ncol = 5,
> >dimnames=list(paste("row", 1:10, sep=""),
> >paste("col", 1:5, sep="")))
> >
> >f <- function(x,y) cor(x,y)
> >
> > When the matrices are squares (# rows = # columns) I have no problems.
> >
> > However, when they are not (as in the example above with 5 columns and
> > 10 rows), I get the following error:
> >
> > > apply(mat1, 1, f, y=mat2)
> > Error in cor(x, y, na.method, method == "kendall") :
> >incompatible dimensions
> >
> > Any help would be appreciated.  Thanks!
> >
> > - Bruce
> >
> >
> >
> > **
> > Please be aware that, notwithstanding the fact that the
> pers...{{dropped}}
> >
> > __
> > R-help@stat.math.ethz.ch mailing list
> > https://stat.ethz.ch/mailman/listinfo/r-help
> > PLEASE do read the posting guide
> http://www.R-project.org/posting-guide.html
> > and provide commented, minimal, self-contained, reproducible code.
> >
>
>
>
> **
> Please be aware that, notwithstanding the fact that the person sending
> this communication has an address in Bear Stearns' e-mail system, this
> person is not an employee, agent or representative of Bear Stearns.
> Accordingly, this person has no power or authority to represent, make
> any recommendation, solicitation, offer or statements or disclose
> information on behalf of or in any way bind Bear Stearns or any of its
> affiliates.
> **
>

__
R-help@stat.math.ethz.ch mailing list
https://stat.ethz.ch/mailman/listinfo/r-help
PLEASE do read the posting guide http://www.R-project.org/posting-guide.html
and provide commented, minimal, self-contained, reproducible code.


Re: [R] apply & incompatible dimensions error

2007-07-24 Thread Bernzweig, Bruce \(Consultant\)
Thanks Gabor!

You state that my apply is taking rows of mat1 with columns of mat2.

Is this because I have the y=mat2 parameter?

> apply(mat1, 1, f, y=mat2)

Actually, what I would like is to run the correlations on a row against
row basis:  mat1 row1 x mat2 row1, etc.

Thanks again,

- Bruce


-Original Message-
From: Gabor Grothendieck [mailto:[EMAIL PROTECTED] 
Sent: Tuesday, July 24, 2007 11:31 AM
To: Bernzweig, Bruce (Consultant)
Cc: r-help@stat.math.ethz.ch
Subject: Re: [R] apply & incompatible dimensions error

Your apply is trying to take the correlations of the rows of mat1 with
the
columns of mat2 which, of course, does not work if they have different
numbers of columns. I think you mean to take the correlations of the
columns
of mat1 with the columns of mat2.  For example, to take the correlations
of the 5 columns of mat1 with the first 4 columns of mat2 try:

> cor(mat1, mat2[,1:4])
col1   col2   col3   col4
col1 -0.34624254 -0.2669519 -0.2705077  0.2183249
col2 -0.26553255 -0.2687643 -0.0865895  0.1819025
col3  0.19474613 -0.2334986  0.1746522  0.2326915
col4  0.09328338  0.5117784  0.2413143 -0.3374916
col5  0.27519716  0.1605331 -0.4057137  0.3282105


On 7/24/07, Bernzweig, Bruce (Consultant) <[EMAIL PROTECTED]> wrote:
> Hi,
>
> I've created the following two matrices (mat1 and mat2) and a function
> (f) to calculate the correlations between the two on a row by row
basis.
>
>mat1 <- matrix(sample(1:500,50), ncol = 5,
>dimnames=list(paste("row", 1:10, sep=""),
>paste("col", 1:5, sep="")))
>
>mat2 <- matrix(sample(501:1000,50), ncol = 5,
>dimnames=list(paste("row", 1:10, sep=""),
>paste("col", 1:5, sep="")))
>
>f <- function(x,y) cor(x,y)
>
> When the matrices are squares (# rows = # columns) I have no problems.
>
> However, when they are not (as in the example above with 5 columns and
> 10 rows), I get the following error:
>
> > apply(mat1, 1, f, y=mat2)
> Error in cor(x, y, na.method, method == "kendall") :
>incompatible dimensions
>
> Any help would be appreciated.  Thanks!
>
> - Bruce
>
>
>
> **
> Please be aware that, notwithstanding the fact that the
pers...{{dropped}}
>
> __
> R-help@stat.math.ethz.ch mailing list
> https://stat.ethz.ch/mailman/listinfo/r-help
> PLEASE do read the posting guide
http://www.R-project.org/posting-guide.html
> and provide commented, minimal, self-contained, reproducible code.
>



**
Please be aware that, notwithstanding the fact that the pers...{{dropped}}

__
R-help@stat.math.ethz.ch mailing list
https://stat.ethz.ch/mailman/listinfo/r-help
PLEASE do read the posting guide http://www.R-project.org/posting-guide.html
and provide commented, minimal, self-contained, reproducible code.


Re: [R] apply & incompatible dimensions error

2007-07-24 Thread Benilton Carvalho
are you positive that your function is doing what you expect it to do?

it looks like you want something like:

sapply(1:10, function(i) cor(mat1[i,], mat2[i,]))

b

On Jul 24, 2007, at 11:05 AM, Bernzweig, Bruce ((Consultant)) wrote:

> Hi,
>
> I've created the following two matrices (mat1 and mat2) and a function
> (f) to calculate the correlations between the two on a row by row  
> basis.
>
>   mat1 <- matrix(sample(1:500,50), ncol = 5,
>   dimnames=list(paste("row", 1:10, sep=""),
>   paste("col", 1:5, sep="")))
>
>   mat2 <- matrix(sample(501:1000,50), ncol = 5,
>   dimnames=list(paste("row", 1:10, sep=""),
>   paste("col", 1:5, sep="")))
>
>   f <- function(x,y) cor(x,y)
>
> When the matrices are squares (# rows = # columns) I have no problems.
>
> However, when they are not (as in the example above with 5 columns and
> 10 rows), I get the following error:
>
>> apply(mat1, 1, f, y=mat2)
> Error in cor(x, y, na.method, method == "kendall") :
> incompatible dimensions
>
> Any help would be appreciated.  Thanks!
>
> - Bruce
>
>
>
> **
> Please be aware that, notwithstanding the fact that the pers... 
> {{dropped}}
>
> __
> R-help@stat.math.ethz.ch mailing list
> https://stat.ethz.ch/mailman/listinfo/r-help
> PLEASE do read the posting guide http://www.R-project.org/posting- 
> guide.html
> and provide commented, minimal, self-contained, reproducible code.

__
R-help@stat.math.ethz.ch mailing list
https://stat.ethz.ch/mailman/listinfo/r-help
PLEASE do read the posting guide http://www.R-project.org/posting-guide.html
and provide commented, minimal, self-contained, reproducible code.


Re: [R] apply & incompatible dimensions error

2007-07-24 Thread Gabor Grothendieck
Your apply is trying to take the correlations of the rows of mat1 with the
columns of mat2 which, of course, does not work if they have different
numbers of columns. I think you mean to take the correlations of the columns
of mat1 with the columns of mat2.  For example, to take the correlations
of the 5 columns of mat1 with the first 4 columns of mat2 try:

> cor(mat1, mat2[,1:4])
col1   col2   col3   col4
col1 -0.34624254 -0.2669519 -0.2705077  0.2183249
col2 -0.26553255 -0.2687643 -0.0865895  0.1819025
col3  0.19474613 -0.2334986  0.1746522  0.2326915
col4  0.09328338  0.5117784  0.2413143 -0.3374916
col5  0.27519716  0.1605331 -0.4057137  0.3282105


On 7/24/07, Bernzweig, Bruce (Consultant) <[EMAIL PROTECTED]> wrote:
> Hi,
>
> I've created the following two matrices (mat1 and mat2) and a function
> (f) to calculate the correlations between the two on a row by row basis.
>
>mat1 <- matrix(sample(1:500,50), ncol = 5,
>dimnames=list(paste("row", 1:10, sep=""),
>paste("col", 1:5, sep="")))
>
>mat2 <- matrix(sample(501:1000,50), ncol = 5,
>dimnames=list(paste("row", 1:10, sep=""),
>paste("col", 1:5, sep="")))
>
>f <- function(x,y) cor(x,y)
>
> When the matrices are squares (# rows = # columns) I have no problems.
>
> However, when they are not (as in the example above with 5 columns and
> 10 rows), I get the following error:
>
> > apply(mat1, 1, f, y=mat2)
> Error in cor(x, y, na.method, method == "kendall") :
>incompatible dimensions
>
> Any help would be appreciated.  Thanks!
>
> - Bruce
>
>
>
> **
> Please be aware that, notwithstanding the fact that the pers...{{dropped}}
>
> __
> R-help@stat.math.ethz.ch mailing list
> https://stat.ethz.ch/mailman/listinfo/r-help
> PLEASE do read the posting guide http://www.R-project.org/posting-guide.html
> and provide commented, minimal, self-contained, reproducible code.
>

__
R-help@stat.math.ethz.ch mailing list
https://stat.ethz.ch/mailman/listinfo/r-help
PLEASE do read the posting guide http://www.R-project.org/posting-guide.html
and provide commented, minimal, self-contained, reproducible code.


[R] apply & incompatible dimensions error

2007-07-24 Thread Bernzweig, Bruce \(Consultant\)
Hi,

I've created the following two matrices (mat1 and mat2) and a function
(f) to calculate the correlations between the two on a row by row basis.

mat1 <- matrix(sample(1:500,50), ncol = 5, 
dimnames=list(paste("row", 1:10, sep=""), 
paste("col", 1:5, sep="")))

mat2 <- matrix(sample(501:1000,50), ncol = 5, 
dimnames=list(paste("row", 1:10, sep=""), 
paste("col", 1:5, sep="")))

f <- function(x,y) cor(x,y)

When the matrices are squares (# rows = # columns) I have no problems.

However, when they are not (as in the example above with 5 columns and
10 rows), I get the following error:

> apply(mat1, 1, f, y=mat2)
Error in cor(x, y, na.method, method == "kendall") : 
incompatible dimensions

Any help would be appreciated.  Thanks!

- Bruce



**
Please be aware that, notwithstanding the fact that the pers...{{dropped}}

__
R-help@stat.math.ethz.ch mailing list
https://stat.ethz.ch/mailman/listinfo/r-help
PLEASE do read the posting guide http://www.R-project.org/posting-guide.html
and provide commented, minimal, self-contained, reproducible code.