Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    primarykey
    data
    text
    <p>In general implicit loops are faster the explicit loops. Try taking the code inside your loop and place it in a function, and then using that function in an lapply, or sapply statement.</p> <pre><code>myfunction = function(&lt;insert relevant parameters here&gt;) { X.sample &lt;- X[ sample(1:nrow(X), 40, replace = FALSE), ] X.sample.1 &lt;- X.sample[1:20, ] X.sample.2 &lt;- X.sample[21:40, ] Y &lt;- as.data.frame(cbind(X.sample.1$ID, X.sample.1$x, X.sample.2$ID, X.sample.2$x)) cor.results &lt;- cor.test(Y[,2], Y[,4], alternative = c("greater"), method = c("pearson")) cor.results$estimate } Z = sapply(x, myfunction) #Here every element of x contains the arguments you want to pass to my function #You can pass multiple arguments separated by commas after the function name error &lt;- qt(0.975, df = (length(Z) - 1)) * (sd(Z))/sqrt(length(Z)) </code></pre> <p>You could do this, but I find it's probably better to just use the <code>boot()</code> function in the <code>boot</code> package if you can.</p> <p>As for the <code>set.seed()</code> You need to set it directly before EVERY time you generate random anything. See below.</p> <pre><code>&gt; rnorm(6) [1] 1.0915017 -0.6229437 -0.9074604 -1.5937133 0.3026445 1.6343924 &gt; set.seed(1001) &gt; rnorm(6) [1] 2.1886481 -0.1775473 -0.1852753 -2.5065362 -0.5573113 -0.1435595 &gt; set.seed(1001) &gt; rnorm(6) [1] 2.1886481 -0.1775473 -0.1852753 -2.5065362 -0.5573113 -0.1435595 &gt; rnorm(6) [1] 1.0915017 -0.6229437 -0.9074604 -1.5937133 0.3026445 1.6343924 &gt; set.seed(1001) &gt; sample(1:5,10,replace=T) [1] 5 3 3 3 3 5 1 1 2 4 &gt; sample(1:5,10,replace=T) [1] 3 1 5 3 2 5 1 2 1 4 &gt; set.seed(1001) &gt; sample(1:5,10,replace=T) [1] 5 3 3 3 3 5 1 1 2 4 &gt; rnorm(6) [1] -0.1435595 1.0915017 -0.6229437 -0.9074604 -1.5937133 0.3026445 &gt; set.seed(1001) &gt; rnorm(6) [1] 2.1886481 -0.1775473 -0.1852753 -2.5065362 -0.5573113 -0.1435595 </code></pre> <p>Hope that helps!</p> <p>When researching the <code>boot</code> function to give you an example I ran into a snag. It only ever returns one row. Strange! I might start a new question about this. In anycase, I think the <code>bootstrap()</code> function in the <code>bootstrap</code> package will do what you are looking for. Here's my example</p> <pre><code>set.seed(1001) X &lt;- rnorm(600, 7, 2) myStat &lt;- function(x, pairs) { index = sample(1:length(x),(pairs*2)) Z = cor(X[index[1:(length(index)/2)]], X[index[((length(index)/2)+1):length(index)]]) return(Z) } b=bootstrap(X,1000,myStat,pairs=20) Z &lt;- b$thetastar error &lt;- qt(0.975, length(Z)-1 * sd(Z)/sqrt(length(Z))) </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.
    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