2

I'm trying to write a function that will create a correlation matrix using a fancy distance estimate (dcorr, Brownian distance). More generally, I want to write code for a generic "correlation" matrix in which you can plug in any distance estimator.

My data is formatted such that columns are variables and rows are observations.

I'm having problems with my basic code. My algorithm is as follows:

  • Use apply to take a variable
  • Pass to function that will again take apply on the entire matrix
  • At this point you should have two pairs of variables
  • Use na.omit to remove missing observations (necessary for dcorr)
  • Calculate dcorr

I was hoping this would result in the correlation matrix but I'm having a lot of problems with basic variable managment. I'm having difficulty passing variables to the apply function. In particular, I want to pass a the column that was pulled in the first apply and pass it to the second apply (that is applied on the entire original matrix)

My code:

dcormatrix <- function(Matrix){
  dcorhelper <- function (Col1){
    as.matrix(apply(Matrix,2,function(Col2){
      B <- na.omit(cbind(Col1,Col2))
      dcor(B[,1],B[,2],index=1)
    },Col1=Col1))
  }
 apply(Matrix,2,dcorhelper(),Matrix=Matrix)
}

Any ideas? I'm sure there's gotta be an easy way to do this.

4

1 に答える 1

1

パッケージdesigndistからチェックアウトすることをお勧めします。veganこれにより、代替の距離/非類似度行列を定義できます。ここを参照してください。

于 2012-08-22T08:58:08.383 に答える