Skip to content

Instantly share code, notes, and snippets.

@guidocor
Created July 27, 2016 10:55
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save guidocor/8c7772b54519ee61aae90091232987e7 to your computer and use it in GitHub Desktop.
Save guidocor/8c7772b54519ee61aae90091232987e7 to your computer and use it in GitHub Desktop.
# helper function that makes a correlation
# Computes correlation between x and y and returns a list with the
# correlation object and the text as APA states to report it
# Also, the function prints the result, you can change this behavior with silent = FALSE
# Correlation reporting in APA Style: http://my.ilstu.edu/~jhkahn/apastats.html
# Brackets of CI in APA Style: http://blog.apastyle.org/apastyle/2010/06/formatting-statistics-using-brackets.html
correlation <- function(x,y, silent = TRUE, my.method="pearson") {
obj <- cor.test(x,y,method = my.method )
apa <- paste0("r(", round(obj$parameter, 2) ,") = ", round(obj$estimate,2) , ", p < ", round(obj$p.value, 4) ,
", 95% CI ","[",round(obj$conf.int[1], 2), ", ", round(obj$conf.int[2], 2), "]" )
if(silent == FALSE) cat(apa)
return(list(cor = obj, text = apa))
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment