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

From: Prof Brian Ripley <ripley_at_stats.ox.ac.uk>
Date: Sat 25 Jun 2005 - 07:44:56 EST

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@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

______________________________________________
R-help@stat.math.ethz.ch mailing list
https://stat.ethz.ch/mailman/listinfo/r-help
PLEASE do read the posting guide! http://www.R-project.org/posting-guide.html
Received on Sat Jun 25 07:49:04 2005

This archive was generated by hypermail 2.1.8 : Fri 03 Mar 2006 - 03:33:02 EST