[R] display p-values and significance levels

Uwe Ligges ligges at statistik.tu-dortmund.de
Sun Feb 1 17:34:31 CET 2009



herwig wrote:
> Hi there,
> 
> I got a piece of code for the Iris data which allows to display correlation
> coefficients for each Iris species in the lower panel (color coded). I would
> now like to add e.g. a "*" to show the significance of each correlation next
> to the  correlation coefficient. 
> 
> Furthermore I would like to make a t.test between the species "setosa" and
> "versicolor" for Sepal.length, ..width..... and so on  and display it int
> the horizontal panel 
> 
> I would appreciate any help,
> 
> Herwig
> 
> here the code I've got at the moment:
> 
> panel.cor <- function(x, y, digits=2, prefix="", splitvar, col.cor, ...)
> 
> {
>    usr <- par("usr"); on.exit(par(usr))
>    par(usr = c(0, 1, 0, 1))
>    r <- abs(cor(x, y))
>    if(!missing(splitvar)) {
>       r <- c(r, abs(sapply(lapply(split(cbind.data.frame(x, y),
> splitvar), cor), function(x)x[1,2])))
>    }
>    txt <- format(c(r, 0.123456789), digits=digits)[1:4]
>    txt <- paste(prefix, txt, sep="")
>    if(missing(col.cor)) col.cor <- c("black", "red", "green3", "blue")
>    for(i in 1:length(txt)) {
>       text(0.5, (1/(length(txt)+1))*i, txt[i], col = col.cor[i])
>    }
>  }
> 
>  pairs(iris[1:4], main = "Anderson's Iris Data -- 3 species",
>      pch = 21, bg = c("red", "green3", "blue")[unclass(iris$Species)],
> lower.panel=panel.cor, splitvar=iris$Species)
> ------------
> #here the code I tried to implement to show the significance levels
> 
> test <- cor.test(x, y, ,use="complete.obs")
>     Signif <- symnum(test$p.value, corr = FALSE, na = FALSE,
>                   cutpoints = c(0, 0.001, 0.01, 0.05, 0.1, 1),
>                   symbols = c("***", "**", "*", ".", " "))



Not sure if this is homework...
first part of your question is implemented as follows (including some 
other code improvements), just use the same ideas for the t-tests ....


panel.cor <- function(x, y, digits=2, prefix="", splitvar, col.cor, ...)
{
     usr <- par(usr = c(0, 1, 0, 1))
     on.exit(par(usr))
     r <- abs(cor(x, y))
     test <- cor.test(x, y, use="complete.obs")[["p.value"]]
     if(!missing(splitvar)){
         temp <- split(data.frame(x=x, y=y), splitvar)
         r <- c(r, abs(sapply(temp, function(x) cor(x)[1,2])))
         test <- c(test, sapply(temp, function(x) cor.test(x$x, x$y, 
use="complete.obs")[["p.value"]]))
     }
     Signif <- symnum(test, corr = FALSE, na = FALSE,
         cutpoints = c(0, 0.001, 0.01, 0.05, 0.1, 1),
         symbols = c("***", "**", "*", ".", " "))
     txt <- format(r, nsmall = digits, digits = digits)
     txt <- paste(prefix, txt, " ", Signif, sep="")
     if(missing(col.cor))
         col.cor <- c("black", "red", "green3", "blue")
     text(0.5, seq_along(txt) / (length(txt)+1), txt, col = col.cor, 
adj=c(0, 0.5))
}

pairs(iris[1:4], main = "Anderson's Iris Data -- 3 species",
       pch = 21, bg = c("red", "green3", "blue")[unclass(iris$Species)],
       lower.panel=panel.cor, splitvar=iris$Species)

Uwe Ligges




More information about the R-help mailing list