【问题标题】:Calculating 95% Confidence Interval for Several Columns at Once in R在 R 中一次计算多列的 95% 置信区间
【发布时间】:2021-07-16 20:23:43
【问题描述】:

我正在使用调查数据(下图示例)来寻找 95% 的置信区间。 Q#d 列(Q1d、Q2d 等)各自对应于调查中的不同问题(李克特量表,结果二分法,1 = 是,0 = 否)。干预列描述结果是干预前 (FALSE) 还是干预后 (TRUE)。我想要做的是获得干预前后每个问题的比例差异的 95% 置信区间。

例如,假设在第一季度,干预前回答“是”的比例为 0.2,干预后回答“是”的比例为 0.5。差异为 0.3 或 30%,我想计算差异的置信区间(假设在 25% 和 35% 之间)。我想对调查中的每个问题(所有 Q1d)都这样做。我一直无法找到一种方法来迭代并为所有问题(列)执行此操作。我已经编写了一个可以为一列成功执行此操作的函数,但是遍历列名对我不起作用,而且我不知道如何将结果存储为向量/数据框。我已经包含了下面的功能。有什么指导吗?

非常感谢!!

get_conf_int <- function(df, colName) {
  myenc <- enquo(colName)
  p <- df %>%
    group_by(Intervention) %>%
    summarize(success=sum(UQ(myenc)==1, na.rm=TRUE), total=n())
  prop.test(x=pull(p,success), n=pull(p, total))$conf.int[2:1]*-100
} 

我可以这样调用函数:

get_conf_int(db, Q1d)

我现在使用 prop.test 来查找置信区间,但也可以使用其他方法。

【问题讨论】:

    标签: r statistics tidyverse


    【解决方案1】:

    我不能保证 prop.table 是否比 binom.test 更好,你应该阅读更多关于这两个的内容。

    library(dplyr)
    
    # just for this example, you have your survey here
    df <- data.frame(Intervention=sample(x = c(TRUE,FALSE), size = 20, replace = TRUE), 
                     Q1d=sample(x = 0:1, size = 20, replace = TRUE),
                     Q2d=sample(x = 0:1, size = 20, replace = TRUE),
                     Q3d=sample(x = 0:1, size = 20, replace = TRUE),
                     Q4d=sample(x = 0:1, size = 20, replace = TRUE),
                     Q5d=sample(x = 0:1, size = 20, replace = TRUE),
                     Q6d=sample(x = 0:1, size = 20, replace = TRUE),
                     Q7d=sample(x = 0:1, size = 20, replace = TRUE))
    
    # vector with the sum of FALSE and the sum of TRUE
    count_Intervention <- c(length(which(!df$Intervention)),length(which(df$Intervention)))
    
    # group by TRUE/FALSE and sum(count) the 1's
    df_sum <- df %>%
      group_by(Intervention) %>%
      summarize(across((colnames(df)[-1]),list(sum)))
    
    # for new info.  I added the pvalue, that might be important
    new_df <- data.frame(Question=as.character(), LowerConfInt=as.numeric(), UpperConfInt=as.numeric(), Pvalue = as.numeric())
    
    #loop
    for (Q_d in colnames(df_sum)[-1]) {
      lower <- prop.test(as.vector(t(df_sum[,Q_d])), count_Intervention)$conf.int[1]
      upper <- prop.test(as.vector(t(df_sum[,Q_d])), count_Intervention)$conf.int[2]
      pvalue <- prop.test(as.vector(t(df_sum[,Q_d])), count_Intervention)$p.value
      new_df <- rbind(new_df, data.frame(Q_d, lower, upper, pvalue)) 
      
    }
    
    new_df
        Q_d      lower      upper     pvalue
    1 Q1d_1 -0.2067593  0.8661000 0.34844258
    2 Q2d_1 -0.9193444 -0.1575787 0.05528499
    3 Q3d_1 -0.4558861  0.5218202 1.00000000
    4 Q4d_1 -0.4558861  0.5218202 1.00000000
    5 Q5d_1 -0.7487377  0.3751114 0.74153726
    6 Q6d_1 -0.2067593  0.8661000 0.34844258
    7 Q7d_1 -0.4558861  0.5218202 1.00000000
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2020-09-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2013-11-26
      • 1970-01-01
      相关资源
      最近更新 更多