title: R: Monte Carlo procedure by Permute or Sample function to generate null distribution
body: <p><a href="https://i.stack.imgur.com/scs4E.png" rel="nofollow noreferrer"><img src="https://i.stack.imgur.com/scs4E.png" alt="sample data" /></a></p>
  <p>From this data set, I have all patient samples (69 rows total) assigned by my cluster analysis and clusters were labelled as column 3 &quot;Cluster.assigned&quot;, 8 clusters in total, UNEQUAL size per cluster. Other columns contains variables, of that I want to test the numerical variables (such as Age) to see if anything is enriched compared to random by chance.</p>
  <p>Now I'm hitting roadblocks due to my coding ability. But my idea is to see the real data as <strong>Observed</strong>, then shuffle the labels of clusters by using sample or permute function, like a Monte Carlo simulation, say 1000 times and call that simulated distribution as <strong>Expected</strong>.</p>
  <p>Using Age column as an example:</p>
  <pre><code>#minimum dummy 30-row data
  Patient.ID &lt;-c(&quot;S3077497&quot;,&quot;S1041120&quot;,&quot;S162465&quot;,&quot;S563275&quot;,&quot;S2911623&quot;,&quot;S3117192&quot;,&quot;S2859024&quot;,&quot;S2088278&quot;,&quot;S3306185&quot;,&quot;S190789&quot;,&quot;S12146451&quot;,&quot;S2170842&quot;,&quot;S115594&quot;,&quot;S2024203&quot;,&quot;S1063872&quot;,&quot;S2914138&quot;,&quot;S303984&quot;,&quot;S570813&quot;,&quot;S2176683&quot;,&quot;S820460&quot;,&quot;S1235729&quot;,&quot;S3009401&quot;,&quot;S2590229&quot;,&quot;S629309&quot;,&quot;S120256&quot;,&quot;S2572773&quot;,&quot;S3180483&quot;,&quot;S3032079&quot;,&quot;S3217608&quot;,&quot;S5566943&quot;)
  
  Cluster.assigned &lt;- c(&quot;cluster1&quot;,&quot;cluster1&quot;,&quot;cluster1&quot;,&quot;cluster1&quot;,&quot;cluster1&quot;,&quot;cluster1&quot;,&quot;cluster1&quot;,&quot;cluster2&quot;,&quot;cluster2&quot;,&quot;cluster2&quot;,&quot;cluster2&quot;,&quot;cluster2&quot;,&quot;cluster2&quot;,&quot;cluster2&quot;,&quot;cluster2&quot;,&quot;cluster2&quot;,&quot;cluster2&quot;,&quot;cluster2&quot;,&quot;cluster2&quot;,&quot;cluster2&quot;,&quot;cluster3&quot;,&quot;cluster3&quot;,&quot;cluster3&quot;,&quot;cluster3&quot;,&quot;cluster3&quot;,&quot;cluster3&quot;,&quot;cluster3&quot;,&quot;cluster4&quot;,&quot;cluster4&quot;,&quot;cluster4&quot;)
  
  Age &lt;- c(61,80,78,69,57,70,60,59,72,82,66,68,70,62,82,80,67,77,74,77,74,74,64,70,74,64,54,73,58,87)
  
  CLL_3S &lt;-cbind(Patient.ID, Cluster.assigned, Age)
  </code></pre>
  <p>To see if there's any cluster that has patients enriched in certain age, the null hypothesis is there's no difference in age distribution across clusters.
  Now I should shuffle the patient labels or shuffle the Age data, say 1000 times, then I should have a simulated dataframe, from which I should be able to calculate the mean and standard deviation of the simulated (Expected)</p>
  <pre><code>#I image to use shuffle to permute 1000 times
  #And combine the simulated into a massive dataframe
   shuffled &lt;- numeric(length=1000)
   N &lt;-nrows(CLL_3S)
  
   set.seed(123)
    for (i in seq_len(length(shuffled) -1)) {
        perm &lt;- shuffle(N)
        .........
  </code></pre>
  <p>Next step is I will then use the actual observation of patient age in each cluster to calculate enrichment by using a Z score. Say obs (value - Expected Mean)/SD.</p>
  <p>Once this process is automated, then I can apply this to other columns of interest and other datasets with different numbers of clusters. I have read something about the sample() and shuffle() but it doesnt really help me solving this particular problem...</p>