Error in contrasts<-
(*tmp*
, value = contr.funs[1 + isOF[nn]]) :
contrasts can be applied only to factors with 2 or more levels
I'm getting this error whenever I try using the svychisq function in the survey package. However the function works when I use the svytable function. The error talks about a factor with 2 or more level - the DIED variable is a factor with 2 levels, 0 and 1.
> svytable(~COHORT+DIED, design=df_srvy)
DIED
COHORT 0 1
1997 26726.584 1647.118
2000 26958.912 1628.692
2003 30248.533 1599.094
2006 36602.173 1586.526
2009 44004.732 2531.597
2012 56037.874 2766.386
> svychisq(~COHORT+DIED, design=df_srvy)
Error in `contrasts<-`(`*tmp*`, value = contr.funs[1 + isOF[nn]]) :
contrasts can be applied only to factors with 2 or more levels
EDIT:
Here's a small subset example of the problem
sample <- structure(list(DISCWT = c(1.36973, 1.4144, 1.41222, 1.41222,
1.4144, 1.4144, 1.41222, 1.41222, 1.4144, 1.41222, 1.41222, 1.41222,
1.41222, 1.4144, 1.4144), COHORT = c(1997L, 2012L, 2000L, 2003L,
2006L, 2006L, 2009L, 2012L, 2012L, 1997L, 2003L, 2006L, 2006L,
2003L, 1997L), DIED = c(1L, 0L, 0L, 0L, 1L, 0L, 0L, 0L, 0L, 1L,
0L, 0L, 0L, 0L, 1L)), row.names = c(NA, -15L), class = c("tbl_df",
"tbl", "data.frame"), .Names = c("DISCWT", "COHORT", "DIED"))
sample_survey <- sample %>% as_survey_design(., weight = DISCWT)
svychisq(~DIED+COHORT, sample_survey)
This error occurs when you attempt to fit a regression model using a predictor variable that is either a factor or character and only has one unique value.
In order to set a contrast in R, you can use the contr. _X_() function for treatment, sum, and Helmert contrasts, or define any contrast manually. Be aware that this changes your dataset. You might want to consider creating a new variable as a copy of your original one, and set the contrasts on that variable.
Factor in R is a variable used to categorize and store the data, having a limited number of different values. It stores the data as a vector of integer values. Factor in R is also known as a categorical variable that stores both string and integer data values as levels.
thanks for the minimal reproducible example
library(srvyr)
library(survey)
sample <- structure(list(DISCWT = c(1.36973, 1.4144, 1.41222, 1.41222,
1.4144, 1.4144, 1.41222, 1.41222, 1.4144, 1.41222, 1.41222, 1.41222,
1.41222, 1.4144, 1.4144), COHORT = c(1997L, 2012L, 2000L, 2003L,
2006L, 2006L, 2009L, 2012L, 2012L, 1997L, 2003L, 2006L, 2006L,
2003L, 1997L), DIED = c(1L, 0L, 0L, 0L, 1L, 0L, 0L, 0L, 0L, 1L,
0L, 0L, 0L, 0L, 1L)), row.names = c(NA, -15L), class = c("tbl_df",
"tbl", "data.frame"), .Names = c("DISCWT", "COHORT", "DIED"))
# error because svychisq dies on tibble types
sample_survey <- sample %>% as_survey_design(., weight = DISCWT)
svychisq(~COHORT+DIED, sample_survey)
# probably somewhere around here in lumley's code
# rowvar <- unique(design$variables[, as.character(rows)])
# colvar <- unique(design$variables[, as.character(cols)])
# works fine
x <- sample
x <- data.frame(x)
sample_survey <- svydesign( ~ 1 , data = x , weight = ~ DISCWT )
svychisq(~COHORT+DIED, sample_survey)
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With