svychisq 的错误 - “对比可以应用于具有 2 个或更多级别的因素" [英] Error with svychisq - 'contrast can be applied to factors with 2 or more levels'
本文介绍了svychisq 的错误 - “对比可以应用于具有 2 个或更多级别的因素"的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
对比度<-
(*tmp*
, value = contr.funs[1 + isOF[nn]]) 中的错误:对比只能应用于具有 2 个或更多水平的因素
Error in contrasts<-
(*tmp*
, value = contr.funs[1 + isOF[nn]]) :
contrasts can be applied only to factors with 2 or more levels
每当我尝试使用调查包中的 svychisq 函数时,都会收到此错误.但是,当我使用 svytable 函数时,该函数有效.该错误涉及具有 2 个或更多级别的因子 - DIED 变量是具有 2 个级别(0 和 1)的因子.
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
这是问题的一个小子集示例
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)
推荐答案
感谢最小的可重现示例
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)
这篇关于svychisq 的错误 - “对比可以应用于具有 2 个或更多级别的因素"的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文