[R] Regression with factor having1 level

Ben Bolker
2016-03-11T00:29:27Z

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.