简体   繁体   English

从R Survey软件包运行svymean时,可以包括unwtd.count吗?

[英]Can I get unwtd.count included when running the svymean from the R Survey package?

I've written an R script to loop through a bunch of variables in a survey and output weighted values, CVs, CIs etc. I would like it to also output the unweighted observations count. 我已经编写了一个R脚本来循环遍历调查中的一堆变量,并输出加权值,CV,CI等。我希望它也输出未加权的观察值。

I know it's a bit of a lazy question because I can calculate unweighted counts on my own and join them back in. I'm just trying to replicate a stata script that would return 'obs' 我知道这是个懒惰的问题,因为我可以自己计算未加权的计数并将其重新加入。我只是想复制一个会返回“ obs”的状态脚本

svy:tab jdvariable, per cv ci obs column format(%14.4g)

This is my calculated values table: 这是我的计算值表:

myresult_year_calc <- svyby(make.formula(newmetricname), # variable to pass to function
by = ~year,  # grouping
design = subset(csurvey, geoname %in% jv_geo), # design object with subset definition
vartype = c("ci","cvpct"), # report variation as ci, and cv percentage
na.rm.all=TRUE,
FUN = svymean # specify function from survey package
)

By using unwtd.count instead of FUN, I get the counts I want. 通过使用unwtd.count而不是FUN,我得到了想要的计数。

myresult_year_obs <- svyby(make.formula(newmetricname), # variable to pass to function
by = ~year,  # grouping
design = subset(csurvey, geoname %in% jv_geo), # design object with subset definition
vartype = c("ci","cvpct"), # report variation as ci, and cv percentage
na.rm.all=TRUE,
unwtd.count
)

Honestly in writing this question I made it 98% through a solution, but I'll ask anyway in case someone knows a more efficient way. 老实说,在写这个问题时,我通过一个解决方案使它达到了98%,但无论如何,我都会问,以防有人知道更有效的方法。

myresult_year_calc and myresult_year_obs both return what I expect, and if I use merge(myresult_year_calc, myresult_year_obs by"year") I get the table I want. myresult_year_calcmyresult_year_obs都返回我期望的值,并且如果我使用merge(myresult_year_calc, myresult_year_obs by"year") 我会得到想要的表。 This actually just gives me one count, per year in this example instead of one count for 'Yes' responses and one count for 'No'. 实际上,在此示例中,这实际上是每年给我一个计数,而不是“是”响应一个计数和“否”一个计数。

Is there any way to get both means and unweighted counts with a single command? 有什么方法可以通过单个命令获得均值和未加权计数吗?

I figured this out by creating a second dsgn function where weights = ~0. 我通过创建第二个dsgn函数(权重=〜0)来解决这个问题。 When I ran svyby using the svytotal function with the unweighted design it followed the formula. 当我在不加权设计中使用svytotal函数运行svyby时,它遵循以下公式。

dsgn2 <- svydesign(ids = ~0, weights = ~0, data = data, na.rm = T)

unweighted_n <- svyby(~interaction(group1,group2), ~as.factor(mean_rating), design = dsgn2, FUN = svytotal, na.rm = T)

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM