---
title: "Adjust alpha"
author: "Kohei Watanabe"
date: "2024-08-21"
output: html_document
editor_options: 
  chunk_output_type: console
---

```{r setup, include=FALSE}
knitr::opts_chunk$set(echo = TRUE, fig.width = 6, fig.height = 4, dpi = 150)

require(quanteda.corpora)
require(quanteda)
require(seededlda)
require(ggmatplot)
require(ggplot2)
```

```{r}
corp <- data_corpus_ungd2017 %>% 
  corpus_reshape()
toks <- tokens(corp, remove_punct = TRUE, remove_symbols = TRUE, 
               remove_number = TRUE) %>% 
  tokens_remove(newsmap::data_dictionary_newsmap_en) %>% 
  tokens_remove(stopwords("en"), min_nchar = 2)
dfmt <- dfm(toks)
```

## Fit

```{r}
label <-  c("symmetric", "asymmetric", "adjusted")
lda_sym <- textmodel_lda(dfmt, k = 10, gamma = 0.25, alpha = 0.5)
lda_asy <- textmodel_lda(dfmt, k = 10, gamma = 0.25, alpha = rep(c(0.75, 0.25), c(5, 5)))
lda_adj <- textmodel_lda(dfmt, k = 10, gamma = 0.25, alpha = 0.5, adjust_alpha = 0.5)

r <- order(sizes(lda_sym), decreasing = TRUE)
terms(lda_sym)[,r]

r <- order(sizes(lda_asy), decreasing = TRUE)
terms(lda_asy)[,r]

r <- order(sizes(lda_adj), decreasing = TRUE)
terms(lda_adj)[,r]
```

```{r}
mat <- cbind(lda_sym$alpha[order(sizes(lda_sym), decreasing = TRUE)],
             lda_asy$alpha[order(sizes(lda_asy), decreasing = TRUE)],
             lda_adj$alpha[order(sizes(lda_adj), decreasing = TRUE)])
dimnames(mat) <- list(paste0("#", seq_len(nrow(mat))), label)

ggmatplot(mat, plot_type = "both", ylab = "Alpha", xlab = "Topics",
          ylim = c(0.2, 0.8)) +
          scale_x_continuous(breaks = 1:10, label = rownames(mat))
```

```{r}
mat <- cbind(sizes(lda_sym)[order(sizes(lda_sym), decreasing = TRUE)],
             sizes(lda_asy)[order(sizes(lda_asy), decreasing = TRUE)],
             sizes(lda_adj)[order(sizes(lda_adj), decreasing = TRUE)])
dimnames(mat) <- list(paste0("#", seq_len(nrow(mat))), label)
ggmatplot(mat, plot_type = "both", ylab = "Topic size", xlab = "Topics",
          ylim = c(0.0, 0.25)) +
          scale_x_continuous(breaks = 1:10, label = rownames(mat))
```

## Evaluation

### Perplexity

```{r}
structure(c(
  perplexity(lda_sym),
  perplexity(lda_asy),
  perplexity(lda_adj)), names = label)
```

### Divergence

```{r}
structure(c(
  divergence(lda_sym, regularize = FALSE),
  divergence(lda_asy, regularize = FALSE),
  divergence(lda_adj, regularize = FALSE)), names = label)
```

## Benchmark

```{r}
microbenchmark::microbenchmark(
  symmetric = textmodel_lda(dfmt, k = 10, alpha = 0.5),
  adjusted = textmodel_lda(dfmt, k = 10, , alpha = 0.5, adjust_alpha = 0.9),
  times = 10
)
```

## Other packages

### keyATM

```{r include=FALSE}
atm <- keyATM::weightedLDA(keyATM::keyATM_read(dfmt), model = "base",
						   number_of_topics = 10)
```

```{r}
keyATM::top_words(atm)
print(atm$priors$alpha)
```

### topicmodels

```{r}
tm <- topicmodels::LDA(convert(dfmt, "topicmodels"), k = 10, method = "VEM",
					   control = list(estimate.alpha = TRUE))
topicmodels::terms(tm, 10)
print(tm@alpha)
```

