r - R : How to set fpc argument (finite population correction) 中的调查包

标签 r random statistics sampling survey

我使用与大小成比例的概率 (PPS) 计划从抽样框架中抽取了一些数据,这样我就根据两个变量的组合对 6 层进行了抽样:性别pre 比例:

      pre
gender  High   Low Medium
     F 0.155 0.155  0.195
     M 0.155 0.155  0.185

现在我想使用 R 包 "survey" 中的 svydesign 指定我的采样数据的设计.我想知道如何定义 fpc(有限人口校正)参数?

文档说:

For PPS sampling without replacement it is necessary to specify the probabilities for each stage of sampling using the fpc argument, and an overall weight argument should not be given.

library(survey)

out <- read.csv('https://raw.githubusercontent.com/rnorouzian/d/master/out.csv')

dstrat <- svydesign(id=~1,strata=~gender+pre, data=out, pps = "brewer", fpc = ????)

最佳答案

如果我们想添加比例列,那么我们按“性别”、“前”分组,通过将计数除以 sum 来创建百分比计数和 left_join

out1 <-  out %>%
           group_by(gender, pre) %>% 
           summarise(n = n(), .groups = 'drop') %>%
           mutate(fpc = n/sum(n)) %>% 
           right_join(out)

或使用 adorn_percentages来自 janitor

library(janitor)
library(tidyr)
out1 <- out %>% 
         tabyl(gender, pre) %>% 
         adorn_percentages(denominator = "all") %>% 
         pivot_longer(cols = -gender, names_to = 'pre', 
             values_to = 'fpc') %>%
        right_join(out)

如果我们需要一个函数

f1 <- function(dat, grp_cols) {
          dat %>%
             group_by(across(all_of(grp_cols))) %>%
              summarise(n = n(), .groups = 'drop') %>%
              mutate(fpc = n/sum(n)) %>% 
              right_join(dat)
  }



f1(out, c("gender", "pre"))
#Joining, by = c("gender", "pre")
# A tibble: 200 x 11
#   gender pre       n   fpc   no. fake.name sector   pretest state email            phone      
#   <chr>  <chr> <int> <dbl> <int> <chr>     <chr>      <int> <chr> <chr>            <chr>      
# 1 F      High     31 0.155     1 Pont      Private     1352 NY    Pont@...com      xxx-xx-6216
# 2 F      High     31 0.155     2 Street    NGO         1438 CA    Street@...com    xxx-xx-6405
# 3 F      High     31 0.155     3 Galvan    Private     1389 NY    Galvan@...com    xxx-xx-9195
# 4 F      High     31 0.155     4 Gorman    NGO         1375 CA    Gorman@...com    xxx-xx-1845
# 5 F      High     31 0.155     5 Jacinto   Private     1386 CA    Jacinto@...com   xxx-xx-6237
# 6 F      High     31 0.155     6 Shah      Public      1384 CA    Shah@...com      xxx-xx-5723
# 7 F      High     31 0.155     7 Randon    Private     1360 TX    Randon@...com    xxx-xx-7542
# 8 F      High     31 0.155     8 Koucherik NGO         1439 NY    Koucherik@...com xxx-xx-9137
# 9 F      High     31 0.155     9 Waters    Industry    1414 TX    Waters@...com    xxx-xx-7560
#10 F      High     31 0.155    10 David     Industry    1396 CA    David@...com     xxx-xx-6498
# … with 190 more rows

关于r - R : How to set fpc argument (finite population correction) 中的调查包,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/64217728/

相关文章:

r - 使用 R 中的 for 循环为每个因素打印多个 ggplots

在没有 Shiny 服务器的 Docker 中运行 Shinyapp

python - 在 python 中延迟抽取随机结果

PHP:生成不包括(0、1、O 和 L)的随机代码

r - 二元正态分布的离散近似

python - 逆文档频率公式

r - plot() 不显示 lme/lmer 的所有诊断图

r - 从大量等长字符串中为每个字符创建一个出现矩阵

c++ - 如何生成范围内的随机数 (-x,x)

language-agnostic - 有关于常见错误输入键的统计数据吗?