rcorr 在 cor 完美工作的地方给出错误(需要相关矩阵的 p 值)
rcorr gives error where cor works perfectly (need p-values for correlation matrix)
我需要为使用 cor
创建的相关矩阵获取 p 值。奇怪的是,当我对相同的数据使用 rcorr
时,出现以下错误:
Error in storage.mode(x) <- "double" :
(list) object cannot be coerced to type 'double'
有什么建议吗?
下面是我创建相关矩阵的代码(给出错误):
library(Hmisc)
corr < rcffull[,c("liwc_WC","liwc_informal","liwc_male",
"liwc_female","liwccsr_csrdic","liwc_negemo",
"liwc_posemo","liwc_risk")]
rcorr(corr)
这个是有效的,但我没有得到 p 值:
cor(corr, use="complete.obs")
Hmisc::rcorr
期望输入是 matrix
并且不会自动转换数据帧,即使它们都是数字。因此,您应该能够根据您的输入调用 as.matrix()
,例如
x <- c(-2, -1, 0, 1, 2)
y <- c(4, 1, 0, 1, 4)
z <- c(1, 2, 3, 4, NA)
v <- c(1, 2, 3, 4, 5)
df = data.frame(x, y, z, v)
# Fails with the same error as in the question
Hmisc::rcorr(df)
# Succeeds
Hmisc::rcorr(as.matrix(df))
我需要为使用 cor
创建的相关矩阵获取 p 值。奇怪的是,当我对相同的数据使用 rcorr
时,出现以下错误:
Error in storage.mode(x) <- "double" :
(list) object cannot be coerced to type 'double'
有什么建议吗?
下面是我创建相关矩阵的代码(给出错误):
library(Hmisc)
corr < rcffull[,c("liwc_WC","liwc_informal","liwc_male",
"liwc_female","liwccsr_csrdic","liwc_negemo",
"liwc_posemo","liwc_risk")]
rcorr(corr)
这个是有效的,但我没有得到 p 值:
cor(corr, use="complete.obs")
Hmisc::rcorr
期望输入是 matrix
并且不会自动转换数据帧,即使它们都是数字。因此,您应该能够根据您的输入调用 as.matrix()
,例如
x <- c(-2, -1, 0, 1, 2)
y <- c(4, 1, 0, 1, 4)
z <- c(1, 2, 3, 4, NA)
v <- c(1, 2, 3, 4, 5)
df = data.frame(x, y, z, v)
# Fails with the same error as in the question
Hmisc::rcorr(df)
# Succeeds
Hmisc::rcorr(as.matrix(df))