如何从`svyby`中获取可读表格?

时间:2018-02-01 10:06:09

标签: r survey

我想最终得到一个三维表,显示每个变量的比例(即百分比),由两个二分变量分开。这是我编写的代码,我认为它将生成我正在寻找的简化版本

> Testdesign <- svydesign(id = ~V021, strata= ~V022, weights = ~SAMPW, data= testydf)
> Tableone <- svyby( ~ V104 + V025  , ~ V013 + V502 , Testdesign , svyciprop,
 vartype="ci", method="beta", na.rm = TRUE 

然后要有一个可读表我明白它必须通过ftable,但我不明白错误。我看到this explanation,但遗憾的是,我还没有为我清理事情

> ftable(Tableone)

Error in rbind(matrix("", nrow = length(xcv), ncol = length(xrv)), 
charQuote(makeNames(xrv)),  : 
number of columns of matrices must match (see arg 3)

我可以尝试使用Crosstab函数,但我不确定如何将其与复杂的调查数据相结合(因此开始时选择survey

stargazer看起来很有前景,但它似乎没有正确读取svyby输出,所以ftable似乎仍然是必要的?

编辑:这是一个可以与上面的代码一起使用的子集,对于那些神奇的文本块感到抱歉

 > testydf <- structure(list(V001 = c(1, 1, 1, 1, 1, 2, 2, 2, 3, 3, 3, 3, 3, 
4, 4, 4, 5, 5, 5, 5, 5, 5, 6, 6, 6, 7, 7, 7, 7, 7), V002 = c(124, 
166, 178, 178, 216, 2, 2, 214, 34, 149, 167, 221, 248, 62, 72, 
196, 6, 14, 51, 101, 188, 221, 129, 167, 186, 14, 18, 26, 97, 
111), V003 = c(2, 2, 2, 3, 2, 1, 2, 2, 2, 2, 1, 2, 2, 2, 2, 2, 
2, 1, 2, 2, 2, 2, 6, 6, 3, 2, 1, 2, 2, 3), V004 = c(1, 1, 1, 
1, 1, 2, 2, 2, 3, 3, 3, 3, 3, 4, 4, 4, 5, 5, 5, 5, 5, 5, 6, 6, 
6, 7, 7, 7, 7, 7), V005 = c(118748, 118748, 118748, 118748, 118748, 
1258174, 1258174, 1258174, 1931847, 1931847, 1931847, 1931847, 
1931847, 47672, 47672, 47672, 911631, 911631, 911631, 911631, 
911631, 911631, 2005349, 2005349, 2005349, 1060772, 1060772, 
1060772, 1060772, 1060772), V013 = c(3, 3, 3, 1, 3, 5, 1, 5, 
3, 2, 7, 2, 4, 2, 2, 7, 5, 3, 2, 5, 2, 4, 1, 1, 2, 2, 4, 7, 3, 
1), V106 = c(2, 3, 1, 1, 1, 1, 1, 1, 1, 1, 0, 1, 1, 2, 1, 1, 
0, 1, 2, 0, 1, 1, 1, 0, 1, 1, 1, 1, 1, 1), SEX = c(1, 1, 1, 1, 
1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 
1, 1, 1, 1, 1), V502 = c(0, 1, 1, 0, 1, 2, 0, 1, 0, 1, 1, 1, 
1, 0, 1, 1, 1, 1, 1, 1, 1, 1, 0, 2, 2, 1, 1, 1, 1, 0), V025 = c(1, 
1, 1, 1, 1, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 
2, 2, 2, 2, 2, 2, 2, 2), V190A = c(4, 5, 1, 1, 2, 2, 2, 1, 1, 
4, 4, 2, 1, 5, 5, 3, 1, 1, 1, 1, 1, 5, 2, 4, 2, 2, 2, 2, 2, 3
), V130 = c(2, 2, 4, 4, 6, 5, 5, 1, 6, 6, 3, 2, 6, 3, 5, 3, 1, 
1, 1, 1, 1, 1, 5, 5, 6, 5, 5, 2, 2, 2), V131 = c(8, 1, 1, 1, 
1, 1, 1, 1, 5, 5, 5, 1, 5, 10, 2, 4, 1, 1, 1, 1, 1, 1, 2, 1, 
5, 8, 8, 8, 8, 8), V732 = c(NA_real_, NA_real_, NA_real_, NA_real_, 
NA_real_, NA_real_, NA_real_, NA_real_, NA_real_, NA_real_, NA_real_, 
NA_real_, NA_real_, NA_real_, NA_real_, NA_real_, NA_real_, NA_real_, 
NA_real_, NA_real_, NA_real_, NA_real_, NA_real_, NA_real_, NA_real_, 
NA_real_, NA_real_, NA_real_, NA_real_, NA_real_), V836 = c(1, 
1, 1, 0, 1, 1, 1, 1, 1, 1, 1, 1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99, 99), V791 = c(0, 
0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 
0, 0, 0, 0, 0, 0, 0, 0), V791B = c(0, 0, 0, 0, 0, 0, 0, 0, 0, 
0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0
), HIV02 = c(6918, 6928, 6882, 6883, 6919, 9523, 9521, 10340, 
446, 441, 445, 437, 436, 14071, 14065, 14066, 11443, 12111, 11328, 
11395, 11644, 11309, 14547, 14626, 15067, 12464, 12466, 12459, 
12462, 12468), HIV03 = structure(c(0, NA, 0, 0, 0, 1, 0, 0, 0, 
0, 0, 0, 0, 0, 0, 1, 1, 0, 0, 0, 0, 0, 0, 0, 1, 0, 0, 0, 0, 0
), labels = structure(c(0, 1, 2, 3, 4, 5, 6, 7, 9), .Names = c("HIV negative", 
"HIV  positive", "HIV2 positive", "HIV1 & HIV2 positive", "ERROR : V-, W+, M+", 
"ERROR : V-, W+, M-", "ERROR : V-, W-, M+", "Indeterminate", 
"Inconclusive")), class = "labelled"), HIV05 = c(109959, 109959, 
109959, 109959, 109959, 1212890, 1212890, 1212890, 1856921, 1856921, 
1856921, 1856921, 1856921, 45717, 45717, 45717, 864793, 864793, 
864793, 864793, 864793, 864793, 1974222, 1974222, 1974222, 1066907, 
1066907, 1066907, 1066907, 1066907), HIV06 = structure(c(0, 1, 
0, 0, 0, 1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1, 1, 0, 0, 0, 0, 0, 0, 
0, 1, 0, 0, 0, 0, 0), labels = structure(c(0, 1), .Names = c("Negative", 
"Positive")), class = "labelled"), V104 = c(0, 0, 0, 0, 0, 0, 
0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 
0, 0, 0), TRANSEX = c(NA_real_, NA_real_, NA_real_, NA_real_, 
NA_real_, NA_real_, NA_real_, NA_real_, NA_real_, NA_real_, NA_real_, 
NA_real_, NA_real_, NA_real_, NA_real_, NA_real_, NA_real_, NA_real_, 
NA_real_, NA_real_, NA_real_, NA_real_, NA_real_, NA_real_, NA_real_, 
NA_real_, NA_real_, NA_real_, NA_real_, NA_real_), HIVKNOW = c(3, 
2, 2, 2, 4, 7, 7, 4, 5, 4, 7, 4, 5, 4, 3, 4, 3, 6, 4, 2, 3, 3, 
3, 4, 5, 5, 5, 4, 6, 4), HIVKCAT = c(1, 0, 0, 0, 1, 2, 2, 1, 
2, 1, 2, 1, 2, 1, 1, 1, 1, 2, 1, 0, 1, 1, 1, 1, 2, 2, 2, 1, 2, 
1), V021 = c(1, 1, 1, 1, 1, 2, 2, 2, 3, 3, 3, 3, 3, 4, 4, 4, 
5, 5, 5, 5, 5, 5, 6, 6, 6, 7, 7, 7, 7, 7), V022 = c(18, 18, 18, 
18, 18, 13, 13, 13, 33, 33, 33, 33, 33, 11, 11, 11, 25, 25, 25, 
25, 25, 25, 9, 9, 9, 27, 27, 27, 27, 27), V023 = c(18, 18, 18, 
18, 18, 13, 13, 13, 33, 33, 33, 33, 33, 11, 11, 11, 25, 25, 25, 
25, 25, 25, 9, 9, 9, 27, 27, 27, 27, 27), V024 = c(2, 2, 2, 2, 
2, 2, 2, 2, 3, 3, 3, 3, 3, 1, 1, 1, 2, 2, 2, 2, 2, 2, 1, 1, 1, 
2, 2, 2, 2, 2), SAMPW = c(0.109959, 0.109959, 0.109959, 0.109959, 
0.109959, 1.21289, 1.21289, 1.21289, 1.856921, 1.856921, 1.856921, 
1.856921, 1.856921, 0.045717, 0.045717, 0.045717, 0.864793, 0.864793, 
0.864793, 0.864793, 0.864793, 0.864793, 1.974222, 1.974222, 1.974222, 
1.066907, 1.066907, 1.066907, 1.066907, 1.066907)), .Names = c("V001", 
"V002", "V003", "V004", "V005", "V013", "V106", "SEX", "V502", 
"V025", "V190A", "V130", "V131", "V732", "V836", "V754CP", "V754DP", 
"V754JP", "V754WP", "V756", "V823", "V824", "V761", "V525", "V732.1", 
"V791", "V791B", "HIV02", "HIV03", "HIV05", "HIV06", "V104", 
"TRANSEX", "HIVKNOW", "HIVKCAT", "V021", "V022", "V023", "V024", 
"SAMPW"), row.names = c("1", "2", "3", "4", "5", "6", "7", "8", 
"9", "10", "11", "12", "13", "14", "15", "16", "17", "18", "19", 
"20", "21", "22", "23", "24", "25", "26", "27", "28", "29", "30"
), class = "data.frame")

我尝试了因素更新无济于事非常感谢!

1 个答案:

答案 0 :(得分:0)

?svyciprop

公式指定单个二进制变量的模型公式

设置

library(survey)
data(api)
dclus1<-svydesign(id=~dnum, fpc=~fpc, data=apiclus1)

正确破解

svyciprop( ~ I( api99 > 500 ) + I( api00 > 500 ) , dclus1 )

也正确破解

svyby( ~ I( api99 > 500 ) + I( api00 > 500 ) , ~ sch.wide , dclus1, svyciprop)