【问题标题】:Using dplyr to create a ICCs table使用 dplyr 创建 ICCs 表
【发布时间】:2021-10-16 00:13:48
【问题描述】:

我正在尝试为多个评估者和多个变量创建一个包含 ICC 的表,我正在尝试使用函数和 dplyr,但它没有按预期工作。

这是数据框的结构和预期的 ICCs 表:

# Create data frame
ID <- c("r1", "r1", "r1", "r1", "r1", "r2", "r2", "r2", "r2", "r2", "r3", "r3", "r3", "r3", "r3")
V1.1 <- c(3, 3, 3, 3, 3, 3, 2, 3, 3, 1, 2, 2, 1, 1, 2)
V2.1 <- c(1, 1, 2, 1, 2, 1, 1, 1, 2, 1, 1, 2, 1, 1, 3)
V3.1 <- c(1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1)
V4.1 <- c(2, 2, 2, 1, 2, 2, 2, 2, 2, 1, 2, 2, 1, 1, 2)
V1.2 <- c(3, 3, 3, 3, 3, 3, 2, 3, 2, 2, 3, 2, 1, 2, 1)
V2.2 <- c(1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 2, 2)
V3.2 <- c(1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1)
V4.2 <- c(2, 4, 2, 1, 3, 2, 1, 3, 2, 2, 3, 2, 1, 2, 1)

df <- data.frame(ID, V1.1, V2.1, V3.1, V4.1, V1.2, V2.2, V3.2, V4.2)

# Empty data frame for ICCs
ids <- c("r1", "r2", "r3")
vars <- c("V1", "V2", "V3", "V4")

icc_table <- data.frame(ID = ids)
icc_table <- cbind(icc_table, matrix(NA, nrow = length(ids), ncol = length(vars)))
names(icc_table)[2:ncol(icc_table)] <- vars

这是尝试使用函数和 dplyr 创建 ICCs 表:

# ICC function
icc.fun <- function(data, x1, x2){
    result <- irr::icc(subset(data, select = c(x1, x2)), 
                  model = "twoway",
                  type = "agreement",
                  unit = "single")
    result$value
}

# Table attempt
icc_table <- df %>%
    pivot_longer(cols = -ID, names_to = c("criteria", ".value"), names_pattern = "(V\\d)\\.(\\d)") %>% 
    group_by(ID, criteria) %>% 
    rename("val1" = `1`, "val2" = `2`) %>%
    summarise(icc = icc.fun(df, val1, val2), .groups = "drop") %>% 
    pivot_wider(id_cols = ID, names_from = criteria, values_from = icc)

但是,它不起作用,它返回一个包含很多 NA 的表。当我尝试该功能时,它似乎工作正常,所以我猜这是 dplyr 代码的问题。如果您有除 dplyr 之外的任何其他解决方案,也欢迎!

谢谢!

【问题讨论】:

    标签: r function dplyr icc


    【解决方案1】:

    我认为问题出在icc.funsummarise() 中的subset() 之间,请尝试:

    # ICC function
    icc.fun <- function(x1, x2){
        result <- irr::icc(data.frame(x1, x2)), 
                      model = "twoway",
                      type = "agreement",
                      unit = "single")
        result$value
    }
    
    # Table attempt
    icc_table <- df %>%
        pivot_longer(cols = -ID, names_to = c("criteria", ".value"), names_pattern = "(V\\d)\\.(\\d)") %>% 
        group_by(ID, criteria) %>% 
        rename("val1" = `1`, "val2" = `2`) %>%
        summarise(icc = icc.fun(val1, val2), .groups = "drop") %>% 
        pivot_wider(id_cols = ID, names_from = criteria, values_from = icc)
    

    【讨论】:

    • 你说得对,问题出在summarise()。我改用group_modify()
    【解决方案2】:

    如果它对某人有用,这是我找到的解决方案:

    1. 我通过使用 R 基础对数据进行子集化来简化函数
    # ICC function
    icc.fun <- function(data, x1, x2){
        result <- icc(data[ ,c(x1, x2)], 
                  model = "twoway",
                  type = "agreement",
                  unit = "single")
        result$value
    }
    
    1. 我用group_modify()代替summarise(),加上enframe()
    # Create ICC table
        icc_table <- df %>%
        pivot_longer(cols = -ID, names_to = c("criteria", ".value"), names_pattern = "(V\\d)\\.(\\d)") %>% 
        group_by(ID, criteria) %>% 
        rename("val1" = `1`, "val2" = `2`) %>%
        group_modify(~ {
            icc.fun(.x, "val1", "val2") %>%
        tibble::enframe(name = "variable", value = "icc")
            }) %>%
        pivot_wider(id_cols = ID, names_from = criteria, values_from = icc)
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2014-07-03
      相关资源
      最近更新 更多