gpt4 book ai didi

r - 求加权比例的标准误(调查数据)

转载 作者:行者123 更新时间:2023-12-04 07:42:07 26 4
gpt4 key购买 nike

我创建了一个用户函数来为我的分类调查数据生成交叉表。例如,我编写了一个函数来按性别查找 Q50_1。看起来不错。
enter image description here
但是现在,想加一栏,与相应的 SE 对于每一行,但我找不到解决方案。这是我想要的输出(在这个例子中 SE 是假的)。
enter image description here
这是我的数据:

data_in <- read_table2("Q50_1   Q50_2   Q38 Q90 pov gender  wgt
never always Yes 2 High M 1.3
sometimes always No 2 Med F 0.4
always sometimes Yes 4 Low F 1.2
never never No 2 High M 0.5
always always No 4 High M 0.7
sometimes never Yes 3 Low F 0.56
sometimes never Yes 2 Med F 0.9
")
这是我创建的用于生成交叉表的函数:
library(tidyverse)
library(pollster)
# USER FUNCTION THAT RUNS CROSS TABS (FEEDS INTO NEXT FUNCTION)
xtab_func <- function(data, col, target,weight){
col <- sym(col)
target <- enquo(target)
weight <- enquo(weight)
pollster::crosstab(df = data, !!target, !!col, !!weight, format = "long") %>% pivot_longer(cols = starts_with("Q"))
}




crosstab <- function(data, target, columns, weight,target_name){

target <- enquo(target)
weight <- enquo(weight)
target_name <- enquo(target_name)

d <- list()

for (i in columns){
x <- xtab_func(data, i, !!target,!!weight)
x$i <- i

d[[i]] <- x
df <- do.call(rbind, d) %>%
rename(group = !!target,
question = name) %>%
mutate(group_level =!!target_name) %>%
select(-i)
}
return(df)


}


crosstab(data_in,gender, c("Q50_1","Q50_2"),wgt,"gender")
This post显示了它背后的数学原理,但我在应用程序上找不到任何内容。任何帮助表示赞赏!
编辑
我可以使用 pollster:moe_crosstab 来产生误差幅度,但 pollster 似乎没有办法添加标准误差。
enter image description here

最佳答案

我们可以修改函数来计算误差幅度 ( moe_crosstab ) 并根据置信区间水平计算标准误差

library(dplyr)
library(purrr)
library(tidyr)
library(pollster)
crosstab <- function(data, target, columns, weight,target_name, zval){

target <- enquo(target)
weight <- enquo(weight)
target_name <- enquo(target_name)
return(purrr::map_dfr(columns, ~ xtab_func(data,
.x, !!target, !!weight, zval)))

}




xtab_func <- function(data, col, target, weight, zval){
col <- sym(col)
target <- enquo(target)
weight <- enquo(weight)
pollster::crosstab(df = data, !!target, !!col, !!weight, format = "long") %>%
bind_cols(pollster::moe_crosstab(df = data, !!col,
!!target, !! weight) %>%
transmute(SE = moe/zval)) %>%
pivot_longer(cols = starts_with("Q"))
}
-测试
crosstab(data_in,gender, c("Q50_1", "Q50_2"), wgt, "gender", 1.96)

关于r - 求加权比例的标准误(调查数据),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/67406872/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com