#Given an n x p matrix x, this function returns a n x 1 vector #corresponding to unique preceeding rows of x. It is used in #constructing a Lack of Fit test for regression models. rowmatch<-function(x) { x<-as.matrix(x) u<-unique(x) xchar<-apply(x,1,function(x){paste(x,collapse="")}) uchar<-apply(u,1,function(x){paste(x,collapse="")}) charmatch(xchar,uchar) } #This function performs a Lack of Fit test for a linear #regression model. # #rm - model object corresponding to a linear regression model. lof<-function(rm) { mf<-model.frame(rm) x<-mf[,2:ncol(mf)] m<-rowmatch(x) if(length(m)==max(m)) stop("need at least one replicate") newdata<-data.frame(mf,m=m) u=paste(as.character(rm$call$formula)[2],"~","factor(m)") fm<-lm(as.formula(u),data=newdata) anova(rm,fm) }