R语言相关性分析

Python031

R语言相关性分析,第1张

1.  R语言自带函数cor(data, method=" ")可以快速计算出相关系数 ,数据类型:data.frame

 如data.frame为:zz, 绘图如下:

a. single protein:线性回归画法

1. ggplot(zz,aes(x=a, y=HDL))+

   geom_point(alpha=1,colour="#FFA54F")+

   geom_smooth(method = lm,colour="#8B658B")+

   #scale_color_brewer(palette = "Set1")+

   theme_bw()+

   labs(x="Ferritin",y="HDL.C",title="Pearson’s correlation test of ferritin and HDL.C")+

   annotate("text", x = 1000, y = 2.5, label = "r = -0.51",colour="black",size=4)

2. library(ggstatsplot)

 ggscatterstats(data = alldata,

               y = TRANSFUSION.UNIT,

                x = NPTXR,

                centrality.para = "mean",  #"mean" or "median"                         

               margins = "both",                                       

                xfill = "#D8BFD8",

                yfill = "#EEDD82",

                #line.size= ,

                line.color="#8B6969",

               point.color="#2F4F4F",

                marginal.size=4,

               marginal.type = "density", # "histogram", "boxplot", "density", "violin", "densigram")

                title = "Relationship between TRANSFUSION.UNIT and NPTXR")

b. ggcorrplot, 全部蛋白 global correlation map 画法

ggcorrplot(cor(alldata))

2.  summary(lm(y~x),method=" ") %>%.[["coefficients"]]   正规线性回归

     (其实就是:a<-lm(y~x1+x2+...,data)

      plot(summary(lm(y~x),method=" ")) #绘图

3.  ggcor部分数据绘图:  数据类型为data.frame,纵坐标为各指标or各蛋白,行为观测值。

data <- fortify_cor(alldata[,10:11],alldata,cluster.type = "col")

ggcor<-ggcor(data,label_size=0.5) +

  geom_colour()+

  theme(axis.text.x = element_text(colour = "black",size = 4.7),

                                                        axis.text.y=element_text(size=5.5),

                                                        axis.ticks=element_blank())+

  geom_num(aes(num=r),colour="black",size=1.5)

4. corrr包画法

datasets::mtcars %>%

  correlate() %>%

  focus(-cyl, -vs, mirror = TRUE) %>%

  rearrange() %>%

  network_plot(min_cor = .2)

相关性分析是指对两个或多个具备相关性的变量元素进行分析,从而衡量两个变量因素的相关密切程度。相关性分析旨在研究两个或两个以上随机变量之间相互依存关系的方向和密切程度。

一般来讲研究对象(样品或处理组)之间使用距离分析,而元素(物种或环境因子)之间进行相关性分析 。两个变量之间的相关性可以用简单相关系数(例如皮尔森相关系数等)进行表示,相关系数越接近1,两个元素相关性越大,相关系数越接近0,两个元素越独立。

Pearson相关系数是用于表示相关性大小的最常用指标,数值介于-1~1之间,越接近0相关性越低,越接近-1或1相关性越高。正负号表明相关方向,正号为正相关、负号为负相关。适用于两个正态分布的连续变量。

利用两变量的秩次大小来进行分析,属于非参数统计方法。适用于不满足Pearson相关系数正态分布要求的连续变量。也可以用于有序分类变量的之间的相关性测量。

Kendall's Tau相关系数是一种非参数检验,适用于两个有序分类变量。

此外衡量两个变量之间关系的方法还有:卡方检验、Fisher精确检验等。

Pearson、Spearman、Kendall相关系数都可以通过cor函数实现,cov协方差函数参数同cor函数。

ggcorrplot包内只有2个函数,一个cor_pmat()用于计算p值,一个ggcorrplot()用于绘图。ggcorrplot相当于精简版的corrplot包,只有主题更加丰富多样。

This function computes and returns the distance matrix computed by using the specified distance measure to compute the distances between the rows of a data matrix.

这个函数用特定的方法计算矩阵的行之间的距离,并返回距离矩阵。

scale是对矩阵的每一列进行标准化,如果要对行标准化需要先转置。如 heatmapdata <- t(scale(t(heatmapdata)))