RSS=function(data,groups,subset){ nsub=length(subset) ids=NULL for(i in 1:nsub) ids=c(ids,which(groups==subset[i])) gr.avg=mean(data[ids]) residuals=data[ids]-gr.avg result=sum(residuals^2) return(result) }