So I have r and n values for a number of correlations. And I'd like to back-calculate the p value. I know there are tonnes of calculators online to do this easily, but I am looking for a way to do it in R.
Want to get p values given r and n for simple correlations.
r = -0.088227
, n = 237
p = 0.175832
I guess with correlation you mean a Pearson Product-Moment Correlation. If so you can write a function for this
rn_to_p <- function(rval, n){
# calculate r to t
tval <- rval/(sqrt((1-rval**2)/(n-2)))
# look up p value for t
pval <- 2*pt(abs(tval), n-2, lower=FALSE)
return(pval)}
Here is an example
# make it reproducible
set.seed(1)
# generate data
df <- data.frame(matrix(rnorm(200), ncol= 2))
# apply the function
rn_to_p(rval=cor.test(df$X1, df$X2)$estimate, n= nrow(df))
# 0.9921663
# see results of cor.test
cor.test(df$X1, df$X2)
# ... p-value = 0.9922 ...
For your example...
> rn_to_p(-0.088227, n = 237)
[1] 0.1758329
The formula to transform the Pearson's coefficient r to a t value can be found here, for example.