支出表中的加权累计百分比(升序-降序)

问题描述 投票:1回答:1

我想使用expss包建立累计百分比表,包括升序(0% -> 100%)和降序(100% -> 0%)。目前已经有一个函数(即 fre())为升序,虽然结果表的可定制性不高。

我想把这些计算结果包含在一个叫做 tab_stat_fun 指令,并设法获得了未加权数据集的理想输出。考虑下面的例子(infert 数据集)。)

infert %>%
  tab_cells(age) %>%
  tab_cols(total()) %>%
  tab_stat_cases(label="N", total_row_position="above", total_statistic="u_cases", total_label="TOTAL") %>%
  tab_stat_cpct(label="%Col.", total_row_position="above", total_statistic="u_cpct", total_label="TOTAL") %>%
  tab_stat_fun(label="% Asc.", function(x){100*cumsum(table(sort(x)))/sum(table(sort(x)))}) %>%
  tab_stat_fun(label="% Desc.", function(x){100-(100*cumsum(table(sort(x)))/sum(table(sort(x))))}) %>%
  tab_pivot(stat_position="inside_columns")

很好用,但如果我想用一个数字向量来权衡这些结果(为了演示,),这将不可避免地导致错误,因为sum和cumsum都不接受权重参数(据我所知)。infert$w <- as.vector(x=rep(2, times=nrow(infert)), mode='numeric')),这将不可避免地导致一个错误,因为sum和cumsum都不接受权重参数(据我所知)。

是否有一个特殊的内置函数可以解决这个问题?或者是一个函数的组合,可能意味着年龄向量乘以权重向量?

r cumsum weighted expss
1个回答
1
投票

没有这样一个现成的函数。不过我们可以利用你的方法,只需将 base::tablebase::xtabs. 后者可以计算加权频率。

library(expss)
data(infert)
infert$w <- as.vector(x=rep(2, times=nrow(infert)), mode='numeric')

cumpercent = function(x, weight = NULL){
    if(is.null(weight)) weight = rep(1, length(x))
    counts = xtabs(weight ~ x)
    100*cumsum(counts)/sum(counts)    
}

infert %>%
    tab_cells(age) %>%
    tab_cols(total()) %>%
    tab_weight(w) %>% 
    tab_stat_cases(label="N", total_row_position="above", total_statistic="u_cases", total_label="TOTAL") %>%
    tab_stat_cpct(label="%Col.", total_row_position="above", total_statistic="u_cpct", total_label="TOTAL") %>%
    tab_stat_fun(label="% Asc.", cumpercent) %>%
    tab_stat_fun(label="% Desc.", function(x, weight = NULL){100-cumpercent(x, weight)}) %>%
    tab_pivot(stat_position="inside_columns")
© www.soinside.com 2019 - 2024. All rights reserved.