suppressPackageStartupMessages(
suppressWarnings(library(tidyverse)))
purr:map
, dplyr::matches
with regular expression and psych
to get the reliability statisticsThis is an R Markdown document. Markdown is a simple formatting syntax for authoring HTML, PDF, and MS Word documents. For more details on using R Markdown see http://rmarkdown.rstudio.com.
When you click the Knit button a document will be generated that includes both content as well as the output of any embedded R code chunks within the document. You can embed an R code chunk like this:
tbl <-
tibble::tribble(
~id, ~poms1.0, ~poms2.0, ~poms3.0, ~poms4.0, ~poms5.0, ~poms6.0, ~fact1.0, ~fact2.0, ~fact3.0, ~fact4.0,
1, 0, 0, 0, 0L, 0, 0, 4L, 4L, 1L, 0L,
2, 0, 0, 0, 0L, 0, 0, 2L, 3L, 4L, 4L,
3, 1, 1, 1, 2L, 0, 1, 2L, 2L, 3L, 1L,
4, 1, 0, 3, 4L, 0, 2, 1L, 3L, 4L, 4L,
5, 4, 2, 2, 2L, 0, 0, 3L, 3L, 4L, 3L,
6, 3, 2, 0, 0L, 3, 2, 1L, 4L, 3L, 3L,
7, 0, 0, 1, 0L, 0, 1, 3L, 4L, 4L, 3L,
8, 0, 0, 3, 2L, 0, 0, 2L, 4L, 2L, 4L,
9, 1, 0, 4, 4L, 1, 0, 1L, 2L, 3L, 4L,
10, 4, 0, 4, 2L, 2, 0, 2L, 4L, 1L, 4L
)
# This is to collect variables that start with "poms" and "fact", and end with ".0". The .* means anything in between
reg_poms <- "^poms.*\\.0$"
reg_fact <- "^fact.*\\.0$"
# create a vector with the strings - also name the vector
regs <- c(reg_poms, reg_fact) %>% set_names(c("poms", "fact"))
alpha_item <-
map_df(regs, ~
tbl %>%
select(dplyr::matches(.x)) %>%
psych::alpha(check.keys = TRUE) %>% .$item.stats %>%
tibble::rownames_to_column()
# capture the name of the regex that is now using
,.id = "scale"
)
## Warning in psych::alpha(., check.keys = TRUE): Some items were negatively correlated with total scale and were automatically reversed.
## This is indicated by a negative sign for the variable name.
## Warning in psych::alpha(., check.keys = TRUE): Some items were negatively correlated with total scale and were automatically reversed.
## This is indicated by a negative sign for the variable name.
alpha_item
## scale rowname n raw.r std.r r.cor r.drop mean
## 1 poms poms1.0 10 0.4857403 0.5181370 0.4952325 0.1334076 1.4
## 2 poms poms2.0 10 0.7452923 0.7845231 0.7739268 0.6399810 0.5
## 3 poms poms3.0- 10 0.6380878 0.5678473 0.5798204 0.3375382 2.2
## 4 poms poms4.0- 10 0.6486589 0.5376768 0.5325595 0.3624619 2.4
## 5 poms poms5.0 10 0.5961371 0.6418564 0.5581326 0.4051846 0.6
## 6 poms poms6.0 10 0.4064735 0.5176213 0.3874585 0.2320490 0.6
## 7 fact fact1.0- 10 0.7927992 0.8008282 0.7538007 0.6180797 1.9
## 8 fact fact2.0- 10 0.5320112 0.6294473 0.4857089 0.3062376 0.7
## 9 fact fact3.0 10 0.7170459 0.7114425 0.5463002 0.4378886 2.9
## 10 fact fact4.0 10 0.7680706 0.6917880 0.6207992 0.4493457 3.0
## sd
## 1 1.6465452
## 2 0.8498366
## 3 1.6193277
## 4 1.5776213
## 5 1.0749677
## 6 0.8432740
## 7 0.9944289
## 8 0.8232726
## 9 1.1972190
## 10 1.4142136
alpha_raw <-
map_df(regs, ~
tbl %>%
select(dplyr::matches(.x)) %>%
psych::alpha(check.keys = TRUE) %>% .$total %>%
tibble::rownames_to_column()
,.id = "scale"
)
## Warning in psych::alpha(., check.keys = TRUE): Some items were negatively correlated with total scale and were automatically reversed.
## This is indicated by a negative sign for the variable name.
## Warning in psych::alpha(., check.keys = TRUE): Some items were negatively correlated with total scale and were automatically reversed.
## This is indicated by a negative sign for the variable name.
alpha_raw
## scale rowname raw_alpha std.alpha G6(smc) average_r S/N ase
## 1 poms 0.5756802 0.6343274 0.8697055 0.2242737 1.734687 0.2246157
## 2 fact 0.6570902 0.6690544 0.7213779 0.3357296 2.021645 0.1736649
## mean sd median_r
## 1 1.283333 0.7455630 0.2278306
## 2 2.125000 0.7927624 0.3472827
writexl::write_xlsx(
list(
alpha_raw = alpha_raw,
alpha_item = alpha_item
),
"cronbach.xlsx")