Search code examples
ralphacomposite

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


I want to calculate a composite score and cronbach's alpha for multiple variables in my data frame and add the results as columns to the data frame.

Here is what my data frame looks like:

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

Here is what I tried and of course this doesn't work, but maybe it gives you an idea of what I'm aiming at:

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

In the end, I would be very happy if my data frame looked like this (alpha and composite should be rounded and two decimal points displayed):

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

I hope this is clear. Please tell me if I'm missing sth. Thanks!


Solution

  • The question's problem is divided in the following two functions.

    1. Function comp_and_alph is the question's function corrected, creates comp and alpha scores of the columns matching one pattern only.
    2. Function comp_and_alph_all matches all patterns in variable_name.

    The functions are meant to work together, preferably calling 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)
    

    Data.

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