Gene Expression Correlation
2
1
Entering edit mode
9.6 years ago

I want to correlate the expression of gene-setA with SetB using Pearson's correlation in R. Can someone help with the script?

R • 8.6k views
ADD COMMENT
0
Entering edit mode

Yes, but that correlated values of each row. e.g.: SetA-row1 vs SetB-row1..and so on...It did not create a matrix with each gene of set A correlation with all genes of SetB

ADD REPLY
1
Entering edit mode

not sure what you are trying to achieve, do you want to correlate the same gene in two conditions? one gene against all the others in the other conditions? everything against everything? how do your data look? could you post a sample of the data so we can better help you out?

ADD REPLY
0
Entering edit mode

e.g.:

SetA

Gene    Expression
a1  142
a2  111
a3  5

SetB

Gene    Expression
b1  200
b2  4
b3  67

I want to create a aXb correlation matrix

      a1            a2            a3
b1    cor(a1,b1)    cor(a2,b1)    cor(a3,b1)
b2    cor(a1,b2)    cor(a2,b2)    cor(a3,b2)
b3    cor(a1,b3)    cor(a2,b3)    cor(a3,b3)

Also, What if the 2 gene sets are of unequal sizes

ADD REPLY
2
Entering edit mode

You can't take the correlation of a pair of samples where there is only one observation, because the two samples you are comparing have no standard deviation, and the definition of the Pearson's correlation coefficient requires the calculation of a standard deviation or distance from the mean, which has no meaning for one observation: http://en.wikipedia.org/wiki/Pearson_product-moment_correlation_coefficient

You need at least three observations of your two variables a and b, and both vectors usually need to be of same size to do a Pearson correlation test with cor.test(). You can tell the correlation test how to handle NA values with na.action = "na.exclude" (i.e., where you don't have a matching observation in one of the two paired samples, you exclude data from both vectors).

I suggest reading about correlation and reading the relevant R documentation. What you're asking for doesn't seem to make much sense as described.

ADD REPLY
0
Entering edit mode

I got an error when running this function:

> for i in 1 to 100 {         # go through rows in first matrix
Error: unexpected symbol in "for i"
> for i in 1 to 100 {         # go through rows in first matrix
Error: unexpected symbol in "for i"
ADD REPLY
1
Entering edit mode

TriS wrote a pseudo code, which could be functional or just the guideline.

You should read a short tutorial to get yourself acquainted to the loops in R
A Tutorial on Loops in R - Usage and Alternatives

# this is the syntax for R
for(i in 1:100 ){print (i)}

ADD REPLY
2
Entering edit mode
9.6 years ago

Type in ?cor.test within R, and take a look at the provided example at the bottom of the documentation.

ADD COMMENT
2
Entering edit mode
9.6 years ago
TriS ★ 4.7k

Yeah so...there are a few things you want to keep in mind

  1. I hate saying it but use Google first for a quick search
  2. What's your script so far? how does it look? it's easier to help you if you already have something
  3. If unequal size you either use only the matching samples or use simulation of missing data to estimate the correlation coefficient by repeating the simulation enough times...but it seems that pairwise deletion is easier and leads to pretty much the same results

Now, as starting point I can give you pseudocode which can look like:

mat1 <- matrix 100x10
mat2 <- matrix 100x10
results <- c()
for I in 1 to 100 {         # go through rows in first matrix
    for j I in 1 to 100 {         # go through rows in second matrix
      correlation <- calculateCorrelation(mat1[i,], mat2[j,])         # get correlation
      temp <- c(results, temp) # create a vector with all the correlation of one row with all the rows in the second matrix
    }
    results <- rbind(results, temp)         # create the 100x100 matrix
}

This will give you what you want and if matrix 1 and matrix 2 are different size it should be trivial to tweak

ADD COMMENT

Login before adding your answer.

Traffic: 2622 users visited in the last hour
Help About
FAQ
Access RSS
API
Stats

Use of this site constitutes acceptance of our User Agreement and Privacy Policy.

Powered by the version 2.3.6