[英]psych::alpha does not return mean, sd
The psych
package contains a function ?alpha
which calculates test reliability and some item statistics. psych
软件包包含一个函数?alpha
,用于计算测试可靠性和某些项目统计信息。 When fed raw data (a data.frame with binary values for correct/incorrect answers), it returns, among other things, mean and st. 当馈送原始数据(具有正确/错误答案的二进制值的data.frame)时,除其他外,它返回均值和st。 dev. 开发。 for each item. 对于每个项目。 However, sometimes it doesn't, and only provides item-whole correlation. 但是,有时它不提供,而仅提供整个项目的关联。
Why is that? 这是为什么?
The docu states that mean and sd are only calculated "For data matrices [...]", and that x
is "A data.frame or matrix of data, or a covariance or correlation matrix". docu状态仅在“对于数据矩阵”中计算平均值和sd,而x
是“数据的数据帧或矩阵,或协方差或相关矩阵”。 But how does it know whether I'm giving it raw data or a correlation matrix? 但是怎么知道我是给它原始数据还是相关矩阵呢?
The alpha
command of psych
detects if the input x
is a correlation matrix using the isCorrelation
command. psych
的alpha
命令使用isCorrelation
命令检测输入的x
是否为相关矩阵。 Let consider the dataset given in this tutorial : 让我们考虑一下本教程中给出的数据集:
datafilename <- "http://personality-project.org/R/datasets/extraversion.items.txt"
items <- read.table(datafilename,header=TRUE)
df <- with(items, data.frame(q_262 ,q_1480 ,q_819 ,q_1180 ,q_1742 ))
The object df
is a data.frame
whose columns represent the responses to five items. 对象df
是一个data.frame
其列表示对五个项目的响应。 The command isCorrelation
(used inside alpha
) correctly detect that this is not a correlation matrix: 命令isCorrelation
(在alpha
内使用)正确地检测到这不是一个相关矩阵:
library(psych)
isCorrelation(df)
[1] FALSE
If we calculate the correlation matrix of df
and pass to isCorrelation
, we get again a correct answer: 如果我们计算df
的相关矩阵并传递给isCorrelation
,我们将再次获得正确的答案:
mtx <- cor(df)
isCorrelation(mtx)
[1] TRUE
Looking inside isCorrelation
one can see that a correlation matrix is defined as an object which is not a data.frame
(hence, is a matrix
) and which is symmetric: 在isCorrelation
内部查看,可以看到相关矩阵定义为不是data.frame
(因此是matrix
)并且对称的对象:
isCorrelation <- function (x)
{
return(!is.data.frame(x) && isSymmetric(unname(x)))
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.