Search code examples
rexpss

How do you get significant differences to show with custom table, when using text in Expss?


How do you get significant differences to show with custom table, when using text instead of labelled numerics as input? (should be + or – in each cell).

#Generate data
aw = function(n) { 
    sample(x = c("Aware","Consider","Buy"), n, replace = T, prob = c(0.1, 0.4, 0.5)) 
}
awa<-aw(3000)

gen = function(n) { 
    sample(x = c("Male","Female"), n, replace = T, prob = c(0.8, 0.2)) 
}
gende<-gen(3000)

ag = function(n) { 
    sample(x = c("<20","20 to 50","50+"), n, replace = T, prob = c(0.3, 0.2, 0.5)) 
}

age<-ag(3000)

data<-data.frame(awa,gende,age)
#Banner create
banner = calc(data, list(total(), gende,age))
#Custom table with significant differences calculated relative to total
data %>%
tab_significance_options(compare_type = "adjusted_first_column",subtable_marks = "both",sig_labels_first_column = c("-", "+"),mode = c("append")) %>%
tab_cells(awa) %>%
tab_cols(banner) %>%
tab_stat_cpct() %>%
tab_pivot()

Solution

  • There is a function tab_last_sig_cpct which calculate significance for the last statistics. Statistics should be column percent.

    library(expss)
    #Generate data
    aw = function(n) { 
        sample(x = c("Aware","Consider","Buy"), n, replace = T, prob = c(0.1, 0.4, 0.5)) 
    }
    awa<-aw(3000)
    
    gen = function(n) { 
        sample(x = c("Male","Female"), n, replace = T, prob = c(0.8, 0.2)) 
    }
    gende<-gen(3000)
    
    ag = function(n) { 
        sample(x = c("<20","20 to 50","50+"), n, replace = T, prob = c(0.3, 0.2, 0.5)) 
    }
    
    age<-ag(3000)
    
    data<-data.frame(awa,gende,age)
    #Banner create
    banner = calc(data, list(total(), gende,age))
    #Custom table with significant differences calculated relative to total
    data %>%
        tab_significance_options(compare_type = "adjusted_first_column",subtable_marks = "both",sig_labels_first_column = c("-", "+"),mode = c("append")) %>%
        tab_cells(awa) %>%
        tab_cols(banner) %>%
        tab_stat_cpct() %>%
        tab_last_sig_cpct(mode = "replace") %>% 
        tab_pivot()
    
     # |     |              | #Total | Female |   Male |    <20 | 20 to 50 |  50+ |
     # | --- | ------------ | ------ | ------ | ------ | ------ | -------- | ---- |
     # | awa |        Aware |   10.0 |  7.0 - | 10.8 + |  9.5   |     10.3 | 10.2 |
     # |     |          Buy |   51.1 | 53.8   | 50.4   | 48.6   |     51.4 | 52.5 |
     # |     |     Consider |   38.8 | 39.1   | 38.8   | 41.9 + |     38.3 | 37.3 |
     # |     | #Total cases |   3000 |  611   | 2389   |  884   |      601 | 1515 |