2

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!

Rita
  • 59
  • 7
  • In the data showed, there is no column named 'Section', is it 'Subscale'? – akrun Jul 06 '20 at 22:46
  • Try with `library(dplyr);library(purrr);df %>% select(-Student) %>% group_split(Subscale) %>% map(~ alpha(.[-1]))` – akrun Jul 06 '20 at 22:48
  • I made a mistake, but Section means Subscale. I just edit it in the question. Sorry! – Rita Jul 07 '20 at 07:43

1 Answers1

5

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))
akrun
  • 874,273
  • 37
  • 540
  • 662
  • Thank you very much! It worked perfectly. I saved the results using a list (like this: map(~list(alpha(.[-1]))). Is it possible to extract from this list the value of the raw_alpha for each of subscale? – Rita Jul 07 '20 at 08:54
  • 1
    @Rita There is a `raw_alpha` in `total` and `alpha.drop`. Which one you want to extract i.e. either `%>%map(~ alpha(.[-1])$alpha.drop$raw_alpha)` or `%>% map(~ alpha(.[-1])$total$raw_alpha)` gets the `raw_alpha` – akrun Jul 07 '20 at 18:58