[R] "Error in contrasts" in step wise regression

Prof Brian Ripley ripley at stats.ox.ac.uk
Fri Jun 24 23:44:56 CEST 2005


On Fri, 24 Jun 2005, Young Cho wrote:

> Hi,
>
> I have a problem in getting step function work.

This is not coming from step(), but (AFAIK) from model.matrix() called by 
lm(). One way to debug it is to try fitting the models directly.

> I am getting the following error:
>
>> fit1 <- lm(Response~1)
>> fmla <- as.formula(paste(" ~ ",paste(colnames,collapse="+")))
>> sfit <- step(fit1,scope=list(upper= fmla,lower= ~1),k=log(nrow(dat)))
> Start:  AIC= -1646.66
> Response ~ 1
> Error in "contrasts<-"(`*tmp*`, value = "contr.treatment") :
>        contrasts can be applied only to factors with 2 or more levels
>
> But if i count the unique values in each column by
>
> A <- NULL
> for (ii in 1:length(colnames)){
>        A[ii] <- length(unique( eval(parse(text=paste('dat$',colnames[ii])))))
> }
>
> I do not see any column with only 1 value. Is there some other possible 
> reason why I am getting the error? Thanks a lot!

It says `levels', not values.  So try

 	sapply(dat, nlevels)

The values can include NA, which is not a level (usually).  E.g.

> x <- factor(c(1, NA))
> nlevels(x)
[1] 1
> length(unique(x))
[1] 2

(Incidentally, you are assuming variables are found in dat, and you should 
use

 	lm(Response ~ 1, data=dat)

to ensure that.  And your calculation can be done more legibly as

 	sapply(dat, function(x) length(unique(x)))

.)

-- 
Brian D. Ripley,                  ripley at stats.ox.ac.uk
Professor of Applied Statistics,  http://www.stats.ox.ac.uk/~ripley/
University of Oxford,             Tel:  +44 1865 272861 (self)
1 South Parks Road,                     +44 1865 272866 (PA)
Oxford OX1 3TG, UK                Fax:  +44 1865 272595




More information about the R-help mailing list