Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    text
    copied!<p>I thought of writing a comment, but I have too much to say.</p> <p>First of all, if you think apply goes faster, look at <a href="https://stackoverflow.com/questions/2275896/is-rs-apply-family-more-than-syntactic-sugar">Is R&#39;s apply family more than syntactic sugar?</a> . It might be, but it's far from guaranteed.</p> <p>Next, please don't grow matrices as you move through your code, that slows down your code incredibly. preallocate the matrix and fill it up, that can increase your code speed more than a tenfold. You're growing different vectors and matrices through your code, that's insane (forgive me the strong speech)</p> <p>Then, look at the help page of <code>?subset</code> and the warning given there:</p> <blockquote> <p>This is a convenience function intended for use interactively. For programming it is better to use the standard subsetting functions like [, and in particular the non-standard evaluation of argument subset can have unanticipated consequences.</p> </blockquote> <p>Always. Use. Indices. </p> <p>Further, You recalculate the same values over and over again. <code>fre_res_2</code> for example is calculated for every res_2 and state_2 as many times as you have combinations of <code>res_1</code> and <code>state_1</code>. That's just a waste of resources. Get out of your loops what you don't need to recalculate, and save it in matrices you can just access again. </p> <p>Heck, now I'm at it: Please use vectorized functions. Think again and see what you can drag out of the loops : This is what I see as the core of your calculation:</p> <pre><code>cov &lt;- (freq_both - (freq_res_1)*(freq_res_2)) / (sqrt(freq_res_1*(1-freq_res_1))*sqrt(freq_res_2*(1-freq_res_2))) </code></pre> <p>As I see it, you can construct a matrix freq_both, freq_res_1 and freq_res_2 and use them as input for that one line. And that will be the whole covariance matrix (don't call it <code>cov</code>, <code>cov</code> is a function). Exit loops. Enter fast code.</p> <p>Given the fact I have no clue what's in c_alignment, I'm not going to rewrite your code for you, but you definitely should get rid of the C way of thinking and start thinking R. </p> <p>Let this be a start: <a href="http://www.burns-stat.com/pages/Tutor/R_inferno.pdf" rel="nofollow noreferrer">The R Inferno</a></p>
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload