典型相關(guān)分析(Canonical Correlation analysis, CCA)是研究兩組變量之間相關(guān)關(guān)系的一種統(tǒng)計方法。如果每組變量中只包含一個變量,相關(guān)關(guān)系可以用相關(guān)系數(shù)來度量。但是每組變量中變量個數(shù)大于1時,比如A組溫度和濕度兩個變量,B組樹高胸徑兩個變量,度量這兩組變量之間的相關(guān)關(guān)系,可以利用主成分的思想,把兩組變量的相關(guān)關(guān)系分別轉(zhuǎn)化成兩個綜合變量的最大可能的相關(guān)關(guān)系,就是典型相關(guān)分析(CCA)。
——摘自《應(yīng)用統(tǒng)計分析與R語言實戰(zhàn)》
-
文字描述稍顯啰嗦,直接看圖
image.png
image.png
以上圖片出自教程http://my.ilstu.edu/~wjschne/444/CanonicalCorrelation.html#(4)
例子和R語言實現(xiàn)
第一個小實例:《應(yīng)用統(tǒng)計分析與R語言實戰(zhàn)》第八章第6小節(jié)例題
- 目的:研究兒童形態(tài)與肺通氣功能的關(guān)系
數(shù)據(jù)
| 健康兒童形態(tài) | 肺通氣功能 |
|---|---|
| 身高 x1 (cm) | 肺活量 y1 (L) |
| 體重 x2 (kg) | 靜息通氣 y2 (L) |
| 胸圍 x3 (cm) | 每分鐘最大通氣量 y3 (L) |
- 代碼
df<-read.table("clipboard",header=T)
df
ndat<-scale(df)
A<-ndat[,1:3]
B<-ndat[,4:6]
res.ca<-cancor(A,B,xcenter=FALSE,ycenter = FALSE)
res.ca
第二個小實例
- 原文地址
https://stats.idre.ucla.edu/r/dae/canonical-correlation-analysis/ - 數(shù)據(jù)集
600個大學(xué)新生(college freshman),四個學(xué)術(shù)變量(academic variables): Read; Write; math; Science。性別。3個心理變量(psychological variables): locus_of_Control;self_ Concept; Motivation。(這三個變量具體是什么含義我不太明白,有時間可以多看看心理學(xué)方面的書)
研究人員感興趣的是(Researchers are intersted in)心理變量和學(xué)術(shù)變量還有性別之間的關(guān)系是怎樣的?特別的(In particular)。研究人員感興趣的是需要多少維度(how many dimensions (canonical variables))來理解兩組變量之間的關(guān)系
- 安裝需要用到的包并加載
install.packages("GGally")
install.packages("CCA")
library(ggplot2)
library(GGally)
library(CCA)
- 讀入數(shù)據(jù)
mm<-read.csv("https://stats.idre.ucla.edu/stat/data/mmreg.csv")
head(mm)
colnames(mm)<-c("Control","Concept","Motivation","Read","Write","Math","Science","Gender")
summary(mm)
xtabs(~Gender,data=mm)
#輸出結(jié)果
Gender
0 1
273 327
#
psych<-mm[,1:3]
acad<-mm[,4:8]
- 查看兩組變量內(nèi)和組間的相關(guān)性,使用到的是CCA包中的matcor函數(shù)(we will look at the correlations within and between the two sets of variables using the matcor function from the CCA package.)
library(CCA)
matcor(psych,acad)
#輸出結(jié)果
$Xcor
Control Concept Motivation
Control 1.0000000 0.1711878 0.2451323
Concept 0.1711878 1.0000000 0.2885707
Motivation 0.2451323 0.2885707 1.0000000
$Ycor
Read Write Math Science Gender
Read 1.00000000 0.6285909 0.6792757 0.6906929 -0.04174278
Write 0.62859089 1.0000000 0.6326664 0.5691498 0.24433183
Math 0.67927568 0.6326664 1.0000000 0.6495261 -0.04821830
Science 0.69069291 0.5691498 0.6495261 1.0000000 -0.13818587
Gender -0.04174278 0.2443318 -0.0482183 -0.1381859 1.00000000
$XYcor
Control Concept Motivation Read Write
Control 1.0000000 0.17118778 0.24513227 0.37356505 0.35887684
Concept 0.1711878 1.00000000 0.28857075 0.06065584 0.01944856
Motivation 0.2451323 0.28857075 1.00000000 0.21060992 0.25424818
Read 0.3735650 0.06065584 0.21060992 1.00000000 0.62859089
Write 0.3588768 0.01944856 0.25424818 0.62859089 1.00000000
Math 0.3372690 0.05359770 0.19501347 0.67927568 0.63266640
Science 0.3246269 0.06982633 0.11566948 0.69069291 0.56914983
Gender 0.1134108 -0.12595132 0.09810277 -0.04174278 0.24433183
Math Science Gender
Control 0.3372690 0.32462694 0.11341075
Concept 0.0535977 0.06982633 -0.12595132
Motivation 0.1950135 0.11566948 0.09810277
Read 0.6792757 0.69069291 -0.04174278
Write 0.6326664 0.56914983 0.24433183
Math 1.0000000 0.64952612 -0.04821830
Science 0.6495261 1.00000000 -0.13818587
Gender -0.0482183 -0.13818587 1.00000000
小實例二
http://my.ilstu.edu/~wjschne/444/CanonicalCorrelation.html#(1)
小實例三
http://ecology.msu.montana.edu/labdsv/R/labs/lab12/lab12.html#cats
小實例四
小實例五
http://userweb.eng.gla.ac.uk/umer.ijaz/bioinformatics/ecological.html
好長的R代碼,必須要重復(fù)這篇
參考文獻
- https://blog.csdn.net/u012409883/article/details/17091861
- https://wenku.baidu.com/view/dda604ec844769eae109ed53.html
- 賴江山, 米湘成. 基于Vegan軟件包的生態(tài)學(xué)數(shù)據(jù)排序分析[C].全國生物多樣性保護與持續(xù)利用研討會. 2010.
- https://mp.weixin.qq.com/s/8D4vlBIhyYg0ZHtsdGn_kA 《環(huán)境因子關(guān)聯(lián)分析——我應(yīng)該選擇CCA還是RDA分析?》
- http://blog.sciencenet.cn/blog-3334560-1104737.html 用ggvegan包進行db-RDA微生物環(huán)境因子分析
- https://github.com/vqv/ggbiplot/blob/master/README.md ggbioplot 主成分分析結(jié)果畫圖
- https://stackoverflow.com/questions/32194193/plotting-rda-vegan-in-ggplot Plotting RDA (vegan) in ggplot
- https://github.com/gavinsimpson/ggvegan ggvegan
- https://github.com/joey711/phyloseq/issues/274 Plotting environmental vectorsn CCA
- http://huboqiang.cn/2016/03/03/RscatterPlotPCA R plot PCA in ggplot
- https://stackoverflow.com/questions/30549459/r-visualize-cca-plot-in-ggplot-wrong-arrows-length R visualize cca plot in ggplot - Wrong arrowslength
歡迎大家關(guān)注我的公眾號
小明的數(shù)據(jù)分析筆記本

