Complex tables with expss package

杀马特。学长 韩版系。学妹 提交于 2020-04-16 02:51:10

问题


Hello to all expss experts (@Gregory Demin, if you read this message!), after few days discovering this package, I achieved nice things but still struggle a bit to create complex crosstabs with the tab_* family of functions, especially to create combinations with significance tests.

Let's start with an example given on the reference manual:

library(expss)
mtcars %>%
  tab_significance_options(keep = "none", sig_labels = NULL, subtable_marks = "greater", mode = "append") %>%
  tab_cols(total(), vs, am) %>%
  tab_cells(cyl, gear) %>%
  tab_stat_cpct() %>%
  tab_last_add_sig_labels() %>%
  tab_last_sig_cpct() %>%
  tab_last_hstack("inside_columns") %>%
  tab_pivot(stat_position = "inside_rows")

From this point, I do not know if the following actions are possible, and if so what scripts would do the trick:

1) It is quite simple with 'fre' function to display counts and percentages side by side, but is limited to this only purpose. How can we add the cases to the crosstab? (in the form of cases/percents/tests, in 3 distinct columns)

2) By default the significance tests output in this example is LETTERS, at 0.05 level. Both parameters can be changed. But is it possible to include two significance levels in a single table calculation? Something in the spirit of:

sig_level = c(0.01, 0.05)
sig_labels = c(LETTERS, letters)

3) Last (probably an easy one?), is there a possibility to force display of zeros? I have factor levels with frequencies=0, displayed with 0s in base R tables. With expss the label stays but the rows/columns remain empty.

Again, maybe what I am looking for does not exist with expss, but at least I will be sure of it. Thank you!


回答1:


Your second point (two-level significance) is not possible right now. However you can add second level significance with additional calculations on specially prepared table. 1 and 3 are quite easy:

library(expss)
data(mtcars)
mtcars %>%
    tab_significance_options(keep = "none", sig_labels = NULL, subtable_marks = "greater", mode = "append") %>%
    tab_cols(total(), vs, am) %>%
    tab_cells(cyl, gear) %>%
    # block for cases 
    tab_stat_cases(label = "cases") %>% 
    tab_last_add_sig_labels() %>%
    # block for percent statistic
    tab_stat_cpct(label = "%") %>% # percent
    tab_last_add_sig_labels() %>%
    tab_last_sig_cpct() %>%
    tab_pivot(stat_position = "inside_columns") %>% 
    # converts NA to zero
    recode(as.criterion(is.numeric) & is.na ~ 0, TRUE ~ copy)

UPDATE: You can specify parts of the chain as custom functions to avoid repetition:

library(expss)
data(mtcars)

### tab cols
my_banner = mtcars %>%
    tab_cols(total(), vs, am)

### table and formattig
my_custom_table = . %>% 
    tab_significance_options(keep = "none", sig_labels = NULL, subtable_marks = "greater", mode = "append") %>%
    # block for cases 
    tab_stat_cases(label = "cases") %>% 
    tab_last_add_sig_labels() %>%
    # block for percent statistic
    tab_stat_cpct(label = "%") %>% # percent
    tab_last_add_sig_labels() %>%
    tab_last_sig_cpct() %>%
    tab_pivot(stat_position = "inside_columns") %>% 
    # converts NA to zero
    recode(as.criterion(is.numeric) & is.na ~ 0, TRUE ~ copy)

 ### here we build table
 my_banner %>%
    tab_cells(cyl, gear) %>%
    my_custom_table()


来源:https://stackoverflow.com/questions/60958192/complex-tables-with-expss-package

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!