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"