Dear StackOverflow Community:
I am trying to make a matrix of p-values that corresponds to a matrix I have obtained by obtaining correlation values
My data is the following (just doing 5 rows for simplicity, my real data is 3 columns for each data frame with 50 rows).
FG_Smooth <- data.frame(FS_1 = c(0.43, 0.33, 3.47, 5.26, 1.09), FS2 = c(0.01, 0.02, 6.86, 3.27, 0.86), FS_3 = c(0.07, 0.36, 1.91, 5.61, 0.84), row.names = c("Group_3", "Thermo", "Embryophyta", "Flavo", "Cyclo"))
FMG_Smooth <- data.frame(GS_1 = c(1.13, 1.20, 0.52, 2.81, 0.70), GS_2 = c(1.18, 1.7, 0.42, 2.93, 0.78), GS_3 = c(1.17, 1.11, 0.60, 3.10, 0.87), row.names = c("Proline", "Trigonelline", "L-Lysine", "Nioctine", "Caffeate"))
library(Hmisc)
rcorr(t(FG_Smooth), t(FMG_Smooth), type = "pearson")
But I get this error:
Error in rcorr(t(FG_Smooth), t(FMG_Smooth), type = "pearson") : must have >4 observations
I only have 3 biological samples of each - so I am unable to use the rcorr
command that has been suggested mulitple time in multiple posts. The rcorr
command gives you 1) the matrix of correlations; and 2) p-values for the correlations.
So, to side-step this issue, I have ran the following: as suggested in other posts:
library(stats)
cor(t(FG_Smooth), t(FMG_Smooth), method = "pearson")
This works and gives a matrix of all of my correlations.
My next step is to find the p-values associated with each value in the correlation matrix. The function cor.test
only gives an overall p-value, which isn't what I need.
After perusing multiple posts - I ran across this one: rcorr() function for correlations
I followed directions to code given:
tblcols <- expand.grid(1:ncol(FG_Smooth), 1:ncol(FMG_Smooth))
cfunc <- function(var1, var2) {
cor.test(FG_Smooth[,var1], FMG_Smooth[,var2], method="pearson")
}
res <- mapply(function(a,b) {
cfunc(var1 = a, var2 = b)
}, tblcols$Var1, tblcols$Var2)
head(res)
[,1] [,2] [,3] [,4] [,5] [,6] [,7]
statistic 1.324125 -0.1022017 2.422883 0.9131595 -0.3509424 1.734178 1.53494
parameter 3 3 3 3 3 3 3
p.value 0.2773076 0.9250449 0.09392613 0.4284906 0.74883 0.1812997 0.2223626
estimate 0.6073388 -0.05890371 0.8135079 0.4663678 -0.1985814 0.7075406 0.663238
null.value 0 0 0 0 0 0 0
alternative "two.sided" "two.sided" "two.sided" "two.sided" "two.sided" "two.sided" "two.sided"
[,8] [,9]
statistic -0.009291327 2.880821
parameter 3 3
p.value 0.99317 0.06348644
estimate -0.005364273 0.8570256
null.value 0 0
alternative "two.sided" "two.sided"
This only gives me 9 p-values and not a matrix of p-values that corresponds to each correlation value obtained with the cor
command. For this example, it would be a 5x5 matrix of p-values, since the cor
command results in a 5x5 matrix of correlation values.
Is there a diff. way to do this?