0

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.

  • 1
    It looks like you checked the number of levels per variable, but possibly not the number of levels per variable *within* each "PROTEIN" group. Since you're fitting a model separately for each protein group this is the level you must do your checks at. – aosmith Jul 06 '18 at 20:29
  • 1
    Ah thank you. I realized it was related to the number of levels per variable within each protein. Much appreciated. Just need to figure out how to filter by that now, many thanks again! – RLearner256 Jul 08 '18 at 14:23

0 Answers0