2016-09-21 17 views
4

contrasts<-のエラー(*tmp*、値= contr.funs [1 + isOF [nn]]): コントラストは、 2つ以上のレベルsvychisqのエラー - 'コントラストは2つ以上のレベルの要素に適用できます'

調査パッケージでsvychisq関数を使用しようとすると、このエラーが発生します。しかし、この関数はsvytable関数を使うと機能します。 - 2つ以上のレベルの要因に関するエラー交渉が死亡した変数は、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 

EDIT有する因子である:ここでは、問題の小さなサブセット例です

最小限の再現性例えば
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) 
+0

クラス 'tbl_svy'と' survey.design2'がサポートされていません(?sの 'を参照してください。 vychisq')。 – cuttlefish44

答えて

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) 
関連する問題