Re: [R] Correlate

2022-08-26 Thread Bert Gunter
gt;
> >> Please see below:
> >>
> >>>
> >>> On Mon, Aug 22, 2022 at 12:23 PM Ebert,Timothy Aaron  
> >>> wrote:
> >>>>
> >>>> I (maybe) agree, but I would go further than that. There are assumptions 
> >>>> associated with the test that are missing. It is not clear that the 
> >>>> relationships are all linear. Regardless of a "significant outcome" all 
> >>>> of the relationships need to be explored in more detail than what is 
> >>>> provided in the correlation test.
> >>>>
> >>>> Multiplicity adjustment as in : 
> >>>> https://www.sciencedirect.com/science/article/pii/S019724561069 is 
> >>>> not an issue that I can see in these data from the information provided. 
> >>>> At least not in the same sense as used in the link.
> >>>>
> >>>> My first guess at the meaning of "multiplicity adjustment" was closer to 
> >>>> the experimentwise error rate in a multiple comparison procedure. 
> >>>> https://dictionary.apa.org/experiment-wise-error-rateEssentially, the 
> >>>> type 1 error rate is inflated the more test you do and if you perform 
> >>>> enough tests you find significant outcomes by chance alone. There is 
> >>>> great significance in the Redskins rule: 
> >>>> https://en.wikipedia.org/wiki/Redskins_Rule.
> >>>>
> >>>> A simple solution is to apply a Bonferroni correction where alpha is 
> >>>> divided by the number of comparisons. If there are 250, then 0.05/250 = 
> >>>> 0.0002. Another approach is to try to discuss the outcomes in a way that 
> >>>> makes sense. What is the connection between a football team's last home 
> >>>> game an the election result that would enable me to take another team 
> >>>> and apply their last home game result to the outcome of a different 
> >>>> election?
> >>>>
> >>>> Another complication is if variables x2 through x250 are themselves 
> >>>> correlated. Not enough information was provided in the problem to know 
> >>>> if this is an issue, but 250 orthogonal variables in a real dataset 
> >>>> would be a bit unusual considering the experimentwise error rate 
> >>>> previously mentioned.
> >>>>
> >>>> Large datasets can be very messy.
> >>>>
> >>>>
> >>>> Tim
> >>>>
> >>>> -Original Message-
> >>>> From: Bert Gunter 
> >>>> Sent: Monday, August 22, 2022 12:07 PM
> >>>> To: Ebert,Timothy Aaron 
> >>>> Cc: Val ; r-help@R-project.org 
> >>>> (r-help@r-project.org) 
> >>>> Subject: Re: [R] Correlate
> >>>>
> >>>> [External Email]
> >>>>
> >>>> ... But of course the p-values are essentially meaningless without some 
> >>>> sort of multiplicity adjustment.
> >>>> (search on "multiplicity adjustment" for details). :-(
> >>>>
> >>>> -- Bert
> >>>>
> >>>>
> >>>> On Mon, Aug 22, 2022 at 8:59 AM Ebert,Timothy Aaron  
> >>>> wrote:
> >>>>>
> >>>>> A somewhat clunky solution:
> >>>>> for(i in colnames(dat)){
> >>>>> print(cor.test(dat[,i], dat$x1, method = "pearson", use = 
> >>>>> "complete.obs")$estimate)
> >>>>> print(cor.test(dat[,i], dat$x1, method = "pearson", use =
> >>>>> "complete.obs")$p.value) }
> >>
> >> Because of missing data, this computes the correlations on different
> >> subsets of the data. A simple solution is to filter the data for NAs:
> >>
> >> D <- na.omit(dat)
> >>
> >> More comments below:
> >>
> >>>>>
> >>>>> Rather than printing you could set up an array or list to save the 
> >>>>> results.
> >>>>>
> >>>>>
> >>>>> Tim
> >>>>>
> >>>>> -Original Message-
> >>>>> From: R-help  On Behalf Of Val
> >>>>> Sent: Monday, August 22, 2022 11:09 AM
> >>>>> To: r-help@R-project.org (r-help@r-project.org) 
> >>>>> Subject: [R]

Re: [R] Correlate

2022-08-26 Thread Val
4561069 is 
> >>>> not an issue that I can see in these data from the information provided. 
> >>>> At least not in the same sense as used in the link.
> >>>>
> >>>> My first guess at the meaning of "multiplicity adjustment" was closer to 
> >>>> the experimentwise error rate in a multiple comparison procedure. 
> >>>> https://dictionary.apa.org/experiment-wise-error-rateEssentially, the 
> >>>> type 1 error rate is inflated the more test you do and if you perform 
> >>>> enough tests you find significant outcomes by chance alone. There is 
> >>>> great significance in the Redskins rule: 
> >>>> https://en.wikipedia.org/wiki/Redskins_Rule.
> >>>>
> >>>> A simple solution is to apply a Bonferroni correction where alpha is 
> >>>> divided by the number of comparisons. If there are 250, then 0.05/250 = 
> >>>> 0.0002. Another approach is to try to discuss the outcomes in a way that 
> >>>> makes sense. What is the connection between a football team's last home 
> >>>> game an the election result that would enable me to take another team 
> >>>> and apply their last home game result to the outcome of a different 
> >>>> election?
> >>>>
> >>>> Another complication is if variables x2 through x250 are themselves 
> >>>> correlated. Not enough information was provided in the problem to know 
> >>>> if this is an issue, but 250 orthogonal variables in a real dataset 
> >>>> would be a bit unusual considering the experimentwise error rate 
> >>>> previously mentioned.
> >>>>
> >>>> Large datasets can be very messy.
> >>>>
> >>>>
> >>>> Tim
> >>>>
> >>>> -Original Message-
> >>>> From: Bert Gunter 
> >>>> Sent: Monday, August 22, 2022 12:07 PM
> >>>> To: Ebert,Timothy Aaron 
> >>>> Cc: Val ; r-help@R-project.org 
> >>>> (r-help@r-project.org) 
> >>>> Subject: Re: [R] Correlate
> >>>>
> >>>> [External Email]
> >>>>
> >>>> ... But of course the p-values are essentially meaningless without some 
> >>>> sort of multiplicity adjustment.
> >>>> (search on "multiplicity adjustment" for details). :-(
> >>>>
> >>>> -- Bert
> >>>>
> >>>>
> >>>> On Mon, Aug 22, 2022 at 8:59 AM Ebert,Timothy Aaron  
> >>>> wrote:
> >>>>>
> >>>>> A somewhat clunky solution:
> >>>>> for(i in colnames(dat)){
> >>>>> print(cor.test(dat[,i], dat$x1, method = "pearson", use = 
> >>>>> "complete.obs")$estimate)
> >>>>> print(cor.test(dat[,i], dat$x1, method = "pearson", use =
> >>>>> "complete.obs")$p.value) }
> >>
> >> Because of missing data, this computes the correlations on different
> >> subsets of the data. A simple solution is to filter the data for NAs:
> >>
> >> D <- na.omit(dat)
> >>
> >> More comments below:
> >>
> >>>>>
> >>>>> Rather than printing you could set up an array or list to save the 
> >>>>> results.
> >>>>>
> >>>>>
> >>>>> Tim
> >>>>>
> >>>>> -Original Message-
> >>>>> From: R-help  On Behalf Of Val
> >>>>> Sent: Monday, August 22, 2022 11:09 AM
> >>>>> To: r-help@R-project.org (r-help@r-project.org) 
> >>>>> Subject: [R] Correlate
> >>>>>
> >>>>> [External Email]
> >>>>>
> >>>>> Hi all,
> >>>>>
> >>>>> I have a data set with  ~250  variables(columns).  I want to calculate
> >>>>> the correlation of  one variable with the rest of the other variables
> >>>>> and also want  the p-values  for each correlation.  Please see the
> >>>>> sample data and my attempt.  I  have got the correlation but unable to
> >>>>> get the p-values
> >>>>>
> >>>>> dat <- read.table(text="x1 x2 x3 x4
> >>>>>  1.68 -0.96 -1.25  0.61
> >>>>> -0.0

Re: [R] Correlate

2022-08-22 Thread John Fox

Dear Val,

On 2022-08-22 1:33 p.m., Val wrote:

For the time being  I am assuming the relationship across  variables
is linear.  I want get the values first  and detailed examining  of
the relationship will follow later.


This seems backwards to me, but I'll refrain from commenting further on 
whether what you want to do makes sense and instead address how to do it 
(not, BTW, because I disagree with Bert's and Tim's remarks).


Please see below:



On Mon, Aug 22, 2022 at 12:23 PM Ebert,Timothy Aaron  wrote:


I (maybe) agree, but I would go further than that. There are assumptions associated with 
the test that are missing. It is not clear that the relationships are all linear. 
Regardless of a "significant outcome" all of the relationships need to be 
explored in more detail than what is provided in the correlation test.

Multiplicity adjustment as in : 
https://www.sciencedirect.com/science/article/pii/S019724561069 is not an 
issue that I can see in these data from the information provided. At least not 
in the same sense as used in the link.

My first guess at the meaning of "multiplicity adjustment" was closer to the 
experimentwise error rate in a multiple comparison procedure. 
https://dictionary.apa.org/experiment-wise-error-rateEssentially, the type 1 error rate 
is inflated the more test you do and if you perform enough tests you find significant 
outcomes by chance alone. There is great significance in the Redskins rule: 
https://en.wikipedia.org/wiki/Redskins_Rule.

A simple solution is to apply a Bonferroni correction where alpha is divided by 
the number of comparisons. If there are 250, then 0.05/250 = 0.0002. Another 
approach is to try to discuss the outcomes in a way that makes sense. What is 
the connection between a football team's last home game an the election result 
that would enable me to take another team and apply their last home game result 
to the outcome of a different election?

Another complication is if variables x2 through x250 are themselves correlated. 
Not enough information was provided in the problem to know if this is an issue, 
but 250 orthogonal variables in a real dataset would be a bit unusual 
considering the experimentwise error rate previously mentioned.

Large datasets can be very messy.


Tim

-Original Message-
From: Bert Gunter 
Sent: Monday, August 22, 2022 12:07 PM
To: Ebert,Timothy Aaron 
Cc: Val ; r-help@R-project.org (r-help@r-project.org) 

Subject: Re: [R] Correlate

[External Email]

... But of course the p-values are essentially meaningless without some sort of 
multiplicity adjustment.
(search on "multiplicity adjustment" for details). :-(

-- Bert


On Mon, Aug 22, 2022 at 8:59 AM Ebert,Timothy Aaron  wrote:


A somewhat clunky solution:
for(i in colnames(dat)){
   print(cor.test(dat[,i], dat$x1, method = "pearson", use = 
"complete.obs")$estimate)
   print(cor.test(dat[,i], dat$x1, method = "pearson", use =
"complete.obs")$p.value) }


Because of missing data, this computes the correlations on different 
subsets of the data. A simple solution is to filter the data for NAs:


D <- na.omit(dat)

More comments below:



Rather than printing you could set up an array or list to save the results.


Tim

-Original Message-
From: R-help  On Behalf Of Val
Sent: Monday, August 22, 2022 11:09 AM
To: r-help@R-project.org (r-help@r-project.org) 
Subject: [R] Correlate

[External Email]

Hi all,

I have a data set with  ~250  variables(columns).  I want to calculate
the correlation of  one variable with the rest of the other variables
and also want  the p-values  for each correlation.  Please see the
sample data and my attempt.  I  have got the correlation but unable to
get the p-values

dat <- read.table(text="x1 x2 x3 x4
1.68 -0.96 -1.25  0.61
   -0.06  0.41  0.06 -0.96
   .0.08  1.14  1.42
0.80 -0.67  0.53 -0.68
0.23 -0.97 -1.18 -0.78
   -1.03  1.11 -0.61.
2.15 .0.02  0.66
0.35 -0.37 -0.26  0.39
   -0.66  0.89   .-1.49
0.11  1.52  0.73  -1.03",header=TRUE)

#change all to numeric
 dat[] <- lapply(dat, function(x) as.numeric(as.character(x)))


This data manipulation is unnecessary. Just specify the argument 
na.strings="." to read.table().




 data_cor <- cor(dat[ , colnames(dat) != "x1"],  dat$x1, method =
"pearson", use = "complete.obs")

Result
   [,1]
x2 -0.5845835
x3 -0.4664220
x4  0.7202837

How do I get the p-values ?


Taking a somewhat different approach from cor.test(), you can apply 
Fisher's z-transformation (recall that D is the data filtered for NAs):


> 2*pnorm(abs(atanh(data_cor)), sd=1/sqrt(nrow(D) - 3), lower.tail=FALSE)
[,1]
x2 0.2462807
x3 0.3812854
x4 0.1156939

I hope

Re: [R] Correlate

2022-08-22 Thread Val
For the time being  I am assuming the relationship across  variables
is linear.  I want get the values first  and detailed examining  of
the relationship will follow later.

On Mon, Aug 22, 2022 at 12:23 PM Ebert,Timothy Aaron  wrote:
>
> I (maybe) agree, but I would go further than that. There are assumptions 
> associated with the test that are missing. It is not clear that the 
> relationships are all linear. Regardless of a "significant outcome" all of 
> the relationships need to be explored in more detail than what is provided in 
> the correlation test.
>
> Multiplicity adjustment as in : 
> https://www.sciencedirect.com/science/article/pii/S019724561069 is not an 
> issue that I can see in these data from the information provided. At least 
> not in the same sense as used in the link.
>
> My first guess at the meaning of "multiplicity adjustment" was closer to the 
> experimentwise error rate in a multiple comparison procedure. 
> https://dictionary.apa.org/experiment-wise-error-rateEssentially, the type 1 
> error rate is inflated the more test you do and if you perform enough tests 
> you find significant outcomes by chance alone. There is great significance in 
> the Redskins rule: https://en.wikipedia.org/wiki/Redskins_Rule.
>
> A simple solution is to apply a Bonferroni correction where alpha is divided 
> by the number of comparisons. If there are 250, then 0.05/250 = 0.0002. 
> Another approach is to try to discuss the outcomes in a way that makes sense. 
> What is the connection between a football team's last home game an the 
> election result that would enable me to take another team and apply their 
> last home game result to the outcome of a different election?
>
> Another complication is if variables x2 through x250 are themselves 
> correlated. Not enough information was provided in the problem to know if 
> this is an issue, but 250 orthogonal variables in a real dataset would be a 
> bit unusual considering the experimentwise error rate previously mentioned.
>
> Large datasets can be very messy.
>
>
> Tim
>
> -Original Message-
> From: Bert Gunter 
> Sent: Monday, August 22, 2022 12:07 PM
> To: Ebert,Timothy Aaron 
> Cc: Val ; r-help@R-project.org (r-help@r-project.org) 
> 
> Subject: Re: [R] Correlate
>
> [External Email]
>
> ... But of course the p-values are essentially meaningless without some sort 
> of multiplicity adjustment.
> (search on "multiplicity adjustment" for details). :-(
>
> -- Bert
>
>
> On Mon, Aug 22, 2022 at 8:59 AM Ebert,Timothy Aaron  wrote:
> >
> > A somewhat clunky solution:
> > for(i in colnames(dat)){
> >   print(cor.test(dat[,i], dat$x1, method = "pearson", use = 
> > "complete.obs")$estimate)
> >   print(cor.test(dat[,i], dat$x1, method = "pearson", use =
> > "complete.obs")$p.value) }
> >
> > Rather than printing you could set up an array or list to save the results.
> >
> >
> > Tim
> >
> > -Original Message-
> > From: R-help  On Behalf Of Val
> > Sent: Monday, August 22, 2022 11:09 AM
> > To: r-help@R-project.org (r-help@r-project.org) 
> > Subject: [R] Correlate
> >
> > [External Email]
> >
> > Hi all,
> >
> > I have a data set with  ~250  variables(columns).  I want to calculate
> > the correlation of  one variable with the rest of the other variables
> > and also want  the p-values  for each correlation.  Please see the
> > sample data and my attempt.  I  have got the correlation but unable to
> > get the p-values
> >
> > dat <- read.table(text="x1 x2 x3 x4
> >1.68 -0.96 -1.25  0.61
> >   -0.06  0.41  0.06 -0.96
> >   .0.08  1.14  1.42
> >0.80 -0.67  0.53 -0.68
> >0.23 -0.97 -1.18 -0.78
> >   -1.03  1.11 -0.61.
> >2.15 .0.02  0.66
> >0.35 -0.37 -0.26  0.39
> >   -0.66  0.89   .-1.49
> >0.11  1.52  0.73  -1.03",header=TRUE)
> >
> > #change all to numeric
> > dat[] <- lapply(dat, function(x) as.numeric(as.character(x)))
> >
> > data_cor <- cor(dat[ , colnames(dat) != "x1"],  dat$x1, method =
> > "pearson", use = "complete.obs")
> >
> > Result
> >   [,1]
> > x2 -0.5845835
> > x3 -0.4664220
> > x4  0.7202837
> >
> > How do I get the p-values ?
> >
> > Thank you,
> >
> > __
> > R-help@r-project.org mailing list -- T

Re: [R] Correlate

2022-08-22 Thread Ebert,Timothy Aaron
I (maybe) agree, but I would go further than that. There are assumptions 
associated with the test that are missing. It is not clear that the 
relationships are all linear. Regardless of a "significant outcome" all of the 
relationships need to be explored in more detail than what is provided in the 
correlation test.

Multiplicity adjustment as in : 
https://www.sciencedirect.com/science/article/pii/S019724561069 is not an 
issue that I can see in these data from the information provided. At least not 
in the same sense as used in the link.

My first guess at the meaning of "multiplicity adjustment" was closer to the 
experimentwise error rate in a multiple comparison procedure. 
https://dictionary.apa.org/experiment-wise-error-rateEssentially, the type 1 
error rate is inflated the more test you do and if you perform enough tests you 
find significant outcomes by chance alone. There is great significance in the 
Redskins rule: https://en.wikipedia.org/wiki/Redskins_Rule.

A simple solution is to apply a Bonferroni correction where alpha is divided by 
the number of comparisons. If there are 250, then 0.05/250 = 0.0002. Another 
approach is to try to discuss the outcomes in a way that makes sense. What is 
the connection between a football team's last home game an the election result 
that would enable me to take another team and apply their last home game result 
to the outcome of a different election?

Another complication is if variables x2 through x250 are themselves correlated. 
Not enough information was provided in the problem to know if this is an issue, 
but 250 orthogonal variables in a real dataset would be a bit unusual 
considering the experimentwise error rate previously mentioned.

Large datasets can be very messy.


Tim

-Original Message-
From: Bert Gunter  
Sent: Monday, August 22, 2022 12:07 PM
To: Ebert,Timothy Aaron 
Cc: Val ; r-help@R-project.org (r-help@r-project.org) 

Subject: Re: [R] Correlate

[External Email]

... But of course the p-values are essentially meaningless without some sort of 
multiplicity adjustment.
(search on "multiplicity adjustment" for details). :-(

-- Bert


On Mon, Aug 22, 2022 at 8:59 AM Ebert,Timothy Aaron  wrote:
>
> A somewhat clunky solution:
> for(i in colnames(dat)){
>   print(cor.test(dat[,i], dat$x1, method = "pearson", use = 
> "complete.obs")$estimate)
>   print(cor.test(dat[,i], dat$x1, method = "pearson", use = 
> "complete.obs")$p.value) }
>
> Rather than printing you could set up an array or list to save the results.
>
>
> Tim
>
> -Original Message-
> From: R-help  On Behalf Of Val
> Sent: Monday, August 22, 2022 11:09 AM
> To: r-help@R-project.org (r-help@r-project.org) 
> Subject: [R] Correlate
>
> [External Email]
>
> Hi all,
>
> I have a data set with  ~250  variables(columns).  I want to calculate 
> the correlation of  one variable with the rest of the other variables 
> and also want  the p-values  for each correlation.  Please see the 
> sample data and my attempt.  I  have got the correlation but unable to 
> get the p-values
>
> dat <- read.table(text="x1 x2 x3 x4
>1.68 -0.96 -1.25  0.61
>   -0.06  0.41  0.06 -0.96
>   .0.08  1.14  1.42
>0.80 -0.67  0.53 -0.68
>0.23 -0.97 -1.18 -0.78
>   -1.03  1.11 -0.61.
>2.15 .0.02  0.66
>0.35 -0.37 -0.26  0.39
>   -0.66  0.89   .-1.49
>0.11  1.52  0.73  -1.03",header=TRUE)
>
> #change all to numeric
> dat[] <- lapply(dat, function(x) as.numeric(as.character(x)))
>
> data_cor <- cor(dat[ , colnames(dat) != "x1"],  dat$x1, method = 
> "pearson", use = "complete.obs")
>
> Result
>   [,1]
> x2 -0.5845835
> x3 -0.4664220
> x4  0.7202837
>
> How do I get the p-values ?
>
> Thank you,
>
> __
> R-help@r-project.org mailing list -- To UNSUBSCRIBE and more, see
> https://nam10.safelinks.protection.outlook.com/?url=https%3A%2F%2Fstat
> .ethz.ch%2Fmailman%2Flistinfo%2Fr-help&data=05%7C01%7Ctebert%40ufl
> .edu%7C871d5009dd3c455f398f08da84585e4a%7C0d4da0f84a314d76ace60a62331e
> 1b84%7C0%7C0%7C637967812337328788%7CUnknown%7CTWFpbGZsb3d8eyJWIjoiMC4w
> LjAwMDAiLCJQIjoiV2luMzIiLCJBTiI6Ik1haWwiLCJXVCI6Mn0%3D%7C3000%7C%7C%7C
> &sdata=3iAfMs1QzQARKF3lqUI8s43PX4IIkgEuQ9PUDyUtpqY%3D&reserved
> =0 PLEASE do read the posting guide 
> https://nam10.safelinks.protection.outlook.com/?url=http%3A%2F%2Fwww.r
> -project.org%2Fposting-guide.html&data=05%7C01%7Ctebert%40ufl.edu%
> 7C871d5009dd3c455f398f08da84585e4a%7C0d4da0f84a314d76ace60a62331e1b84%
> 7C0%7C0%7C637967812

Re: [R] Correlate rows of 2 matrices

2013-09-23 Thread arun
Hi Ira,

I tried the ?lapply().  Looks like it edges the ?for() loop.
For e.g.
 

set.seed(435)
m1 <- matrix(rnorm(2000*30), ncol=30)
m2 <-  matrix(rnorm(2000*30), ncol= 30)
 corsP<-vector()
  
 system.time({for(i in 1:2000) corsP[i] =  cor(m1[i,], m2[i,])})
 # user  system elapsed 
 # 0.124   0.000   0.122 
system.time({corsP2<- unlist(lapply(1:2000,function(i) cor(m1[i,],m2[i,])))})
# user  system elapsed 
# 0.108   0.000   0.110 
identical(corsP,corsP2)
#[1] TRUE


system.time(corsP3<- diag(cor(t(m1),t(m2
#  user  system elapsed 
#  0.272   0.004   0.276 



mNew<- rbind(m1,m2)
 indx<-rep(seq(nrow(mNew)/2),2)
system.time({corsP4<- tapply(seq_along(indx),list(indx),FUN=function(x) 
cor(t(mNew[x,]),t(mNew[x,]))[2])})
#   user  system elapsed 
#  0.156   0.000   0.160 
attr(corsP4,"dimnames")<- NULL
all.equal(corsP,as.vector(corsP4))
#[1] TRUE


A.K.



From: Ira Sharenow 
To: arun  
Sent: Monday, September 23, 2013 5:45 PM
Subject: Re: Correlate rows of 2 matrices



Arun,

What department are you in? Are you on LinkedIn?

The loop takes about a second. I do not know how to use lapply/sapply with more 
than one object and a function of two variables such as cor().

When there are 2,000 columns it cannot be right to compute 4,000,000 
correlations in order to use the 2,000 that are along the diagonal.

Ira 
On 9/23/2013 2:12 PM, arun wrote:

Ira, I work as a postdoc at Wayne State Univ. in Detroit. I didn't check the 
speed of ?diag().  It could be a bit slower because it first computes the whole 
correlation and then take the diagonal elements.  In that respect, loop will 
save the time.  Would be worth checking whether ?lapply() improves the speed 
compared to ?for(). Arun   
From: Ira Sharenow  To: arun  
Sent: Monday, September 23, 2013 4:42 PM
Subject: Re: Correlate rows of 2 matrices Arun, On a contract, I work for this 
San Francisco firm. But I work from home. 
http://www.manifoldpartners.com/Home.html How about yourself? Where are you 
located? Incidentally for my large matrix in addition to computing the pearson 
correlation matrix with use = "pairwise.complete.obs" (85 seconds), I also have 
to do spearman calculations. The code ran for 27 minutes. I only need about 
2000 correlations, but I am computing 2000* 2000 correlations. Using a loop 
reduced the time to about 1 second Please note that this initial data set is 
one of the smaller ones I will be working on. Ira 
On 9/23/2013 11:54 AM, arun wrote: Hi Ira,
Glad it worked for you. I would also choose the one you selected.  
BTW, where do you work?
Regards,
Arun 
From: Ira Sharenow  To: arun  
Sent: Monday, September 23, 2013 2:47 PM
Subject: Re: Correlate rows of 2 matrices Arun, Thanks for your help. I am very 
impressed with your ability to string together functions in order to achieve a 
desired result. On the other hand I prefer simplicity and I will have to 
explain my code to my boss who might have to eventually modify my code after 
I’ve moved on. I decided to go with your first option. It worked quite well.
diag(cor(t(m1),t(m2))) Thanks again. Ira 
On 9/22/2013 6:57 PM, Ira Sharenow wrote: Arun, 
>  
>I have a new problem for you.  I have two data frames (or matrices) and row by 
>row I want to take the correlations. So if I have a 3 row by 10 column matrix, 
>I would produce 3 correlations. Is there a way to merge the matrices and then 
>use some sort of split? Ideas/solutions much appreciated. m1 = 
>matrix(rnorm(30), nrow = 3)
m2 = matrix(rnorm(30), nrow = 3) 
>set.seed(22) 
>m1 = matrix(rnorm(30), nrow = 3)
m2 = matrix(rnorm(30), nrow = 3)
for(i in 1:3) corsP[i] =  cor(m1[i,], m2[i,])
corsP 
>[1] -0.50865019 -0.27760046  0.01423144 
>Thanks. Ira         

__
R-help@r-project.org 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] Correlate rows of 2 matrices

2013-09-22 Thread arun
Hi,
You may try:
set.seed(49)
m1 = matrix(rnorm(30), nrow = 3)
m2 = matrix(rnorm(30), nrow = 3)
 corsP<-vector()
  for(i in 1:3) corsP[i] =  cor(m1[i,], m2[i,])
 corsP
#[1]  0.58411274 -0.02382329  0.03760757

diag(cor(t(m1),t(m2)))
#[1]  0.58411274 -0.02382329  0.03760757

#or
mNew<- rbind(m1,m2)
 indx<-rep(seq(nrow(mNew)/2),2)
 sapply(split(seq_len(nrow(mNew)),indx),function(x) 
cor(t(mNew[x,]),t(mNew[x,]))[2])
 # 1   2   3 
 #0.58411274 -0.02382329  0.03760757 
#or
tapply(seq_along(indx),list(indx),FUN=function(x) 
cor(t(mNew[x,]),t(mNew[x,]))[2])
 # 1   2   3 
 #0.58411274 -0.02382329  0.03760757 
A.K.







From: Ira Sharenow 
To: arun  
Sent: Sunday, September 22, 2013 9:57 PM
Subject: Correlate rows of 2 matrices



Arun,

I have a new problem for you. 

I have two data frames (or matrices) and row by row I want to take the 
correlations.

So if I have a 3 row by 10 column matrix, I would produce 3 correlations.

Is there a way to merge the matrices and then use some sort of split?

Ideas/solutions much appreciated.

m1 = matrix(rnorm(30), nrow = 3)
m2 = matrix(rnorm(30), nrow = 3)

> set.seed(22)
> m1 = matrix(rnorm(30), nrow = 3)
> m2 = matrix(rnorm(30), nrow = 3)
> for(i in 1:3) corsP[i] =  cor(m1[i,], m2[i,])
> corsP
[1] -0.50865019 -0.27760046  0.01423144

Thanks.

Ira  

__
R-help@r-project.org 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] Correlate two time series

2009-09-22 Thread milton ruser
Have you looked at ts() ?

On Tue, Sep 22, 2009 at 5:49 PM, Maggie  wrote:

> I was wondering how to correlate two time series in R? I have to
> plotted waver files I need to correlate to one another to see how well
> they align..
>
> Any guidance would be very much appreciated!
>
> __
> R-help@r-project.org 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.
>

[[alternative HTML version deleted]]

__
R-help@r-project.org 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.