如何在 R 中使用调查权重创建单向频率 table
How to create one way frequency table with survey weights in R
我需要创建单向频率 table。
tables 必须包括变量的比例。
使用prop.table(svytable()),我可以一次提取一个变量的比例。
library(survey)
round(prop.table(svytable(~varA,design=designA))*100,digits=2)
但是,我需要将每个变量的每个输出连接成一个 table 的代码。我尝试了以下但它不是 table.
c(output1,output2...)
例如,具有 2 个级别的 VarA 和 VarB 的预期输出
|Var Name|Proportion|
| A| |
|Yes | 50.0|
|No | 50.0|
| B| |
|Yes | 20.0|
|No | 80.0|
使用 c() 的实际输出只是一个向量。
Yes No Yes No
50 50 20 80
样本输入(数据)
structure(list(psu = structure(c(1, 1, 2, 2), format.sas = "BEST"),
strata = structure(c(1, 2, 3, 4), format.sas = "BEST"), wt =structure(c(1.7,
0.8, 1.2, 0.3), format.sas = "BEST"), residence = structure(c("Urban",
"Rural", "Urban", "Urban"), format.sas = "$CHAR"), income = structure(c("High", "Low", "Low", "High"), format.sas = "$CHAR")),
label = "TEST", row.names = c(NA, -4L), class = c("tbl_df", "tbl", "data.frame"))
有关如何为单个变量提取比例的示例。
分配图书馆
library(survey)
声明 svydesign
testdesign<- svydesign(id=test$psu, strata=test$strata, weight=test$wt, nest=TRUE, data=test)
产生 svytable
a=round(prop.table(svytable(~residence,design=testdesign))*100,digits=2)
这是您要实现的目标吗?
library(survey)
prop.table(svytable(~residence + income,design=testdesign), 1) * 100
# income
#residence High Low
# Rural 0.0 100.0
# Urban 62.5 37.5
如果我们想合并两个输出,我们需要相同的列名,这可以通过 setNames
完成
output1 <- round(prop.table(svytable(~residence,design=testdesign))*100,digits=2)
output2 <- round(prop.table(svytable(~income,design=testdesign))*100,digits=2)
rbind(setNames(as.data.frame(output1), c("Var1", "Freq")),
setNames(as.data.frame(output2), c("Var1", "Freq")))
# Var1 Freq
#1 Rural 20
#2 Urban 80
#3 High 50
#4 Low 50
我需要创建单向频率 table。 tables 必须包括变量的比例。
使用prop.table(svytable()),我可以一次提取一个变量的比例。
library(survey)
round(prop.table(svytable(~varA,design=designA))*100,digits=2)
但是,我需要将每个变量的每个输出连接成一个 table 的代码。我尝试了以下但它不是 table.
c(output1,output2...)
例如,具有 2 个级别的 VarA 和 VarB 的预期输出
|Var Name|Proportion|
| A| |
|Yes | 50.0|
|No | 50.0|
| B| |
|Yes | 20.0|
|No | 80.0|
使用 c() 的实际输出只是一个向量。
Yes No Yes No
50 50 20 80
样本输入(数据)
structure(list(psu = structure(c(1, 1, 2, 2), format.sas = "BEST"),
strata = structure(c(1, 2, 3, 4), format.sas = "BEST"), wt =structure(c(1.7,
0.8, 1.2, 0.3), format.sas = "BEST"), residence = structure(c("Urban",
"Rural", "Urban", "Urban"), format.sas = "$CHAR"), income = structure(c("High", "Low", "Low", "High"), format.sas = "$CHAR")),
label = "TEST", row.names = c(NA, -4L), class = c("tbl_df", "tbl", "data.frame"))
有关如何为单个变量提取比例的示例。
分配图书馆
library(survey)
声明 svydesign
testdesign<- svydesign(id=test$psu, strata=test$strata, weight=test$wt, nest=TRUE, data=test)
产生 svytable
a=round(prop.table(svytable(~residence,design=testdesign))*100,digits=2)
这是您要实现的目标吗?
library(survey)
prop.table(svytable(~residence + income,design=testdesign), 1) * 100
# income
#residence High Low
# Rural 0.0 100.0
# Urban 62.5 37.5
如果我们想合并两个输出,我们需要相同的列名,这可以通过 setNames
output1 <- round(prop.table(svytable(~residence,design=testdesign))*100,digits=2)
output2 <- round(prop.table(svytable(~income,design=testdesign))*100,digits=2)
rbind(setNames(as.data.frame(output1), c("Var1", "Freq")),
setNames(as.data.frame(output2), c("Var1", "Freq")))
# Var1 Freq
#1 Rural 20
#2 Urban 80
#3 High 50
#4 Low 50