Note that there are some explanatory texts on larger screens.

plurals
  1. POIs there a more efficient way of nesting three for loops?
    primarykey
    data
    text
    <p>I have been struggling for a while in converting the code below to use the *apply family of functions, so am now asking the StackOverflow community for a little help. Some background, this is part of a method I am developing to analyze propensity score methods for three groups. As such, I am starting with three matrices representing the distances (difference in propensity scores) between each pair of groups. That is, matrix d1 is A x B, d2 is B x C, and d3 is C x A. What I need to do is find triplets that minimize the overall distance as well as be less than some caliper. I've simplified the example as best as I could to run while getting at what I am trying to.</p> <p>Couple of notes:</p> <ul> <li><p>The distance less than the caliper check (<code>row1 &lt;- row1[row1 &lt; caliper]</code>) could be done at the end if I were to simply create a data.frame (or matrix) of all possible combinations. However, even with the small number of groups I set here would result in 3,000 rows!</p></li> <li><p>I order the vectors before moving onto the next step. Again, if I were to have a matrix of all possible combinations this could be eliminated. In my current version I have another line that will only look at the n smallest elements in order to reduce the execution time.</p></li> <li><p>This example has pretty small groups. I am working on a dataset where the groups have between 5,000 and 8,000 subjects each.</p></li> </ul> <p>Thanks in advance for any help. I am working on a paper for this and would be happy to give a acknowledgements. Also, I plan on attending the useR! conference in Spain and will buy a beer for whomever helps :-)</p> <pre><code>groups &lt;- c('Control','Treat1','Treat2') group.sizes &lt;- c(15, 10, 20) set.seed(2112) d1 &lt;- matrix(abs(rnorm(group.sizes[1] * group.sizes[2], mean=0, sd=1)), nrow=group.sizes[1], ncol=group.sizes[2], dimnames=list(1:group.sizes[1], (group.sizes[1]+1):(group.sizes[1] + group.sizes[2])) ) d2 &lt;- matrix(abs(rnorm(group.sizes[2] * group.sizes[3], mean=0, sd=1)), nrow=group.sizes[2], ncol=group.sizes[3], dimnames=list((group.sizes[1]+1):(group.sizes[1] + group.sizes[2]), (group.sizes[2] + group.sizes[1] + 1):(sum(group.sizes)) ) ) d3 &lt;- matrix(abs(rnorm(group.sizes[3] * group.sizes[1], mean=0, sd=1)), nrow=group.sizes[3], ncol=group.sizes[1], dimnames=list((group.sizes[2] + group.sizes[1] + 1):(sum(group.sizes)), 1:group.sizes[1]) ) caliper &lt;- 1 results &lt;- data.frame(v1=character(), v2=character(), v3=character(), d1=numeric(), d2=numeric(), d3=numeric()) for(i1 in dimnames(d1)[[1]]) { row1 &lt;- d1[i1,] row1 &lt;- row1[row1 &lt; caliper] row1 &lt;- row1[order(row1)] for(i2 in names(row1)) { row2 &lt;- d2[i2,] row2 &lt;- row2[row2 &lt; caliper] row2 &lt;- row2[order(row2)] for(i3 in names(row2)) { val &lt;- d3[i3,i1] if(val &lt; caliper) { results &lt;- rbind(results, data.frame(v1=i1, v2=i2, v3=i3, d1=row1[i2], d2=row2[i3], d3=val)) } } } } head(results) </code></pre>
    singulars
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

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