expss 中的百分比不准确 table

inaccurate percentages in expss table

我正在分析一些调查数据并使用 expss 创建 tables。

我们的一个问题是关于品牌知名度。我有 3 种类型的品牌:BrandA 是大部分样本子集看到的品牌,BrandB 是较小(互斥!)样本子集看到的品牌,BrandC 是每个受访者看到的品牌。

我想将此认知度问题视为一个多选问题,并报告了解每个品牌的人(实际看到该品牌的人)的百分比。 (在这种情况下,值为 1 表示受访者了解该品牌。)

我能得到的最接近的方法是使用下面的代码,但是 tab_stat_cpct() 没有报告准确的百分比或案例数,正如您在附件 table 中看到的那样。当您将 table 中列出的总百分比与手动计算的总百分比(即通过 mean(data$BrandA, na.rm = TRUE))进行比较时,它报告的值对于 BrandA 和 BranB 来说太低,而一个值太高BrandC 高。 (更不用说案例总数应该是 25 个。)

我已经阅读了文档,我知道这个问题是由于 tab_stat_cpct() 如何定义 "case" 来计算百分比,但我没有看到将调整该定义以执行我需要的参数。我错过了什么吗?还是有其他方法可以报告准确的百分比?谢谢!

set.seed(123)

data <- data.frame(
    Age = sample(c("25-34", "35-54", "55+"), 25, replace = TRUE),
    BrandA = c(1, 0, 0, 1, 0, 1, NA, NA, NA, NA, NA, NA, NA, 1, 
               0, 1, 1, 1, 1, 0, 1, 1, 0, 0, 1),
    BrandB = c(NA, NA, NA, NA, NA, NA, 1, 1, 0, 1, 0, 1, 1, NA, 
               NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA),
    BrandC = c(1, 0, 1, 0, 1, 1, 1, 0, 0, 1, 1, 0, 1, 0, 1, 0, 
               1, 1, 1, 0, 1, 0, 1, 0, 1)
)

data %>%
    tab_cells(mrset(as.category(BrandA %to% BrandC))) %>%
    tab_cols(total(), Age) %>%
    tab_stat_cpct() %>%
    tab_last_sig_cpct() %>%
    tab_pivot()

##    |              | #Total |     Age |       |      |
##    |              |        |   25-34 | 35-54 |  55+ |
##    |              |        |       A |     B |    C |
##    | ------------ | ------ | ------- | ----- | ---- |
##    |       BrandA |   52.4 |  83.3 B |  28.6 | 50.0 |
##    |       BrandB |   23.8 |         |  42.9 | 25.0 |
##    |       BrandC |   71.4 | 100.0 C |  71.4 | 50.0 |
##    | #Total cases |     21 |     6   |     7 |    8 |

认为多响应集中的所有项目都具有相同的基数。 mdset 的基础是我们至少有一个非空项(值为 1 的项)的情况数。这就是为什么您的品牌的基数是 21。如果我们将分别处理每个项目,那么我们需要显示每个项目的总计以计算重要性。在很多情况下,这是非常不方便的。

根据您的情况,您可以使用以下功能:

library(expss)
tab_stat_dich = function(data, total_label = NULL, total_statistic = "u_cases", 
                         label = NULL){

    if (missing(total_label) && !is.null(data[["total_label"]])) {
        total_label = data[["total_label"]]
    } 
    if(is.null(total_label)){
        total_label = "#Total"
    }

    # calculate means
    res = eval.parent(
        substitute(
            tab_stat_mean_sd_n(data, weighted_valid_n = "w_cases" %in% total_statistic,
                               labels = c("|", "@@@@@", total_label),
                               label = label)
        )
    )
    curr_tab = res[["result"]][[length(res[["result"]])]]
    # drop standard deviation
    curr_tab = curr_tab[c(TRUE, FALSE, TRUE), ]

    # convert means to percent
    curr_tab[c(TRUE, FALSE), -1] = curr_tab[c(TRUE, FALSE), -1] * 100
    ## clear row labels
    curr_tab[[1]] = gsub("^(.+?)\|(.+)$", "\2", curr_tab[[1]], perl = TRUE )

    res[["result"]][[length(res[["result"]])]] = curr_tab
    res
}

set.seed(123)
data <- data.frame(
    Age = sample(c("25-34", "35-54", "55+"), 25, replace = TRUE),
    BrandA = c(1, 0, 0, 1, 0, 1, NA, NA, NA, NA, NA, NA, NA, 1, 
               0, 1, 1, 1, 1, 0, 1, 1, 0, 0, 1),
    BrandB = c(NA, NA, NA, NA, NA, NA, 1, 1, 0, 1, 0, 1, 1, NA, 
               NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA),
    BrandC = c(1, 0, 1, 0, 1, 1, 1, 0, 0, 1, 1, 0, 1, 0, 1, 0, 
               1, 1, 1, 0, 1, 0, 1, 0, 1)
)

res = data %>%
    tab_cells(BrandA %to% BrandC) %>%
    tab_cols(total(), Age) %>%
    tab_stat_dich() %>%
    tab_last_sig_cpct() %>%
    tab_pivot() 

res
# |        | #Total |   Age |        |      |
# |        |        | 25-34 |  35-54 |  55+ |
# |        |        |     A |      B |    C |
# | ------ | ------ | ----- | ------ | ---- |
# | BrandA |   61.1 |  71.4 | 83.3 C | 20.0 |
# | #Total |     18 |     7 |    6   |    5 |
# | BrandB |   71.4 | 100.0 | 66.7   | 50.0 |
# | #Total |      7 |     2 |    3   |    2 |
# | BrandC |   60.0 |  55.6 | 66.7   | 57.1 |
# | #Total |     25 |     9 |    9   |    7 |

# if we want to drop totals
where(res, !grepl("#", row_labels))
# |        | #Total |   Age |        |      |
# |        |        | 25-34 |  35-54 |  55+ |
# |        |        |     A |      B |    C |
# | ------ | ------ | ----- | ------ | ---- |
# | BrandA |   61.1 |  71.4 | 83.3 C | 20.0 |
# | BrandB |   71.4 | 100.0 | 66.7   | 50.0 |
# | BrandC |   60.0 |  55.6 | 66.7   | 57.1 |