svychisq出错 - '对比可以应用于2级或更高级别的因素'

时间:2016-09-21 15:41:11

标签: r survey

contrasts<-中的错误(*tmp*,值= contr.funs [1 + isOF [nn]]):   对比只能适用于具有2级或更多级别的因素

每当我尝试在调查包中使用svychisq函数时,我都会收到此错误。但是当我使用svytable函数时,该函数可以正常工作。该错误涉及具有2个或更多级别的因子--DIED变量是具有2个级别0和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

编辑:

这是问题的一个小子集示例

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)

1 个答案:

答案 0 :(得分:6)

感谢最小的可重复示例

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)