# Randomization test on correlation # The program will first read the data and calculate r. # Then it will calculate parametric estimates of 95% confidence limits # This assumes that the first column of data is ID # and columns 2 and 3 are X and Y data <- read.table(file.choose(), header = T) x <- data[,2] y <- data[,3] N <- length(x) r <- cor(x,y) ################### # Function to compute Fisher's rprime Fisher <- function(r) { rp <- 0.5*log(abs(1+r)/(1-r)) rp } ################## # Function to go from rprime to r rback <- function(rp) { rback <- (exp(rp/.5)-1)/(exp(rp/.5)+1) } ################## # CI on r CI <- function(rp,N) { CIlp <- rp - 1.96*sqrt(1/(N-3)) CIupp <- rp + 1.96*sqrt(1/(N-3)) CIlow <- rback(CIlp) CIup <- rback(CIupp) CInt <- c(CIlow, CIup) CInt # The following line works fine uless you source the file. #return(list(Lower.Conf.Limit.R = CIlow, Upper.Conf.Limit.R=CIup)) } rp <- Fisher(r) # Calculate and print the confidence interval ConfInt <- CI(rp,N) cat("The correlation is ",r, " on ", N-1, " df.", "\n") cat("The 95% confidence limits are ", "\n") print(ConfInt)