r 计算数据框中多个变量的综合得分和 cronbach 的 alpha,并将它们添加为列

r compute composite score and cronbach's alpha for multiple variables in a data frame and add them as columns

我想计算数据框中多个变量的综合分数和 cronbach 的 alpha,并将结果作为列添加到数据框中。

这是我的数据框的样子:

t1pp_1  t1pp_2  t1pp_3  t1pp_4  t1se_1  t1se_2  t1se_3  t1se_4  t1cpl_1 t1cpl_2 t1cpl_3 t1cpl_4
6   3   5   3   4   3   4   3   1   2   2   3
7   4   7   6   5   5   4   5   5   5   5   5
4   4   6   5   4   4   4   4   1   2   3   2
5   5   7   5   4   5   4   5   5   4   4   4
4   2   6   6   4   4   3   4   4   4   2   3
6   5   7   5   1   1   4   4   1   2   2   2

这是我尝试过的方法,当然这不起作用,但也许它可以让您了解我的目标:

library(multicon)
library(psych)
library(dplyr)

comp_and_alph <- function(data = my_data, variable_name) {
  dplyr::select(data,contains("variable_name")) %>%
    mutate(t1pp_comp = multicon::composite(.)) # is there a way to get the variable name with the '_comp'and '_alph' ending? - Maybe with paste??
    mutate(t1_alph = psych::alph(.)) %>%
      round(.$total, 2))
}

最后,如果我的数据框看起来像这样(alpha 和 composite 应该四舍五入并显示两位小数),我会很高兴:

t1pp_1  t1pp_2  t1pp_3  t1pp_4  t1se_1  t1se_2  t1se_3  t1se_4  t1cpl_1 t1cpl_2 t1cpl_3 t1cpl_4 t1pp_comp   t1pp_alph   t1se_comp   t1se_alph   t1cpl_comp  t1cpl_alph
6   3   5   3   4   3   4   3   1   2   2   3   3   3   3   3   3   3
7   4   7   6   5   5   4   5   5   5   5   5   5   5   5   5   5   5
4   4   6   5   4   4   4   4   1   2   3   2   2   2   2   2   2   2
5   5   7   5   4   5   4   5   5   4   4   4   4   4   4   4   4   4
4   2   6   6   4   4   3   4   4   4   2   3   3   3   3   3   3   3
6   5   7   5   1   1   4   4   1   2   2   2   2   2   2   2   2   2

我希望这是清楚的。请告诉我我是否遗漏了某事。 谢谢!

题目的问题分为以下两个功能

  1. 函数 comp_and_alph 是问题的更正函数,创建 compalpha 仅匹配一个模式的列的分数。
  2. 函数 comp_and_alph_all 匹配 variable_name.
  3. 中的所有模式

这些函数应该协同工作,最好调用 comp_and_alpha_all

comp_and_alph <- function(data = my_data, variable_name, ...) {
  data %>%
    select(matches(variable_name)) %>%
    mutate(comp = composite(.),
           alpha = alpha(., ...)$scores) %>%
    rename_at(vars(c("comp", "alpha")), ~paste(variable_name, .,sep = "_"))

}

comp_and_alph_all <- function(data, variables, ...){
  res <- lapply(variables, function(v){
    comp_and_alph(data, v, ...)
  })
  Reduce(function(x, y){merge(x, y)}, init = list(data), res)
}

comp_and_alph_all(df1, c("t1pp", "t1se"), check.keys = TRUE)

数据.

df1 <-
structure(list(t1pp_1 = c(6L, 7L, 4L, 5L, 4L, 6L), t1pp_2 = c(3L, 
4L, 4L, 5L, 2L, 5L), t1pp_3 = c(5L, 7L, 6L, 7L, 6L, 7L), t1pp_4 = c(3L, 
6L, 5L, 5L, 6L, 5L), t1se_1 = c(4L, 5L, 4L, 4L, 4L, 1L), t1se_2 = c(3L, 
5L, 4L, 5L, 4L, 1L), t1se_3 = c(4L, 4L, 4L, 4L, 3L, 4L), t1se_4 = c(3L, 
5L, 4L, 5L, 4L, 4L), t1cpl_1 = c(1L, 5L, 1L, 5L, 4L, 1L), t1cpl_2 = c(2L, 
5L, 2L, 4L, 4L, 2L), t1cpl_3 = c(2L, 5L, 3L, 4L, 2L, 2L), t1cpl_4 = c(3L, 
5L, 2L, 4L, 3L, 2L)), class = "data.frame", row.names = c(NA, -6L))