Dear all, I am new to programming in R. I deal with microarray data,which is a data frame object type. I need to carry out a few statistical procedures on this, one of them being the pearson corelation. I need to do this between each row which is a gene. So the desired result is a square matrix with the pearson corelation value between each row. So the first column would be (1,1)=0,(1,2),(1,3) and so on. I uploaded the data frame as "a":- a <- read.csv("a.csv", header= TRUE, row.names=1) and then I started the script:- pearson <- function(a){ r <- matrix[x,y] for(x in as.vector(a[,1], mode="double")){ x++{ for(y in as.vector(a[2,], mode="double")){ y <- x+1 x++ { r <- (cor.test(as.vector(as.matrix(a)[x,], mode="double"), as.vector(as.matrix(a)[y,], mode="double"))$p.value) } } } r[x,y]==r[y,x] } return(r) } However whenever I run it,I get the error:-> pearson(a)Error in matrix[x, y] : object of type 'closure' is not subsettable Please help! Best Regards Sumona Mitra
On Jul 12, 2011, at 7:27 AM, Mitra, Sumona wrote:> Dear all, > > I am new to programming in R.You see to think there is a "++" operation in R. That is not so.> I deal with microarray data,which is a data frame object type. I > need to carry out a few statistical procedures on this, one of them > being the pearson corelation. I need to do this between each row > which is a gene. So the desired result is a square matrix with the > pearson corelation value between each row. So the first column would > be (1,1)=0,(1,2),(1,3) and so on.I do not understand what that means. You should offer either a minimal dataset or at the very least the results of str(a).> > I uploaded the data frame as "a":-If by that you mean you made a failed effort at attaching the data in a file, then you need to read the Posting Guide for what the server will accept as a file type.> > a <- read.csv("a.csv", header= TRUE, row.names=1) > > and then I started the script:- > > pearson <- function(a){ > r <- matrix[x,y] > for(x in as.vector(a[,1], mode="double")){I do not see a need for as.vector here or at any point later. a[,1] would already be a vector and if it is not numeric to begin with, then you are going to get junk.> x++{ > for(y in as.vector(a[2,], mode="double")){ > y <- x+1 > x++ > { > r <- (cor.test(as.vector(as.matrix(a)[x,], mode="double"), > as.vector(as.matrix(a)[y,], mode="double"))$p.value) > } > } > } > r[x,y]==r[y,x] > } > return(r) > } > > However whenever I run it,I get the error:- > >> pearson(a) > Error in matrix[x, y] : object of type 'closure' is not subsettable > > Please help! > > Best Regards > Sumona Mitra > > ______________________________________________ > 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.David Winsemius, MD West Hartford, CT
On 12 July 2011 12:27, Mitra, Sumona <sumona.mitra@kcl.ac.uk> wrote:> Dear all, > > I am new to programming in R. >You sure are ;-) I deal with microarray data,which is a data frame object type. I need to> carry out a few statistical procedures on this, one of them being the > pearson corelation. I need to do this between each row which is a gene. So > the desired result is a square matrix with the pearson corelation value > between each row. So the first column would be (1,1)=0,(1,2),(1,3) and so > on. > > I uploaded the data frame as "a":- > > a <- read.csv("a.csv", header= TRUE, row.names=1) > > and then I started the script:- > > pearson <- function(a){ > r <- matrix[x,y] >I bet the problem you are getting is here. You want r to be a x by y matrix. To do this, try r<- matrix(nrow=x, ncol=y). But you haven't defined the x and y, unless we are missing that part of your code. As I understand it, you want the correlation matrix between all the rows of your matrix. If so, then look at the help file for cor. (ie type "?cor".) You will find that it automatically prints the correlatoins between all columns of a matrix. So, once your data is correctly read in, you should be able to just do: cor(t(a))> for(x in as.vector(a[,1], mode="double")){ > x++{ > for(y in as.vector(a[2,], mode="double")){ > y <- x+1 > x++ >This code looks like a horrible mess. It's almost never right to loop through your vectors. In addition, there is no such thing as "++", as somebody mentioned.> { > r <- (cor.test(as.vector(as.matrix(a)[x,], mode="double"), > as.vector(as.matrix(a)[y,], mode="double"))$p.value) > } > } > } > r[x,y]==r[y,x] > } > return(r) > } > > However whenever I run it,I get the error:- > > > pearson(a) > Error in matrix[x, y] : object of type 'closure' is not subsettable > > Please help! > > Best Regards > Sumona Mitra > > ______________________________________________ > R-help@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. >[[alternative HTML version deleted]]
Dear all, Thank you so much for your help. I was able to get a script that ran and did what i wanted it to do. It was as follows:-> correlation <- function(a)+ { + r <- matrix(nrow = dim(a)[1], ncol=dim(a)[1]) + for(x in c(2:dim(a)[1])) + { + for(y in c(x:dim(a)[1])) + { + r[x,y] <- (cor.test(as.vector(as.matrix(a)[x,], mode="double"), as.vector(as.matrix(a)[y,], mode="double"))$p.value) + r[y,x] <- r[x,y] + } + } + return(r) + } Now I want to extend this to include all the possible correlations that cor.test can do. One of the possible arguments of cor.test is method="method", where method can be pearson, spearman or kendall. To include this I modified my code in the following way:-> correlation <- function(a, b)+ { + r <- matrix(nrow = dim(a)[1], ncol=dim(a)[1]) + for(x in c(2:dim(a)[1])) + { + for(y in c(x:dim(a)[1])) + { + r[x,y] <- (cor.test(as.vector(as.matrix(a)[x,], mode="double"), as.vector(as.matrix(a)[y,], mode="double"), method="b")$p.value) + r[y,x] <- r[x,y] + } + } + return(r) + } This when executed though, gives the following error:-> correlation(a, spearman)Error in match.arg(method) : 'arg' should be one of ?pearson?, ?kendall?, ?spearman? Please help! Sumona
Maybe Matching Threads
- Pearson corelation and p-value for matrix
- calculating correlation of a Supply/Demand measure and price change (in high frequency time series data)
- making spearman correlation cor() call fail with log(0) as input
- what does this error mean?
- By() with method = spearman