I have two data matrices that I want to make the correlation between each column from data1 and each column from data 2 and also calculate the p-value Matrices dont have the same size and I tried such a script.> bg <- read.table (file.choose(), header=T, row.names) > bg > Otu00022 Otu00029 Otu00039 Otu00042 Otu00101 Otu00105 Otu00125 Otu00131 Otu00137 Otu00155 Otu00158 Otu00172 Otu00181 Otu00185 Otu00190 Otu00209 Otu00218 > Gi20Jun11 0.001217 0 0.001217 0 0.000000 0 0 0 0.001217 0 0 0 0 0 0.001217 0 0.001217 > Gi40Jun11 0.000000 0 0.000000 0 0.000000 0 0 0 0.000000 0 0 0 0 0 0.000000 0 0.000000 > Gi425Jun11 0.000000 0 0.000000 0 0.000000 0 0 0 0.000000 0 0 0 0 0 0.000000 0 0.000000 > Gi45Jun11 0.000000 0 0.000000 0 0.001513 0 0 0 0.000000 0 0 0 0 0 0.000000 0 0.000000 > Gi475Jun11 0.000000 0 0.000000 0 0.000000 0 0 0 0.000000 0 0 0 0 0 0.000000 0 0.000000 > Gi50Jun11 0.000000 0 0.000000 0 0.000000 0 0 0 0.000000 0 0 0 0 0 0.000000 0 0.000000ag <- read.table (file.choose(), header=T, row.names) for (i in 1:(ncol(bg))) for (j in 1:(ncol(ag))) print(c(i,j)) final_matrix <- matrix(rep("0",ncol(bg)*ncol(ag)),ncol=ncol(bg),nrow=ncol(ag)) cor <- cor.test(as.vector(as.matrix(bg[,i])),as.vector(as.matrix(ag[,j])), method="spearman") #but the output is not matrice with all the values but a single correlation value data: bg[, i] and ag[, j] t = 2.2992, df = 26, p-value = 0.02978 alternative hypothesis: true correlation is not equal to 0 95 percent confidence interval: 0.04485289 0.67986803 sample estimates: cor 0.4110515 # How I can creat an outfile with all the correlations and p-values? Thank you very much! ?zg?l
On 02/13/2013 08:48 PM, Ozgul Inceoglu wrote:> I have two data matrices that I want to make the correlation between each column from data1 and each column from data 2 and also calculate the p-value Matrices dont have the same size and I tried such a script. >> bg<- read.table (file.choose(), header=T, row.names) >> bg >> Otu00022 Otu00029 Otu00039 Otu00042 Otu00101 Otu00105 Otu00125 Otu00131 Otu00137 Otu00155 Otu00158 Otu00172 Otu00181 Otu00185 Otu00190 Otu00209 Otu00218 >> Gi20Jun11 0.001217 0 0.001217 0 0.000000 0 0 0 0.001217 0 0 0 0 0 0.001217 0 0.001217 >> Gi40Jun11 0.000000 0 0.000000 0 0.000000 0 0 0 0.000000 0 0 0 0 0 0.000000 0 0.000000 >> Gi425Jun11 0.000000 0 0.000000 0 0.000000 0 0 0 0.000000 0 0 0 0 0 0.000000 0 0.000000 >> Gi45Jun11 0.000000 0 0.000000 0 0.001513 0 0 0 0.000000 0 0 0 0 0 0.000000 0 0.000000 >> Gi475Jun11 0.000000 0 0.000000 0 0.000000 0 0 0 0.000000 0 0 0 0 0 0.000000 0 0.000000 >> Gi50Jun11 0.000000 0 0.000000 0 0.000000 0 0 0 0.000000 0 0 0 0 0 0.000000 0 0.000000 > ag<- read.table (file.choose(), header=T, row.names) > > for (i in 1:(ncol(bg))) > for (j in 1:(ncol(ag))) > print(c(i,j)) > final_matrix<- matrix(rep("0",ncol(bg)*ncol(ag)),ncol=ncol(bg),nrow=ncol(ag)) > > cor<- cor.test(as.vector(as.matrix(bg[,i])),as.vector(as.matrix(ag[,j])), method="spearman") > > #but the output is not matrice with all the values but a single correlation value > > data: bg[, i] and ag[, j] > t = 2.2992, df = 26, p-value = 0.02978 > alternative hypothesis: true correlation is not equal to 0 > 95 percent confidence interval: > 0.04485289 0.67986803 > sample estimates: > cor > 0.4110515 > > # How I can creat an outfile with all the correlations and p-values? >Hi Ozgul, As we don't know what the "ag" data frame looks like, I'll have to fake it. As your "bg" data frame is 6 rows with 17 columns, I'll make "ag" 6 rows with 3 columns. I'll also fake "bg" as I'm too lazy to write it all out. If "ag" differs in both rows and columns, you will have to pass use="pairwise.complete.obs" to the "cor.test" function. bg<-data.frame(Otu00022=rnorm(6),Otu00039=rnorm(6), Otu00042=rnorm(6),Otu00101=rnorm(6)) ag<-data.frame(col1=rnorm(6),col2=rnorm(6),col3=rnorm(6)) allcor<-function(data1,data2) { corlist<-list() for(i in 1:length(data1)) { for(j in 1:length(data2)) corlist[[length(data2)*(i-1)+j]]<-cor.test(data1[,i],data2[,j]) } return(corlist) } Jim
HI, #ag data was created bg<- as.matrix(read.table(text=" ???? Otu00022 Otu00029 Otu00039 Otu00042 Otu00101 Otu00105 Otu00125 Otu00131 Otu00137 Otu00155 Otu00158 Otu00172 Otu00181 Otu00185 Otu00190 Otu00209 Otu00218 Gi20Jun11? 0.001217??????? 0 0.001217??????? 0 0.000000??????? 0??????? 0??????? 0 0.001217??????? 0??????? 0??????? 0??????? 0??????? 0 0.001217??????? 0 0.001217 Gi40Jun11? 0.000000??????? 0 0.000000??????? 0 0.000000??????? 0??????? 0??????? 0 0.000000??????? 0??????? 0??????? 0??????? 0??????? 0 0.000000??????? 0 0.000000 Gi425Jun11 0.000000??????? 0 0.000000??????? 0 0.000000??????? 0??????? 0??????? 0 0.000000??????? 0??????? 0??????? 0??????? 0??????? 0 0.000000??????? 0 0.000000 Gi45Jun11? 0.000000??????? 0 0.000000??????? 0 0.001513??????? 0??????? 0??????? 0 0.000000??????? 0??????? 0??????? 0??????? 0??????? 0 0.000000??????? 0 0.000000 Gi475Jun11 0.000000??????? 0 0.000000??????? 0 0.000000??????? 0??????? 0??????? 0 0.000000??????? 0??????? 0??????? 0??????? 0??????? 0 0.000000??????? 0 0.000000 Gi50Jun11? 0.000000??????? 0 0.000000??????? 0 0.000000??????? 0??????? 0??????? 0 0.000000??????? 0??????? 0??????? 0??????? 0??????? 0 0.000000??????? 0 0.000000 ",sep="",header=TRUE,stringsAsFactors=F)) set.seed(128) ag<- matrix(rnorm(30),nrow=6) colnames(ag)<- paste("ag",1:5,sep="") bg_ag<-expand.grid(colnames(bg),colnames(ag),stringsAsFactors=FALSE) ?attr(bg_ag,"out.attrs")<- NULL ?library(Hmisc) #correlation resr<-do.call(rbind,lapply(split(bg_ag,1:nrow(bg_ag)),function(x) {res<-rcorr(cbind(bg[,x[,1]],ag[,x[,2]]),type="spearman")$r; row.names(res)<- rep(paste(x[1],x[2],sep="_"),2);res})) ?head(resr) #???????????????? [,1]????? [,2] #Otu00022_ag1 1.0000000 0.1309307 #Otu00022_ag1 0.1309307 1.0000000 #Otu00029_ag1 1.0000000?????? NaN #Otu00029_ag1?????? NaN 1.0000000 #Otu00039_ag1 1.0000000 0.1309307 #Otu00039_ag1 0.1309307 1.0000000 #p-values resP<-do.call(rbind,lapply(split(bg_ag,1:nrow(bg_ag)),function(x) {res<-rcorr(cbind(bg[,x[,1]],ag[,x[,2]]),type="spearman")$P; row.names(res)<- rep(paste(x[1],x[2],sep="_"),2);res})) ? head(resP) #????????????????? [,1]????? [,2] #Otu00022_ag1??????? NA 0.8047262 #Otu00022_ag1 0.8047262??????? NA #Otu00029_ag1??????? NA?????? NaN #Otu00029_ag1?????? NaN??????? NA #Otu00039_ag1??????? NA 0.8047262 #Otu00039_ag1 0.8047262??????? NA #If you need only the values indx<-row(resr)%%2!=1 ?resPnew<-as.matrix(resP[indx[,1],1]) ?resrnew<-as.matrix(resr[indx[,1],1]) head(resPnew) #????????????????? [,1] #Otu00022_ag1 0.8047262 #Otu00029_ag1?????? NaN #Otu00039_ag1 0.8047262 #Otu00042_ag1?????? NaN #Otu00101_ag1 0.1583024 #Otu00105_ag1?????? NaN head(resrnew) #?????????????????? [,1] #Otu00022_ag1? 0.1309307 #Otu00029_ag1??????? NaN #Otu00039_ag1? 0.1309307 #Otu00042_ag1??????? NaN #Otu00101_ag1 -0.6546537 #Otu00105_ag1??????? NaN A.K. ----- Original Message ----- From: Ozgul Inceoglu <Ozgul.Inceoglu at ulb.ac.be> To: r-help at r-project.org Cc: Sent: Wednesday, February 13, 2013 4:48 AM Subject: [R] spearman correlation and p-value as a matrix I have two data matrices that I want to make the correlation between each column from data1 and each column from data 2 and also calculate the p-value Matrices dont have the same size and I tried such a script.> bg <- read.table (file.choose(), header=T, row.names) > bg > Otu00022 Otu00029 Otu00039 Otu00042 Otu00101 Otu00105 Otu00125 Otu00131 Otu00137 Otu00155 Otu00158 Otu00172 Otu00181 Otu00185 Otu00190 Otu00209 Otu00218 > Gi20Jun11 0.001217 0 0.001217 0 0.000000 0 0 0 0.001217 0 0 0 0 0 0.001217 0 0.001217 > Gi40Jun11 0.000000 0 0.000000 0 0.000000 0 0 0 0.000000 0 0 0 0 0 0.000000 0 0.000000 > Gi425Jun11 0.000000 0 0.000000 0 0.000000 0 0 0 0.000000 0 0 0 0 0 0.000000 0 0.000000 > Gi45Jun11 0.000000 0 0.000000 0 0.001513 0 0 0 0.000000 0 0 0 0 0 0.000000 0 0.000000 > Gi475Jun11 0.000000 0 0.000000 0 0.000000 0 0 0 0.000000 0 0 0 0 0 0.000000 0 0.000000 > Gi50Jun11 0.000000 0 0.000000 0 0.000000 0 0 0 0.000000 0 0 0 0 0 0.000000 0 0.000000ag <- read.table (file.choose(), header=T, row.names) for (i in 1:(ncol(bg))) for (j in 1:(ncol(ag))) print(c(i,j)) final_matrix <- matrix(rep("0",ncol(bg)*ncol(ag)),ncol=ncol(bg),nrow=ncol(ag)) cor <- cor.test(as.vector(as.matrix(bg[,i])),as.vector(as.matrix(ag[,j])), method="spearman") #but the output is not matrice with all the values but a single correlation value data:? bg[, i] and ag[, j] t = 2.2992, df = 26, p-value = 0.02978 alternative hypothesis: true correlation is not equal to 0 95 percent confidence interval: 0.04485289 0.67986803 sample estimates: ? ? ? cor 0.4110515 # How I can creat an outfile with all the correlations and p-values? Thank you very much! ?zg?l ______________________________________________ R-help at r-project.org mailing list https://stat.ethz.ch/mailman/listinfo/r-help PLEASE do read the posting guide http://www.R-project.org/posting-guide.html and provide commented, minimal, self-contained, reproducible code.
Thank you all for the answers. I really need to learn a lot. Bu I also discover cor2m(x, y, trim = TRUE, alpha = 0.05) in ecodist package, which gives an output file with significant correlations. ?zg?l Yj>HI,> >#ag data was created >bg<- as.matrix(read.table(text=" >???? Otu00022 Otu00029 Otu00039 Otu00042 Otu00101 Otu00105 Otu00125 Otu00131 Otu00137 Otu00155 Otu00158 Otu00172 Otu00181 Otu00185 Otu00190 Otu00209 Otu00218 >Gi20Jun11? 0.001217??????? 0 0.001217??????? 0 0.000000??????? 0??????? 0??????? 0 0.001217??????? 0??????? 0??????? 0??????? 0??????? 0 0.001217??????? 0 0.001217 >Gi40Jun11? 0.000000??????? 0 0.000000??????? 0 0.000000??????? 0??????? 0??????? 0 0.000000??????? 0??????? 0??????? 0??????? 0??????? 0 0.000000??????? 0 0.000000 >Gi425Jun11 0.000000??????? 0 0.000000??????? 0 0.000000??????? 0??????? 0??????? 0 0.000000??????? 0??????? 0??????? 0??????? 0??????? 0 0.000000??????? 0 0.000000 >Gi45Jun11? 0.000000??????? 0 0.000000??????? 0 0.001513??????? 0??????? 0??????? 0 0.000000??????? 0??????? 0??????? 0??????? 0??????? 0 0.000000??????? 0 0.000000 >Gi475Jun11 0.000000??????? 0 0.000000??????? 0 0.000000??????? 0??????? 0??????? 0 0.000000??????? 0??????? 0??????? 0??????? 0??????? 0 0.000000??????? 0 0.000000 >Gi50Jun11? 0.000000??????? 0 0.000000??????? 0 0.000000??????? 0??????? 0??????? 0 0.000000??????? 0??????? 0??????? 0??????? 0??????? 0 0.000000??????? 0 0.000000 >",sep="",header=TRUE,stringsAsFactors=F)) >set.seed(128) >ag<- matrix(rnorm(30),nrow=6) >colnames(ag)<- paste("ag",1:5,sep="") >bg_ag<-expand.grid(colnames(bg),colnames(ag),stringsAsFactors=FALSE) >?attr(bg_ag,"out.attrs")<- NULL >?library(Hmisc) >#correlation >resr<-do.call(rbind,lapply(split(bg_ag,1:nrow(bg_ag)),function(x) {res<-rcorr(cbind(bg[,x[,1]],ag[,x[,2]]),type="spearman")$r; row.names(res)<- rep(paste(x[1],x[2],sep="_"),2);res})) >?head(resr) >#???????????????? [,1]????? [,2] >#Otu00022_ag1 1.0000000 0.1309307 >#Otu00022_ag1 0.1309307 1.0000000 >#Otu00029_ag1 1.0000000?????? NaN >#Otu00029_ag1?????? NaN 1.0000000 >#Otu00039_ag1 1.0000000 0.1309307 >#Otu00039_ag1 0.1309307 1.0000000 > >#p-values >resP<-do.call(rbind,lapply(split(bg_ag,1:nrow(bg_ag)),function(x) {res<-rcorr(cbind(bg[,x[,1]],ag[,x[,2]]),type="spearman")$P; row.names(res)<- rep(paste(x[1],x[2],sep="_"),2);res})) >? head(resP) >#????????????????? [,1]????? [,2] >#Otu00022_ag1??????? NA 0.8047262 >#Otu00022_ag1 0.8047262??????? NA >#Otu00029_ag1??????? NA?????? NaN >#Otu00029_ag1?????? NaN??????? NA >#Otu00039_ag1??????? NA 0.8047262 >#Otu00039_ag1 0.8047262??????? NA > >#If you need only the values >indx<-row(resr)%%2!=1 >?resPnew<-as.matrix(resP[indx[,1],1]) >?resrnew<-as.matrix(resr[indx[,1],1]) > >head(resPnew) >#????????????????? [,1] >#Otu00022_ag1 0.8047262 >#Otu00029_ag1?????? NaN >#Otu00039_ag1 0.8047262 >#Otu00042_ag1?????? NaN >#Otu00101_ag1 0.1583024 >#Otu00105_ag1?????? NaN > >head(resrnew) >#?????????????????? [,1] >#Otu00022_ag1? 0.1309307 >#Otu00029_ag1??????? NaN >#Otu00039_ag1? 0.1309307 >#Otu00042_ag1??????? NaN >#Otu00101_ag1 -0.6546537 >#Otu00105_ag1??????? NaN > >A.K. > > > > >----- Original Message ----- >From: Ozgul Inceoglu <Ozgul.Inceoglu at ulb.ac.be> >To: r-help at r-project.org >Cc: >Sent: Wednesday, February 13, 2013 4:48 AM >Subject: [R] spearman correlation and p-value as a matrix > >I have two data matrices that I want to make the correlation between each column from data1 and each column from data 2 and also calculate the p-value Matrices dont have the same size and I tried such a script. >> bg <- read.table (file.choose(), header=T, row.names) >> bg >> Otu00022 Otu00029 Otu00039 Otu00042 Otu00101 Otu00105 Otu00125 Otu00131 Otu00137 Otu00155 Otu00158 Otu00172 Otu00181 Otu00185 Otu00190 Otu00209 Otu00218 >> Gi20Jun11 0.001217 0 0.001217 0 0.000000 0 0 0 0.001217 0 0 0 0 0 0.001217 0 0.001217 >> Gi40Jun11 0.000000 0 0.000000 0 0.000000 0 0 0 0.000000 0 0 0 0 0 0.000000 0 0.000000 >> Gi425Jun11 0.000000 0 0.000000 0 0.000000 0 0 0 0.000000 0 0 0 0 0 0.000000 0 0.000000 >> Gi45Jun11 0.000000 0 0.000000 0 0.001513 0 0 0 0.000000 0 0 0 0 0 0.000000 0 0.000000 >> Gi475Jun11 0.000000 0 0.000000 0 0.000000 0 0 0 0.000000 0 0 0 0 0 0.000000 0 0.000000 >> Gi50Jun11 0.000000 0 0.000000 0 0.000000 0 0 0 0.000000 0 0 0 0 0 0.000000 0 0.000000 >ag <- read.table (file.choose(), header=T, row.names) > >for (i in 1:(ncol(bg))) >for (j in 1:(ncol(ag))) >print(c(i,j)) >final_matrix <- matrix(rep("0",ncol(bg)*ncol(ag)),ncol=ncol(bg),nrow=ncol(ag)) > >cor <- cor.test(as.vector(as.matrix(bg[,i])),as.vector(as.matrix(ag[,j])), method="spearman") > >#but the output is not matrice with all the values but a single correlation value > >data:? bg[, i] and ag[, j] >t = 2.2992, df = 26, p-value = 0.02978 >alternative hypothesis: true correlation is not equal to 0 >95 percent confidence interval: >0.04485289 0.67986803 >sample estimates: >? ? ? cor >0.4110515 > ># How I can creat an outfile with all the correlations and p-values? > >Thank you very much! >?zg?l > >______________________________________________ >R-help at r-project.org mailing list >https://stat.ethz.ch/mailman/listinfo/r-help >PLEASE do read the posting guide http://www.R-project.org/posting-guide.html >and provide commented, minimal, self-contained, reproducible code. > > >?zg?l Inceoglu, PhD. Universit? Libre de Bruxelles Ecology of Aquatic Systems Campus de la plaine CP221 Boulevard du triomphe 1050 Bruxelles Belgium
Dear Ozgul, You could use ?table() or ?count from library(plyr) I assume that you wanted to count only from the first column. ?data2<- read.csv("cor3.csv",sep="\t",stringsAsFactors=FALSE) ?res1<-as.data.frame(table(data2[,1])) res2<-count(data2[,1]) colnames(res1)<- colnames(res2) ?identical(res1,res2) #[1] TRUE A.K. ----- Original Message ----- From: Ozgul Inceoglu <Ozgul.Inceoglu at ulb.ac.be> To: arun <smartpink111 at yahoo.com> Cc: Sent: Thursday, February 14, 2013 6:17 AM Subject: re:Re: [R] spearman correlation and p-value as a matrix Dear Arun, I bothered you this week alot, but it seems like that you know your way in R very well. I would appreciate if you can help me with one more thing. I have 2 data files. One is the average value of each OTU. Second file has the correlation between each OTU, but I would like to plot how many times a OTU correlated against its relative abundance. As you see in data2 file, OTU0001 have around 53 correlations. Is it possible to count that in R with a script? Because my table is too long to count by eye. I saw some script to count numeric values but the column one is text. And I will plot data1$average data2$number of occurence (e.g.53) Thank you very much! ? data1 name??? average Otu0001??? 0.059348357 Otu0002??? 0.030641714 Otu0003??? 0.011322821 Otu0004??? 0.080583357 Otu0005??? 0.022009357 data2 Var1??? Var2??? Freq??? Otu0001??? Otu00002??? 0.50308393782931??? Otu0001??? Otu00003??? 0.835976014100782??? Otu0001??? Otu00021??? 0.913558114482734??? Otu0001??? Otu00027??? 0.905670284922677??? Otu0001??? Otu00046??? 0.581582392764371??? Otu0001??? Otu00047??? 0.867801609030349??? Otu0001??? Otu00057??? 0.678979275105813??? Otu0001??? Otu00066??? 0.665704636891185??? Otu0001??? Otu00083??? 0.573139564297305??? Otu0001??? Otu00084??? 0.654793315615399??? Otu0001??? Otu00114??? 0.545574076996571??? Otu0001??? Otu00133??? 0.860299149983324??? Otu0001??? Otu00149??? 0.565446769229162??? Otu0001??? Otu00154??? 0.651905880329591??? Otu0001??? Otu00165??? 0.588000078791058??? Otu0001??? Otu00199??? 0.636977290142002??? Otu0001??? Otu00242??? 0.733670340504493??? Otu0001??? Otu00265??? 0.56572800115343??? Otu0001??? Otu00271??? 0.52274494018959??? Otu0001??? Otu00290??? 0.565784159732485??? Otu0001??? Otu00498??? 0.69700879002542??? Otu0001??? Otu00543??? 0.688857846593924??? Otu0001??? Otu00619??? 0.568561351623871??? Otu0001??? Otu00646??? 0.710973182296609??? Otu0001??? Otu00915??? 0.67389659584841??? Otu0001??? Otu00949??? 0.658902464342605??? Otu0001??? Otu01035??? 0.730329506746752??? Otu0001??? Otu01327??? 0.680119572628299??? Otu0001??? Otu01631??? 0.537404605465553??? Otu0001??? Otu01638??? 0.624999817224687??? Otu0001??? Otu01642??? 0.572231019174333??? Otu0001??? Otu01649??? 0.551184564584919??? Otu0001??? Otu01651??? 0.600983572760604??? Otu0001??? Otu01655??? 0.522736661453493??? Otu0001??? Otu01657??? 0.756955802070428??? Otu0001??? Otu01669??? 0.540295474451908??? Otu0001??? Otu01678??? 0.628300552686822??? Otu0001??? Otu01691??? 0.527963252499515??? Otu0001??? Otu01711??? 0.58532121170348??? Otu0001??? Otu01722??? 0.521754446131501??? Otu0001??? Otu01729??? 0.626564045909824??? Otu0001??? Otu01745??? 0.685208099879848??? Otu0001??? Otu01748??? 0.533461010235307??? Otu0001??? Otu01776??? 0.708721334709103??? Otu0001??? Otu01795??? 0.774684206920168??? Otu0001??? Otu01802??? 0.515477574463339??? Otu0001??? Otu01828??? 0.595278507951326??? Otu0001??? Otu01836??? 0.521155845049491??? Otu0001??? Otu01856??? 0.597043163472699??? Otu0001??? Otu01897??? 0.510475231901277??? Otu0001??? Otu01946??? 0.514243028056249??? Otu0001??? Otu02289??? 0.547405617805684??? Otu0002??? Otu00003??? 0.694555195101993??? Otu0002??? Otu00021??? 0.851014016127985??? Otu0002??? Otu00027??? 0.846202663607256??? Otu0002??? Otu00039??? 0.512465964406187??? Otu0002??? Otu00047??? 0.730301483589089??? Otu0002??? Otu00057??? 0.573490708638711??? Otu0002??? Otu00066??? 0.539961837085781??? Otu0002??? Otu00083??? 0.553157813265034??? Otu0002??? Otu00084??? 0.530240909220586??? Otu0002??? Otu00133??? 0.784857667907929??? Otu0002??? Otu00154??? 0.511528128122173??? Otu0002??? Otu00165??? 0.53831396859667??? Otu0002??? Otu00242??? 0.669315163629151??? Otu0002??? Otu00303??? 0.500475996445905??? Otu0002??? Otu00498??? 0.628615400548539??? Otu0002??? Otu00543??? 0.567818884280764??? Otu0002??? Otu00619??? 0.75244056951144??? Otu0002??? Otu00646??? 0.529522331732556??? Otu0002??? Otu00915??? 0.696241613567686??? Otu0002??? Otu00991??? 0.639059878477682??? Otu0002??? Otu01035??? 0.563754887455874??? Otu0002??? Otu01327??? 0.554745741443052??? Otu0002??? Otu01651??? 0.748640849069562??? Otu0002??? Otu01655??? 0.555344064103094??? Otu0002??? Otu01657??? 0.747195570271361??? Otu0002??? Otu01669??? 0.577474772318958??? Otu0002??? Otu01673??? 0.57235592816339??? Otu0002??? Otu01678??? 0.532054607967116??? Otu0002??? Otu01692??? 0.510778063738462??? Otu0002??? Otu01698??? 0.576035950309876??? Otu0002??? Otu01711??? 0.573412678434289??? Otu0002??? Otu01722??? 0.509439531502377??? Otu0002??? Otu01729??? 0.804007560392851??? Otu0002??? Otu01745??? 0.581593731432025??? Otu0002??? Otu01751??? 0.550207028205676??? Otu0002??? Otu01776??? 0.636377400797969??? Otu0002??? Otu01794??? 0.56342976112525??? Otu0002??? Otu01795??? 0.728674360956472??? Otu0002??? Otu01828??? 0.511204782529687??? Otu0002??? Otu01843??? 0.530105896684999??? Otu0003??? Otu00003??? 0.556594527311079??? Otu0003??? Otu00021??? 0.714168542495792??? Otu0003??? Otu00027??? 0.725098427544048??? Otu0003??? Otu00039??? 0.61965996355808??? Otu0003??? Otu00047??? 0.584934423175894??? Otu0003??? Otu00071??? 0.518614228559661??? Otu0003??? Otu00083??? 0.623468071193137??? Otu0003??? Otu00133??? 0.713676244293711??? Otu0003??? Otu00165??? 0.511466294820907??? Otu0003??? Otu00173??? 0.505441079098521??? Otu0003??? Otu00242??? 0.624930028988917??? Otu0003??? Otu00304??? 0.569530490988998??? Otu0003??? Otu00310??? 0.549922967908749??? Otu0003??? Otu00619??? 0.757942798980849??? Otu0003??? Otu00915??? 0.7176779954026??? Otu0003??? Otu00991??? 0.781576134554794??? Otu0003??? Otu01651??? 0.721487507136356??? Otu0003??? Otu01657??? 0.754496459495486??? Otu0003??? Otu01669??? 0.646458548457953??? Otu0003??? Otu01673??? 0.664770709865955??? Otu0003??? Otu01692??? 0.612569448712928??? Otu0003??? Otu01698??? 0.647668633288045??? Otu0003??? Otu01711??? 0.612561320274266??? Otu0003??? Otu01721??? 0.62061641275095??? Otu0003??? Otu01729??? 0.783249389771596??? Otu0003??? Otu01755??? 0.545766876049415??? Otu0003??? Otu01776??? 0.565929776218531??? Otu0003??? Otu01794??? 0.627392378650643??? Otu0003??? Otu01795??? 0.714717623091678??? Otu0003??? Otu01843??? 0.634005545496746??? Otu0003??? Otu01895??? 0.604070097405862??? Otu0003??? Otu02005??? 0.530714153809096??? Otu0004??? Otu00002??? 0.593137998828159??? Otu0004??? Otu00003??? 0.667086578833277??? Otu0004??? Otu00010??? 0.807842496549089??? Otu0004??? Otu00011??? 0.548624698052775??? Otu0004??? Otu00013??? 0.510344625908429??? Otu0004??? Otu00021??? 0.617648466154296??? Otu0004??? Otu00046??? 0.597857723445282??? Otu0004??? Otu00047??? 0.721857212062533??? Otu0004??? Otu00057??? 0.501491534404914??? Otu0004??? Otu00067??? 0.782197719763571??? Otu0004??? Otu00084??? 0.597125357015794??? Otu0004??? Otu00114??? 0.702886677174453??? Otu0004??? Otu00119??? 0.640090184931907??? Otu0004??? Otu00125??? 0.655686116969408??? Otu0004??? Otu00130??? 0.530181012591603??? Otu0004??? Otu00133??? 0.637948887965445??? Otu0004??? Otu00143??? 0.528939127164607??? Otu0004??? Otu00149??? 0.714451018178487??? Otu0004??? Otu00150??? 0.644296813063963??? Otu0004??? Otu00154??? 0.689195532237303??? Otu0004??? Otu00165??? 0.599856550664088??? Otu0004??? Otu00175??? 0.519602448838287??? Otu0004??? Otu00199??? 0.5829767621024??? Otu0004??? Otu00223??? 0.670053732330698??? Otu0004??? Otu00252??? 0.586402767458116??? Otu0004??? Otu00256??? 0.506550312986315??? Otu0004??? Otu00261??? 0.52655831389696??? Otu0004??? Otu00271??? 0.750524441430203??? Otu0004??? Otu00274??? 0.594215209143337??? Otu0004??? Otu00290??? 0.8790743770077??? Otu0004??? Otu00333??? 0.626301582988709??? Otu0004??? Otu00383??? 0.556202065156764??? Otu0004??? Otu00543??? 0.704129662960999??? Otu0004??? Otu00646??? 0.8271349261136??? Otu0004??? Otu00689??? 0.842186325891778??? Otu0004??? Otu00825??? 0.708565309478537??? Otu0004??? Otu00851??? 0.584203923919377??? Otu0004??? Otu00949??? 0.790579843465587??? Otu0004??? Otu00970??? 0.607709549330321??? Otu0004??? Otu01035??? 0.865518844853625??? Otu0004??? Otu01327??? 0.750309955803488??? Otu0004??? Otu01638??? 0.618105147426932??? Otu0004??? Otu01642??? 0.874270049304982??? Otu0004??? Otu01661??? 0.54192029772368??? Otu0004??? Otu01662??? 0.710040347441829???
you are right! I was so happy to find out the p-value criteria, I totally forgot about the spearman correlation, no I dont think there is an option of spearman correlation. Thank you for pointing that out! Cheers, ?>Dear Ozgul, > >I made a comment in the previous email, which I didn't check it. >Thank you for the ?cor2m(). >I checked the results of the example dataset with cor2m().? Does it have an option for spearman correlation?? > >The values below seem to be pearson >#cor2m >?cor2m(bg,ag,trim=TRUE,alpha=0.05) ######I >????? Otu00022 Otu00029?? Otu00039 Otu00042? Otu00101 Otu00105 Otu00125 >ag1? 0.0000000????? NaN? 0.0000000????? NaN 0.0000000????? NaN????? NaN >ag2? 0.0000000????? NaN? 0.0000000????? NaN 0.7743597????? NaN????? NaN >ag3 -0.8901029????? NaN -0.8901029????? NaN 0.0000000????? NaN????? NaN >ag4? 0.0000000????? NaN? 0.0000000????? NaN 0.0000000????? NaN????? NaN >ag5? 0.0000000????? NaN? 0.0000000????? NaN 0.0000000????? NaN????? NaN >??? Otu00131?? Otu00137 Otu00155 Otu00158 Otu00172 Otu00181 Otu00185?? Otu00190 >ag1????? NaN? 0.0000000????? NaN????? NaN????? NaN????? NaN????? NaN? 0.0000000 >ag2????? NaN? 0.0000000????? NaN????? NaN????? NaN????? NaN????? NaN? 0.0000000 >ag3????? NaN -0.8901029????? NaN????? NaN????? NaN????? NaN????? NaN -0.8901029 >ag4????? NaN? 0.0000000????? NaN????? NaN????? NaN????? NaN????? NaN? 0.0000000 >ag5????? NaN? 0.0000000????? NaN????? NaN????? NaN????? NaN????? NaN? 0.0000000 >??? Otu00209?? Otu00218 >ag1????? NaN? 0.0000000 >ag2????? NaN? 0.0000000 >ag3????? NaN -0.8901029 >ag4????? NaN? 0.0000000 >ag5????? NaN? 0.0000000 > >#Pearson correlation from my solution: >resr<-do.call(rbind,lapply(split(bg_ag,1:nrow(bg_ag)),function(x) > {res<-rcorr(cbind(bg[,x[,1]],ag[,x[,2]]))$r; row.names(res)<- >rep(paste(x[1],x[2],sep="_"),2);res})) >?indx<-row(resr)%%2!=1 >resrnew<-as.matrix(resr[indx[,1],1]) >resrnew1<-data.frame(read.table(text=row.names(resrnew),sep="_",stringsAsFactors=FALSE),value=resrnew) >?row.names(resrnew1)<-1:nrow(resrnew1) >library(reshape2) >?dcast(resrnew1,V2~V1,value.var="value")?? V2??? Otu00022 Otu00029??? Otu00039 Otu00042?? Otu00101 Otu00105 Otu00125 >#1 ag1 -0.12705141????? NaN -0.12705141????? NaN -0.6394308????? NaN????? NaN >#2 ag2 -0.61522514????? NaN -0.61522514????? NaN? 0.7743597????? NaN????? NaN >#3 ag3 -0.89010286????? NaN -0.89010286????? NaN -0.2655363????? NaN????? NaN >#4 ag4? 0.03036290????? NaN? 0.03036290????? NaN -0.5638320????? NaN????? NaN >#5 ag5? 0.08266317????? NaN? 0.08266317????? NaN -0.2582930????? NaN????? NaN >?# Otu00131??? Otu00137 Otu00155 Otu00158 Otu00172 Otu00181 Otu00185??? Otu00190 >#1????? NaN -0.12705141????? NaN????? NaN????? NaN????? NaN????? NaN -0.12705141 >#2????? NaN -0.61522514????? NaN????? NaN????? NaN????? NaN????? NaN -0.61522514 >#3????? NaN -0.89010286????? NaN????? NaN????? NaN????? NaN????? NaN -0.89010286 >#4????? NaN? 0.03036290????? NaN????? NaN????? NaN????? NaN????? NaN? 0.03036290 >#5????? NaN? 0.08266317????? NaN????? NaN????? NaN????? NaN????? NaN? 0.08266317 >?# Otu00209??? Otu00218 >#1????? NaN -0.12705141 >#2????? NaN -0.61522514 >#3????? NaN -0.89010286 >#4????? NaN? 0.03036290 >#5????? NaN? 0.08266317 > >A.K. > > > >----- Original Message ----- >From: Ozgul Inceoglu <Ozgul.Inceoglu at ulb.ac.be> >To: R help <r-help at r-project.org> >Cc: >Sent: Thursday, February 14, 2013 1:10 AM >Subject: Re: [R] spearman correlation and p-value as a matrix > >Thank you all for the answers. I really need to learn a lot. >Bu I also discover cor2m(x, y, trim = TRUE, alpha = 0.05) >in ecodist package, which gives an output file with significant correlations. > >?zg?l > >Yj>HI, >> >>#ag data was created >>bg<- as.matrix(read.table(text=" >>???? Otu00022 Otu00029 Otu00039 Otu00042 Otu00101 Otu00105 Otu00125 Otu00131 Otu00137 Otu00155 Otu00158 Otu00172 Otu00181 Otu00185 Otu00190 Otu00209 Otu00218 >>Gi20Jun11? 0.001217??????? 0 0.001217??????? 0 0.000000??????? 0??????? 0??????? 0 0.001217??????? 0??????? 0??????? 0??????? 0??????? 0 0.001217??????? 0 0.001217 >>Gi40Jun11? 0.000000??????? 0 0.000000??????? 0 0.000000??????? 0??????? 0??????? 0 0.000000??????? 0??????? 0??????? 0??????? 0??????? 0 0.000000??????? 0 0.000000 >>Gi425Jun11 0.000000??????? 0 0.000000??????? 0 0.000000??????? 0??????? 0??????? 0 0.000000??????? 0??????? 0??????? 0??????? 0??????? 0 0.000000??????? 0 0.000000 >>Gi45Jun11? 0.000000??????? 0 0.000000??????? 0 0.001513??????? 0??????? 0??????? 0 0.000000??????? 0??????? 0??????? 0??????? 0??????? 0 0.000000??????? 0 0.000000 >>Gi475Jun11 0.000000??????? 0 0.000000??????? 0 0.000000??????? 0??????? 0??????? 0 0.000000??????? 0??????? 0??????? 0??????? 0??????? 0 0.000000??????? 0 0.000000 >>Gi50Jun11? 0.000000??????? 0 0.000000??????? 0 0.000000??????? 0??????? 0??????? 0 0.000000??????? 0??????? 0??????? 0??????? 0??????? 0 0.000000??????? 0 0.000000 >>",sep="",header=TRUE,stringsAsFactors=F)) >>set.seed(128) >>ag<- matrix(rnorm(30),nrow=6) >>colnames(ag)<- paste("ag",1:5,sep="") >>bg_ag<-expand.grid(colnames(bg),colnames(ag),stringsAsFactors=FALSE) >>?attr(bg_ag,"out.attrs")<- NULL >>?library(Hmisc) >>#correlation >>resr<-do.call(rbind,lapply(split(bg_ag,1:nrow(bg_ag)),function(x) {res<-rcorr(cbind(bg[,x[,1]],ag[,x[,2]]),type="spearman")$r; row.names(res)<- rep(paste(x[1],x[2],sep="_"),2);res})) >>?head(resr) >>#???????????????? [,1]????? [,2] >>#Otu00022_ag1 1.0000000 0.1309307 >>#Otu00022_ag1 0.1309307 1.0000000 >>#Otu00029_ag1 1.0000000?????? NaN >>#Otu00029_ag1?????? NaN 1.0000000 >>#Otu00039_ag1 1.0000000 0.1309307 >>#Otu00039_ag1 0.1309307 1.0000000 >> >>#p-values >>resP<-do.call(rbind,lapply(split(bg_ag,1:nrow(bg_ag)),function(x) {res<-rcorr(cbind(bg[,x[,1]],ag[,x[,2]]),type="spearman")$P; row.names(res)<- rep(paste(x[1],x[2],sep="_"),2);res})) >>? head(resP) >>#????????????????? [,1]????? [,2] >>#Otu00022_ag1??????? NA 0.8047262 >>#Otu00022_ag1 0.8047262??????? NA >>#Otu00029_ag1??????? NA?????? NaN >>#Otu00029_ag1?????? NaN??????? NA >>#Otu00039_ag1??????? NA 0.8047262 >>#Otu00039_ag1 0.8047262??????? NA >> >>#If you need only the values >>indx<-row(resr)%%2!=1 >>?resPnew<-as.matrix(resP[indx[,1],1]) >>?resrnew<-as.matrix(resr[indx[,1],1]) >> >>head(resPnew) >>#????????????????? [,1] >>#Otu00022_ag1 0.8047262 >>#Otu00029_ag1?????? NaN >>#Otu00039_ag1 0.8047262 >>#Otu00042_ag1?????? NaN >>#Otu00101_ag1 0.1583024 >>#Otu00105_ag1?????? NaN >> >>head(resrnew) >>#?????????????????? [,1] >>#Otu00022_ag1? 0.1309307 >>#Otu00029_ag1??????? NaN >>#Otu00039_ag1? 0.1309307 >>#Otu00042_ag1??????? NaN >>#Otu00101_ag1 -0.6546537 >>#Otu00105_ag1??????? NaN >> >>A.K. >> >> >> >> >>----- Original Message ----- >>From: Ozgul Inceoglu <Ozgul.Inceoglu at ulb.ac.be> >>To: r-help at r-project.org >>Cc: >>Sent: Wednesday, February 13, 2013 4:48 AM >>Subject: [R] spearman correlation and p-value as a matrix >> >>I have two data matrices that I want to make the correlation between each column from data1 and each column from data 2 and also calculate the p-value Matrices dont have the same size and I tried such a script. >>> bg <- read.table (file.choose(), header=T, row.names) >>> bg >>> Otu00022 Otu00029 Otu00039 Otu00042 Otu00101 Otu00105 Otu00125 Otu00131 Otu00137 Otu00155 Otu00158 Otu00172 Otu00181 Otu00185 Otu00190 Otu00209 Otu00218 >>> Gi20Jun11 0.001217 0 0.001217 0 0.000000 0 0 0 0.001217 0 0 0 0 0 0.001217 0 0.001217 >>> Gi40Jun11 0.000000 0 0.000000 0 0.000000 0 0 0 0.000000 0 0 0 0 0 0.000000 0 0.000000 >>> Gi425Jun11 0.000000 0 0.000000 0 0.000000 0 0 0 0.000000 0 0 0 0 0 0.000000 0 0.000000 >>> Gi45Jun11 0.000000 0 0.000000 0 0.001513 0 0 0 0.000000 0 0 0 0 0 0.000000 0 0.000000 >>> Gi475Jun11 0.000000 0 0.000000 0 0.000000 0 0 0 0.000000 0 0 0 0 0 0.000000 0 0.000000 >>> Gi50Jun11 0.000000 0 0.000000 0 0.000000 0 0 0 0.000000 0 0 0 0 0 0.000000 0 0.000000 >>ag <- read.table (file.choose(), header=T, row.names) >> >>for (i in 1:(ncol(bg))) >>for (j in 1:(ncol(ag))) >>print(c(i,j)) >>final_matrix <- matrix(rep("0",ncol(bg)*ncol(ag)),ncol=ncol(bg),nrow=ncol(ag)) >> >>cor <- cor.test(as.vector(as.matrix(bg[,i])),as.vector(as.matrix(ag[,j])), method="spearman") >> >>#but the output is not matrice with all the values but a single correlation value >> >>data:? bg[, i] and ag[, j] >>t = 2.2992, df = 26, p-value = 0.02978 >>alternative hypothesis: true correlation is not equal to 0 >>95 percent confidence interval: >>0.04485289 0.67986803 >>sample estimates: >>? ? ? cor >>0.4110515 >> >># How I can creat an outfile with all the correlations and p-values? >> >>Thank you very much! >>?zg?l >> >>______________________________________________ >>R-help at r-project.org mailing list >>https://stat.ethz.ch/mailman/listinfo/r-help >>PLEASE do read the posting guide http://www.R-project.org/posting-guide.html >>and provide commented, minimal, self-contained, reproducible code. >> >> >> > >?zg?l Inceoglu, PhD. > >Universit? Libre de Bruxelles >Ecology of Aquatic Systems >Campus de la plaine CP221? >Boulevard du triomphe? >1050 Bruxelles Belgium? > >______________________________________________ >R-help at r-project.org mailing list >https://stat.ethz.ch/mailman/listinfo/r-help >PLEASE do read the posting guide http://www.R-project.org/posting-guide.html >and provide commented, minimal, self-contained, reproducible code. > > >?zg?l Inceoglu, PhD. Universit? Libre de Bruxelles Ecology of Aquatic Systems Campus de la plaine CP221 Boulevard du triomphe 1050 Bruxelles Belgium