1

I am trying to include an independent variable in my logistic regression model using the function surveyglm.

It keeps giving me the error code

"Error in contrasts<-(*tmp*, value = contr.funs[1 + isOF[nn]]) : contrasts can be applied only to factors with 2 or more levels".

The code is:

finaldes<-svydesign(id=~CPSUM, strata=~CSTRATM, weights=~PATWT, nest=TRUE,data=newpts)

(log<-svyglm(formula=offlabel ~ AGER + siteofcare + MSA, design=finaldes))

summary(log)

And the variable that is giving me trouble is siteofcare. When I check the number of levels, I get 2 as an output so I don't understand why this isn't working.

> levels(newpts$siteofcare)
[1] "Outpt" "Hosp" 
> nlevels(newpts$siteofcare)
[1] 2
M--
  • 25,431
  • 8
  • 61
  • 93
AVR
  • 31
  • 2
  • `x = factor(1, levels=0:1)` had two levels but will throw an error. Hm where have you used droplevels? – user20650 Jun 15 '17 at 21:28
  • I used it when creating the variable and adding it to my dataset. newpts$siteofcare<-droplevels(siteofcare), where siteofcare is the original with extra levels. – AVR Jun 16 '17 at 14:56
  • 1
    okay, does you data have any missing rows. If so, perhaps the rows that are removed for this reason in the modelling, only leave one level of that variable. What does `table(finaldes$variables$siteofcare)` give – user20650 Jun 16 '17 at 15:09
  • If you `dput` your dataset we may be able to reproduce problem. – Carl Jun 16 '17 at 17:04

1 Answers1

0

coerce all four of the columns used in your svyglm call to factor types in the data.frame newpts before the svydesign statement

Anthony Damico
  • 5,779
  • 7
  • 46
  • 77