代码之家  ›  专栏  ›  技术社区  ›  CK7

在Expss中使用文本时,如何在自定义表中显示显著差异?

  •  0
  • CK7  · 技术社区  · 7 年前

    #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()
    
    0 回复  |  直到 7 年前
        1
  •  0
  •   Gregory Demin    7 年前

    有一个函数 tab_last_sig_cpct 计算最后一次统计的显著性。统计信息应为列百分比。

    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 |