![](/img/trans.png)
[英]After nesting and using map from tidyverse, how unnest all descriptive results from psych package?
[英]Using tidyverse to get raw_alpha values with the psych package from different datasets / databases
我花了一天时间寻找这个答案,但我几乎要放弃了。 实际上,我确实想像是一个非常简单的情况,但是我会很高兴得到任何帮助。
假设我有两个数据集 ,第一个获得所有学生的所有ID
library(tidyverse)
library(psych)
ds_of_students <- data.frame(id=(1:4), school=c("public","private"))
第二个具有测试的所有结果。 假设每一列都是一个ID 。
ds_of_results <- structure(list(i1 = c(1, 2, 4, 4),
i2 = c(3, 3, 2, 2),
i3 = c(2, 3, 3, 5),
i4 = c(4, 1, 3, 2)),
class = c("tbl_df", "tbl",
"data.frame"), row.names = c(NA, -4L))
现在,我需要报告一张按学校分组的学生ID表,然后得出结果(实际上,这是Cronbach alpha结果,这在心理学中很常见)。
ds_of_students %>%
group_by(school) %>%
summarise(n=n(),
id = paste(id, collapse = ",")) %>%
mutate(item2=psych::alpha(ds_of_results[c(id)])$total[1])
我有这个消息
Error in mutate_impl(.data, dots) :
Evaluation error: Columns `2,4`, `1,3` not found.
但是当我以传统方式跑步时,它会起作用
psych::alpha(ds_of_results[c(1,3)])$total[1]
我尝试使用粘贴, noquote , gsub ans strcol
请运行此代码以获得可重复的结果。 非常感谢!
library(tidyverse)
library(psych)
ds_of_students <- data.frame(id=(1:4), school=c("public","private"))
ds_of_results <- structure(list(i1 = c(1, 2, 4, 4),
i2 = c(3, 3, 2, 2),
i3 = c(2, 3, 3, 5),
i4 = c(4, 1, 3, 2)),
class = c("tbl_df", "tbl",
"data.frame"), row.names = c(NA, -4L))
ds_of_students %>%
group_by(school) %>%
summarise(n=n(),
id = paste(id, collapse = ",")) %>%
mutate(item2=psych::alpha(ds_of_results[c(id)])$total[1])
alpha(ds_of_results[c(1,3)])$total[1]
为了使我的问题更现实,那就是真实的数据集,在这里我必须计算Cronbach的alpha项以及每个组的项。
get_alpha <- function(x) {
raw_alpha <-
psych::alpha(
ds_of_results[, ds_of_students[ds_of_students$school == x, 1]])$total[1]
ids <-
paste0(names(ds_of_results[, ds_of_students[ds_of_students$school == x, 1]]),
collapse = ",")
data.frame(
school = x,
id = ids,
raw_alpha = raw_alpha
)
}
map_df(levels(ds_of_students$school), get_alpha)
结果
school id raw_alpha
1 private i2,i4 0.00
2 public i1,i3 0.85
您的代码中存在几个问题:
mutate
使用数据框内的变量,而psych::alpha
需要整个数据框。 因此,我认为您无法使用mutate获得alpha值
您使用$total
提取psych::alpha
给出的数据帧列表的一个元素,但是在管道中不起作用(管道不处理列表,仅适用于数据帧)
因此,基本上, psych::alpha
,它需要整个数据帧作为输入并输出数据帧列表,在经典的dplyr
争用工作流程中不能很好地发挥作用。
我不确定这是您要寻找的内容,但是请尝试一下并告诉我您是否获得了预期的结果。 这样替换您的summarise
调用(还要注意mutate调用中的“取消列出”):
ds_of_students %>% mutate(id=lapply(strsplit(id,","),as.integer))
group_by(school) %>%
summarise(id = list(id)) %>%
mutate(item2=psych::alpha(ds_of_results[unlist(id)])$total[1])
我在这里所做的是将您的粘贴替换为列表,以便将数字保留为数字,并且可以在下一步顺利进行时传递给子集调用。 当然,如果id是字符,这也将起作用,假设ds_of_results中的列名称是ds_of_students的ID。 您需要将其与unlist
一起传递,以便子集将其作为简单的向量而不是作为具有一个vector元素的列表来获取。
使用您的虚假数据,我得到此错误:
Some items ( i2 i4 ) were negatively correlated with the total scale and
probably should be reversed.
To do this, run the function again with the 'check.keys=TRUE' option# A tibble: 2 x 3
school id item2
<fct> <list> <data.frame>
1 private <int [2]> -1
2 public <int [2]> -1
Warning messages:
1: In cor.smooth(r) : Matrix was not positive definite, smoothing was done
2: In psych::alpha(ds_of_results[unlist(id)]) :
Some items were negatively correlated with the total scale and probably
should be reversed.
To do this, run the function again with the 'check.keys=TRUE' option
3: In cor.smooth(R) : Matrix was not positive definite, smoothing was done
4: In cor.smooth(R) : Matrix was not positive definite, smoothing was done
但这可能只是假数据本身的问题,而不是代码问题。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.