![](/img/trans.png)
[英]In Shiny, is it possible to make variables in session shareable to some users, but not all of them?
[英]How to Make a Correlation Matrix for Some Variables and not all of Them
我有一個包含 70 個變量的數據集。 變量的名稱如 bio1 到 bio70。 我只需要檢查一個變量(例如 bio2)與其他 70 個變量的相關性。 我使用了以下代碼
## Generate scatterplot matrix
splom(MyData, panel = panel.smoothScatter, raster= TRUE, na=TRUE)
# Generate Correlations
cor(MyData, use = "pairwise.complete.obs")
corrplot.mixed(cor(MyData, use="pairwise.complete.obs"), lower.col = "black")
但是這些代碼為我創建了一個 70 x 70 的矩陣,我不需要它。 我怎樣才能改變這些代碼來給出一個變量(例如 bio2)與其他變量的相關矩陣? 謝謝
您沒有提供數據集,所以我將使用tidyverse
和correlation
包向您展示 R 的iris
數據集。 首先加載庫:
#### Load Libraries ####
library(correlation)
library(tidyverse)
然后從那里您可以使用以下代碼運行相關矩陣:
#### Correlation Matrix Default ####
iris %>%
correlation()
# Correlation Matrix (pearson-method)
Parameter1 | Parameter2 | r | 95% CI | t(148) | p
-------------------------------------------------------------------------
Sepal.Length | Sepal.Width | -0.12 | [-0.27, 0.04] | -1.44 | 0.152
Sepal.Length | Petal.Length | 0.87 | [ 0.83, 0.91] | 21.65 | < .001***
Sepal.Length | Petal.Width | 0.82 | [ 0.76, 0.86] | 17.30 | < .001***
Sepal.Width | Petal.Length | -0.43 | [-0.55, -0.29] | -5.77 | < .001***
Sepal.Width | Petal.Width | -0.37 | [-0.50, -0.22] | -4.79 | < .001***
Petal.Length | Petal.Width | 0.96 | [ 0.95, 0.97] | 43.39 | < .001***
p-value adjustment method: Holm (1979)
Observations: 150
如果你只想 select 萼片變量,你可以改用這段代碼:
#### Only Use Sepal Variables ####
iris %>%
select(Sepal.Length,
Sepal.Width) %>%
correlation()
現在給你這個有限的矩陣:
# Correlation Matrix (pearson-method)
Parameter1 | Parameter2 | r | 95% CI | t(148) | p
-------------------------------------------------------------------
Sepal.Length | Sepal.Width | -0.12 | [-0.27, 0.04] | -1.44 | 0.152
p-value adjustment method: Holm (1979)
Observations: 150
另一種方法是取消選擇您不需要的變量:
#### Alternative ####
iris %>%
select(-Petal.Length,
-Petal.Width) %>%
correlation()
似乎您還想要相關性 plot。我更喜歡使用ggcorrplot
,因為它看起來更好並且更易於使用。 這是一個簡單的例子,只從矩陣中取消選擇一個變量:
#### Using ggcorrplot ####
library(ggcorrplot)
corr <- iris %>%
select(-Petal.Length) %>%
correlation()
corr
ggcorrplot(corr = corr,
type = "lower",
lab = T)
給你這個:
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.