2

我有一个与 expss 表相关的(新)问题。我写了一个非常简单的UDF(依赖几个expss函数),如下:

library(expss)
z_indices <- function(x, m_global, std_global, weight=NULL){
  if(is.null(weight)) weight = rep(1, length(x))
  z <- (w_mean(x, weight)-m_global)/std_global
  indices <- 100+(z*100)
  return(indices)
}

基于infert数据集(加上任意权重的向量)的可重现示例:

data(infert)
infert$w <- as.vector(x=rep(2, times=nrow(infert)), mode='numeric')
infert %>%
  tab_cells(age, parity) %>%
  tab_cols(total(), education, case %nest% list(total(), education)) %>%
  tab_weight(w) %>%
  tab_stat_valid_n(label="N") %>%
  tab_stat_mean(label="Mean") %>%
  tab_stat_fun(label="Z", function(x, m_global, std_global, weight=NULL){
    z_indices(x, m_global=w_mean(infert$age, infert$w),std_global=w_sd(infert$age, infert$w))
    }) %>%
  tab_pivot(stat_position="inside_columns")

该表被计算并且第一行的输出(几乎)如预期的那样。然后第二行的事情变得一团糟,因为这两个参数都z_indices明确地引用了infert$age,这infert$parity是预期的。我的问题:有没有办法动态传递tab_cellsas 函数参数tab_stat_fun的变量以匹配正在处理的变量?我猜这发生在函数声明中,但不知道如何继续......

谢谢!

编辑 2020 年 4 月 28 日: @Gregory Demin 的回答在推断数据集的范围内效果很好,尽管为了更好地扩展更大的数据帧,我编写了以下循环:

var_df <- data.frame("age"=infert$age, "parity"=infert$parity)
tabZ=infert
for(each in names(var_df)){
  tabZ = tabZ %>%
    tab_cells(var_df[each]) %>%
    tab_cols(total(), education) %>%
    tab_weight(w) %>%
    tab_stat_valid_n(label="N") %>%
    tab_stat_mean(label="Mean") %>%
    tab_stat_fun(label="Z", function(x, m_global, std_global, weight=NULL){
      z_indices(x, m_global=w_mean(var_df[each], infert$w),std_global=w_sd(var_df[each], infert$w))
    })
} 
tabZ = tabZ %>% tab_pivot()

希望这对未来的其他 expss 用户有所启发!

4

1 回答 1

1

这种情况没有通用的解决方案。中的函数tab_stat_fun始终在单元格内计算,因此您无法在其中获取全局值。但是,在您的情况下,我们可以在汇总之前计算 z-index。不是那么灵活的解决方案,但它有效:

# function for weighted z-score
w_z_index = function(x, weight = NULL){
    if(is.null(weight)) weight = rep(1, length(x))
    z <- (x - w_mean(x, weight))/w_sd(x, weight)
    indices <- 100+(z*100)
    return(indices)
}

data(infert)
infert$w <- rep(2, times=nrow(infert))
infert %>%
    tab_cells(age, parity) %>%
    tab_cols(total(), education, case %nest% list(total(), education)) %>%
    tab_weight(w) %>%
    tab_stat_valid_n(label="N") %>%
    tab_stat_mean(label="Mean") %>%
    # here we get z-index instead of original variables
    tab_cells(age = w_z_index(age, w), parity = w_z_index(parity, w)) %>%
    tab_stat_mean(label="Z") %>%
    tab_pivot(stat_position="inside_columns")

更新。更具可扩展性的方法:

w_z_index = function(x, weight = NULL){
    if(is.null(weight)) weight = rep(1, length(x))
    z <- (x - w_mean(x, weight))/w_sd(x, weight)
    indices <- 100+(z*100)
    return(indices)
}

w_z_index_df = function(df, weight = NULL){
    df[] = lapply(df, w_z_index, weight = weight)
    df
}

data(infert)
infert$w <- rep(2, times=nrow(infert))
infert %>%
    tab_cells(age, parity) %>%
    tab_cols(total(), education, case %nest% list(total(), education)) %>%
    tab_weight(w) %>%
    tab_stat_valid_n(label="N") %>%
    tab_stat_mean(label="Mean") %>%
    # here we get z-index instead of original variables
    # we process a lot of variables at once
    tab_cells(w_z_index_df(data.frame(age, parity))) %>%
    tab_stat_mean(label="Z") %>%
    tab_pivot(stat_position="inside_columns")
于 2020-04-27T19:57:37.567 回答