#In order to implement the code, the data must be in two way table format. #The data must be read into R using matrix(scan("data")). #Function to compute the means of each level effect sct.effects <- function(y){ n.dim <- dim(y)[1] mean.diff <- c() for (i in 1:n.dim){ mean.diff <- c(mean.diff,mean(y[,i])) } list(effects=mean.diff) } #Function for residual sct.resid <- function(orig.matrix,col.effect,row.effect){ n.dim <- dim(orig.matrix) median.matrix <- matrix(1,n.dim[1],n.dim[2]) for(i in 1:n.dim[2]){ orig.matrix[,i] <- orig.matrix[,i] - row.effect } for(j in 1:n.dim[1]){ orig.matrix[j,] <- orig.matrix[j,] - col.effect } grand.median <- median(orig.matrix) median.matrix <- grand.median*median.matrix orig.matrix <- orig.matrix - median.matrix list(col=col.effect,row=row.effect,overall=grand.median,residual=orig.matrix) } #Function that computes results from SCT. sct.fn <- function(x) { temp.dim <- dim(x) row.matrix <- matrix(0,temp.dim[1],temp.dim[1]) col.matrix <- matrix(0,temp.dim[2],temp.dim[2]) temp.coldiff <- c() med.coldiff <- 0 n <- temp.dim[2] m <- temp.dim[1] for( i in 1:4 ) { for( j in (i+1):5){ temp.coldiff <- x[,i]-x[,j] med.coldiff <- median(temp.coldiff) col.matrix[j,i] <- med.coldiff } } for( i in 1:m-1 ) { for( j in (i+1):m){ temp.coldiff <- x[i,]-x[j,] med.coldiff <- median(temp.coldiff) row.matrix[j,i] <- med.coldiff } } col.matrix <- col.matrix + -1*t(col.matrix) row.matrix <- row.matrix + -1*t(row.matrix) row.effect <- sct.effects(row.matrix)$effects col.effect <- sct.effects(col.matrix)$effects sct.resid(x,col.effect,row.effect) }