Error in `contrasts<-`(`*tmp*`, value = contr.funs[1 + isOF[nn]]

I am trying to run a loop for an anova analysis in R. But I keep on getting this error message:
Error in contrasts<-(*tmp*, value = contr.funs[1 + isOF[nn]]) : contrasts can be applied only to factors with 2 or more levels
I have read that you can get this error if your variables have less that 2 levels or have NA's. But I have double checked and this is not an issue. It seems to be related to the "Diet" variable as I can run the code if I remove it. Does anyone know why this is?

data_long <- gather(data, key="class", value="value", -Diet, -Gender, -Weight, -Sample_ID)
head(data_long)

    Weight Diet    Sample_ID       Gender      class      value
1   LOW    MILK       8               F          CE18.1     0
2   LOW    MILK       9               F          CE18.1     10
3   LOW    MILK       17              F          CE18.1     3
4   LOW    MILK       30              M          CE18.1     NA
5   LOW    MILK       31              M          CE18.1     1
6   LOW    MILK       37              M          CE18.1     5

str(data_long)
'data.frame':	35892 obs. of  6 variables:
 $ Weight   : Factor w/ 2 levels "LOW","HIGH": 1 1 1 1 1 1 1 1 1 1 ...
 $ Diet     : Factor w/ 2 levels "MILK","VEG": 1 1 1 1 1 1 1 1 1 2 ...
 $ Sample_ID: Factor w/ 36 levels "1","2","3","4",..: 7 8 15 26 27 32 33 34 36 2 ...
 $ Gender   : Factor w/ 2 levels "F","M": 1 1 1 2 2 2 1 2 1 2 ...
 $ class    : Factor w/ 997 levels "CE18.1","CE18.2",..: 1 1 1 1 1 1 1 1 1 1 ...
 $ value    : num  0 10 3 NA 1 5 0 0 0 0 ...

output<-lapply(split(data_long, data_long$class), function(i){
  m0<-lm(value~Diet*Weight+Gender,data=i)
  m1<-lm(value~Diet+Weight+Gender,data=i)
  anova(m0,m1)
})

You are splitting by 997 class levels, can you guarantee that the factor variables have multiple levels within these splits ?

you could throw in some print statments to see where it breaks down.

output<-lapply(split(data_long, data_long$class), function(i){

print(table(data_long$Diet))
print(table(data_long$Weight))
print(table(data_long$Gender))

  m0<-lm(value~Diet*Weight+Gender,data=i)
  m1<-lm(value~Diet+Weight+Gender,data=i)
  anova(m0,m1)
})

Thank you for your reply :slight_smile:

No, I cannot guarantee that - there are probably some classes that have all NA or 0 values. I have previously used this code on a very similar dataset also containing NA and 0's and there were no issues. Also if I remove the Diet factor from my model it will run without any problems.

I tried adding what you said and got this output - not sure what it means though:

MILK VEG
17946 17946

LOW HIGH
17946 17946

F     M 

17946 17946

MILK VEG
17946 17946

LOW HIGH
17946 17946

F     M 

17946 17946

This topic was automatically closed 21 days after the last reply. New replies are no longer allowed.

If you have a query related to it or one of the replies, start a new topic and refer back with a link.