1

R/expss 专家您好!这是对此问题的后续问题-> 带有 expss 包的复杂表

我使用出色的 expss 包将小计添加到已经很复杂的表中,它适用于大多数任务(计数、比例、平均值......)。然而,我发现统计测试评估在一个没有小计的表和完全相同的小计之间存在差异。@Gregory Demin,您的知识将不胜感激:)

一个例子来说明我的话,使用数据集包中可用的推断数据集:

example <- infert %>%
  tab_significance_options(sig_level=0.2, keep="none", sig_labels=NULL, subtable_marks="greater", mode="append") %>%
  tab_cols(total(), education) %>%
  tab_cells(parity) %>%
  # block for cases
  tab_stat_cases(label="N", total_row_position="above", total_statistic="u_cases", total_label="TOTAL") %>% 
  tab_last_add_sig_labels() %>%
  # block for percent statistic - Subtable tests  
  tab_stat_cpct(label="%Col.", total_row_position="above", total_statistic="u_cpct", total_label="TOTAL") %>%
  tab_last_add_sig_labels() %>%
  tab_last_sig_cpct(label="T.1", compare_type="subtable") %>%
  # block for percent statistic - First column tests
  tab_stat_cpct(label="T.2", total_row_position="above", total_statistic="u_cpct", total_label="TOTAL") %>%
  tab_last_add_sig_labels() %>%
  tab_last_sig_cpct(compare_type="first_column", mode="replace") %>%
  tab_pivot(stat_position="inside_columns") %>%
  # converts NA to zero
  recode(as.criterion(is.numeric) & is.na ~ 0, TRUE ~ copy)
example <- example[,-c(4,5)]
print(example)

注意: sig_level 非常高(20%)来说明这个具体问题,不要惊慌 :) 这是起点,我很好。然后我们只添加小计(第 5 行):

example2 <- infert %>%
  tab_significance_options(sig_level=0.2, keep="none", sig_labels=NULL, subtable_marks="greater", mode="append") %>%
  tab_cols(total(), education) %>%
  tab_cells(parity) %>%
  tab_subtotal_cells("#FIRST 3"=c(1,2,3),"#LAST 3"=c(4,5,6), position = "above") %>%
  # block for cases
  tab_stat_cases(label="N", total_row_position="above", total_statistic="u_cases", total_label="TOTAL") %>% 
  tab_last_add_sig_labels() %>%
  # block for percent statistic - Subtable tests  
  tab_stat_cpct(label="%Col.", total_row_position="above", total_statistic="u_cpct", total_label="TOTAL") %>%
  tab_last_add_sig_labels() %>%
  tab_last_sig_cpct(label="T.1", compare_type="subtable") %>%
  # block for percent statistic - First column tests
  tab_stat_cpct(label="T.2", total_row_position="above", total_statistic="u_cpct", total_label="TOTAL") %>%
  tab_last_add_sig_labels() %>%
  tab_last_sig_cpct(compare_type="first_column", mode="replace") %>%
  tab_pivot(stat_position="inside_columns") %>%
  # converts NA to zero
  recode(as.criterion(is.numeric) & is.na ~ 0, TRUE ~ copy)
example2 <- example2[,-c(4,5)]
print(example2)

我不知道发生了什么,但是这次显着性检验的结果不一样。此外,我觉得没有对两个小计行计算显着性检验。有什么见解吗?

4

1 回答 1

1

对于百分比之间的显着性检验,我们需要总体统计中的案例。所以我们将用两行进行总统计。在所有包含总病例数的操作行之后将被删除。 significance_cpct使用#符号来检测总行数。并且#在小计中会导致不正确的结果。

考虑到以上所有:

example2 <- infert %>%
    tab_significance_options(sig_level=0.2, keep="none", sig_labels=NULL, subtable_marks="greater", mode="append") %>%
    tab_cols(total(), education) %>%
    tab_cells(parity) %>%
    tab_subtotal_cells("FIRST 3"=c(1,2,3),"LAST 3"=c(4,5,6), position = "above") %>%
    # block for cases
    tab_stat_cases(label="N", total_row_position="above", total_statistic="u_cases", total_label="TOTAL") %>% 
    tab_last_add_sig_labels() %>%
    # block for percent statistic - Subtable tests  
    # note additional total statistic
    tab_stat_cpct(label="%Col.", total_row_position="above", total_statistic= c("u_cases", "u_cpct"), 
                  total_label=c("TO DELETE", "TOTAL")) %>%
    tab_last_add_sig_labels() %>%
    tab_last_sig_cpct(label="T.1", compare_type="subtable") %>%
    # block for percent statistic - First column tests
    tab_stat_cpct(label="T.2", total_row_position="above", total_statistic= c("u_cases", "u_cpct"), 
                  total_label=c("TO DELETE", "TOTAL")) %>%
    tab_last_add_sig_labels() %>%
    tab_last_sig_cpct(compare_type="first_column", mode="replace") %>%
    tab_pivot(stat_position="inside_columns") %>%
    # drop row with TO_DELETE
    where(!grepl("TO DELETE", row_labels)) %>% 
    # converts NA to zero
    recode(as.criterion(is.numeric) & is.na ~ 0, TRUE ~ copy)
example2 <- example2[,-c(4,5)]
print(example2)

更新与net列:

data(infert)
example2 <- infert %>%
    apply_labels(
        education = "Education"
    ) %>% 
    tab_significance_options(sig_level=0.2, keep="none", sig_labels=NULL, subtable_marks="greater", mode="append") %>%
    tab_cols(total(), net(education, "LESS THAN 12 Y.O."=levels(education)[1:2])) %>%
    tab_cells(parity) %>%
    tab_subtotal_cells("FIRST 3"=c(1,2,3),"LAST 3"=c(4,5,6), position = "above") %>%
    # block for cases
    tab_stat_cases(label="N", total_row_position="above", total_statistic="u_cases", total_label="TOTAL") %>% 
    tab_last_add_sig_labels() %>%
    # block for percent statistic - Subtable tests  
    # note additional total statistic
    tab_stat_cpct(label="%Col.", total_row_position="above", total_statistic= c("u_cases", "u_cpct"), 
                  total_label=c("TO DELETE", "TOTAL")) %>%
    tab_last_add_sig_labels() %>%
    tab_last_sig_cpct(label="T.1", compare_type="subtable") %>%
    # block for percent statistic - First column tests
    tab_stat_cpct(label="T.2", total_row_position="above", total_statistic= c("u_cases", "u_cpct"), 
                  total_label=c("TO DELETE", "TOTAL")) %>%
    tab_last_add_sig_labels() %>%
    tab_last_sig_cpct(compare_type="first_column", mode="replace") %>%
    tab_pivot(stat_position="inside_columns") %>%
    # drop row with TO_DELETE
    where(!grepl("TO DELETE", row_labels)) %>% 
    # converts NA to zero
    recode(as.criterion(is.numeric) & is.na ~ 0, TRUE ~ copy)
example2 <- example2[,-c(4,5)]
print(example2)
于 2020-04-03T00:28:02.773 回答