私は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に渡すことができるように、ループのみt
、p
とr
リスト:それはすべて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
これはDavidの答えと同じですか? - あなたの 'lapply'関数のアプローチについてもう少し説明してください。 - それは何のメリットですか? –
同じ結果が得られます。適用ファミリを使用する最大の理由は、速度とコード効率です。 forループはclunkyになりがちで、コーダーのエラーになりがちです。あなたの頭を掴むために少し時間がかかりますが、私はまだそれに取り組んでいますが、私はforループから始める間違いをしたからです!以下のプロットと同じにする場合は、適用前に 'par(mfrow = c(2,2))'を追加してください。 – Badger
はい、これは@ Davidの答えと同じですが、より正式なRの方法です。 Rでは、*関数の適用ファミリを使用することで、forループを回避する傾向があります(ループのためだけであるが、構文が異なる)。利点はよりコンパクトなコードと潜在的なスピードアップです。あなたのケースでは、スピードの利点はありませんが、他の人はそこにあります。 – emilliman5