Created
September 5, 2022 13:27
-
-
Save abikoushi/2054e7433e8f44b4675380b028655318 to your computer and use it in GitHub Desktop.
Covariance plot (https://stats.stackexchange.com/questions/18058/how-would-you-explain-covariance-to-someone-who-understands-only-the-mean)
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
library(mvtnorm) | |
library(dplyr) | |
library(ggplot2) | |
combrect <- function(X){ | |
B <- combn(nrow(X),2) | |
#shuffle <- sample.int(nrow(B)) | |
apply(B, 2, function(i){ | |
data.frame(x = X[i[1],1], xmax = X[i[2],1], | |
y = X[i[1],2], ymax = X[i[2],2])}) | |
} | |
Sig2 <- matrix(c(1,-0.5,-0.5,1),2,2) | |
X2 <- rmvnorm(8, sigma = Sig2) | |
#X2 <- X2[sample.int(8),] | |
datc <- bind_rows(combrect(X2)) %>% | |
mutate(color = if_else((xmax-x)*(ymax-y)>0, "positive", "negative")) | |
ggplot(datc)+ | |
geom_rect(aes(xmin=x, xmax=xmax, ymin=y, ymax=ymax, fill=color))+ | |
geom_point(data = as.data.frame(X2), aes(x=V1, y=V2))+ | |
scale_fill_manual(values = c(rgb(0,1,1,0.1), rgb(1,1,0,0.1)))+ | |
theme_bw(14) | |
ggsave("./Desktop/rectplot.png") |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment