1

I recently downloaded this R package (http://cran.r-project.org/web/packages/energy/index.html) and was able to successfully install it and run it on my system and calculate distance correlation.

The problem is for loops in R takes forever and I am an R newbie. The source code is actually written in some files in C, which R uses to evaluate. I have these two files in src folder in the package utilities.c and dcov.c Which has functions I need to implement.

I tried to compile dcov.c which uses rmath.h and r.h apparently and I got a bunch of errors about Calloc/Free used in the program.

Any idea how I can just compile the C programs?

Gert Arnold
  • 105,341
  • 31
  • 202
  • 291
user1775614
  • 439
  • 7
  • 12
  • 2
    Probably substantially easier to [speed up your loop](http://stackoverflow.com/q/2908822/636656) than to try reinventing the wheel. – Ari B. Friedman Oct 25 '12 at 22:17
  • The thing is I am basically calculating a correlation coefficient matrix like cor(M) would do. I have to do it a column against every other column in M. I am not at all confident that can be done in R. I ve been reading up a lot on loop optimization in R. – user1775614 Oct 26 '12 at 18:39
  • Basically all i am trying to do is : `for( i in 1:n){ for(j in (i+1):n) { dcormat[i,j]<-dcor(M[,i],M[,j]) } } ` If there is some way of speeding this up, that I am missing, that would be great. dcor is the function in the package. M is the input matrix and dcormat is just a square matrix with correlation coefficients. – user1775614 Oct 26 '12 at 18:51
  • Loops are slow. Vectorization is fast. See the link in my previous comments for suggestions, and post your question if you still need help. You almost certainly don't need C to make that algorithm fast. – Ari B. Friedman Oct 26 '12 at 21:45
  • FOr the past 2 days I ve been trying to vectorise the code snippet from my previous comment. I am drawing blanks. Any ideas? The problem is that dcor doesnt accept matrix inputs. It accepts 2 vectors and and computes the coefficient. I am not sure how to change the functionality so that i can vectorise my inputs. any help will be greatly appreciated. – user1775614 Oct 30 '12 at 00:45

0 Answers0