Использование svyciprop для двух переменных
Я смотрел на то, как рассчитать CI для пропорций по двум категориальным переменным. Я видел этот ответ, который приближается к тому, что я ищу, но я хочу, чтобы пропорции вычислялись между всеми возможными комбинациями двух переменных. Я получил его на работу с помощью svymeans
а также confint
и это выход, который я ищу, но с помощью svyciprop
, Этот пример взят из данных API в пакете опроса с использованием фрейма данных apiclus1.
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
1 ответ
Решение
Спасибо за минимальный воспроизводимый пример. может быть, есть более чистый способ сделать это, но я думаю, это то, что вы ищете? Спасибо
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' ) )
)