Rで解析:多変数の関係把握に便利です!!「ggcorrplot」パッケージ

多変数の関係を把握するのに大変便利なパッケージだと思います。表などでも構いませんが、一度に情報を確認するのには図がやっぱり便利です。パッケージは図式化以外にも相関係数のp値を算出するコマンドも収録されています。

でも、相関係数やそのp値は外れ値があると誤った解釈につながりますので一度、散布図やヒストグラムなどで分布や頻度を確認することをオススメします。

なお、本パッケージにはggplot2が利用されています。ggplot2は下記記事を参考にしてください。

・ggplot2の紹介
 https://www.karada-good.net/analyticsr/r-78/

パッケージバージョンは0.1.3。実行コマンドはwindows 11のR version 4.1.2で確認しています。

パッケージのインストール

下記、コマンドを実行してください。

#パッケージのインストール
install.packages("ggcorrplot")

実行コマンド

詳細はコメント、パッケージのヘルプを確認してください。

#パッケージの読み込み
library("ggcorrplot")

###データ例の作成#####
n <- 10
#listに一度データを格納
ListData <- vector("list", n)
#連続処理
for(i in seq(n)){
  ListData[[i]] <- sample(1:50, n, replace = TRUE)}
#成分名を付与
names(ListData) <- paste0("Group", 1:n)
#データフレーム化
TestData <- as.data.frame(ListData)
#基本コマンドで相関係数を計算:corコマンド
#距離の計算:methodオプション;"pearson","kendall","spearman"の指定が可能
CorResult <- cor(TestData, method = "pearson")
########

#相関係数のp-valuesを計算:cor_pmatコマンド
PCorResult <- cor_pmat(TestData)

#結果をプロット:ggcorrplotコマンド
#プロットデータの指定:corrオプション
#タイルの形を指定:methodオプション;"square":四角,"circle":丸
#プロット領域:typeオプション;"full":全体,"lower":下部,"upper":上部
#色の指定:colorsオプション
#相関係数の表示:labオプション;TRUE:表示
#hcclustコマンドでクラスタリング処理:hc.orderオプション
#hc.order:TRUEの場合
ggcorrplot(corr = CorResult, hc.order = TRUE, method = "square", title = "TEST PLOT",
           colors = c("#4b61ba", "white", "red"), lab = TRUE)
#hc.order:FALSEの場合
ggcorrplot(corr = CorResult, hc.order = FALSE, method = "square", title = "TEST PLOT",
           colors = c("#4b61ba", "white", "red"), lab = TRUE)

#cor_pmatコマンドの結果を適応
#結果データを指定:p.matオプション
#0.5から-0.5のタイル設定:"pch";バツ印,"blank";塗り無し
#p.mat:blankの場合
ggcorrplot(corr = CorResult, method = "square", title = "TEST p-value PLOT",
           type = "lower", p.mat = PCorResult, insig = "blank")
#p.mat:pchの場合
ggcorrplot(corr = CorResult, method = "square", title = "TEST p-value PLOT",
           type = "lower", p.mat = PCorResult, insig = "pch")

出力例

・hc.order:TRUEの場合

hc.order:TRUE

・hc.order:FALSEの場合

hc.order:FALSE

・p.mat:blankの場合

p.mat:blank

・p.mat:pchの場合

p.mat:pch

少しでも、あなたの解析が楽になりますように!!

Prices and shipping availability may change. Please refer to the product page at time of purchase.
Content displayed on this site is provided by Amazon and may be updated or removed.
Amazon Associate, karada-good earns income through qualifying sales.
タイトルとURLをコピーしました