stopwords <- c("the","a","an","and","or","to","of","in","on","for","with","we","our",
"that","this","is","are","be","by","as","from","using","study","results",
"method","approach","paper","show","these","which","can","based","present")
tokenize <- function(t) {
w <- unlist(regmatches(tolower(t), gregexpr("[[:alpha:]]+", tolower(t))))
w <- w[nchar(w) >= 3]; w[!w %in% stopwords]
}
toks <- lapply(abstracts$text, tokenize)
vocab <- sort(unique(unlist(toks[cluster %in% ids])))
counts <- vapply(ids, function(g)
as.integer(table(factor(unlist(toks[cluster == g]), levels = vocab))),
integer(length(vocab)))
rownames(counts) <- vocab # vocabulary x topic counts
yw <- rowSums(counts); alpha <- length(vocab) * yw / sum(yw) # informative prior
a0 <- sum(alpha); N <- sum(counts)
top_terms <- function(gi, n = 8) {
y <- counts[, gi]; ni <- sum(y); r <- yw - y
delta <- log((y + alpha) / (ni + a0 - y - alpha)) -
log((r + alpha) / (N - ni + a0 - r - alpha))
z <- delta / sqrt(1 / (y + alpha) + 1 / (r + alpha))
names(sort(z, decreasing = TRUE))[seq_len(n)]
}
data.frame(
topic = labels[as.character(ids)],
distinctive_terms = vapply(seq_along(ids),
function(gi) paste(top_terms(gi), collapse = ", "), character(1))
)
#> topic
#> 1 Cryptography and security
#> 2 Condensed matter and quantum materials
#> 3 Epidemiology and public health
#> 4 Number theory and combinatorics
#> 5 Genomics and molecular biology
#> 6 Cosmology and astrophysics
#> 7 Climate and atmospheric science
#> 8 Deep learning
#> distinctive_terms
#> 1 surface, knowledge, error, quantum, previously, parameter, security, standard
#> 2 quantum, high, efficient, power, states, temperature, supports, spin
#> 3 case, health, effectiveness, disease, screening, supports, programmes, identifies
#> 4 theory, number, coding, group, wide, known, under, mild
#> 5 disease, type, precision, identity, resolves, regulatory, cell, burden
#> 6 structure, dark, functions, mass, constant, measurements, mild, spectrum
#> 7 surface, temperature, forecasting, climate, strong, forcing, heterogeneity, sea
#> 8 models, gap, networks, throughput, distribution, efficiency, reduces, training