corrr包

# install.packages(“remotes”)
#remotes :: install_github(“tidymodels / corrr”)

library(MASS)
library(corrr)
set.seed(1)

# 模拟三列,相关性为.7
mu <- rep(0, 3)
Sigma <- matrix(.7, nrow = 3, ncol = 3) + diag(3)*.3
seven <- mvrnorm(n = 1000, mu = mu, Sigma = Sigma)

# 模拟三列,相关性为.4 
mu <- rep(0, 3)
Sigma <- matrix(.4, nrow = 3, ncol = 3) + diag(3)*.6
four <- mvrnorm(n = 1000, mu = mu, Sigma = Sigma)

# Bind together
d <- cbind(seven, four)
colnames(d) <- paste0("v", 1:ncol(d))

# Insert some missing values
d[sample(1:nrow(d), 100, replace = TRUE), 1] <- NA
d[sample(1:nrow(d), 200, replace = TRUE), 5] <- NA

# Correlate
x <- correlate(d)
class(x)

library(dplyr)

# Filter rows by correlation size
x %>% filter(v1 > .6)

x <- datasets::mtcars %>%
  correlate() %>%    #创建相关数据框(cor_df)
  focus(-cyl, -vs, mirror = TRUE) %>%  #专注于没有'cyl'和'vs'的cor_df
  rearrange() %>%  #重新排列相关性
  shave() #shave()上三角或下三角(设置为NA)

#漂亮印花的相关性
fashion(x)
#与形状的相关性代替值
rplot(x)

data("airquality")

#网络中的相关性
datasets::airquality %>%
  correlate() %>%
  network_plot(min_cor = .2)

评论

此博客中的热门博文

V2ray websocket(ws)+tls+nginx分流

Rstudio 使用代理