I'm running a linear mixed model and keep getting the error -
Error in `contrasts<-`(`*tmp*`, value = contr.funs[1 + isOF[nn]]) :
contrasts can be applied only to factors with 2 or more levels
First off I have reviewed the helpful link - Error in contrasts when defining a linear model in R
Yet when I try various means of assessing my factor levels, summary(df)
, is.factor(df$variable)
, nlevels(df$variable)
and the suggested ifelse(n <- sapply(m, function(x) length(levels(x))) == 1, "DROP", "NODROP")
The latter for example gives me "NODROP" on every variable. While nlevel(df$variable) states 2, Summary(df) accurately quantifies how many rows are in each of the 2 levels of this factor too.
Consistently I see the specific variable that is stopping the model run (when I remove it, the model runs without issue) be noted as having 2 factors which is correct and in my mind should run. I've had no issues running this exact model with a different binary factor variable, but this one seems to consistently run into this factor issue that doesn't make sense to me given the checks seemingly confirming that R is treating the column as a 2-level factor.
Is there something I can do to resolve this seeming error that wasn't mentioned in the prior thread? There are no exotic characters - simply "CB" and "BA" which are tissue region identifiers.
The exact line of code is below.
models <- dlply(Controls, "PROTEIN", function(df) lmer(ABUNDANCE ~ REGION + PMI + AGE + (1|PEPTIDE), data=df, REML=FALSE))
REGION is the problem child. I tried options(lmerControl=list(check.nlev.gtr.1 = "ignore"))
to no avail as well.
Thanks for any suggestions.