跨两个变量使用 svyciprop
Using svyciprop across two variables
我一直在研究如何计算 CI 两个分类变量的比例。我已经看到这个 answer,它接近我正在寻找的东西,但我想要计算两个变量的所有可能组合之间的比例。我已经使用 svymeans
和 confint
让它工作,这是我正在寻找的输出,但使用 svyciprop
。此示例来自使用 apiclus1 数据框的调查包中的 api 数据。
library(survey)
data(api)
dclus1<-svydesign(id=~dnum, fpc=~fpc, data=apiclus1)
cbind(svymean(~interaction(sch.wide, stype), design = dclus1),
confint(svymean(~interaction(sch.wide, stype), design = dclus1)))
2.5 % 97.5 %
interaction(sch.wide, stype)No.E 0.06557377 0.031454436 0.09969311
interaction(sch.wide, stype)Yes.E 0.72131148 0.635732262 0.80689069
interaction(sch.wide, stype)No.H 0.01639344 -0.002458860 0.03524575
interaction(sch.wide, stype)Yes.H 0.06010929 0.018347771 0.10187081
interaction(sch.wide, stype)No.M 0.04371585 0.005196326 0.08223537
interaction(sch.wide, stype)Yes.M 0.09289617 0.050121072 0.13567128
感谢提供最小的可重现示例。可能有更简洁的方法来执行此操作,但我认为这就是您所追求的?谢谢
library(survey)
data(api)
dclus1<-svydesign(id=~dnum, fpc=~fpc, data=apiclus1)
# figure out the levels
levels <- svytable( ~ interaction(sch.wide, stype) , dclus1 )
# calculate svyciprop zero/one for each possible level
svyciprop_fun <-
function( this_level , ci = FALSE , ... ){
this_formula <- as.formula( paste0( "~as.numeric( '" , this_level , "' == interaction(sch.wide, stype))"))
res <- svyciprop( this_formula , dclus1 , ... )
if( ci ) res <- confint( res )
res
}
# use the default method
cbind(
do.call( rbind , lapply( names( levels ) , svyciprop_fun ) ) ,
do.call( rbind , lapply( names( levels ) , svyciprop_fun , ci = TRUE ) )
)
# use a different method=
cbind(
do.call( rbind , lapply( names( levels ) , svyciprop_fun , method = 'asin' ) ) ,
do.call( rbind , lapply( names( levels ) , svyciprop_fun , ci = TRUE , method = 'asin' ) )
)
我一直在研究如何计算 CI 两个分类变量的比例。我已经看到这个 answer,它接近我正在寻找的东西,但我想要计算两个变量的所有可能组合之间的比例。我已经使用 svymeans
和 confint
让它工作,这是我正在寻找的输出,但使用 svyciprop
。此示例来自使用 apiclus1 数据框的调查包中的 api 数据。
library(survey)
data(api)
dclus1<-svydesign(id=~dnum, fpc=~fpc, data=apiclus1)
cbind(svymean(~interaction(sch.wide, stype), design = dclus1),
confint(svymean(~interaction(sch.wide, stype), design = dclus1)))
2.5 % 97.5 %
interaction(sch.wide, stype)No.E 0.06557377 0.031454436 0.09969311
interaction(sch.wide, stype)Yes.E 0.72131148 0.635732262 0.80689069
interaction(sch.wide, stype)No.H 0.01639344 -0.002458860 0.03524575
interaction(sch.wide, stype)Yes.H 0.06010929 0.018347771 0.10187081
interaction(sch.wide, stype)No.M 0.04371585 0.005196326 0.08223537
interaction(sch.wide, stype)Yes.M 0.09289617 0.050121072 0.13567128
感谢提供最小的可重现示例。可能有更简洁的方法来执行此操作,但我认为这就是您所追求的?谢谢
library(survey)
data(api)
dclus1<-svydesign(id=~dnum, fpc=~fpc, data=apiclus1)
# figure out the levels
levels <- svytable( ~ interaction(sch.wide, stype) , dclus1 )
# calculate svyciprop zero/one for each possible level
svyciprop_fun <-
function( this_level , ci = FALSE , ... ){
this_formula <- as.formula( paste0( "~as.numeric( '" , this_level , "' == interaction(sch.wide, stype))"))
res <- svyciprop( this_formula , dclus1 , ... )
if( ci ) res <- confint( res )
res
}
# use the default method
cbind(
do.call( rbind , lapply( names( levels ) , svyciprop_fun ) ) ,
do.call( rbind , lapply( names( levels ) , svyciprop_fun , ci = TRUE ) )
)
# use a different method=
cbind(
do.call( rbind , lapply( names( levels ) , svyciprop_fun , method = 'asin' ) ) ,
do.call( rbind , lapply( names( levels ) , svyciprop_fun , ci = TRUE , method = 'asin' ) )
)