Tableone 无法处理来自调查 svydesign 对象的因素

Tableone unable to process factors from a survey svydesign object

我正在尝试使用 tableone 包生成 table 一个摘要,其中数据输入是使用调查包制作的调查设计对象。根据 table 一份文档,这应该是可能的,事实上,它适用于我同事的工作区。 Tableone 能够处理连续变量,但不能处理因子。下面是一个可重现的例子

library(survey, tableone)
age<- c(55,66,77,33,44)
gender<-c("Male","Male","Female","Male","Female")
weights<-c(2.3,1.0,3.0,2.3,1.0)

df<- data.frame(age,gender,weights)
svy <- svydesign(id = ~0, data = df, weights=~weights)
t1 <- svyCreateTableOne(data = svy, vars=c("age", "gender")) 

我收到一条错误消息:“Summary.factor(c(3L, 1L), na.rm = TRUE) 出错: “总和”对因素没有意义

无论我是将 'gender' 列保留为字符,还是在创建 svy1 之前将其更改为一个因子,这种情况都会持续存在。

我是 运行 RStudio 1.1.442 上的 R ver 3.4.4。

有人遇到这个问题或有解决这个问题的建议吗?谢谢!

如果您从提供给 svyCreateTableOne 的变量列表中删除分类变量,它会起作用:

> t1 <- svyCreateTableOne(data = svy, vars=c("age", "weights")) 
> t1

                      Overall      
  n                    9.60        
  age (mean (sd))     56.60 (19.03)
  weights (mean (sd))  2.25 (0.79) 

根据我对 tableone 包的理解,您必须分别检查分类变量和定量变量。如果要按性别拆分,请将其添加为 strata:

> t1 <- svyCreateTableOne(data = svy, vars=c("age", "weights"), strata=c("gender")) 
> t1
                     Stratified by gender
                      Female        Male          p      test
  n                    4.00          5.60                    
  age (mean (sd))     68.75 (20.21) 47.93 (15.99)  0.205     
  weights (mean (sd))  2.50 (1.22)   2.07 (0.61)   0.552     

将变量 "weights" 重命名为其他名称(例如 "ps_Weights"),它将 运行。

library(survey)
library(tableone)
age<- c(55,66,77,33,44)
gender<-c("Male","Male","Female","Male","Female")
ps_weights<-c(2.3,1.0,3.0,2.3,1.0)

df<- data.frame(age,gender,ps_weights)
svy <- svydesign(id = ~0, data = df, weights=~ps_weights)
t1 <- svyCreateTableOne(data = svy, vars=c("age", "gender"))
print(t1)

                Overall      
n 9.60
age (mean (sd)) 56.60 (19.03)
gender = Male (%) 5.6 (58.3)