4
$\begingroup$

Where can we find C code for statistical computing? R is kind of slow when doing a lot of iterations. C is a lot faster but coding is always heavy-duty. I am wondering does anybody have any good source websites on statistical computing in C. Basically if most of the functions in R, especially matrix manipulations, are rewritten in C code, it will be super nice.

I already know some websites. 1. http://lib.stat.cmu.edu/apstat/

  1. http://people.sc.fsu.edu/~jburkardt/c_src/c_src.html

I would appreciate if you can provide me with other similar websites.

$\endgroup$
  • $\begingroup$ Are you aware that some underlying code in R is actually implemented in C? You can frequently see calls from R to C in the source codes. Also, instead of using loops for iterations it may help formulating your program in vectors and matrices (where that is possible), that should work faster. $\endgroup$ – Richard Hardy Jan 23 '15 at 20:39
  • 2
    $\begingroup$ The Rcpp package for R allows you to implement C directly in your code. There are a lot of packages out there that utilize Rcpp to help with tasks that are generally faster in C, such as 'sliding window' problems. If your use case is specific to matrix operations, I would consider Matlab as a good alternative (which is written in Fortran - which is faster than C) $\endgroup$ – TLJ Jan 23 '15 at 22:02
  • 1
    $\begingroup$ Is the point of looking for C code to be able to program in C, to speed up calculations, to provide interfaces to other platforms, or something else? Incidentally, it is still the case, some 60+ years after people started developing and publishing statistical algorithms, that the sources of many of the best routines are given in Fortran, not C. $\endgroup$ – whuber Jan 23 '15 at 22:02
4
$\begingroup$

There is an introductory statistics book which uses C for everything, including a library written by the author Ben Klemens. It's available as a free online pdf from the author's website:

http://modelingwithdata.org/

I haven't used the C code, but have found parts of the book quite useful, both as an introduction to C and for the statistical topics. I believe it also has some useful info on SQL.

| cite | improve this answer | |
$\endgroup$
0
$\begingroup$

R, if used correctly, should be fast. As mentioned in the comment, R is largely written in C and Fortran and is quite optimized. Make sure you are using the right data structures and leveraging the built in indexing/vector/matrix methods instead of too many loops. You may also want to compile your R code. Check this out for some useful tips:

http://www.noamross.net/blog/2013/4/25/faster-talk.html

If you really know what your doing in R and it's still too slow, try switching to NumPy. NumPy's often faster than R, and much easier to do data analysis with than C. I can't recommend trying to write your own version of R in C.

| cite | improve this answer | |
$\endgroup$
  • $\begingroup$ Avoid loops in R, as others have already suggested. If you can substitute indexing/vector/matrix methods as @ac5 recommends, things speed up a lot. If loops are unavoidable, use the resources for parallel computation in R, like the foreach package. That way you can draw on the wealth of tools available in R without having to start from scratch for performing rapid calculations. $\endgroup$ – EdM Jan 23 '15 at 21:57
  • 1
    $\begingroup$ I'll layer on that IMO if you are new to R, you should immediately start learning w/ the data.table package. $\endgroup$ – TLJ Jan 23 '15 at 22:06
0
$\begingroup$

You might not want to write all your code in C for statistical computing. As THE statistical language, R is the most powerful language for statistical analysis without any doubt. So what you might really want to do is to speed up your R code with some techniques. Some helpful ways are the following.

  1. Vectorization.
  2. Calling C code from R;
  3. High performance and parallel computing in R.
| cite | improve this answer | |
$\endgroup$

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.