Search code examples
rcorrelationalgebrasurfacepearson

Correlation surface in R


This is my third attempt on explaining my problem. Hope I can get it right this time, it is difficult problem to explain.

I have 90x19 matrix where each of the 90 rows is a series of 19 measurements on a certain wavelength. The 90 wavelengths goes from 400 to 700. Then I have a length 19x1 vector.

I want to create a matrix where each cell shows the Pearson correlation coefficient between the sum of each combination of wavelengths (for all the 19 values of the matrix) and the 19x1 vector.

Plotting this surface looks exactly like this

enter image description here

Please let me know if you need more information or a better explanation. Really need some help here ! :)

Bests

The vector is the following:

v<-c(116, 100, 148, 132, 81, 136, 145, 116, 87, 126, 62, 124, 129, 
108, 127, 134, 142, 99, 132)

And the data-frame header is the following:

data<-structure(list(`1` = c(2, 2, 2, 2), `2` = c(1, 1, 1, 1), `3` = c(2, 
2, 2, 2), `4` = c(2, 2, 2, 3), `5` = c(13, 14, 14, 15), `6` = c(2, 
2, 2, 2), `7` = c(2, 2, 2, 2), `8` = c(0, 0, 0, 0), `9` = c(5, 
5, 5, 5), `10` = c(0, 0, 0, 0), `11` = c(114, 119, 122, 125), 
    `12` = c(8, 8, 8, 8), `13` = c(7, 7, 7, 8), `14` = c(10, 
    10, 10, 10), `15` = c(12, 12, 12, 12), `16` = c(6, 6, 6, 
    6), `17` = c(4, 4, 4, 4), `18` = c(17, 18, 18, 19), `19` = c(13, 
    14, 14, 14)), .Names = c("1", "2", "3", "4", "5", "6", "7", 
"8", "9", "10", "11", "12", "13", "14", "15", "16", "17", "18", 
"19"), row.names = c("402.37700182806", "405.70862540288", "409.04076825642", 
"412.37342090064"), class = "data.frame")

Solution

  • I simulated the data (without correlation), but see if this is what you want:

    nr=90
    data<-data.frame(matrix(runif(nr*19,2,15),nrow=nr,ncol=19))
    row.names(data)<-round(seq(400,700, length.out = nrow(data)),4)
    
    cm=matrix(NA,nrow(data),nrow(data))
    for (i in 1:nrow(data))
      for(j in i:nrow(data))cm[j,i]<- cor(apply(data[c(j,i),],2,sum),v)
    
    colnames(cm)<-row.names(cm)<-round(as.numeric(row.names(data)),0)
    
    #Build the plot    
    library(lattice)
    library(reshape)
    
    cmd<-cbind(var=as.numeric(row.names(data)),data.frame(cm))
    cmd<-cbind(melt(cmd, id=c("var")),var2=rep(as.numeric(row.names(data)),each=nrow(data)))
    
    levelplot(value~var*var2,data=cmd,  
              col.regions=colorRampPalette(c("blue","green", "red")),
              at=seq(-1,1,0.1),
              xlab="x", ylab="x", main="")
    

    enter image description here