# Rによる相関偏相関散布図

## 相関偏相関行列（相関偏相関散布図の前処理）

library(ppcor)
setwd("C:/Rtest")
cor1<-cor(Data)
pcor1 <- (pcor(Data)\$estimate)
corpcor1 <- cor1
n <- ncol(Data)
for (j in 1:n) {
for (i in 1:n) {
if(i < j){
corpcor1[i,j] <- pcor1[i,j]
}
}
}
round(cor1,3)
# correlation matrix 相関行列
round(pcor1,3)
# partial correlation matrix 偏相関行列
round(corpcor1,3)
# correlation - partial correlation matrix 相関偏相関行列

## 相関偏相関散布図

Val1 <- 0
Val2 <- 0
Val12 <- 0
Val1Name <- 0
Val2Name <- 0
Val12Name <- 0
cor1 <- 0
pcor1 <- 0
cor2 <- 0
pcor2 <- 0
corpcorPlot <- data.frame(Val1,Val2,Val12,Val1Name,Val2Name,Val12Name,cor1,pcor1,cor2,pcor2)
k <- 1
for (j in 1:n) {
for (i in 1:n) {
if(i > j){
corpcorPlot[k,1] <- i
corpcorPlot[k,2] <- j
corpcorPlot[k,3] <- paste(corpcorPlot[k,1],corpcorPlot[k,2],sep = "_")
corpcorPlot[k,4] <- colnames(corpcor1)[i]
corpcorPlot[k,5] <- rownames(corpcor1)[j]
corpcorPlot[k,6] <- paste(corpcorPlot[k,4],corpcorPlot[k,5],sep = "_")
corpcorPlot[k,7] <- corpcor1[i,j]
corpcorPlot[k,8] <- corpcor1[j,i]
corpcorPlot[k,9] <- corpcorPlot[k,7]^2
corpcorPlot[k,10] <- corpcorPlot[k,8]^2
k <- k + 1
}
}
}
library(ggplot2)
ggplot(corpcorPlot, aes(x=cor1, y=pcor1,label=Val12Name)) + geom_text()
# correlation - partial correlation matrix plot 相関偏相関散布図

ggplot(corpcorPlot, aes(x=cor2, y=pcor2,label=Val12Name)) + geom_text()
# square correlation - partial correlation matrix plot 相関偏相関２乗散布図