Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- # Function to calculate cosine dissimilarity matrix
- # Thanks to: https://stats.stackexchange.com/a/149865/121489
- cos.sim <- function(ma, mb) {
- mat <- tcrossprod(ma, mb);
- t1 <- sqrt(apply(ma, 1, crossprod));
- t2 <- sqrt(apply(mb, 1, crossprod));
- return(mat / outer(t1, t2));
- }
- # Generate sample data
- set.seed(2017);
- x1 <- matrix(rnorm(45), ncol = 5);
- # Calculate the cosine dissimilarity matrix
- m <- cos.sim(x1, x1);
- # Show heatmap
- library(tidyverse);
- m %>%
- as_tibble() %>%
- rownames_to_column("x1") %>%
- gather(x2, value, 2:10) %>%
- mutate(x2 = gsub("V", "", x2)) %>%
- ggplot(aes(x1, x2)) + geom_tile(aes(fill = value));
Add Comment
Please, Sign In to add comment