[R] contrasts error message in lm

Marc Schwartz marc_schwartz at comcast.net
Tue Jul 31 22:15:08 CEST 2007


On Tue, 2007-07-31 at 21:28 +0200, A Serebrenik wrote:
> Dear all,
> 
> I would like to find a linear regression model for a rather large dataset 
> (27 independent variables). However, when I run lm the following error is 
> reported:
> 
> > out <- lm(Result ~ AppealA + AppealsB + AppealC + AppealD + AppealE + 
> Apply + ApplyAmount + Aprove + Closecase + Decidelocally + Healthassessment + 
> HealthassessmentHealth + Postponedecision + Propertyassessment + PropertyassessmentPropertyvalue 
> + RejectA + RejectB + RejectC + RejectD + RejectE + Reportacceptance +
> + ReportrejectionA + ReportrejectionB + ReportrejectionC + ReportrejectionD +
> + ReportrejectionE + Timeout)
> 
> Error in `contrasts<-`(`*tmp*`, value = "contr.treatment") :
>          contrasts can be applied only to factors with 2 or more levels
> 
> 
> I've checked the documenation but somehow I fail to find an explanation 
> what does it mean...
> 
> Thanks in advance,
> Alexander Serebrenik


One or more of your IV's has a single factor level.  In other words for
example, instead of having "Yes" and "No" values, it only has one of the
two. Treatment contrasts are predicated on having a base or reference
level, against which the other levels for the factor are compared.

This can occur either natively in the source dataset, or as a
consequence of removing rows in your dataset that have missing values.
In the latter case, a factor with multiple levels may end up having a
single level as a consequence of the removal of all rows with the other
levels of that factor.

By default, R (and many other statistical applications) will remove
records containing missing values prior to applying regression
techniques on the dataset.

See ?lm and ?na.action (referenced in the former) for more information.

You can use:

  DF.tmp <- na.omit(DF)

where 'DF' is your source dataset, to remove rows with missing values.

Then use:

  summary(DF.tmp)

to review the residual dataset created and see what you have left.


In looking at your formula above, it may be that your variables are not
in a single data frame (I don't see a 'data' argument). In which case,
you can achieve a similar result by using:

  DF <- model.frame(Result ~ TheRestOfYourFormulaHere..)


and then using:

  summary(DF)


See ?model.frame

HTH,

Marc Schwartz



More information about the R-help mailing list