0

I am trying to run a regression on some factor variables, I am getting the error "contrasts can be applied only to factors with 2 or more levels". I have referenced the dubug_contr_error code from How to debug "contrasts can be applied only to factors with 2 or more levels" error?, and have found that one R has dropped a level from two variables where I have made a factor.

Any way to force R to recognize all the values?

dat<-dat%>%
  mutate(operatortype=ifelse(`Facility Operator`%in% c("AGS", "AHTNA (GUARD)","ASSET (GUARD)", "CCA", "CFDFC", 
                                                       "GEO", "GPS-ASSET", "ICA", 
                                                       "LASALLE CORRECTIONS", "M&TC"), "Private", operatortype))%>%
  mutate(operatortype=ifelse(`Facility Operator`%in% c("COUNTY", "COUNTY (CORRECTIONS)", "COUNTY (JAILER)",
                                                       "COUNTY (PRISON)", "COUNTY (SHERIFF)", "BOP",
                                                       "CITY"), "Public", operatortype))

dat$ownertype<-NA
dat<-dat%>%
  mutate(ownertype=ifelse(`Facility Owner`%in% c("CCA","GEO"), "Private", ownertype))%>%
  mutate(ownertype=ifelse(`Facility Owner`%in% c("COUNTY", "COUNTY (CORRECTIONS)", "COUNTY (JAILER)",
                                                       "COUNTY (PRISON)", "COUNTY (SHERIFF)", "CITY"), "City/County", ownertype))%>%
  mutate(ownertype=ifelse(`Facility Owner`%in% c("BOP", "OFDT"), "Federal", ownertype))%>%
  mutate(ownertype=ifelse(`Facility Owner`%in% c("ICE"), "ICE", ownertype))
  


dat$providertype<-NA
dat<-dat%>%
  mutate(providertype=ifelse(`Provider Type`!="IHSC"& !is.na(`Provider Type`), "Private", "Public"))
dat$providertype<-factor(dat$providertype, levels=c("Public", "Private"), ordered = TRUE)
dat$ownertype<-factor(dat$ownertype, levels=c("ICE", "City/County", "Federal", "Private"), ordered = FALSE)
dat$operatortype<-factor(dat$operatortype, levels=c("Public", "Private"), ordered = TRUE)

table(thefinale$operatortype)

 Public Private 
   1106    1126 
table(thefinale$providertype)

 Public Private 
    901    1702 
table(thefinale$ownertype)

        ICE City/County     Federal     Private 
        143        1681          12         367


debug_contr_error()

$levels$operatortype
[1] "Private"

$levels$ownertype
[1] "ICE"         "City/County" "Private"    

$levels$providertype
[1] "Public"  "Private"


josephn
  • 39
  • 5
  • 1
    Please provide a reproducible example containing output of `dput(dat)` and the exact function call of the regression model. – danlooo May 12 '22 at 07:37
  • I tried to create a reproducible example isolating just the columns of interest (with repeated values of the original strings and a vector using ```rbinom``` ) but the error is not reproducible there. I'm not sure why. – josephn May 12 '22 at 09:01
  • Try a very simple model formula and ensure that each covariate has >=2 levels. Note that NA does not count as a level here. – danlooo May 12 '22 at 11:35

0 Answers0