Search code examples
rpsych

How to compute Cronbach's alpha for multiple subscales at the same time using R?


I am a beginner using R. I am struggling when I try to compute the Cronbach's alpha for multiple subscales. I was using the psych package to do it.

Here is an example of my database: I have 2 different subscales. Subscale A includes items 1 to 3 and Subscale B includes items 4 to 5.

Subscale  Student  Item1  Item2  Item3  Item4  Item5  
   A        1       1      0     1       NA    NA
   A        2       0      1     1       NA    NA
   A        3       1      1     1       NA    NA
   B        1       NA     NA    NA      1     1
   B        2       NA     NA    NA      1     0
   B        3       NA     NA    NA      0     0

To compute the Cronbach's alpha for each subscale at the same time. I tried to use psych package.

Reliability <- group_by(df,Subscale) %>%
alpha()

However, I get the following error: Likely variables with missing values are Item4 Item5 Error in principal(x, scores = FALSE) : I am sorry: missing values (NAs) in the correlation matrix do not allow me to continue. Please drop those variables and try again. In addition: Warning messages: 1: In var(if (is.vector(x) || is.factor(x)) x else as.double(x), na.rm = na.rm) : NAs introduced by coercion 2: In alpha(.) : Item = Subscale had no variance and was deleted

Is it possible to compute Cronbach's alpha for these subscales at the same time creating a new data frame or a list with the results? Or should I use the split function two separate the subscales and calculate the alphas for each one separately?

Thanks in advance!


Solution

  • Assuming that 'Subscale' is the column name, the x for alpha should be either a data.frame or matrix. An option is to group_split into a list of data.frames, then loop over the list with map and apply the alpha on the 'Item' columns

    library(dplyr)
    library(purrr)
    library(psych)
    df %>% 
        select(-Student) %>%
        group_split(Subscale) %>%
        map(~ alpha(.[-1]))
    

    data

    df <- structure(list(Subscale = c("A", "A", "A", "B", "B", "B"), Student = c(1L, 
    2L, 3L, 1L, 2L, 3L), Item1 = c(1L, 0L, 1L, NA, NA, NA), Item2 = c(0L, 
    1L, 1L, NA, NA, NA), Item3 = c(1L, 1L, 1L, NA, NA, NA), Item4 = c(NA, 
    NA, NA, 1L, 1L, 0L), Item5 = c(NA, NA, NA, 1L, 0L, 0L)), class = "data.frame", 
    row.names = c(NA, 
    -6L))