Correlation matrix one side with significance

9 messages Options
Embed this post
Permalink
Martin Kaffanke

Correlation matrix one side with significance

Reply Threaded More More options
Print post
Permalink
Hi there!

In my case,

cor(d[1:20])

makes me a good correlation matrix.

Now I'd like to have it one sided, means only the left bottom side to be
printed (the others are the same) and I'd like to have * where the
p-value is lower than 0.05 and ** lower than 0.01.

How can I do this?

And another thing: Is there a way to output that table as a latex table?

Thanks,
Martin

--
Ihr Partner für Webdesign, Webapplikationen und Webspace.
http://www.roomandspace.com/
Martin Kaffanke +43 650 4514224


______________________________________________
[hidden email] 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.

signature.asc (196 bytes) Download Attachment
Henrique Dallazuanna

Re: Correlation matrix one side with significance

Reply Threaded More More options
Print post
Permalink
Try this:

On 05/03/2008, Martin Kaffanke <[hidden email]> wrote:

> Hi there!
>
>  In my case,
>
>  cor(d[1:20])
>
>  makes me a good correlation matrix.
>
>  Now I'd like to have it one sided, means only the left bottom side to be
>  printed (the others are the same) and I'd like to have * where the
>  p-value is lower than 0.05 and ** lower than 0.01.
>
>  How can I do this?

d <- matrix(rexp(16, 2), 4)
corr <- cor(d)
sign <- symnum(cor(d), cutpoints=c(0.05, 0.01), corr = T,
symbols=c("***", "**", "*"), abbr=T, diag=F)

noquote(mapply(function(x, y)paste(x, format(y, dig=3), sep=''),
as.data.frame(unclass(sign)), as.data.frame(corr)))



>  And another thing: Is there a way to output that table as a latex table?

See ?latex function in Hmisc package and also xtable package

>  Thanks,
>  Martin
>
>
>  --
>  Ihr Partner für Webdesign, Webapplikationen und Webspace.
>  http://www.roomandspace.com/
>  Martin Kaffanke +43 650 4514224
>
> ______________________________________________
>  [hidden email] 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.
>
>
>


--
Henrique Dallazuanna
Curitiba-Paraná-Brasil
25° 25' 40" S 49° 16' 22" O

______________________________________________
[hidden email] 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.
Martin Kaffanke

Re: Correlation matrix one side with significance

Reply Threaded More More options
Print post
Permalink

Am Mittwoch, den 05.03.2008, 14:38 -0300 schrieb Henrique Dallazuanna:

> Try this:
>
> On 05/03/2008, Martin Kaffanke <[hidden email]> wrote:
> > Hi there!
> >
> >  In my case,
> >
> >  cor(d[1:20])
> >
> >  makes me a good correlation matrix.
> >
> >  Now I'd like to have it one sided, means only the left bottom side to be
> >  printed (the others are the same) and I'd like to have * where the
> >  p-value is lower than 0.05 and ** lower than 0.01.
> >
> >  How can I do this?
>
> d <- matrix(rexp(16, 2), 4)
> corr <- cor(d)
> sign <- symnum(cor(d), cutpoints=c(0.05, 0.01), corr = T,
> symbols=c("***", "**", "*"), abbr=T, diag=F)
>
> noquote(mapply(function(x, y)paste(x, format(y, dig=3), sep=''),
> as.data.frame(unclass(sign)), as.data.frame(corr)))
Seems that we mark the value itself, but not the p-value.

So lets say, in a way I have to get the lower left half of a

cor(el[1:20])

Then I need to calc all the values with a cor.test() to see for the
p-value.  And the p-value should be lower than .05 or .01 -> this should
make the * to the value.

Thanks,
Martin


______________________________________________
[hidden email] 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.

signature.asc (196 bytes) Download Attachment
Henrique Dallazuanna

Re: Correlation matrix one side with significance

Reply Threaded More More options
Print post
Permalink
Please provide a example of what you want

On 06/03/2008, Martin Kaffanke <[hidden email]> wrote:

>
>  Am Mittwoch, den 05.03.2008, 14:38 -0300 schrieb Henrique Dallazuanna:
>
> > Try this:
>  >
>  > On 05/03/2008, Martin Kaffanke <[hidden email]> wrote:
>  > > Hi there!
>  > >
>  > >  In my case,
>  > >
>  > >  cor(d[1:20])
>  > >
>  > >  makes me a good correlation matrix.
>  > >
>  > >  Now I'd like to have it one sided, means only the left bottom side to be
>  > >  printed (the others are the same) and I'd like to have * where the
>  > >  p-value is lower than 0.05 and ** lower than 0.01.
>  > >
>  > >  How can I do this?
>  >
>  > d <- matrix(rexp(16, 2), 4)
>  > corr <- cor(d)
>  > sign <- symnum(cor(d), cutpoints=c(0.05, 0.01), corr = T,
>  > symbols=c("***", "**", "*"), abbr=T, diag=F)
>  >
>  > noquote(mapply(function(x, y)paste(x, format(y, dig=3), sep=''),
>  > as.data.frame(unclass(sign)), as.data.frame(corr)))
>
>
> Seems that we mark the value itself, but not the p-value.
>
>  So lets say, in a way I have to get the lower left half of a
>
>  cor(el[1:20])
>
>  Then I need to calc all the values with a cor.test() to see for the
>  p-value.  And the p-value should be lower than .05 or .01 -> this should
>  make the * to the value.
>
>  Thanks,
>
> Martin
>
>


--
Henrique Dallazuanna
Curitiba-Paraná-Brasil
25° 25' 40" S 49° 16' 22" O

______________________________________________
[hidden email] 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.
Chuck Cleland

Re: Correlation matrix one side with significance

Reply Threaded More More options
Print post
Permalink
In reply to this post by Martin Kaffanke
On 3/6/2008 2:07 PM, Martin Kaffanke wrote:

> Am Mittwoch, den 05.03.2008, 14:38 -0300 schrieb Henrique Dallazuanna:
>> Try this:
>>
>> On 05/03/2008, Martin Kaffanke <[hidden email]> wrote:
>>> Hi there!
>>>
>>>  In my case,
>>>
>>>  cor(d[1:20])
>>>
>>>  makes me a good correlation matrix.
>>>
>>>  Now I'd like to have it one sided, means only the left bottom side to be
>>>  printed (the others are the same) and I'd like to have * where the
>>>  p-value is lower than 0.05 and ** lower than 0.01.
>>>
>>>  How can I do this?
>> d <- matrix(rexp(16, 2), 4)
>> corr <- cor(d)
>> sign <- symnum(cor(d), cutpoints=c(0.05, 0.01), corr = T,
>> symbols=c("***", "**", "*"), abbr=T, diag=F)
>>
>> noquote(mapply(function(x, y)paste(x, format(y, dig=3), sep=''),
>> as.data.frame(unclass(sign)), as.data.frame(corr)))
>
> Seems that we mark the value itself, but not the p-value.
>
> So lets say, in a way I have to get the lower left half of a
>
> cor(el[1:20])
>
> Then I need to calc all the values with a cor.test() to see for the
> p-value.  And the p-value should be lower than .05 or .01 -> this should
> make the * to the value.
>
> Thanks,
> Martin

   Do you want something like the following, but with the upper triangle
removed?

corstars <- function(x){
require(Hmisc)
x <- as.matrix(x)
R <- rcorr(x)$r
p <- rcorr(x)$P
mystars <- ifelse(p < .01, "**|", ifelse(p < .05, "* |", "  |"))
R <- format(round(cbind(rep(-1.111, ncol(x)), R), 3))[,-1]
Rnew <- matrix(paste(R, mystars, sep=""), ncol=ncol(x))
diag(Rnew) <- paste(diag(R), "  |", sep="")
rownames(Rnew) <- colnames(x)
colnames(Rnew) <- paste(colnames(x), "|", sep="")
Rnew <- as.data.frame(Rnew)
return(Rnew)
}

corstars(swiss[,1:4])
             Fertility| Agriculture| Examination| Education|
Fertility     1.000  |     0.353* |    -0.646**|  -0.664**|
Agriculture   0.353* |     1.000  |    -0.687**|  -0.640**|
Examination  -0.646**|    -0.687**|     1.000  |   0.698**|
Education    -0.664**|    -0.640**|     0.698**|   1.000  |

   I will leave the removing the upper triangle part to you - should be
examples in the archives.

> ------------------------------------------------------------------------
>
> ______________________________________________
> [hidden email] 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.

--
Chuck Cleland, Ph.D.
NDRI, Inc.
71 West 23rd Street, 8th floor
New York, NY 10010
tel: (212) 845-4495 (Tu, Th)
tel: (732) 512-0171 (M, W, F)
fax: (917) 438-0894

______________________________________________
[hidden email] 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.
Martin Kaffanke

Re: Correlation matrix one side with significance

Reply Threaded More More options
Print post
Permalink
Thank you, thats really good and gives me very good information.

Thanks,
Martin

Am Donnerstag, den 06.03.2008, 14:35 -0500 schrieb Chuck Cleland:

> On 3/6/2008 2:07 PM, Martin Kaffanke wrote:
> > Am Mittwoch, den 05.03.2008, 14:38 -0300 schrieb Henrique Dallazuanna:
> >> Try this:
> >>
> >> On 05/03/2008, Martin Kaffanke <[hidden email]> wrote:
> >>> Hi there!
> >>>
> >>>  In my case,
> >>>
> >>>  cor(d[1:20])
> >>>
> >>>  makes me a good correlation matrix.
> >>>
> >>>  Now I'd like to have it one sided, means only the left bottom side to be
> >>>  printed (the others are the same) and I'd like to have * where the
> >>>  p-value is lower than 0.05 and ** lower than 0.01.
> >>>
> >>>  How can I do this?
> >> d <- matrix(rexp(16, 2), 4)
> >> corr <- cor(d)
> >> sign <- symnum(cor(d), cutpoints=c(0.05, 0.01), corr = T,
> >> symbols=c("***", "**", "*"), abbr=T, diag=F)
> >>
> >> noquote(mapply(function(x, y)paste(x, format(y, dig=3), sep=''),
> >> as.data.frame(unclass(sign)), as.data.frame(corr)))
> >
> > Seems that we mark the value itself, but not the p-value.
> >
> > So lets say, in a way I have to get the lower left half of a
> >
> > cor(el[1:20])
> >
> > Then I need to calc all the values with a cor.test() to see for the
> > p-value.  And the p-value should be lower than .05 or .01 -> this should
> > make the * to the value.
> >
> > Thanks,
> > Martin
>
>    Do you want something like the following, but with the upper triangle
> removed?
>
> corstars <- function(x){
> require(Hmisc)
> x <- as.matrix(x)
> R <- rcorr(x)$r
> p <- rcorr(x)$P
> mystars <- ifelse(p < .01, "**|", ifelse(p < .05, "* |", "  |"))
> R <- format(round(cbind(rep(-1.111, ncol(x)), R), 3))[,-1]
> Rnew <- matrix(paste(R, mystars, sep=""), ncol=ncol(x))
> diag(Rnew) <- paste(diag(R), "  |", sep="")
> rownames(Rnew) <- colnames(x)
> colnames(Rnew) <- paste(colnames(x), "|", sep="")
> Rnew <- as.data.frame(Rnew)
> return(Rnew)
> }
>
> corstars(swiss[,1:4])
>              Fertility| Agriculture| Examination| Education|
> Fertility     1.000  |     0.353* |    -0.646**|  -0.664**|
> Agriculture   0.353* |     1.000  |    -0.687**|  -0.640**|
> Examination  -0.646**|    -0.687**|     1.000  |   0.698**|
> Education    -0.664**|    -0.640**|     0.698**|   1.000  |
>
>    I will leave the removing the upper triangle part to you - should be
> examples in the archives.
>
> > ------------------------------------------------------------------------
> >
> > ______________________________________________
> > [hidden email] 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.
>
--
Ihr Partner für Webdesign, Webapplikationen und Webspace.
http://www.roomandspace.com/
Martin Kaffanke +43 650 4514224


______________________________________________
[hidden email] 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.

signature.asc (196 bytes) Download Attachment
Gabor Grothendieck

Re: Correlation matrix one side with significance

Reply Threaded More More options
Print post
Permalink
If your purpose is simply to represent a correlation matrix it in a more
compact way see ?symnum, the corrgram package and an example in the
book Multivariate Data Visualization (regarding which gives a lattice
implementation).

On Fri, Mar 7, 2008 at 2:15 PM, Martin Kaffanke
<[hidden email]> wrote:

> Thank you, thats really good and gives me very good information.
>
> Thanks,
> Martin
>
> Am Donnerstag, den 06.03.2008, 14:35 -0500 schrieb Chuck Cleland:
>> On 3/6/2008 2:07 PM, Martin Kaffanke wrote:
>> > Am Mittwoch, den 05.03.2008, 14:38 -0300 schrieb Henrique Dallazuanna:
>> >> Try this:
>> >>
>> >> On 05/03/2008, Martin Kaffanke <[hidden email]> wrote:
>> >>> Hi there!
>> >>>
>> >>>  In my case,
>> >>>
>> >>>  cor(d[1:20])
>> >>>
>> >>>  makes me a good correlation matrix.
>> >>>
>> >>>  Now I'd like to have it one sided, means only the left bottom side to be
>> >>>  printed (the others are the same) and I'd like to have * where the
>> >>>  p-value is lower than 0.05 and ** lower than 0.01.
>> >>>
>> >>>  How can I do this?
>> >> d <- matrix(rexp(16, 2), 4)
>> >> corr <- cor(d)
>> >> sign <- symnum(cor(d), cutpoints=c(0.05, 0.01), corr = T,
>> >> symbols=c("***", "**", "*"), abbr=T, diag=F)
>> >>
>> >> noquote(mapply(function(x, y)paste(x, format(y, dig=3), sep=''),
>> >> as.data.frame(unclass(sign)), as.data.frame(corr)))
>> >
>> > Seems that we mark the value itself, but not the p-value.
>> >
>> > So lets say, in a way I have to get the lower left half of a
>> >
>> > cor(el[1:20])
>> >
>> > Then I need to calc all the values with a cor.test() to see for the
>> > p-value.  And the p-value should be lower than .05 or .01 -> this should
>> > make the * to the value.
>> >
>> > Thanks,
>> > Martin
>>
>>    Do you want something like the following, but with the upper triangle
>> removed?
>>
>> corstars <- function(x){
>> require(Hmisc)
>> x <- as.matrix(x)
>> R <- rcorr(x)$r
>> p <- rcorr(x)$P
>> mystars <- ifelse(p < .01, "**|", ifelse(p < .05, "* |", "  |"))
>> R <- format(round(cbind(rep(-1.111, ncol(x)), R), 3))[,-1]
>> Rnew <- matrix(paste(R, mystars, sep=""), ncol=ncol(x))
>> diag(Rnew) <- paste(diag(R), "  |", sep="")
>> rownames(Rnew) <- colnames(x)
>> colnames(Rnew) <- paste(colnames(x), "|", sep="")
>> Rnew <- as.data.frame(Rnew)
>> return(Rnew)
>> }
>>
>> corstars(swiss[,1:4])
>>              Fertility| Agriculture| Examination| Education|
>> Fertility     1.000  |     0.353* |    -0.646**|  -0.664**|
>> Agriculture   0.353* |     1.000  |    -0.687**|  -0.640**|
>> Examination  -0.646**|    -0.687**|     1.000  |   0.698**|
>> Education    -0.664**|    -0.640**|     0.698**|   1.000  |
>>
>>    I will leave the removing the upper triangle part to you - should be
>> examples in the archives.
>>
>> > ------------------------------------------------------------------------
>> >
>> > ______________________________________________
>> > [hidden email] 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.
>>
> --
> Ihr Partner für Webdesign, Webapplikationen und Webspace.
> http://www.roomandspace.com/
> Martin Kaffanke +43 650 4514224
>
> ______________________________________________
> [hidden email] 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.
>
>

______________________________________________
[hidden email] 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.
Liviu Andronic

Re: Correlation matrix one side with significance

Reply Threaded More More options
Print post
Permalink
In reply to this post by Martin Kaffanke
On 3/5/08, Martin Kaffanke <[hidden email]> wrote:
>  Now I'd like to have it one sided, means only the left bottom side to be
>  printed (the others are the same) and I'd like to have * where the
>  p-value is lower than 0.05 and ** lower than 0.01.

Look here [1], at "Visualizing Correlations". You might find
interesting the example of a plotted correlation matrix.

Liviu

[1] http://www.statmethods.net/stats/correlations.html

______________________________________________
[hidden email] 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.
Kingsford Jones

Re: Correlation matrix one side with significance

Reply Threaded More More options
Print post
Permalink
On the topic of visualizing correlation, see also

Murdoch, D.J. and Chow, E.D. (1996). A graphical display of large
correlation matrices.
The American Statistician 50, 178-180.

with examples here:

# install.packages('ellipse')
example(plotcorr, package='ellipse')



On Sat, Mar 8, 2008 at 3:01 AM, Liviu Andronic <[hidden email]> wrote:

> On 3/5/08, Martin Kaffanke <[hidden email]> wrote:
>>  Now I'd like to have it one sided, means only the left bottom side to be
>>  printed (the others are the same) and I'd like to have * where the
>>  p-value is lower than 0.05 and ** lower than 0.01.
>
> Look here [1], at "Visualizing Correlations". You might find
> interesting the example of a plotted correlation matrix.
>
> Liviu
>
> [1] http://www.statmethods.net/stats/correlations.html
>
> ______________________________________________
> [hidden email] 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.
>

______________________________________________
[hidden email] 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.