Note that there are some explanatory texts on larger screens.

plurals
  1. POData transformation avoiding nested loops in R
    text
    copied!<p>I have a contingency table data matrix with 6 columns and 37 rows. I need to apply a Chi squared transformation to give me Row profiles and Column profiles for a correspondence analysis.</p> <p>Unfortunately I've been told I will need to use nested loops to transform the data and carry out the CA (rather than doing it the more sensible ways in R). I was given the structure to use for my nested loop:</p> <pre><code>transformed.data=data0 for (row.index in 1:nrow(data)) { for (col.index in 1:ncol(data)) { transfomed.data[row.index,col.index]= "TRANSFORMATION"[row.index,col.index] } } </code></pre> <p>From what i understand by using the nested loop it will apply my <em>"TRANSFORMATION"</em> first to the rows and then to the columns.</p> <p>The transformation I want done on the data to get the row profiles is:</p> <blockquote> <p><strong>(</strong> X( <em>ij</em> ) / sum( X( <strong><em>i</em></strong> ) ) <strong>)</strong> / sqrt( sum( X( <strong><em>j</em></strong> ) ) ) </p> </blockquote> <p>While the transformation I want done on the data to get the column profiles is:</p> <blockquote> <p><strong>(</strong> X( <em>ij</em> ) / sum( X( <strong><em>j</em></strong> ) ) <strong>)</strong> / sqrt( sum( X( <strong><em>i</em></strong> ) ) ) </p> </blockquote> <p>What would I enter as my <em>"TRANSFORMATION"</em> in the last line of the nested loop to get it to output my desired transformation for profiles. Otherwise if I've miss understood the point of a nested loop here please describe what it would allow me to do.</p> <p>This is the code for a subset of my data:</p> <pre><code>matrix(c(15366,2079,411,366,23223,2667,699,819,31632,2724,717,1473,49938,3111,1062,11964) ,nrow=4,ncol=4,byrow=T) </code></pre> <p>So using this subset alone I would expect the row profile for the first row to be: </p> <pre><code>0.002432689 0.0003291397 6.506803e-05 5.794379e-05 </code></pre> <p>And the column profile for the first column to be:</p> <pre><code>0.0009473414, 0.0132572344, 0.0572742202, 0.0132863528 </code></pre>
 

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