1

I have survey data performed using the same questionnaire in different languages. I would like to write an elegant dplyr/tidyverse code for the reliability for each language, using psych::alpha within. Let's imagine, that the data frame (df) looks like that: simulated table

I want to calculate item and scale reliability for Q_1:Q_6, for each group indicated by the group_var variable and the code I wrote looks like this

require(tidyverse)
require(psych)
require(broom)

    df %>% 
      select(group_var, Q_1:Q_6) %>%
      as.data.frame() %>% 
      group_by(group_var) %>% 
      do(tidy(psych::alpha(c(Q_1:Q_6))))

but when I run the code, I got an error message:

Error in psych::alpha(c(Q_1:Q_6)) : 
  object 'Q_1' not found

What is wrong with the code? Thanks in advance.

1 Answers1

1

I don't think tidy works on psych::alpha(), using an example:

r4 <- sim.congeneric()
tidy(alpha(r4))
Error: No tidy method for objects of class psych

So tidy is out of question, unless there is a Best thing you can do is wrap them up in a list within a tibble:

library(dplyr)
library(tidyr)
library(purrr)
library(psych)
library(broom)

df = data.frame(group_var=sample(LETTERS[1:6],100,replace=TRUE),
matrix(sample(0:3,900,replace=TRUE),nrow=100))
colnames(df)[-1] = c(paste0("Q_",1:6), paste0("V_", 23:25))

res = df %>% 
select(group_var, Q_1:Q_6) %>%
nest(data=Q_1:Q_6) %>%
mutate(alpha = map(data,
~alpha(.x,keys=c("Q_1","Q_2","Q_3","Q_4","Q_5","Q_6"))
))

res$alpha[[1]]

Reliability analysis   
Call: alpha(x = .x, keys = c("Q_1", "Q_2", "Q_3", "Q_4", "Q_5", "Q_6"))

  raw_alpha std.alpha G6(smc) average_r   S/N ase mean   sd median_r
     -0.37      -0.3    0.13     -0.04 -0.23 0.6  1.6 0.36    0.039

 lower alpha upper     95% confidence boundaries
-1.54 -0.37 0.81 

 Reliability if an item is dropped:
     raw_alpha std.alpha G6(smc) average_r      S/N alpha se var.r  med.r
Q_1-     -0.38  -0.38221  -0.143  -0.05854 -0.27652     0.61 0.028 -0.080
Q_2-     -0.21  -0.19042   0.173  -0.03305 -0.15996     0.54 0.048  0.066
Q_3-     -0.38  -0.26988   0.096  -0.04439 -0.21252     0.61 0.053  0.046
Q_4-     -0.54  -0.41760  -0.064  -0.06261 -0.29458     0.68 0.045 -0.016
Q_5-     -0.35  -0.26006   0.154  -0.04305 -0.20639     0.60 0.058  0.059
Q_6-      0.03  -0.00088   0.107  -0.00018 -0.00088     0.42 0.024 -0.016

 Item statistics 
      n raw.r std.r  r.cor r.drop mean   sd
Q_1- 13  0.42  0.45  0.552 -0.062 0.77 1.01
Q_2- 13  0.38  0.33 -0.073 -0.162 1.85 1.14
Q_3- 13  0.39  0.38  0.083 -0.058 1.92 0.95
Q_4- 13  0.45  0.47  0.416  0.050 1.62 0.87
Q_5- 13  0.33  0.38 -0.039 -0.073 2.08 0.86
Q_6- 13  0.21  0.18 -0.137 -0.309 1.38 1.12

Non missing response frequency for each item
       0    1    2    3 miss
Q_1 0.08 0.15 0.23 0.54    0
Q_2 0.38 0.23 0.23 0.15    0
Q_3 0.31 0.38 0.23 0.08    0
Q_4 0.15 0.38 0.38 0.08    0
Q_5 0.38 0.31 0.31 0.00    0
Q_6 0.15 0.38 0.15 0.31    0

A quick check seems tidystats might be able to do it, but I ran the example code and doesn't seem to work. So you can try it for yourself.

StupidWolf
  • 45,075
  • 17
  • 40
  • 72
  • Thank you very much. The code you proposed returns an error after the running of `res = tibble(df) %>% select(group_var, Q_1:Q_6) %>% nest(data=Q_1:Q_6) %>% mutate(alpha = map(data, ~alpha(.x,keys=c("Q_1","Q_2","Q_3","Q_4","Q_5","Q_6")) ))` `Error in .f(.x[[i]], ...) : object 'group_var' not found` – Jakhongir Alidjanov May 21 '20 at 13:03
  • 1
    sorry i realize there is a package clash. can you restart your session and use only tidyr, purrr and dplyr like my edited answer? don't load the full tidyverse – StupidWolf May 21 '20 at 13:07
  • I have bit modified your code for the avoidance of a multiplication of the data frames in the environment. Now it looks like this: `(df%>% select(id.study.group, Q_1:Q_6) %>% nest(data=Q_1:Q_6) %>% mutate(cronbach=map(data, ~alpha(.x, keys = (c("Q_1", "Q_2", "Q_3", "Q_4", "Q_4", "Q_5", "Q_6"))))))$cronbach` – Jakhongir Alidjanov May 21 '20 at 15:25
  • adding `summary` between `~` and `alpha` displayes summary statistics for all groups at one sight. – Jakhongir Alidjanov May 21 '20 at 15:31