Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    primarykey
    data
    text
    <p>first of all, you can't have done it exactly this ways as ...</p> <pre><code>&gt; corperm = function(A,B,1000) { Error: unexpected numeric constant in "corperm = function(A,B,1000" </code></pre> <p>The third argument has no name but it should have one! Perhaps you meant</p> <pre><code>&gt; corperm &lt;- function(A, B, n=1000) { # etc </code></pre> <p>Then you need to think about what do you want to achieve. Initially you have two data sets with 3 variables and then you collapse them into two vectors and compute a correlation between the permuted vectors. Why does it make sense? The structure of permuted data set should be the same as the original data set. </p> <pre><code>obs = abs(cor(A,B)) tmp = sapply(1:n,function(z) {abs(cor(sample(A,replace=TRUE),B))}) return(1-sum(obs&gt;tmp)/n) </code></pre> <p>Why do you use replace=TRUE here? This makes sense if you would like to have bootstrap CI-s but (a) it'd be better to use a dedicated function then e.g boot from package boot, and (B) you'd need to do the same with B, i.e. sample(B, replace=TRUE).</p> <p>For permutation test you sample without replacement and it makes no difference whether you do it for both A and B or only A.</p> <p>And how to get the histogram? Well, hist(tmp) would draw you a histogram of the permuted values, and obs is absolute value of the observed correlation. </p> <p>HTHAB</p> <p>(edit) </p> <pre><code>corperm &lt;- function(x, y, N=1000, plot=FALSE){ reps &lt;- replicate(N, cor(sample(x), y)) obs &lt;- cor(x,y) p &lt;- mean(reps &gt; obs) # shortcut for sum(reps &gt; obs)/N if(plot){ hist(reps) abline(v=obs, col="red") } p } </code></pre> <p>Now you can use this on a single pair of variables:</p> <pre><code>corperm(A[,1], B[,1]) </code></pre> <p>To apply it to all pairs, use <code>for</code> or <code>mapply</code>. <code>for</code> is easier to understand so I wouldn't insist in using <code>mapply</code> to get all possible pairs.</p> <pre><code>res &lt;- matrix(NA, nrow=NCOL(A), ncol=NCOL(B)) for(iii in 1:3) for(jjj in 1:3) res[iii,jjj] &lt;- corperm(A[,iii], B[,jjj], plot=FALSE) rownames(res)&lt;-names(A) colnames(res) &lt;- names(B) print(res) </code></pre> <p>To make all histograms, use plot=TRUE above.</p>
    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. VO
      singulars
      1. This table or related slice is empty.
    2. VO
      singulars
      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