0

I am creating a logistic regression model using some data set. Despite removing factors containing only one level I am still getting the error:

> fit <- glm(Intraop_Blood_Products ~ ., family = binomial(), na.action = na.omit, data = rel_data)
Error in `contrasts<-`(`*tmp*`, value = contr.funs[1 + isOF[nn]]) : 
  contrasts can be applied only to factors with 2 or more levels

Although I cannot share the particular composition of the data. The function str(rel_data) shows that all factor variables contain two or more levels. Additionally, I tried using complete_case_data <- rel_data[complete.cases(rel_data),] to remove all NAs from the data set as another user suggested that glm() could be throwing this error due to the presence of NAs, yet the error persists. If you could offer any insight please let me know. Thank you!

  • a factor can have two or more levels but only one level present in the data ex - `factor(c("a"),levels = c("a","b"))` . check the amount of unique values actually present in your data. – Mike Jun 23 '21 at 18:16
  • 1
    Solved the issue. Initially na.omit would remove rows containing NAs which resulted in a data set which previously had categorical variables labelled as having two or more factor levels to have only one after then rows were eliminated. The same happened for complete.cases(). I had to check summary(complete_case_data) for each variable to identify the issue. – Daniel Rodriguez Jun 23 '21 at 18:25

0 Answers0