Last active
March 27, 2018 13:24
-
-
Save Selbosh/509f2fedef0f8ee0f26cd8599180e9dd to your computer and use it in GitHub Desktop.
Playing with Ioannis Kosmidis's cranly package
This file contains 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
# devtools::install_github("ikosmidis/cranly") | |
library(cranly) | |
package_db <- clean_CRAN_db() | |
(cranly_ts <- attr(package_db, "timestamp")) | |
package_network <- build_network(package_db) | |
library(igraph) | |
cranlig <- as.igraph(package_network, reverse = TRUE) | |
npkgs <- vcount(cranlig) | |
nlinks <- ecount(cranlig) | |
# Convert to weighted graph. | |
E(cranlig)$weight <- 1 | |
cranlig <- simplify(cranlig, | |
remove.multiple = TRUE, | |
remove.loops = FALSE, | |
edge.attr.comb = list(weight = 'sum', 'ignore')) | |
# Add player zero, as in http://selbydavid.com/influence/analysis.html#player-0 | |
cranlig0 <- add.vertices(cranlig, 1, name = '__Package 0__') | |
cranlig0['__Package 0__', ] <- cranlig0[, '__Package 0__'] <- .15 * nlinks / 2 / npkgs # very slow! | |
# Compute PageRanks and scale by outlinks. | |
library(dplyr) | |
PR <- page.rank(cranlig0)$vector | |
outlinks <- strength(cranlig0, mode = 'out') | |
SF <- data_frame(package = names(PR), Scroogefactor = PR / outlinks, PageRank = PR) | |
# Visualise. | |
library(ggplot2) | |
theme_set(theme_minimal()) | |
SF %>% | |
mutate(package = reorder(package, Scroogefactor)) %>% | |
top_n(20) %>% | |
ggplot() + aes(package, Scroogefactor) + geom_col() + | |
labs(title = 'cranly top 20 by (approx.) dependency influence', | |
subtitle = 'Higher scoring packages are more likely to be depended on than to depend on others', | |
caption = paste('Package database as of', cranly_ts)) + | |
coord_flip() | |
# Compare PageRank with scaled version. | |
SF %>% | |
left_join(summary(package_network), by = 'package') %>% | |
ggplot(aes(degree, Scroogefactor)) + | |
geom_point() + | |
scale_y_log10() + scale_x_sqrt() + | |
labs(title = 'cranly Scroogefactors and degrees', | |
subtitle = paste('Package database as of', cranly_ts)) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment