【发布时间】:2017-01-29 23:14:11
【问题描述】:
contrasts<-(*tmp*, value = 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)
【问题讨论】:
-
class
tbl_svy和survey.design2不受支持(请参阅?svychisq)。