I have been trying to fit a sequential polynomial regression model in R and have encountered the following issue: while poly(x)
offers a quick way, this function does not respect the hierarchical principle, which roughly states that before moving to higher orders, all terms of lower order should have been included in the model.
One solution here, might be to handpick the order of entrance into the model yourself as I have done with a toy dataset below
pred<-matrix(c(rnorm(30),rnorm(30)),ncol=2)
y<-rnorm(30)
polys<-poly(pred,degree=4,raw=T)
z<-matrix(c(
#order 2
polys[,2],polys[,6],polys[,9],
#order 3
polys[,3],polys[,7],polys[,10],polys[,12],
#order 4
polys[,4],polys[,8],polys[,11],polys[,13],polys[,14]),
ncol=12)
polyreg3<-function(x){
BICm<-rep(0,dim(x)[2])
for(i in 1:dim(x)[2]){
model<-lm(y~pred[,1]+pred[,2]+x[,1:i]) #include one additional term each time
BICm[i]<-BIC(model)
}
list(BICm=BICm)
}
polyreg3(z)
which.min(polyreg3(z)$BICm)
but this is largely impractical for larger degrees of polynomials. I was wondering then, is there a way to deal with this issue, preferably by adapting my code?