回答公众号留言的2个关于相关性分析的问题

时间:2022-07-22
本文章向大家介绍回答公众号留言的2个关于相关性分析的问题,主要内容包括其使用实例、应用技巧、基本知识点总结和需要注意事项,具有一定的参考价值,需要的朋友可以参考一下。

之前录制了一期视频介绍了 R语言相关性分析和结果可视化简单小例子

B站的链接是

https://www.bilibili.com/video/BV1Ne41147eR

有朋友在公众号留言遇到了一些问题,今天记录一下问题中我能够解决的两个

第一个问题是

使用Hmisc包中的rcorr()函数做相关性分析,他的数据是4行5列,其中有一行数据有两个缺失值 我用R语言自带的数据集iris试一下,首先是取数据的前四行和四列

df<-iris[1:4,1:4]

相关性性分析

Hmisc::rcorr(as.matrix(df))

就会遇到报错 Error in Hmisc::rcorr(as.matrix(df)) : must have >4 observations 这个报错的意思应该是你的数据最少有5行

rcorr()这个函数的帮助文档也写到了a numeric matrix with at least 5 rows and at least 2 columns (if y is absent). For print, x is an object produced by rcorr.

另外的一个知识点:如果想要用某个包里的函数,有两种办法,第一种办法是先使用library()函数加载这个包,然后直接输入函数名;另外一种办法是不加载,直接使用包名+两个冒号+函数,比如Hmisc::rcorr(as.matrix(df))

第二个问题是

使用psych包中的corr.test()函数做相关性分析,遇到警告

Warning message:
In psych::corr.test(df, method = "pearson") :
  Number of subjects must be greater than 3 to find confidence intervals.

如果只是为了做相关性分析可以忽略这个警告,因为这个函数还会同时计算相关系数的置信区间,要求数据大于三行 可以看下3行数据和4行数据的区别

df<-iris[1:3,1:3]
print(psych::corr.test(df,method="pearson"),short=F)

Call:psych::corr.test(x = df, method = "pearson")
Correlation matrix 
             Sepal.Length Sepal.Width Petal.Length
Sepal.Length         1.00        0.60         0.87
Sepal.Width          0.60        1.00         0.11
Petal.Length         0.87        0.11         1.00
Sample Size 
[1] 3
Probability values (Entries above the diagonal are adjusted for multiple tests.) 
             Sepal.Length Sepal.Width Petal.Length
Sepal.Length         0.00        1.00            1
Sepal.Width          0.59        0.00            1
Petal.Length         0.33        0.93            0

 Confidence intervals based upon normal theory.  To get bootstrapped values, try cor.ci
            raw.lower raw.r raw.upper raw.p lower.adj upper.adj
Spl.L-Spl.W        -1  0.60       NaN  0.59        -1       NaN
Spl.L-Ptl.L        -1  0.87       NaN  0.33        -1       NaN
Spl.W-Ptl.L        -1  0.11       NaN  0.93        -1       NaN
Warning message:
In psych::corr.test(df, method = "pearson") :
  Number of subjects must be greater than 3 to find confidence intervals.

4行数据

df<-iris[1:4,1:3]
print(psych::corr.test(df,method="pearson"),short=F)
Call:psych::corr.test(x = df, method = "pearson")
Correlation matrix 
             Sepal.Length Sepal.Width Petal.Length
Sepal.Length         1.00        0.63        -0.18
Sepal.Width          0.63        1.00        -0.19
Petal.Length        -0.18       -0.19         1.00
Sample Size 
[1] 4
Probability values (Entries above the diagonal are adjusted for multiple tests.) 
             Sepal.Length Sepal.Width Petal.Length
Sepal.Length         0.00        1.00            1
Sepal.Width          0.37        0.00            1
Petal.Length         0.82        0.81            0

 Confidence intervals based upon normal theory.  To get bootstrapped values, try cor.ci
            raw.lower raw.r raw.upper raw.p lower.adj upper.adj
Spl.L-Spl.W     -0.84  0.63      0.99  0.37     -0.93      1.00
Spl.L-Ptl.L     -0.97 -0.18      0.94  0.82     -0.97      0.94
Spl.W-Ptl.L     -0.97 -0.19      0.94  0.81     -0.98      0.97

还有一个问题是他的数据有13行1000多列,计算相关性好长时间也没有得到结果。 1000多列对于R语言来说可能属于大数据了,R语言里如何处理这种较大规模的数据我也不太懂。

欢迎大家关注我的公众号

小明的数据分析笔记本