Search code examples
rstatisticsexpss

How to generate Z-test including totals of variables in columns in expss?


Two questions in fact.

  1. How to add totals for variables in columns in expss?
  2. Is it possible to perform Z-test for variables in columns including total as a different category?

Below you can find a piece of code I'd run but it didn't work... I mean I couldn't even add totals on the right/left side of column variable...

test_table = tab_significance_options(data = df, compare_type = "subtable", bonferroni = TRUE, subtable_marks = "both") %>%
  tab_cells(VAR1) %>%
  tab_total_statistic("w_cpct") %>%
  tab_cols(VAR2) %>%
  tab_stat_cpct() %>%
  tab_cols(total(VAR2)) %>%
  tab_last_sig_cpct() %>%
  tab_pivot(stat_position = "outside_columns")

I would be grateful for any advice.


Solution

  • To compare with first column you need additionally specify "first_column" in the 'compare_type'. Secondary, for correct result one of total statistic should be cases. Taking into the account all of the above:

    library(expss)
    data(mtcars)
    test_table = mtcars %>% 
        tab_significance_options(compare_type = c("first_column", "subtable"), bonferroni = TRUE, subtable_marks = "both") %>%
        tab_total_statistic(c("u_cases", "w_cpct")) %>%
        tab_cells(gear) %>%
        tab_cols(total(am), am) %>%
        tab_stat_cpct() %>%
        tab_last_sig_cpct() %>% 
        tab_pivot() 
    
    test_table
    
    # |      |                  | #Total |       am |          |
    # |      |                  |        |        0 |        1 |
    # |      |                  |        |        A |        B |
    # | ---- | ---------------- | ------ | -------- | -------- |
    # | gear |                3 |   46.9 | 78.9 +   |          |
    # |      |                4 |   37.5 | 21.1 < B | 61.5 > A |
    # |      |                5 |   15.6 |          | 38.5     |
    # |      |     #Total cases |     32 |   19     |   13     |
    # |      | #Total wtd. cpct |    100 |  100     |  100     |