Консенсусная сеть

Консенсусная сеть

library(ape) library(phangorn) library(tidyverse) library(RColorBrewer)

df <- read.table(“table_with_frequencies.txt”, header = TRUE, sep = ““, quote =”"“, stringsAsFactors = FALSE, check.names = FALSE)

df_t <- t(df)

n_predictors <- min(100, ncol(df_t)) df_subset <- df_t[, 1:n_predictors] df_matr <- as.matrix(df_subset)

get_nj_tree <- function(x) { d <- dist(x, method = “manhattan”) nj(d) }

set.seed(42)

tree_base <- get_nj_tree(df_matr)

bs_analysis <- boot.phylo(phy = tree_base, x = df_matr, FUN = get_nj_tree, B = 100, trees = TRUE, quiet = TRUE)

tree_list <- bs_analysis$trees

net <- consensusNet(tree_list, prob = 0.3, rooted = FALSE)

labels <- net$tip.label authors <- gsub(“_.*“,”“, labels)

unique_authors <- unique(authors)

my_colors <- brewer.pal(n = max(3, length(unique_authors)), name = “Set1”) if (length(unique_authors) > length(my_colors)) { my_colors <- colorRampPalette(my_colors)(length(unique_authors)) }

tip_colors <- my_colors[match(authors, unique_authors)]

par(bg = “white”, mar = c(5, 4, 6, 10) + 0.1)

plot(net, “2D”, show.edge.label = FALSE, show.tip.label = TRUE, tip.color = tip_colors, edge.color = “gray50”, edge.width = 2.2, cex = 1.0, label.offset = 0.8, font = 2)

title(main = “Стилометрическая консенсусная сеть(100 MFW, Manhattan Dist, Prob 0.3)”, cex.main = 1.6, col.main = “black”, font.main = 2)

legend(“topright”, inset = c(-0.35, 0), legend = unique_authors, col = my_colors[seq_along(unique_authors)], pch = 19, pt.cex = 2, cex = 1.1, bty = “n”, xpd = TRUE, title = “Авторы”)