2008-04-09

Beautiful Correlation Tables in R

I have achieved another victory in getting R to produce SPSS-like results. In experimental psychology, an analysis of measurement variable correlations is a common method in the course of a statistical analysis. Thus, I wanted R to produce a publication-quality output similar to SPSS: a correlation matrix of measurement variables that contains only the lower triangle of observations, where observations have two decimal digits and are flagged with stars (*, **, and ***) according to levels of statistical significance. However, as statmethods notices:
Unfortunately, neither cor( ) or cov( ) produce tests of significance, although you can use the cor.test( ) function to test a single correlation coefficient.
I did a little research and found this post on the R-help list. I modified Chuck Cleland's code a little so that the following command on the swiss data frame (provided in the Hmisc package) produces a beautiful output:

> corstarsl(swiss[,1:4])

Fertility Agriculture Examination
Fertility
Agriculture 0.35*
Examination -0.65*** -0.69***
Education -0.66*** -0.64*** 0.70***

If one employs the xtable package that produces LaTeX tables from within R, xtable(corstarsl(swiss[,1:4])) produces this:
Isn't that beautiful? I like it a lot. Here's the code (as I said, much of it taken from here):

corstarsl <- function(x){
require(Hmisc)
x <- as.matrix(x)
R <- rcorr(x)$r
p <- rcorr(x)$P

## define notions for significance levels; spacing is important.
mystars <- ifelse(p < .001, "***", ifelse(p < .01, "** ", ifelse(p < .05, "* ", " ")))

## trunctuate the matrix that holds the correlations to two decimal
R <- format(round(cbind(rep(-1.11, ncol(x)), R), 2))[,-1]

## build a new matrix that includes the correlations with their apropriate stars
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="")

## remove upper triangle
Rnew <- as.matrix(Rnew)
Rnew[upper.tri(Rnew, diag = TRUE)] <- ""
Rnew <- as.data.frame(Rnew)

## remove last column and return the matrix (which is now a data frame)
Rnew <- cbind(Rnew[1:length(Rnew)-1])
return(Rnew)
}

Labels: , , ,

2008-04-08

Parachute use to prevent death and major trauma related to gravitational challenge: systematic review of randomised controlled trials

Marvelous piece mocking exaggerated demands for randomized experimental designs. Some journal review editors should definitely read this.
"Objectives: To determine whether parachutes are effective in preventing major trauma related to gravitational challenge. ... It is a truth universally acknowledged that a medical intervention justified by observational data must be in want of verification through a randomised controlled trial. ... Results: We were unable to identify any randomised controlled trials of parachute intervention. Conclusions: As with many interventions intended to prevent ill health, the effectiveness of parachutes has not been subjected to rigorous evaluation by using randomised controlled trials. Advocates of evidence based medicine have criticised the adoption of interventions evaluated by using only observational data. We think that everyone might benefit if the most radical protagonists of evidence based medicine organized and participated in a double blind, randomised, placebo controlled, crossover trial of the parachute".

Labels:

2008-04-01

More Beautiful Error Bars in R

The rather complex structure and syntax of R (at least to the spoiled SPSS user that I am) comes with a steep learning curve but also with a huge profit: Flexibility. I managed to produce multiple clustered error bars in R today that come across better than a comparable SPSS output:
With regard to my experiment, the graph shows that despite the fact that an ANOVA does not deliver a significant interaction effect of microworld and participant gender, the effect of stereotype threat varies over different microworlds. FSYS produces the smallest gender effects and exhibits the smallest (and statistically insignificant) gender differences in the no stereotype threat condition.

With regard to R, two days of extensive reading and trial-and-error (and my sketchy previous knowledge) have enabled me to achieve almost all the graphical functionality I require (ANOVA interaction plots are next). Maybe R's learning curve isn't that steep after all. What I learned today: the use of the par() function for changing R's graphic output settings and using that to create a multiple figure environment that I then filled with three custom-generated error bars.

Labels: , , , , ,