使用 dplyr 创建 ICC table

Using dplyr to create a ICCs table

我正在尝试为多个评估者和多个变量创建一个带有 ICC 的 table,我正在尝试使用一个函数和 dplyr,但它没有像我预期的那样工作。

这是数据帧的结构和预期的 ICC table:

# 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 创建 ICC table:

# 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)

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

谢谢!

我认为问题出在您的 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)

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

  1. 我通过使用 R base 对数据进行子集化来简化函数
# 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)