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.
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:
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]
}
}
From what i understand by using the nested loop it will apply my "TRANSFORMATION" first to the rows and then to the columns.
The transformation I want done on the data to get the row profiles is:
( X( ij ) / sum( X( i ) ) ) / sqrt( sum( X( j ) ) )
While the transformation I want done on the data to get the column profiles is:
( X( ij ) / sum( X( j ) ) ) / sqrt( sum( X( i ) ) )
What would I enter as my "TRANSFORMATION" 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.
This is the code for a subset of my data:
matrix(c(15366,2079,411,366,23223,2667,699,819,31632,2724,717,1473,49938,3111,1062,11964)
,nrow=4,ncol=4,byrow=T)
So using this subset alone I would expect the row profile for the first row to be:
0.002432689 0.0003291397 6.506803e-05 5.794379e-05
And the column profile for the first column to be:
0.0009473414, 0.0132572344, 0.0572742202, 0.0132863528