[R] Regression with factor having1 level

Ben Bolker bbolker at gmail.com
Fri Mar 11 01:29:27 CET 2016

Robert McGehee <rmcgehee <at> gmail.com> writes:

> Hello R-helpers,
> I'd like a function that given an arbitrary formula and a data frame
> returns the residual of the dependent variable, and maintains all
>  NA values.
> Here's an example that will give me what I want if my formula is y~x1+x2+x3
> and my data frame is df:
> resid(lm(y~x1+x2+x3, data=df, na.action=na.exclude))
> Here's the catch, I do not want my function to ever fail due to a factor
> with only one level. A one-level factor may appear because 1) the user
> passed it in, or 2) (more common) only one factor in a term is left after
> na.exclude removes the other NA values.

 [snip to try to make Gmane happy]
> Can anyone provide me a straight forward recommendation for how 
> to do this?

  The only approach I can think of is to screen for single-level factors
yourself and remove these factors from the
formula. It's a little tricky; you can't call model.frame() with a single-level
factor (that's where the error comes from), and you have to strip out NA
values yourself so you can see which factors end up with only a single
level after NA removal.

More information about the R-help mailing list