2016-11-10 3 views
1

私は9リストのリストを持っています。次のコードを参照してください。ここでは、9リストすべてではなく、p,rおよびtをピアソン、スピアソンおよびケンドールの相関にそれぞれループします。 テスト機能がcorrplot(M.cor, ...)ここで、現在の擬似コードは、次のされ、完全な擬似コードの下に表示さRのリストのサブセットをループする方法は?

for (i in p.mat.all) { 
... 
} 

コード9つのリスト

List of 9 
$ r  : num [1:11, 1:11] 1 -0.991 -0.993 -0.956 0.939 ... 
    ..- attr(*, "dimnames")=List of 2 
    .. ..$ : chr [1:11] "mpg" "cyl" "disp" "hp" ... 
    .. ..$ : chr [1:11] "mpg" "cyl" "disp" "hp" ... 
$ n  : num 11 
$ t  : num [1:11, 1:11] Inf -21.92 -25.4 -9.78 8.22 ... 
    ..- attr(*, "dimnames")=List of 2 
    .. ..$ : chr [1:11] "mpg" "cyl" "disp" "hp" ... 
    .. ..$ : chr [1:11] "mpg" "cyl" "disp" "hp" ... 
$ p  : num [1:11, 1:11] 0.00 4.04e-09 1.09e-09 4.32e-06 1.78e-05 ... 
    ..- attr(*, "dimnames")=List of 2 
    .. ..$ : chr [1:11] "mpg" "cyl" "disp" "hp" ... 
    .. ..$ : chr [1:11] "mpg" "cyl" "disp" "hp" ... 
$ se : num [1:11, 1:11] 0 0.0452 0.0391 0.0978 0.1143 ... 
    ..- attr(*, "dimnames")=List of 2 
    .. ..$ : chr [1:11] "mpg" "cyl" "disp" "hp" ... 
    .. ..$ : chr [1:11] "mpg" "cyl" "disp" "hp" ... 
$ adjust: chr "none" 
$ sym : logi TRUE 
$ ci : NULL 
$ Call : language psych::corr.test(x = M.cor, method = c("pearson", "kendall", "spearman"),  adjust = "none", ci = F) 
- attr(*, "class")= chr [1:2] "psych" "corr.test" 
num [1:11, 1:11] 1 -0.991 -0.993 -0.956 0.939 ... 
- attr(*, "dimnames")=List of 2 
    ..$ : chr [1:11] "mpg" "cyl" "disp" "hp" ... 
    ..$ : chr [1:11] "mpg" "cyl" "disp" "hp" ... 
num [1:11, 1:11] Inf -21.92 -25.4 -9.78 8.22 ... 
- attr(*, "dimnames")=List of 2 
    ..$ : chr [1:11] "mpg" "cyl" "disp" "hp" ... 
    ..$ : chr [1:11] "mpg" "cyl" "disp" "hp" ... 
num [1:11, 1:11] 0.00 4.04e-09 1.09e-09 4.32e-06 1.78e-05 ... 
- attr(*, "dimnames")=List of 2 
    ..$ : chr [1:11] "mpg" "cyl" "disp" "hp" ... 
    ..$ : chr [1:11] "mpg" "cyl" "disp" "hp" ... 
のリストについて mtcarsテストデータ

library("psych") 
library("corrplot") 

M <- mtcars 

M.cor <- cor(M) 

p.mat.all <- psych::corr.test(M.cor, method = c("pearson", "kendall", "spearman"), 
    adjust = "none", ci = F) 

str(p.mat.all) 

str(p.mat.all$r) 

str(p.mat.all$t) 

str(p.mat.all$p) 

出力付き

テスト関数との3つの相関をループする疑似コード3.3:彼らはテスト機能corrplot

Rに渡すことができるように、ループのみtprリスト:それはすべて9つのリスト

for (i in p.mat.all) { 
    p.mat <- i 
    print("p.mat ===========") 
    print(i)  

    alpha <- 0.05 
    corrplot(M.cor, 
      method="color", 
      type="upper", 
      addCoefasPercent = TRUE, 
      tl.col = "black", 
      tl.pos = "td", 
      p.mat = p.mat, sig.level = alpha, insig = "blank", 
      order = "original" 
) 
} 

の予想される出力を通過するので、それが動作しません。 1つの
OS:Debianは8.5

答えて

4

または*機能を適用するとのための出力:

lapply(p.mat.all[c("r","p","t")], function(x) { 
    # x takes now first p.mat.all$r, then p.mat.all$p, etc 
    print("p.mat ===========") 
    print(x)  

    alpha <- 0.05 
    corrplot(M.cor, 
      method="color", 
      type="upper", 
      addCoefasPercent = TRUE, 
      tl.col = "black", 
      tl.pos = "td", 
      p.mat = x, sig.level = alpha, insig = "blank", 
      order = "original" 
) 
}) 
+0

これはDavidの答えと同じですか? - あなたの 'lapply'関数のアプローチについてもう少し説明してください。 - それは何のメリットですか? –

+0

同じ結果が得られます。適用ファミリを使用する最大の理由は、速度とコード効率です。 forループはclunkyになりがちで、コーダーのエラーになりがちです。あなたの頭を掴むために少し時間がかかりますが、私はまだそれに取り組んでいますが、私はforループから始める間違いをしたからです!以下のプロットと同じにする場合は、適用前に 'par(mfrow = c(2,2))'を追加してください。 – Badger

+1

はい、これは@ Davidの答えと同じですが、より正式なRの方法です。 Rでは、*関数の適用ファミリを使用することで、forループを回避する傾向があります(ループのためだけであるが、構文が異なる)。利点はよりコンパクトなコードと潜在的なスピードアップです。あなたのケースでは、スピードの利点はありませんが、他の人はそこにあります。 – emilliman5

1

私の提案は、すなわち、ループの前にリストをフィルタリングすることである

for (i in p.mat.all[c("t", "p", "r")]) { 
    ... 
} 

r corrplot、t corrplotとp corrplot

enter image description here

+1

ニースグラフ、編集のために感謝を。処理が正しいようです。あなたが見ているのは、各変数間の3つの異なる相関関係です。あなたが達成しようとする目標は何ですか? – David

+0

これをチャットに移動できますか? – David

+0

http://chat.stackoverflow.com/rooms/127822/room-for-david-and-masi – David

関連する問題